@@ -100,11 +100,11 @@ type TransformProps<T> = {
100
100
: ConstructorToType < T [ K ] >
101
101
}
102
102
103
- function baseStyled < T extends object > ( target : string | InstanceType < any > , propsDefinition ?: PropsDefinition < T > ) : StyledComponent < T > {
103
+ function baseStyled < T extends object > ( target : string | InstanceType < any > , propsDefinition ?: PropsDefinition < T > , defaultAttrs ?: unknown ) : StyledComponent < T > {
104
104
if ( ! isValidElementType ( target ) ) {
105
105
throw new Error ( 'The element is invalid.' )
106
106
}
107
- let defaultAttrs : unknown
107
+
108
108
function styledComponent < P > (
109
109
stylesOrProps : TemplateStringsArray | PropsDefinition < P > | CSSStyleObject | StyleFunction < P & TransformProps < T > > ,
110
110
...expressions : (
@@ -132,25 +132,25 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
132
132
}
133
133
else {
134
134
// 是props定义
135
- return baseStyled ( target , { ...propsDefinition , ...stylesOrProps } as PropsDefinition < T & P > ) as StyledComponent < T & P >
135
+ return baseStyled ( target , { ...propsDefinition , ...stylesOrProps } as PropsDefinition < T & P > , defaultAttrs ) as StyledComponent < T & P >
136
136
}
137
137
}
138
138
139
139
// 正常的样式模板字符串处理
140
140
const cssStringsWithExpression = insertExpressions ( stylesOrProps as TemplateStringsArray , expressions )
141
- return createStyledComponent < P > ( cssStringsWithExpression )
141
+ return createStyledComponent < P > ( cssStringsWithExpression , defaultAttrs )
142
142
}
143
143
144
144
styledComponent . attrs = function < A = object > (
145
145
attrs : object | ( ( props : PropsDefinition < T > & A ) => object ) ,
146
146
) {
147
- defaultAttrs = attrs
148
- return styledComponent
147
+ // 创建一个新的 styled 组件实例,而不是修改当前实例的共享状态
148
+ return baseStyled ( target , propsDefinition , attrs ) as StyledComponent < A & T >
149
149
}
150
150
151
151
// 添加props方法支持链式调用
152
152
styledComponent . props = function < P extends object > ( newPropsDefinition : PropsDefinition < P > ) {
153
- return baseStyled ( target , { ...propsDefinition , ...newPropsDefinition } as PropsDefinition < T & P > ) as StyledComponent < T & P >
153
+ return baseStyled ( target , { ...propsDefinition , ...newPropsDefinition } as PropsDefinition < T & P > , defaultAttrs ) as StyledComponent < T & P >
154
154
}
155
155
156
156
// 将CSS对象转换为CSS字符串
@@ -168,7 +168,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
168
168
function createStyledComponentFromObject < P > ( cssObject : CSSStyleObject ) {
169
169
const cssString = cssObjectToString ( cssObject )
170
170
const cssWithExpression = [ cssString ] as ExpressionType < any > [ ]
171
- return createStyledComponent < P > ( cssWithExpression )
171
+ return createStyledComponent < P > ( cssWithExpression , defaultAttrs )
172
172
}
173
173
174
174
// 从样式函数创建组件
@@ -178,10 +178,10 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
178
178
const cssObject = styleFunction ( props )
179
179
return cssObjectToString ( cssObject )
180
180
} ] as ExpressionType < any > [ ]
181
- return createStyledComponent < P > ( cssWithExpression )
181
+ return createStyledComponent < P > ( cssWithExpression , defaultAttrs )
182
182
}
183
183
184
- function createStyledComponent < P > ( cssWithExpression : ExpressionType < any > [ ] ) {
184
+ function createStyledComponent < P > ( cssWithExpression : ExpressionType < any > [ ] , componentDefaultAttrs ?: unknown ) {
185
185
let type : string = target
186
186
if ( isVueComponent ( target ) ) {
187
187
type = 'vue-component'
@@ -195,11 +195,11 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
195
195
const component = defineComponent (
196
196
( props , { slots } ) => {
197
197
const internalAttrs = computed < Record < string , any > > ( ( ) => {
198
- if ( typeof defaultAttrs === 'function' ) {
199
- return defaultAttrs ( props )
198
+ if ( typeof componentDefaultAttrs === 'function' ) {
199
+ return componentDefaultAttrs ( props )
200
200
}
201
- if ( typeof defaultAttrs === 'object' ) {
202
- return defaultAttrs
201
+ if ( typeof componentDefaultAttrs === 'object' ) {
202
+ return componentDefaultAttrs
203
203
}
204
204
return { }
205
205
} )
0 commit comments