File tree Expand file tree Collapse file tree 10 files changed +16
-39
lines changed
packages/ra-ui-materialui/src/field Expand file tree Collapse file tree 10 files changed +16
-39
lines changed Original file line number Diff line number Diff line change @@ -89,9 +89,6 @@ BooleanFieldImpl.displayName = 'BooleanFieldImpl';
89
89
90
90
export const BooleanField = genericMemo ( BooleanFieldImpl ) ;
91
91
92
- BooleanField . propTypes = BooleanFieldImpl . propTypes ;
93
- BooleanField . displayName = 'BooleanField' ;
94
-
95
92
export interface BooleanFieldProps <
96
93
RecordType extends Record < string , unknown > = Record < string , any >
97
94
> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -51,9 +51,6 @@ ChipFieldImpl.displayName = 'ChipFieldImpl';
51
51
52
52
export const ChipField = genericMemo ( ChipFieldImpl ) ;
53
53
54
- ChipField . propTypes = ChipFieldImpl . propTypes ;
55
- ChipField . displayName = 'ChipField' ;
56
-
57
54
export interface ChipFieldProps <
58
55
RecordType extends Record < string , unknown > = Record < string , any >
59
56
> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -136,9 +136,6 @@ DateFieldImpl.displayName = 'DateFieldImpl';
136
136
137
137
export const DateField = genericMemo ( DateFieldImpl ) ;
138
138
139
- DateField . propTypes = DateFieldImpl . propTypes ;
140
- DateField . displayName = 'DateField' ;
141
-
142
139
export interface DateFieldProps <
143
140
RecordType extends Record < string , unknown > = Record < string , any >
144
141
> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -49,9 +49,6 @@ EmailFieldImpl.displayName = 'EmailFieldImpl';
49
49
50
50
export const EmailField = genericMemo ( EmailFieldImpl ) ;
51
51
52
- EmailField . propTypes = EmailFieldImpl . propTypes ;
53
- EmailField . displayName = 'EmailField' ;
54
-
55
52
export interface EmailFieldProps <
56
53
RecordType extends Record < string , unknown > = Record < string , any >
57
54
> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -97,21 +97,12 @@ NumberFieldImpl.propTypes = {
97
97
} ;
98
98
99
99
// what? TypeScript loses the displayName if we don't set it explicitly
100
- // Declaring it first on the NumberFieldImpl makes TS happy to accept it on NumberField
101
100
NumberFieldImpl . displayName = 'NumberFieldImpl' ;
102
- // We have to set the defaultProps on both the NumberFieldImpl and NumberField.
103
- // On NumberFieldImpl because that will make it possible to reference defaultProps in user components by making
104
- // TS happy and allows us to also declare defaultProps on NumberField.
105
101
NumberFieldImpl . defaultProps = {
106
102
textAlign : 'right' ,
107
103
} ;
108
104
109
105
export const NumberField = genericMemo ( NumberFieldImpl ) ;
110
- NumberField . displayName = 'NumberField' ;
111
- // On NumberField because that will allow children inspection to work.
112
- NumberField . defaultProps = NumberFieldImpl . defaultProps ;
113
-
114
- NumberField . propTypes = NumberFieldImpl . propTypes ;
115
106
116
107
export interface NumberFieldProps <
117
108
RecordType extends Record < string , unknown > = Record < string , any >
Original file line number Diff line number Diff line change @@ -73,9 +73,6 @@ RichTextFieldImpl.displayName = 'RichTextFieldImpl';
73
73
74
74
export const RichTextField = genericMemo ( RichTextFieldImpl ) ;
75
75
76
- RichTextField . displayName = 'RichTextField' ;
77
- RichTextField . propTypes = RichTextFieldImpl . propTypes ;
78
-
79
76
// We only support the case when sanitize() returns a string
80
77
// hence we need to force the RETURN_DOM_FRAGMENT and RETURN_DOM
81
78
// options to false
Original file line number Diff line number Diff line change @@ -142,9 +142,6 @@ SelectFieldImpl.propTypes = {
142
142
translateChoice : PropTypes . bool ,
143
143
} ;
144
144
145
- // We have to set the defaultProps on both the SelectFieldImpl and NumberField.
146
- // On SelectFieldImpl because that will make it possible to reference defaultProps in user components by making
147
- // TS happy and allows us to also declare defaultProps on NumberField.
148
145
SelectFieldImpl . defaultProps = {
149
146
optionText : 'name' ,
150
147
optionValue : 'id' ,
@@ -154,11 +151,6 @@ SelectFieldImpl.displayName = 'SelectFieldImpl';
154
151
155
152
export const SelectField = genericMemo ( SelectFieldImpl ) ;
156
153
157
- // On SelectField because that will allow children inspection to work.
158
- SelectField . defaultProps = SelectFieldImpl . defaultProps ;
159
- SelectField . propTypes = SelectFieldImpl . propTypes ;
160
- SelectField . displayName = 'SelectField' ;
161
-
162
154
export interface SelectFieldProps <
163
155
RecordType extends Record < string , unknown > = Record < string , any >
164
156
> extends ChoicesProps ,
Original file line number Diff line number Diff line change @@ -42,9 +42,6 @@ TextFieldImpl.displayName = 'TextFieldImpl';
42
42
43
43
export const TextField = genericMemo ( TextFieldImpl ) ;
44
44
45
- TextField . displayName = 'TextField' ;
46
- TextField . propTypes = TextFieldImpl . propTypes ;
47
-
48
45
export interface TextFieldProps <
49
46
RecordType extends Record < string , unknown > = Record < string , any >
50
47
> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -47,8 +47,6 @@ UrlFieldImpl.propTypes = fieldPropTypes;
47
47
UrlFieldImpl . displayName = 'UrlFieldImpl' ;
48
48
49
49
export const UrlField = genericMemo ( UrlFieldImpl ) ;
50
- UrlField . propTypes = UrlFieldImpl . propTypes ;
51
- UrlField . displayName = 'UrlField' ;
52
50
53
51
export interface UrlFieldProps <
54
52
RecordType extends Record < string , unknown > = Record < string , any >
Original file line number Diff line number Diff line change 1
- import { memo } from 'react' ;
1
+ import { FunctionComponent , memo } from 'react' ;
2
2
3
3
/**
4
4
* A version of React.memo that preserves the original component type allowing it to accept generics.
5
5
* See {@link https://stackoverflow.com/a/70890101}
6
6
*/
7
- export const genericMemo : < T > ( component : T ) => T = memo ;
7
+ export const genericMemo : < T extends FunctionComponent > ( component : T ) => T = <
8
+ T extends FunctionComponent
9
+ > (
10
+ component : T
11
+ ) => {
12
+ const result = ( memo ( component ) as unknown ) as T ;
13
+
14
+ // We have to set the propTypes, defaultProps and displayName on both the field implementation and the memoized version.
15
+ // On the implementation so that the memoized version can pick them up and users may reference the defaultProps in their components.
16
+ // On the memoized version so that components that inspect their children props may read them.
17
+ result . propTypes = component . propTypes ;
18
+ result . defaultProps = component . defaultProps ;
19
+ result . displayName = component . displayName . replace ( 'Impl' , '' ) ;
20
+ return result ;
21
+ } ;
You can’t perform that action at this time.
0 commit comments