@@ -5,12 +5,12 @@ type Param<T> = string
5
5
| Record < string , boolean >
6
6
| [ string , { [ key in keyof T ] ?: T [ key ] } ]
7
7
| {
8
- [ key in keyof T ] ?: T [ key ] extends string | undefined
9
- ? Record < string , string >
10
- : T [ key ] extends boolean | undefined
11
- ? string
12
- : ( Record < string , string > | string )
13
- }
8
+ [ key in keyof T ] ?: T [ key ] extends string | undefined
9
+ ? Record < string , string >
10
+ : T [ key ] extends boolean | undefined
11
+ ? string
12
+ : ( Record < string , string > | string )
13
+ }
14
14
| ( ( valueByProp : T ) => any )
15
15
type ReturnType < T > = { default ?: Partial < T > } & ( ( valueByProp ?: T ) => string )
16
16
@@ -26,7 +26,13 @@ function cv<T extends Record<string, string | number | boolean>>(...params: Arra
26
26
function cv < T extends Record < string , string | number | boolean > > ( firstParam : TemplateStringsArray , ...params : Array < Param < T > > ) : ReturnType < T >
27
27
function cv < T extends Record < string , string | number | boolean > > ( firstParam : TemplateStringsArray | Param < T > , ...params : Array < Param < T > > ) : ReturnType < T > {
28
28
return function getClassNames ( valueByProp : T = { } as any ) {
29
- const mergedValueByProp = Object . assign ( { } , ( getClassNames as ReturnType < T > ) . default , valueByProp )
29
+ // 如果 valueByProps 中的屬性是 undefined 或是 null,則使用 default 中的值
30
+ const defaultProps = ( getClassNames as ReturnType < T > ) . default
31
+ const mergedValueByProp : any = defaultProps ? Object . assign ( { } , defaultProps ) : { }
32
+ for ( const eachProp in valueByProp ) {
33
+ const eachDefaultValue = defaultProps ?. [ eachProp ]
34
+ mergedValueByProp [ eachProp ] = valueByProp [ eachProp ] ?? eachDefaultValue
35
+ }
30
36
const isTemplateLiteral = Array . isArray ( firstParam ) && 'raw' in firstParam
31
37
const classesConditions : [ string , Record < string , string | number | boolean > ] [ ] = [ ]
32
38
const valuesByProp : Record < string , Record < string , string > > = { }
0 commit comments