-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.tsx
300 lines (277 loc) · 9.89 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import type { App, MaybeRef, Plugin, WatchStopHandle } from 'vue';
import { watch, computed, reactive, defineComponent, watchEffect } from 'vue';
import defaultRenderEmpty from './renderEmpty';
import type { RenderEmptyHandler } from './renderEmpty';
import type { Locale } from '../locale-provider';
import LocaleProvider, { ANT_MARK } from '../locale-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import message from '../message';
import notification from '../notification';
import { registerTheme } from './cssVariables';
import defaultLocale from '../locale/en_US';
import type { ValidateMessages } from '../form/interface';
import useStyle from './style';
import useTheme from './hooks/useTheme';
import defaultSeedToken from '../theme/themes/seed';
import type { ConfigProviderInnerProps, ConfigProviderProps, Theme } from './context';
import {
useConfigContextProvider,
useConfigContextInject,
configProviderProps,
useProvideGlobalForm,
defaultIconPrefixCls,
} from './context';
import { useProviderSize } from './SizeContext';
import { useProviderDisabled } from './DisabledContext';
import { createTheme } from '../_util/cssinjs';
import { DesignTokenProvider } from '../theme/internal';
export type {
ConfigProviderProps,
Theme,
SizeType,
Direction,
CSPConfig,
DirectionType,
} from './context';
export const defaultPrefixCls = 'ant';
export { defaultIconPrefixCls };
function getGlobalPrefixCls() {
return globalConfigForApi.prefixCls || defaultPrefixCls;
}
function getGlobalIconPrefixCls() {
return globalConfigForApi.iconPrefixCls || defaultIconPrefixCls;
}
const globalConfigBySet = reactive<ConfigProviderProps>({}); // 权重最大
export const globalConfigForApi: ConfigProviderProps & {
getRootPrefixCls?: (rootPrefixCls?: string, customizePrefixCls?: string) => string;
} = reactive({});
export const configConsumerProps = [
'getTargetContainer',
'getPopupContainer',
'rootPrefixCls',
'getPrefixCls',
'renderEmpty',
'csp',
'autoInsertSpaceInButton',
'locale',
'pageHeader',
];
watchEffect(() => {
Object.assign(globalConfigForApi, globalConfigBySet);
globalConfigForApi.prefixCls = getGlobalPrefixCls();
globalConfigForApi.iconPrefixCls = getGlobalIconPrefixCls();
globalConfigForApi.getPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
if (customizePrefixCls) return customizePrefixCls;
return suffixCls
? `${globalConfigForApi.prefixCls}-${suffixCls}`
: globalConfigForApi.prefixCls;
};
globalConfigForApi.getRootPrefixCls = () => {
// If Global prefixCls provided, use this
if (globalConfigForApi.prefixCls) {
return globalConfigForApi.prefixCls;
}
// Fallback to default prefixCls
return getGlobalPrefixCls();
};
});
type GlobalConfigProviderProps = {
prefixCls?: MaybeRef<ConfigProviderProps['prefixCls']>;
iconPrefixCls?: MaybeRef<ConfigProviderProps['iconPrefixCls']>;
getPopupContainer?: ConfigProviderProps['getPopupContainer'];
};
let stopWatchEffect: WatchStopHandle;
const setGlobalConfig = (params: GlobalConfigProviderProps & { theme?: Theme }) => {
if (stopWatchEffect) {
stopWatchEffect();
}
stopWatchEffect = watchEffect(() => {
Object.assign(globalConfigBySet, reactive(params));
Object.assign(globalConfigForApi, reactive(params));
});
if (params.theme) {
registerTheme(getGlobalPrefixCls(), params.theme);
}
};
export const globalConfig = () => ({
getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => {
if (customizePrefixCls) return customizePrefixCls;
return suffixCls ? `${getGlobalPrefixCls()}-${suffixCls}` : getGlobalPrefixCls();
},
getIconPrefixCls: getGlobalIconPrefixCls,
getRootPrefixCls: () => {
// If Global prefixCls provided, use this
if (globalConfigForApi.prefixCls) {
return globalConfigForApi.prefixCls;
}
// Fallback to default prefixCls
return getGlobalPrefixCls();
},
});
const ConfigProvider = defineComponent({
name: 'AConfigProvider',
inheritAttrs: false,
props: configProviderProps(),
setup(props, { slots }) {
const parentContext = useConfigContextInject();
const getPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {
const { prefixCls = 'ant' } = props;
if (customizePrefixCls) return customizePrefixCls;
const mergedPrefixCls = prefixCls || parentContext.getPrefixCls('');
return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls;
};
const iconPrefixCls = computed(
() => props.iconPrefixCls || parentContext.iconPrefixCls.value || defaultIconPrefixCls,
);
const shouldWrapSSR = computed(() => iconPrefixCls.value !== parentContext.iconPrefixCls.value);
const csp = computed(() => props.csp || parentContext.csp?.value);
const wrapSSR = useStyle(iconPrefixCls);
const mergedTheme = useTheme(
computed(() => props.theme),
computed(() => parentContext.theme?.value),
);
const renderEmptyComponent = (name?: string) => {
const renderEmpty = (props.renderEmpty ||
slots.renderEmpty ||
parentContext.renderEmpty ||
defaultRenderEmpty) as RenderEmptyHandler;
return renderEmpty(name);
};
const autoInsertSpaceInButton = computed(
() => props.autoInsertSpaceInButton ?? parentContext.autoInsertSpaceInButton?.value,
);
const locale = computed(() => props.locale || parentContext.locale?.value);
watch(
locale,
() => {
globalConfigBySet.locale = locale.value;
},
{ immediate: true },
);
const direction = computed(() => props.direction || parentContext.direction?.value);
const space = computed(() => props.space ?? parentContext.space?.value);
const virtual = computed(() => props.virtual ?? parentContext.virtual?.value);
const dropdownMatchSelectWidth = computed(
() => props.dropdownMatchSelectWidth ?? parentContext.dropdownMatchSelectWidth?.value,
);
const getTargetContainer = computed(() =>
props.getTargetContainer !== undefined
? props.getTargetContainer
: parentContext.getTargetContainer?.value,
);
const getPopupContainer = computed(() =>
props.getPopupContainer !== undefined
? props.getPopupContainer
: parentContext.getPopupContainer?.value,
);
const pageHeader = computed(() =>
props.pageHeader !== undefined ? props.pageHeader : parentContext.pageHeader?.value,
);
const input = computed(() =>
props.input !== undefined ? props.input : parentContext.input?.value,
);
const pagination = computed(() =>
props.pagination !== undefined ? props.pagination : parentContext.pagination?.value,
);
const form = computed(() =>
props.form !== undefined ? props.form : parentContext.form?.value,
);
const select = computed(() =>
props.select !== undefined ? props.select : parentContext.select?.value,
);
const componentSize = computed(() => props.componentSize);
const componentDisabled = computed(() => props.componentDisabled);
const wave = computed(() => props.wave ?? parentContext.wave?.value);
const configProvider: ConfigProviderInnerProps = {
csp,
autoInsertSpaceInButton,
locale,
direction,
space,
virtual,
dropdownMatchSelectWidth,
getPrefixCls,
iconPrefixCls,
theme: computed(() => {
return mergedTheme.value ?? parentContext.theme?.value;
}),
renderEmpty: renderEmptyComponent,
getTargetContainer,
getPopupContainer,
pageHeader,
input,
pagination,
form,
select,
componentSize,
componentDisabled,
transformCellText: computed(() => props.transformCellText),
wave,
};
// ================================ Dynamic theme ================================
const memoTheme = computed(() => {
const { algorithm, token, ...rest } = mergedTheme.value || {};
const themeObj =
algorithm && (!Array.isArray(algorithm) || algorithm.length > 0)
? createTheme(algorithm)
: undefined;
return {
...rest,
theme: themeObj,
token: {
...defaultSeedToken,
...token,
},
};
});
const validateMessagesRef = computed(() => {
// Additional Form provider
let validateMessages: ValidateMessages = {};
if (locale.value) {
validateMessages =
locale.value.Form?.defaultValidateMessages ||
defaultLocale.Form?.defaultValidateMessages ||
{};
}
if (props.form && props.form.validateMessages) {
validateMessages = { ...validateMessages, ...props.form.validateMessages };
}
return validateMessages;
});
useConfigContextProvider(configProvider);
useProvideGlobalForm({ validateMessages: validateMessagesRef });
useProviderSize(componentSize);
useProviderDisabled(componentDisabled);
const renderProvider = (legacyLocale: Locale) => {
let childNode = shouldWrapSSR.value ? wrapSSR(slots.default?.()) : slots.default?.();
if (props.theme)
childNode = <DesignTokenProvider value={memoTheme.value}>{childNode}</DesignTokenProvider>;
return (
<LocaleProvider locale={locale.value || legacyLocale} ANT_MARK__={ANT_MARK}>
{childNode}
</LocaleProvider>
);
};
watchEffect(() => {
if (direction.value) {
message.config({
rtl: direction.value === 'rtl',
});
notification.config({
rtl: direction.value === 'rtl',
});
}
});
return () => (
<LocaleReceiver children={(_, __, legacyLocale) => renderProvider(legacyLocale as Locale)} />
);
},
});
ConfigProvider.config = setGlobalConfig;
ConfigProvider.install = function (app: App) {
app.component(ConfigProvider.name, ConfigProvider);
};
export default ConfigProvider as typeof ConfigProvider &
Plugin & {
readonly config: typeof setGlobalConfig;
};