Skip to content

Commit 11498ad

Browse files
author
undefined
committed
fix: appcontext not work & function support #4627
1 parent 9cd4783 commit 11498ad

File tree

5 files changed

+56
-38
lines changed

5 files changed

+56
-38
lines changed

components/modal/ConfirmDialog.tsx

+20-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import type { ModalFuncProps } from './Modal';
33
import Dialog from './Modal';
44
import ActionButton from './ActionButton';
55
import { getConfirmLocale } from './locale';
6-
import type { FunctionalComponent } from 'vue';
6+
import { FunctionalComponent } from 'vue';
77

88
interface ConfirmDialogProps extends ModalFuncProps {
99
afterClose?: () => void;
10-
close: (...args: any[]) => void;
10+
close?: (...args: any[]) => void;
1111
autoFocusButton?: null | 'ok' | 'cancel';
1212
}
1313

14+
function renderSomeContent(_name, someContent) {
15+
if (typeof someContent === 'function') {
16+
return someContent();
17+
}
18+
return someContent;
19+
}
20+
1421
const ConfirmDialog: FunctionalComponent<ConfirmDialogProps> = props => {
1522
const {
1623
icon,
@@ -40,8 +47,10 @@ const ConfirmDialog: FunctionalComponent<ConfirmDialogProps> = props => {
4047
// 默认为 false,保持旧版默认行为
4148
const maskClosable = props.maskClosable === undefined ? false : props.maskClosable;
4249
const runtimeLocale = getConfirmLocale();
43-
const okText = props.okText || (okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
44-
const cancelText = props.cancelText || runtimeLocale.cancelText;
50+
const okText =
51+
renderSomeContent('okText', props.okText) ||
52+
(okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
53+
const cancelText = renderSomeContent('cancelText', props.cancelText) || runtimeLocale.cancelText;
4554
const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
4655
const transitionName = props.transitionName || 'zoom';
4756
const maskTransitionName = props.maskTransitionName || 'fade';
@@ -89,11 +98,15 @@ const ConfirmDialog: FunctionalComponent<ConfirmDialogProps> = props => {
8998
>
9099
<div class={`${contentPrefixCls}-body-wrapper`}>
91100
<div class={`${contentPrefixCls}-body`}>
92-
{icon}
101+
{renderSomeContent('icon', icon)}
93102
{props.title === undefined ? null : (
94-
<span class={`${contentPrefixCls}-title`}>{props.title}</span>
103+
<span class={`${contentPrefixCls}-title`}>
104+
{renderSomeContent('title', props.title)}
105+
</span>
95106
)}
96-
<div class={`${contentPrefixCls}-content`}>{props.content}</div>
107+
<div class={`${contentPrefixCls}-content`}>
108+
{renderSomeContent('content', props.content)}
109+
</div>
97110
</div>
98111
<div class={`${contentPrefixCls}-btns`}>
99112
{cancelButton}

components/modal/Modal.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,20 @@ export interface ModalFuncProps {
9595
prefixCls?: string;
9696
class?: string;
9797
visible?: boolean;
98-
title?: VNodeTypes;
98+
title?: () => VNodeTypes | VNodeTypes;
9999
closable?: boolean;
100-
content?: VNodeTypes;
100+
content?: () => VNodeTypes | VNodeTypes;
101101
// TODO: find out exact types
102102
onOk?: (...args: any[]) => any;
103103
onCancel?: (...args: any[]) => any;
104104
okButtonProps?: ButtonPropsType;
105105
cancelButtonProps?: ButtonPropsType;
106106
centered?: boolean;
107107
width?: string | number;
108-
okText?: VNodeTypes;
108+
okText?: () => VNodeTypes | VNodeTypes;
109109
okType?: LegacyButtonType;
110-
cancelText?: VNodeTypes;
111-
icon?: VNodeTypes;
110+
cancelText?: () => VNodeTypes | VNodeTypes;
111+
icon?: () => VNodeTypes | VNodeTypes;
112112
/* Deprecated */
113113
iconType?: string;
114114
mask?: boolean;
@@ -123,7 +123,10 @@ export interface ModalFuncProps {
123123
autoFocusButton?: null | 'ok' | 'cancel';
124124
transitionName?: string;
125125
maskTransitionName?: string;
126+
127+
/** @deprecated please use `appContext` instead */
126128
parentContext?: any;
129+
appContext?: any;
127130
}
128131

129132
type getContainerFunc = () => HTMLElement;

components/modal/confirm.tsx

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { createApp } from 'vue';
1+
import { createVNode, render as vueRender } from 'vue';
22
import ConfirmDialog from './ConfirmDialog';
33
import type { ModalFuncProps } from './Modal';
44
import { destroyFns } from './Modal';
55

66
import Omit from 'omit.js';
77

8-
export default function confirm(config: ModalFuncProps) {
8+
const confirm = (config: ModalFuncProps) => {
99
const div = document.createElement('div');
1010
document.body.appendChild(div);
11-
let currentConfig = { ...Omit(config, ['parentContext']), close, visible: true } as any;
11+
let currentConfig = {
12+
...Omit(config, ['parentContext', 'appContext']),
13+
close,
14+
visible: true,
15+
} as any;
1216

1317
let confirmDialogInstance = null;
14-
let confirmDialogProps = {};
1518
function close(this: typeof close, ...args: any[]) {
1619
currentConfig = {
1720
...currentConfig,
@@ -25,12 +28,15 @@ export default function confirm(config: ModalFuncProps) {
2528
...currentConfig,
2629
...newConfig,
2730
};
28-
confirmDialogInstance &&
29-
Object.assign(confirmDialogInstance, { confirmDialogProps: currentConfig });
31+
if (confirmDialogInstance) {
32+
Object.assign(confirmDialogInstance.component.props, currentConfig);
33+
confirmDialogInstance.component.update();
34+
}
3035
}
3136
function destroy(...args: any[]) {
3237
if (confirmDialogInstance && div.parentNode) {
33-
confirmDialogInstance.vIf = false; // hack destroy
38+
Object.assign(confirmDialogInstance.component.props, { vIf: false }); // hack destroy
39+
confirmDialogInstance.component.update();
3440
confirmDialogInstance = null;
3541
div.parentNode.removeChild(div);
3642
}
@@ -46,20 +52,14 @@ export default function confirm(config: ModalFuncProps) {
4652
}
4753
}
4854
}
49-
55+
const Wrapper = p => {
56+
return p.vIf ? <ConfirmDialog {...p}></ConfirmDialog> : null;
57+
};
5058
function render(props: ModalFuncProps) {
51-
confirmDialogProps = props;
52-
return createApp({
53-
parent: (config as any).parentContext,
54-
data() {
55-
return { confirmDialogProps, vIf: true };
56-
},
57-
render() {
58-
// 先解构,避免报错,原因不详
59-
const cdProps = { ...this.confirmDialogProps };
60-
return this.vIf ? <ConfirmDialog {...cdProps} /> : null;
61-
},
62-
}).mount(div);
59+
const vm = createVNode(Wrapper, { ...props, vIf: true });
60+
vm.appContext = config.parentContext || config.appContext || vm.appContext;
61+
vueRender(vm, div);
62+
return vm;
6363
}
6464

6565
confirmDialogInstance = render(currentConfig);
@@ -68,4 +68,6 @@ export default function confirm(config: ModalFuncProps) {
6868
destroy: close,
6969
update,
7070
};
71-
}
71+
};
72+
73+
export default confirm;

components/modal/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export { ModalProps, ModalFuncProps } from './Modal';
1313
const info = function (props: ModalFuncProps) {
1414
const config = {
1515
type: 'info',
16-
icon: <InfoCircleOutlined />,
16+
icon: () => <InfoCircleOutlined />,
1717
okCancel: false,
1818
...props,
1919
};
@@ -23,7 +23,7 @@ const info = function (props: ModalFuncProps) {
2323
const success = function (props: ModalFuncProps) {
2424
const config = {
2525
type: 'success',
26-
icon: <CheckCircleOutlined />,
26+
icon: () => <CheckCircleOutlined />,
2727
okCancel: false,
2828
...props,
2929
};
@@ -33,7 +33,7 @@ const success = function (props: ModalFuncProps) {
3333
const error = function (props: ModalFuncProps) {
3434
const config = {
3535
type: 'error',
36-
icon: <CloseCircleOutlined />,
36+
icon: () => <CloseCircleOutlined />,
3737
okCancel: false,
3838
...props,
3939
};
@@ -43,7 +43,7 @@ const error = function (props: ModalFuncProps) {
4343
const warning = function (props: ModalFuncProps) {
4444
const config = {
4545
type: 'warning',
46-
icon: <ExclamationCircleOutlined />,
46+
icon: () => <ExclamationCircleOutlined />,
4747
okCancel: false,
4848
...props,
4949
};

0 commit comments

Comments
 (0)