|
1 |
| -import type { PropType, ExtractPropTypes, UnwrapRef } from 'vue'; |
2 |
| -import { reactive, provide, defineComponent, watch } from 'vue'; |
| 1 | +import type { PropType, ExtractPropTypes, UnwrapRef, App, Plugin, WatchStopHandle } from 'vue'; |
| 2 | +import { reactive, provide, defineComponent, watch, ref, unref, watchEffect } from 'vue'; |
3 | 3 | import PropTypes from '../_util/vue-types';
|
4 | 4 | import defaultRenderEmpty from './renderEmpty';
|
5 | 5 | import type { RenderEmptyHandler } from './renderEmpty';
|
6 | 6 | import type { Locale } from '../locale-provider';
|
7 | 7 | import LocaleProvider, { ANT_MARK } from '../locale-provider';
|
8 | 8 | import type { TransformCellTextProps } from '../table/interface';
|
9 | 9 | import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
10 |
| -import { withInstall } from '../_util/type'; |
11 | 10 | import type { RequiredMark } from '../form/Form';
|
| 11 | +import type { MaybeRef } from '../_util/type'; |
12 | 12 |
|
13 | 13 | export type SizeType = 'small' | 'middle' | 'large' | undefined;
|
14 | 14 |
|
@@ -57,6 +57,56 @@ export const configConsumerProps = [
|
57 | 57 | 'pageHeader',
|
58 | 58 | ];
|
59 | 59 |
|
| 60 | +export const defaultPrefixCls = 'ant'; |
| 61 | +let globalPrefixCls = ref<string>(); |
| 62 | + |
| 63 | +type GlobalConfigProviderProps = { |
| 64 | + prefixCls?: MaybeRef<ConfigProviderProps['prefixCls']>; |
| 65 | +}; |
| 66 | + |
| 67 | +let stopWatchEffect: WatchStopHandle; |
| 68 | +const setGlobalConfig = (params: GlobalConfigProviderProps) => { |
| 69 | + if (stopWatchEffect) { |
| 70 | + stopWatchEffect(); |
| 71 | + } |
| 72 | + stopWatchEffect = watchEffect(() => { |
| 73 | + const prefixCls = unref(params.prefixCls); |
| 74 | + if (prefixCls !== undefined) { |
| 75 | + globalPrefixCls.value = prefixCls; |
| 76 | + } |
| 77 | + }); |
| 78 | +}; |
| 79 | + |
| 80 | +function getGlobalPrefixCls() { |
| 81 | + return globalPrefixCls.value || defaultPrefixCls; |
| 82 | +} |
| 83 | + |
| 84 | +export const globalConfig = () => ({ |
| 85 | + getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => { |
| 86 | + if (customizePrefixCls) return customizePrefixCls; |
| 87 | + return suffixCls ? `${getGlobalPrefixCls()}-${suffixCls}` : getGlobalPrefixCls(); |
| 88 | + }, |
| 89 | + getRootPrefixCls: (rootPrefixCls?: string, customizePrefixCls?: string) => { |
| 90 | + // Customize rootPrefixCls is first priority |
| 91 | + if (rootPrefixCls) { |
| 92 | + return rootPrefixCls; |
| 93 | + } |
| 94 | + |
| 95 | + // If Global prefixCls provided, use this |
| 96 | + if (globalPrefixCls.value) { |
| 97 | + return globalPrefixCls.value; |
| 98 | + } |
| 99 | + |
| 100 | + // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls |
| 101 | + if (customizePrefixCls && customizePrefixCls.includes('-')) { |
| 102 | + return customizePrefixCls.replace(/^(.*)-[^-]*$/, '$1'); |
| 103 | + } |
| 104 | + |
| 105 | + // Fallback to default prefixCls |
| 106 | + return getGlobalPrefixCls(); |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
60 | 110 | export const configProviderProps = {
|
61 | 111 | getTargetContainer: {
|
62 | 112 | type: Function as PropType<() => HTMLElement>,
|
@@ -168,4 +218,12 @@ export const defaultConfigProvider: UnwrapRef<ConfigProviderProps> = reactive({
|
168 | 218 | direction: 'ltr',
|
169 | 219 | });
|
170 | 220 |
|
171 |
| -export default withInstall(ConfigProvider); |
| 221 | +ConfigProvider.config = setGlobalConfig; |
| 222 | +ConfigProvider.install = function (app: App) { |
| 223 | + app.component(ConfigProvider.name, ConfigProvider); |
| 224 | +}; |
| 225 | + |
| 226 | +export default ConfigProvider as typeof ConfigProvider & |
| 227 | + Plugin & { |
| 228 | + readonly config: typeof setGlobalConfig; |
| 229 | + }; |
0 commit comments