File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed
packages/shared/src/utils Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { render } from '@testing-library/react' ;
2
+ import { mergeReactDefaultProps } from '../' ;
3
+ import React from 'react' ;
4
+
5
+ describe ( 'mergeReactDefaultProps' , ( ) => {
6
+ test ( 'base' , ( ) => {
7
+ const receiveProps = jest . fn ( ) ;
8
+ const defaultProps = {
9
+ emptyStr : 5 ,
10
+ undef : 1 ,
11
+ null : 2 ,
12
+ zero : 4 ,
13
+ nan : 3 ,
14
+ } ;
15
+ const App : React . FC < any > = React . forwardRef < HTMLDivElement , any > (
16
+ ( props : any , ref ) => {
17
+ receiveProps ( props ) ;
18
+ return < div ref = { ref } > 1</ div > ;
19
+ } ,
20
+ ) ;
21
+ App . defaultProps = defaultProps ;
22
+ const props = {
23
+ undef : undefined ,
24
+ emptyStr : '' ,
25
+ null : null ,
26
+ nan : NaN ,
27
+ zero : 0 ,
28
+ } ;
29
+ render ( < App { ...props } /> ) ;
30
+ const result = {
31
+ emptyStr : '' ,
32
+ null : null ,
33
+ nan : NaN ,
34
+ undef : 1 ,
35
+ zero : 0 ,
36
+ } ;
37
+ expect ( receiveProps . mock . calls [ 0 ] ) . toEqual ( [ result ] ) ;
38
+
39
+ expect ( mergeReactDefaultProps ( props , defaultProps as any ) ) . toEqual ( result ) ;
40
+ } ) ;
41
+ } ) ;
Original file line number Diff line number Diff line change @@ -5,3 +5,4 @@ export * from './getSizeClassName';
5
5
export * from './getClasses' ;
6
6
export * from './isSameReactEl' ;
7
7
export * from './forwardRefs' ;
8
+ export * from './mergeReactDefaultProps' ;
Original file line number Diff line number Diff line change
1
+ import { RequiredPart } from '@tool-pack/types' ;
2
+ import { defaults } from '@tool-pack/basic' ;
3
+
4
+ /**
5
+ * 用于代替 react 的 defaultProps 合并功能
6
+ */
7
+ export function mergeReactDefaultProps < const T , const P > (
8
+ props : T ,
9
+ defaultProps : P ,
10
+ ) : keyof P extends keyof T ? RequiredPart < T , keyof P > : T & P {
11
+ return defaults ( { ...props } , defaultProps ) as any ;
12
+ }
You can’t perform that action at this time.
0 commit comments