Skip to content

Commit 322158d

Browse files
committed
feat: message support configprovider.config
1 parent bcd69f0 commit 322158d

File tree

3 files changed

+72
-50
lines changed

3 files changed

+72
-50
lines changed

components/message/index.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
import type { CSSProperties, VNodeTypes } from 'vue';
1+
import type { CSSProperties } from 'vue';
22
import Notification from '../vc-notification';
33
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
44
import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled';
55
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
66
import CheckCircleFilled from '@ant-design/icons-vue/CheckCircleFilled';
77
import InfoCircleFilled from '@ant-design/icons-vue/InfoCircleFilled';
8+
import type { VueNode } from '../_util/type';
89

910
let defaultDuration = 3;
1011
let defaultTop: string;
1112
let messageInstance: any;
1213
let key = 1;
13-
let prefixCls = 'ant-message';
14+
let localPrefixCls = '';
1415
let transitionName = 'move-up';
1516
let getContainer = () => document.body;
1617
let maxCount: number;
1718

18-
function getMessageInstance(callback: (i: any) => void) {
19+
function getMessageInstance(args: MessageArgsProps, callback: (i: any) => void) {
1920
if (messageInstance) {
2021
callback(messageInstance);
2122
return;
2223
}
2324
Notification.newInstance(
2425
{
25-
prefixCls,
26+
prefixCls: args.prefixCls || localPrefixCls,
27+
rootPrefixCls: args.rootPrefixCls,
2628
transitionName,
2729
style: { top: defaultTop }, // 覆盖原来的样式
2830
getContainer,
@@ -60,20 +62,21 @@ export interface MessageType {
6062
}
6163

6264
export interface MessageArgsProps {
63-
content: VNodeTypes;
65+
content: string | (() => VueNode) | VueNode;
6466
duration: number | null;
6567
type: NoticeType;
68+
prefixCls?: string;
69+
rootPrefixCls?: string;
6670
onClose?: () => void;
67-
icon?: VNodeTypes;
71+
icon?: (() => VueNode) | VueNode;
6872
key?: string | number;
6973
style?: CSSProperties;
7074
class?: string;
75+
appContext?: any;
7176
}
7277

7378
function notice(args: MessageArgsProps): MessageType {
7479
const duration = args.duration !== undefined ? args.duration : defaultDuration;
75-
const Icon = iconMap[args.type];
76-
const iconNode = Icon ? <Icon /> : '';
7780

7881
const target = args.key || key++;
7982
const closePromise = new Promise(resolve => {
@@ -83,19 +86,22 @@ function notice(args: MessageArgsProps): MessageType {
8386
}
8487
return resolve(true);
8588
};
86-
getMessageInstance(instance => {
89+
getMessageInstance(args, instance => {
8790
instance.notice({
8891
key: target,
8992
duration,
9093
style: args.style || {},
9194
class: args.class,
92-
content: () => {
95+
appContext: args.appContext,
96+
content: ({ prefixCls }) => {
97+
const Icon = iconMap[args.type];
98+
const iconNode = Icon ? <Icon /> : '';
9399
return (
94100
<div
95101
class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}
96102
>
97-
{args.icon || iconNode}
98-
<span>{args.content}</span>
103+
{typeof args.icon === 'function' ? args.icon : args.icon || iconNode}
104+
<span>{typeof args.content === 'function' ? args.content() : args.content}</span>
99105
</div>
100106
);
101107
},
@@ -115,7 +121,7 @@ function notice(args: MessageArgsProps): MessageType {
115121
}
116122

117123
type ConfigDuration = number | (() => void);
118-
type JointContent = VNodeTypes | MessageArgsProps;
124+
type JointContent = VueNode | MessageArgsProps;
119125
export type ConfigOnClose = () => void;
120126

121127
function isArgsProps(content: JointContent): content is MessageArgsProps {
@@ -145,7 +151,7 @@ const api: any = {
145151
defaultDuration = options.duration;
146152
}
147153
if (options.prefixCls !== undefined) {
148-
prefixCls = options.prefixCls;
154+
localPrefixCls = options.prefixCls;
149155
}
150156
if (options.getContainer !== undefined) {
151157
getContainer = options.getContainer;

components/modal/Modal.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ExtractPropTypes, VNodeTypes, CSSProperties, PropType } from 'vue';
1+
import type { ExtractPropTypes, CSSProperties, PropType } from 'vue';
22
import { defineComponent, inject, computed } from 'vue';
33
import classNames from '../_util/classNames';
44
import Dialog from '../vc-dialog';
@@ -13,6 +13,7 @@ import LocaleReceiver from '../locale-provider/LocaleReceiver';
1313
import { getComponent, getSlot } from '../_util/props-util';
1414
import initDefaultProps from '../_util/props-util/initDefaultProps';
1515
import { defaultConfigProvider } from '../config-provider';
16+
import type { VueNode } from '../_util/type';
1617

1718
let mousePosition: { x: number; y: number } | null = null;
1819
// ref: https://github.com/ant-design/ant-design/issues/15795
@@ -95,20 +96,20 @@ export interface ModalFuncProps {
9596
prefixCls?: string;
9697
class?: string;
9798
visible?: boolean;
98-
title?: string | (() => VNodeTypes) | VNodeTypes;
99+
title?: string | (() => VueNode) | VueNode;
99100
closable?: boolean;
100-
content?: string | (() => VNodeTypes) | VNodeTypes;
101+
content?: string | (() => VueNode) | VueNode;
101102
// TODO: find out exact types
102103
onOk?: (...args: any[]) => any;
103104
onCancel?: (...args: any[]) => any;
104105
okButtonProps?: ButtonPropsType;
105106
cancelButtonProps?: ButtonPropsType;
106107
centered?: boolean;
107108
width?: string | number;
108-
okText?: string | (() => VNodeTypes) | VNodeTypes;
109+
okText?: string | (() => VueNode) | VueNode;
109110
okType?: LegacyButtonType;
110-
cancelText?: string | (() => VNodeTypes) | VNodeTypes;
111-
icon?: (() => VNodeTypes) | VNodeTypes;
111+
cancelText?: string | (() => VueNode) | VueNode;
112+
icon?: (() => VueNode) | VueNode;
112113
/* Deprecated */
113114
iconType?: string;
114115
mask?: boolean;

components/vc-notification/Notification.jsx

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { createApp, defineComponent } from 'vue';
1+
import { defineComponent, createVNode, render as vueRender, onMounted, ref, nextTick } from 'vue';
22
import PropTypes from '../_util/vue-types';
33
import { getComponent } from '../_util/props-util';
44
import BaseMixin from '../_util/BaseMixin';
55
import createChainedFunction from '../_util/createChainedFunction';
66
import Notice from './Notice';
77
import { getTransitionGroupProps, TransitionGroup } from '../_util/transition';
8+
import ConfigProvider, { globalConfig } from '../config-provider';
89

910
function noop() {}
1011

@@ -95,7 +96,9 @@ const Notification = defineComponent({
9596
key,
9697
};
9798
return (
98-
<Notice {...noticeProps}>{typeof content === 'function' ? content() : content}</Notice>
99+
<Notice {...noticeProps}>
100+
{typeof content === 'function' ? content({ prefixCls }) : content}
101+
</Notice>
99102
);
100103
});
101104
const className = {
@@ -120,45 +123,57 @@ const Notification = defineComponent({
120123
});
121124

122125
Notification.newInstance = function newNotificationInstance(properties, callback) {
123-
const { getContainer, style, class: className, ...props } = properties || {};
126+
const {
127+
getContainer,
128+
appContext,
129+
prefixCls: customizePrefixCls,
130+
rootPrefixCls: customRootPrefixCls,
131+
...props
132+
} = properties || {};
124133
const div = document.createElement('div');
125134
if (getContainer) {
126135
const root = getContainer();
127136
root.appendChild(div);
128137
} else {
129138
document.body.appendChild(div);
130139
}
131-
const app = createApp({
132-
mounted() {
133-
const self = this;
134-
this.$nextTick(() => {
135-
callback({
136-
notice(noticeProps) {
137-
self.$refs.notification.add(noticeProps);
138-
},
139-
removeNotice(key) {
140-
self.$refs.notification.remove(key);
141-
},
142-
component: self,
143-
destroy() {
144-
app.unmount(div);
145-
if (div.parentNode) {
146-
div.parentNode.removeChild(div);
147-
}
148-
},
140+
let vm = null;
141+
const Wrapper = defineComponent({
142+
setup(_props, { attrs }) {
143+
const notiRef = ref();
144+
onMounted(() => {
145+
nextTick(() => {
146+
callback({
147+
notice(noticeProps) {
148+
notiRef.value?.add(noticeProps);
149+
},
150+
removeNotice(key) {
151+
notiRef.value?.remove(key);
152+
},
153+
destroy() {
154+
vm?.unmount(div);
155+
if (div.parentNode) {
156+
div.parentNode.removeChild(div);
157+
}
158+
},
159+
});
149160
});
150161
});
151-
},
152-
render() {
153-
const p = {
154-
...props,
155-
ref: 'notification',
156-
style,
157-
class: className,
162+
return () => {
163+
const { getPrefixCls, getRootPrefixCls } = globalConfig();
164+
const prefixCls = getPrefixCls('message', customizePrefixCls);
165+
const rootPrefixCls = getRootPrefixCls(customRootPrefixCls, prefixCls);
166+
return (
167+
<ConfigProvider prefixCls={rootPrefixCls}>
168+
<Notification ref={notiRef} {...attrs} prefixCls={prefixCls} />
169+
</ConfigProvider>
170+
);
158171
};
159-
return <Notification {...p} />;
160172
},
161173
});
162-
app.mount(div);
174+
175+
vm = createVNode(Wrapper, props);
176+
vm.appContext = appContext || vm.appContext;
177+
vueRender(vm, div);
163178
};
164179
export default Notification;

0 commit comments

Comments
 (0)