1
1
import 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' ;
3
3
import PropTypes from '../_util/vue-types' ;
4
4
import defaultRenderEmpty from './renderEmpty' ;
5
5
import type { RenderEmptyHandler } from './renderEmpty' ;
@@ -58,7 +58,47 @@ export const configConsumerProps = [
58
58
] ;
59
59
60
60
export 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
+ } ) ;
62
102
63
103
type GlobalConfigProviderProps = {
64
104
prefixCls ?: MaybeRef < ConfigProviderProps [ 'prefixCls' ] > ;
@@ -70,17 +110,10 @@ const setGlobalConfig = (params: GlobalConfigProviderProps) => {
70
110
stopWatchEffect ( ) ;
71
111
}
72
112
stopWatchEffect = watchEffect ( ( ) => {
73
- const prefixCls = unref ( params . prefixCls ) ;
74
- if ( prefixCls !== undefined ) {
75
- globalPrefixCls . value = prefixCls ;
76
- }
113
+ Object . assign ( globalConfigBySet , reactive ( params ) ) ;
77
114
} ) ;
78
115
} ;
79
116
80
- function getGlobalPrefixCls ( ) {
81
- return globalPrefixCls . value || defaultPrefixCls ;
82
- }
83
-
84
117
export const globalConfig = ( ) => ( {
85
118
getPrefixCls : ( suffixCls ?: string , customizePrefixCls ?: string ) => {
86
119
if ( customizePrefixCls ) return customizePrefixCls ;
@@ -93,8 +126,8 @@ export const globalConfig = () => ({
93
126
}
94
127
95
128
// If Global prefixCls provided, use this
96
- if ( globalPrefixCls . value ) {
97
- return globalPrefixCls . value ;
129
+ if ( globalConfigForApi . prefixCls ) {
130
+ return globalConfigForApi . prefixCls ;
98
131
}
99
132
100
133
// [Legacy] If customize prefixCls provided, we cut it to get the prefixCls
@@ -148,6 +181,8 @@ export const configProviderProps = {
148
181
form : {
149
182
type : Object as PropType < { requiredMark ?: RequiredMark } > ,
150
183
} ,
184
+ // internal use
185
+ notUpdateGlobalConfig : Boolean ,
151
186
} ;
152
187
153
188
export type ConfigProviderProps = Partial < ExtractPropTypes < typeof configProviderProps > > ;
@@ -193,6 +228,12 @@ const ConfigProvider = defineComponent({
193
228
} ,
194
229
) ;
195
230
} ) ;
231
+ if ( ! props . notUpdateGlobalConfig ) {
232
+ Object . assign ( globalConfigByCom , configProvider ) ;
233
+ watch ( configProvider , ( ) => {
234
+ Object . assign ( globalConfigByCom , configProvider ) ;
235
+ } ) ;
236
+ }
196
237
197
238
provide ( 'configProvider' , configProvider ) ;
198
239
0 commit comments