1
1
/**
2
2
* Add keys, values of `defaultProps` that does not exist in `props`
3
- * @param { object } defaultProps
4
- * @param { object } props
5
- * @returns { object } resolved props
3
+ * @param defaultProps
4
+ * @param props
5
+ * @returns resolved props
6
6
*/
7
7
export default function resolveProps <
8
8
T extends {
@@ -14,36 +14,41 @@ export default function resolveProps<
14
14
> ( defaultProps : T , props : T ) {
15
15
const output = { ...props } ;
16
16
17
- ( Object . keys ( defaultProps ) as Array < keyof T > ) . forEach ( ( propName ) => {
18
- if ( propName . toString ( ) . match ( / ^ ( c o m p o n e n t s | s l o t s ) $ / ) ) {
19
- output [ propName ] = {
20
- ...( defaultProps [ propName ] as any ) ,
21
- ...( output [ propName ] as any ) ,
22
- } ;
23
- } else if ( propName . toString ( ) . match ( / ^ ( c o m p o n e n t s P r o p s | s l o t P r o p s ) $ / ) ) {
24
- const defaultSlotProps = ( defaultProps [ propName ] || { } ) as T [ keyof T ] ;
25
- const slotProps = props [ propName ] as { } as T [ keyof T ] ;
26
- output [ propName ] = { } as T [ keyof T ] ;
17
+ for ( const key in defaultProps ) {
18
+ if ( Object . prototype . hasOwnProperty . call ( defaultProps , key ) ) {
19
+ const propName = key as keyof T ;
27
20
28
- if ( ! slotProps || ! Object . keys ( slotProps ) ) {
29
- // Reduce the iteration if the slot props is empty
30
- output [ propName ] = defaultSlotProps ;
31
- } else if ( ! defaultSlotProps || ! Object . keys ( defaultSlotProps ) ) {
32
- // Reduce the iteration if the default slot props is empty
33
- output [ propName ] = slotProps ;
34
- } else {
35
- output [ propName ] = { ...slotProps } ;
36
- Object . keys ( defaultSlotProps ) . forEach ( ( slotPropName ) => {
37
- ( output [ propName ] as Record < string , unknown > ) [ slotPropName ] = resolveProps (
38
- ( defaultSlotProps as Record < string , any > ) [ slotPropName ] ,
39
- ( slotProps as Record < string , any > ) [ slotPropName ] ,
40
- ) ;
41
- } ) ;
21
+ if ( propName === 'components' || propName === 'slots' ) {
22
+ output [ propName ] = {
23
+ ...( defaultProps [ propName ] as any ) ,
24
+ ...( output [ propName ] as any ) ,
25
+ } ;
26
+ } else if ( propName === 'componentsProps' || propName === 'slotProps' ) {
27
+ const defaultSlotProps = defaultProps [ propName ] as T [ keyof T ] | undefined ;
28
+ const slotProps = props [ propName ] as { } as T [ keyof T ] | undefined ;
29
+
30
+ if ( ! slotProps ) {
31
+ output [ propName ] = defaultSlotProps || ( { } as T [ keyof T ] ) ;
32
+ } else if ( ! defaultSlotProps ) {
33
+ output [ propName ] = slotProps ;
34
+ } else {
35
+ output [ propName ] = { ...slotProps } ;
36
+
37
+ for ( const slotKey in defaultSlotProps ) {
38
+ if ( Object . prototype . hasOwnProperty . call ( defaultSlotProps , slotKey ) ) {
39
+ const slotPropName = slotKey ;
40
+ ( output [ propName ] as Record < string , unknown > ) [ slotPropName ] = resolveProps (
41
+ ( defaultSlotProps as Record < string , any > ) [ slotPropName ] ,
42
+ ( slotProps as Record < string , any > ) [ slotPropName ] ,
43
+ ) ;
44
+ }
45
+ }
46
+ }
47
+ } else if ( output [ propName ] === undefined ) {
48
+ output [ propName ] = defaultProps [ propName ] ;
42
49
}
43
- } else if ( output [ propName ] === undefined ) {
44
- output [ propName ] = defaultProps [ propName ] ;
45
50
}
46
- } ) ;
51
+ }
47
52
48
53
return output ;
49
54
}
0 commit comments