File tree Expand file tree Collapse file tree
packages/ra-ui-materialui/src/field Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -89,9 +89,6 @@ BooleanFieldImpl.displayName = 'BooleanFieldImpl';
8989
9090export const BooleanField = genericMemo ( BooleanFieldImpl ) ;
9191
92- BooleanField . propTypes = BooleanFieldImpl . propTypes ;
93- BooleanField . displayName = 'BooleanField' ;
94-
9592export interface BooleanFieldProps <
9693 RecordType extends Record < string , unknown > = Record < string , any >
9794> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -51,9 +51,6 @@ ChipFieldImpl.displayName = 'ChipFieldImpl';
5151
5252export const ChipField = genericMemo ( ChipFieldImpl ) ;
5353
54- ChipField . propTypes = ChipFieldImpl . propTypes ;
55- ChipField . displayName = 'ChipField' ;
56-
5754export interface ChipFieldProps <
5855 RecordType extends Record < string , unknown > = Record < string , any >
5956> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -136,9 +136,6 @@ DateFieldImpl.displayName = 'DateFieldImpl';
136136
137137export const DateField = genericMemo ( DateFieldImpl ) ;
138138
139- DateField . propTypes = DateFieldImpl . propTypes ;
140- DateField . displayName = 'DateField' ;
141-
142139export interface DateFieldProps <
143140 RecordType extends Record < string , unknown > = Record < string , any >
144141> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -49,9 +49,6 @@ EmailFieldImpl.displayName = 'EmailFieldImpl';
4949
5050export const EmailField = genericMemo ( EmailFieldImpl ) ;
5151
52- EmailField . propTypes = EmailFieldImpl . propTypes ;
53- EmailField . displayName = 'EmailField' ;
54-
5552export interface EmailFieldProps <
5653 RecordType extends Record < string , unknown > = Record < string , any >
5754> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -97,21 +97,12 @@ NumberFieldImpl.propTypes = {
9797} ;
9898
9999// 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
101100NumberFieldImpl . 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.
105101NumberFieldImpl . defaultProps = {
106102 textAlign : 'right' ,
107103} ;
108104
109105export 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 ;
115106
116107export interface NumberFieldProps <
117108 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';
7373
7474export const RichTextField = genericMemo ( RichTextFieldImpl ) ;
7575
76- RichTextField . displayName = 'RichTextField' ;
77- RichTextField . propTypes = RichTextFieldImpl . propTypes ;
78-
7976// We only support the case when sanitize() returns a string
8077// hence we need to force the RETURN_DOM_FRAGMENT and RETURN_DOM
8178// options to false
Original file line number Diff line number Diff line change @@ -142,9 +142,6 @@ SelectFieldImpl.propTypes = {
142142 translateChoice : PropTypes . bool ,
143143} ;
144144
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.
148145SelectFieldImpl . defaultProps = {
149146 optionText : 'name' ,
150147 optionValue : 'id' ,
@@ -154,11 +151,6 @@ SelectFieldImpl.displayName = 'SelectFieldImpl';
154151
155152export const SelectField = genericMemo ( SelectFieldImpl ) ;
156153
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-
162154export interface SelectFieldProps <
163155 RecordType extends Record < string , unknown > = Record < string , any >
164156> extends ChoicesProps ,
Original file line number Diff line number Diff line change @@ -42,9 +42,6 @@ TextFieldImpl.displayName = 'TextFieldImpl';
4242
4343export const TextField = genericMemo ( TextFieldImpl ) ;
4444
45- TextField . displayName = 'TextField' ;
46- TextField . propTypes = TextFieldImpl . propTypes ;
47-
4845export interface TextFieldProps <
4946 RecordType extends Record < string , unknown > = Record < string , any >
5047> extends FieldProps < RecordType > ,
Original file line number Diff line number Diff line change @@ -47,8 +47,6 @@ UrlFieldImpl.propTypes = fieldPropTypes;
4747UrlFieldImpl . displayName = 'UrlFieldImpl' ;
4848
4949export const UrlField = genericMemo ( UrlFieldImpl ) ;
50- UrlField . propTypes = UrlFieldImpl . propTypes ;
51- UrlField . displayName = 'UrlField' ;
5250
5351export interface UrlFieldProps <
5452 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' ;
22
33/**
44 * A version of React.memo that preserves the original component type allowing it to accept generics.
55 * See {@link https://stackoverflow.com/a/70890101}
66 */
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