|
| 1 | +import type { PropType } from 'vue'; |
| 2 | +import { computed, defineComponent } from 'vue'; |
| 3 | +import { useConfigContextInject } from '../../config-provider/context'; |
| 4 | +import { useLocaleReceiver } from '../../locale-provider/LocaleReceiver'; |
| 5 | +import defaultLocale from '../../locale/en_US'; |
| 6 | +import ConfirmDialog from '../ConfirmDialog'; |
| 7 | +import type { ModalFuncProps } from '../Modal'; |
| 8 | +import initDefaultProps from '../../_util/props-util/initDefaultProps'; |
| 9 | +export interface HookModalProps { |
| 10 | + afterClose: () => void; |
| 11 | + config: ModalFuncProps; |
| 12 | + destroyAction: (...args: any[]) => void; |
| 13 | + open: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +export interface HookModalRef { |
| 17 | + destroy: () => void; |
| 18 | + update: (config: ModalFuncProps) => void; |
| 19 | +} |
| 20 | + |
| 21 | +const comfirmFuncProps = () => ({ |
| 22 | + config: Object as PropType<ModalFuncProps>, |
| 23 | + afterClose: Function as PropType<() => void>, |
| 24 | + destroyAction: Function as PropType<(e: any) => void>, |
| 25 | + open: Boolean, |
| 26 | +}); |
| 27 | + |
| 28 | +export default defineComponent({ |
| 29 | + name: 'HookModal', |
| 30 | + inheritAttrs: false, |
| 31 | + props: initDefaultProps(comfirmFuncProps(), { |
| 32 | + config: { |
| 33 | + width: 520, |
| 34 | + okType: 'primary', |
| 35 | + }, |
| 36 | + }), |
| 37 | + setup(props: HookModalProps, { expose }) { |
| 38 | + const open = computed(() => props.open); |
| 39 | + const innerConfig = computed(() => props.config); |
| 40 | + const { direction, getPrefixCls } = useConfigContextInject(); |
| 41 | + const prefixCls = getPrefixCls('modal'); |
| 42 | + const rootPrefixCls = getPrefixCls(); |
| 43 | + |
| 44 | + const afterClose = () => { |
| 45 | + props?.afterClose(); |
| 46 | + innerConfig.value.afterClose?.(); |
| 47 | + }; |
| 48 | + |
| 49 | + const close = (...args: any[]) => { |
| 50 | + props.destroyAction(...args); |
| 51 | + }; |
| 52 | + |
| 53 | + expose({ destroy: close }); |
| 54 | + const mergedOkCancel = innerConfig.value.okCancel ?? innerConfig.value.type === 'confirm'; |
| 55 | + const [contextLocale] = useLocaleReceiver('Modal', defaultLocale.Modal); |
| 56 | + return () => ( |
| 57 | + <ConfirmDialog |
| 58 | + prefixCls={prefixCls} |
| 59 | + rootPrefixCls={rootPrefixCls} |
| 60 | + {...innerConfig.value} |
| 61 | + close={close} |
| 62 | + open={open.value} |
| 63 | + afterClose={afterClose} |
| 64 | + okText={ |
| 65 | + innerConfig.value.okText || |
| 66 | + (mergedOkCancel ? contextLocale?.value.okText : contextLocale?.value.justOkText) |
| 67 | + } |
| 68 | + direction={innerConfig.value.direction || direction.value} |
| 69 | + cancelText={innerConfig.value.cancelText || contextLocale?.value.cancelText} |
| 70 | + /> |
| 71 | + ); |
| 72 | + }, |
| 73 | +}); |
0 commit comments