Skip to content

Commit 9b32623

Browse files
committed
feat: configProvider.config
1 parent d749fc9 commit 9b32623

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

components/_util/type.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { App, PropType, VNodeChild, Plugin } from 'vue';
1+
import type { App, PropType, VNodeChild, Plugin, Ref } from 'vue';
22

33
// https://stackoverflow.com/questions/46176165/ways-to-get-string-literal-type-of-array-values-without-enum-overhead
44
export const tuple = <T extends string[]>(...args: T) => args;
@@ -39,3 +39,5 @@ export const withInstall = <T>(comp: T) => {
3939

4040
return comp as T & Plugin;
4141
};
42+
43+
export type MaybeRef<T> = T | Ref<T>;

components/config-provider/index.tsx

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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';
33
import PropTypes from '../_util/vue-types';
44
import defaultRenderEmpty from './renderEmpty';
55
import type { RenderEmptyHandler } from './renderEmpty';
66
import type { Locale } from '../locale-provider';
77
import LocaleProvider, { ANT_MARK } from '../locale-provider';
88
import type { TransformCellTextProps } from '../table/interface';
99
import LocaleReceiver from '../locale-provider/LocaleReceiver';
10-
import { withInstall } from '../_util/type';
1110
import type { RequiredMark } from '../form/Form';
11+
import type { MaybeRef } from '../_util/type';
1212

1313
export type SizeType = 'small' | 'middle' | 'large' | undefined;
1414

@@ -57,6 +57,56 @@ export const configConsumerProps = [
5757
'pageHeader',
5858
];
5959

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+
60110
export const configProviderProps = {
61111
getTargetContainer: {
62112
type: Function as PropType<() => HTMLElement>,
@@ -168,4 +218,12 @@ export const defaultConfigProvider: UnwrapRef<ConfigProviderProps> = reactive({
168218
direction: 'ltr',
169219
});
170220

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+
};

components/modal/confirm.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import type { ModalFuncProps } from './Modal';
44
import { destroyFns } from './Modal';
55

66
import Omit from 'omit.js';
7+
import ConfigProvider, { globalConfig } from '../config-provider';
8+
9+
let defaultRootPrefixCls = '';
10+
11+
function getRootPrefixCls() {
12+
return defaultRootPrefixCls;
13+
}
714

815
const confirm = (config: ModalFuncProps) => {
916
const div = document.createElement('div');
@@ -52,8 +59,15 @@ const confirm = (config: ModalFuncProps) => {
5259
}
5360
}
5461
}
55-
const Wrapper = p => {
56-
return p.vIf ? <ConfirmDialog {...p}></ConfirmDialog> : null;
62+
const Wrapper = (p: ModalFuncProps & { vIf: boolean }) => {
63+
const { getPrefixCls } = globalConfig();
64+
const rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
65+
const prefixCls = p.prefixCls || `${rootPrefixCls}-modal`;
66+
return p.vIf ? (
67+
<ConfigProvider prefixCls={rootPrefixCls}>
68+
<ConfirmDialog {...p} prefixCls={prefixCls}></ConfirmDialog>
69+
</ConfigProvider>
70+
) : null;
5771
};
5872
function render(props: ModalFuncProps) {
5973
const vm = createVNode(Wrapper, { ...props, vIf: true });

site/debugger/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// debugger tsx
2-
import Demo from '../../components/table/demo/index.vue';
2+
import Demo from '../../components/modal/demo/index.vue';
33

44
export default {
55
render() {

0 commit comments

Comments
 (0)