2023-10-17 10:35:21 +08:00
|
|
|
|
import { Ref, ref } from 'vue';
|
2023-06-09 13:46:11 +08:00
|
|
|
|
import { Modal } from '@arco-design/web-vue';
|
2023-10-17 10:35:21 +08:00
|
|
|
|
|
2023-08-15 00:10:59 +08:00
|
|
|
|
import { useI18n } from '@/hooks/useI18n';
|
2023-10-17 10:35:21 +08:00
|
|
|
|
|
|
|
|
|
import type { ModalConfig } from '@arco-design/web-vue';
|
2023-06-09 13:46:11 +08:00
|
|
|
|
|
|
|
|
|
export type ModalType = 'info' | 'success' | 'warning' | 'error';
|
|
|
|
|
|
|
|
|
|
export type ModalSize = 'small' | 'medium' | 'large' | 'full';
|
|
|
|
|
|
|
|
|
|
export type ModalMode = 'default' | 'weak';
|
|
|
|
|
|
|
|
|
|
export interface ModalOptions extends ModalConfig {
|
|
|
|
|
mode?: ModalMode;
|
|
|
|
|
type: ModalType;
|
|
|
|
|
size?: ModalSize;
|
2023-08-16 13:22:22 +08:00
|
|
|
|
onBeforeOk: () => Promise<void>;
|
2023-06-09 13:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 00:10:59 +08:00
|
|
|
|
export interface DeleteModalOptions extends ModalConfig {
|
|
|
|
|
title: string;
|
|
|
|
|
content: string;
|
|
|
|
|
}
|
|
|
|
|
const { t } = useI18n();
|
2023-08-16 15:04:57 +08:00
|
|
|
|
|
2023-06-09 13:46:11 +08:00
|
|
|
|
export default function useModal() {
|
2023-08-16 15:04:57 +08:00
|
|
|
|
const beforeOkHandler = (options: any, done: (closed: boolean) => void, loading: Ref) => {
|
|
|
|
|
loading.value = true;
|
|
|
|
|
options?.onBeforeOk().finally(() => {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
done(true);
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-06-09 13:46:11 +08:00
|
|
|
|
return {
|
2023-08-16 15:04:57 +08:00
|
|
|
|
openModal: (options: ModalOptions) => {
|
|
|
|
|
const okHandlerLoading = ref<boolean>(false);
|
2023-08-16 17:54:06 +08:00
|
|
|
|
Modal[options.type]({
|
2023-06-16 15:37:34 +08:00
|
|
|
|
// 默认设置按钮属性,也可通过options传入覆盖
|
2023-06-12 18:29:21 +08:00
|
|
|
|
okButtonProps: {
|
|
|
|
|
type: options.mode === 'weak' ? 'text' : 'primary',
|
|
|
|
|
},
|
|
|
|
|
cancelButtonProps: {
|
|
|
|
|
type: options.mode === 'weak' ? 'text' : 'secondary',
|
2023-08-16 15:04:57 +08:00
|
|
|
|
disabled: okHandlerLoading.value,
|
2023-06-12 18:29:21 +08:00
|
|
|
|
},
|
2023-08-09 10:07:00 +08:00
|
|
|
|
simple: false,
|
2023-08-16 15:04:57 +08:00
|
|
|
|
okLoading: okHandlerLoading.value,
|
2023-06-16 15:37:34 +08:00
|
|
|
|
...options,
|
2023-08-16 13:22:22 +08:00
|
|
|
|
onBeforeOk: (done: (closed: boolean) => void) => {
|
2023-08-16 15:04:57 +08:00
|
|
|
|
beforeOkHandler(options, done, okHandlerLoading);
|
2023-08-16 13:22:22 +08:00
|
|
|
|
},
|
|
|
|
|
onBeforeCancel: () => {
|
2023-08-16 15:04:57 +08:00
|
|
|
|
return !okHandlerLoading.value;
|
2023-08-16 13:22:22 +08:00
|
|
|
|
},
|
2023-06-16 15:37:34 +08:00
|
|
|
|
titleAlign: 'start',
|
2023-08-09 13:57:29 +08:00
|
|
|
|
modalClass: `ms-usemodal ms-usemodal-${options.mode || 'default'} ms-usemodal-${
|
|
|
|
|
options.size || 'small'
|
|
|
|
|
} ms-usemodal-${options.type}`,
|
2023-08-16 15:04:57 +08:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
openDeleteModal: (options: DeleteModalOptions) => {
|
|
|
|
|
const deleteLoading = ref<boolean>(false);
|
2023-10-19 20:07:38 +08:00
|
|
|
|
Modal.error({
|
2023-08-15 00:10:59 +08:00
|
|
|
|
okText: t('common.confirmDelete'),
|
|
|
|
|
cancelText: t('common.cancel'),
|
|
|
|
|
hideCancel: false,
|
2024-02-01 19:03:54 +08:00
|
|
|
|
simple: false,
|
2023-08-15 00:10:59 +08:00
|
|
|
|
okButtonProps: { status: 'danger' },
|
|
|
|
|
titleAlign: 'start',
|
2023-08-15 16:23:42 +08:00
|
|
|
|
modalClass: `ms-usemodal ms-usemodal-warning ms-usemodal-small`,
|
2023-08-15 00:10:59 +08:00
|
|
|
|
...options,
|
2023-08-16 15:04:57 +08:00
|
|
|
|
okLoading: deleteLoading.value,
|
|
|
|
|
cancelButtonProps: {
|
|
|
|
|
disabled: deleteLoading.value,
|
|
|
|
|
},
|
|
|
|
|
onBeforeOk: (done: (closed: boolean) => void) => {
|
|
|
|
|
beforeOkHandler(options, done, deleteLoading);
|
|
|
|
|
},
|
|
|
|
|
onBeforeCancel: () => {
|
|
|
|
|
return !deleteLoading.value;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
2023-06-09 13:46:11 +08:00
|
|
|
|
};
|
|
|
|
|
}
|