11import type { PropType , ExtractPropTypes , UnwrapRef , App , Plugin , WatchStopHandle } from 'vue' ;
2- import { reactive , provide , defineComponent , watch , ref , unref , watchEffect } from 'vue' ;
2+ import { reactive , provide , defineComponent , watch , watchEffect } from 'vue' ;
33import PropTypes from '../_util/vue-types' ;
44import defaultRenderEmpty from './renderEmpty' ;
55import type { RenderEmptyHandler } from './renderEmpty' ;
@@ -58,7 +58,47 @@ export const configConsumerProps = [
5858] ;
5959
6060export const defaultPrefixCls = 'ant' ;
61- const globalPrefixCls = ref < string > ( ) ;
61+
62+ function getGlobalPrefixCls ( ) {
63+ return globalConfigForApi . prefixCls || defaultPrefixCls ;
64+ }
65+ const globalConfigByCom = reactive < ConfigProviderProps > ( { } ) ;
66+ const globalConfigBySet = reactive < ConfigProviderProps > ( { } ) ; // 权重最大
67+ export const globalConfigForApi = reactive <
68+ ConfigProviderProps & {
69+ getRootPrefixCls ?: ( rootPrefixCls ?: string , customizePrefixCls ?: string ) => string ;
70+ }
71+ > ( { } ) ;
72+
73+ watchEffect ( ( ) => {
74+ Object . assign ( globalConfigForApi , globalConfigByCom , globalConfigBySet ) ;
75+ globalConfigForApi . prefixCls = getGlobalPrefixCls ( ) ;
76+ globalConfigForApi . getPrefixCls = ( suffixCls ?: string , customizePrefixCls ?: string ) => {
77+ if ( customizePrefixCls ) return customizePrefixCls ;
78+ return suffixCls
79+ ? `${ globalConfigForApi . prefixCls } -${ suffixCls } `
80+ : globalConfigForApi . prefixCls ;
81+ } ;
82+ globalConfigForApi . getRootPrefixCls = ( rootPrefixCls ?: string , customizePrefixCls ?: string ) => {
83+ // Customize rootPrefixCls is first priority
84+ if ( rootPrefixCls ) {
85+ return rootPrefixCls ;
86+ }
87+
88+ // If Global prefixCls provided, use this
89+ if ( globalConfigForApi . prefixCls ) {
90+ return globalConfigForApi . prefixCls ;
91+ }
92+
93+ // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls
94+ if ( customizePrefixCls && customizePrefixCls . includes ( '-' ) ) {
95+ return customizePrefixCls . replace ( / ^ ( .* ) - [ ^ - ] * $ / , '$1' ) ;
96+ }
97+
98+ // Fallback to default prefixCls
99+ return getGlobalPrefixCls ( ) ;
100+ } ;
101+ } ) ;
62102
63103type GlobalConfigProviderProps = {
64104 prefixCls ?: MaybeRef < ConfigProviderProps [ 'prefixCls' ] > ;
@@ -70,17 +110,10 @@ const setGlobalConfig = (params: GlobalConfigProviderProps) => {
70110 stopWatchEffect ( ) ;
71111 }
72112 stopWatchEffect = watchEffect ( ( ) => {
73- const prefixCls = unref ( params . prefixCls ) ;
74- if ( prefixCls !== undefined ) {
75- globalPrefixCls . value = prefixCls ;
76- }
113+ Object . assign ( globalConfigBySet , reactive ( params ) ) ;
77114 } ) ;
78115} ;
79116
80- function getGlobalPrefixCls ( ) {
81- return globalPrefixCls . value || defaultPrefixCls ;
82- }
83-
84117export const globalConfig = ( ) => ( {
85118 getPrefixCls : ( suffixCls ?: string , customizePrefixCls ?: string ) => {
86119 if ( customizePrefixCls ) return customizePrefixCls ;
@@ -93,8 +126,8 @@ export const globalConfig = () => ({
93126 }
94127
95128 // If Global prefixCls provided, use this
96- if ( globalPrefixCls . value ) {
97- return globalPrefixCls . value ;
129+ if ( globalConfigForApi . prefixCls ) {
130+ return globalConfigForApi . prefixCls ;
98131 }
99132
100133 // [Legacy] If customize prefixCls provided, we cut it to get the prefixCls
@@ -148,6 +181,8 @@ export const configProviderProps = {
148181 form : {
149182 type : Object as PropType < { requiredMark ?: RequiredMark } > ,
150183 } ,
184+ // internal use
185+ notUpdateGlobalConfig : Boolean ,
151186} ;
152187
153188export type ConfigProviderProps = Partial < ExtractPropTypes < typeof configProviderProps > > ;
@@ -193,6 +228,12 @@ const ConfigProvider = defineComponent({
193228 } ,
194229 ) ;
195230 } ) ;
231+ if ( ! props . notUpdateGlobalConfig ) {
232+ Object . assign ( globalConfigByCom , configProvider ) ;
233+ watch ( configProvider , ( ) => {
234+ Object . assign ( globalConfigByCom , configProvider ) ;
235+ } ) ;
236+ }
196237
197238 provide ( 'configProvider' , configProvider ) ;
198239
0 commit comments