Skip to content

Commit 60a4332

Browse files
authored
Merge pull request #9124 from marmelab/9112-destructuring-defaultprops
Remove deprecated defaultProps in jsx components
2 parents d92e02f + 63eb7ca commit 60a4332

23 files changed

+118
-188
lines changed

packages/ra-ui-materialui/src/auth/Login.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { LoginForm as DefaultLoginForm } from './LoginForm';
2828
* );
2929
*/
3030
export const Login = (props: LoginProps) => {
31-
const { children, backgroundImage, ...rest } = props;
31+
const { children = defaultLoginForm, backgroundImage, ...rest } = props;
3232
const containerRef = useRef<HTMLDivElement>();
3333
let backgroundImageLoaded = false;
3434
const checkAuth = useCheckAuth();
@@ -79,6 +79,8 @@ export const Login = (props: LoginProps) => {
7979
);
8080
};
8181

82+
const defaultLoginForm = <DefaultLoginForm />;
83+
8284
export interface LoginProps extends HtmlHTMLAttributes<HTMLDivElement> {
8385
backgroundImage?: string;
8486
children?: ReactNode;
@@ -127,7 +129,3 @@ Login.propTypes = {
127129
children: PropTypes.node,
128130
className: PropTypes.string,
129131
};
130-
131-
Login.defaultProps = {
132-
children: <DefaultLoginForm />,
133-
};

packages/ra-ui-materialui/src/button/BulkUpdateButton.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ import { MutationMode } from 'ra-core';
3434
* );
3535
*/
3636
export const BulkUpdateButton = (props: BulkUpdateButtonProps) => {
37-
const { mutationMode, ...rest } = props;
37+
const { mutationMode = 'undoable', data = defaultData, ...rest } = props;
3838

3939
return mutationMode === 'undoable' ? (
40-
<BulkUpdateWithUndoButton {...rest} />
40+
<BulkUpdateWithUndoButton data={data} {...rest} />
4141
) : (
42-
<BulkUpdateWithConfirmButton mutationMode={mutationMode} {...rest} />
42+
<BulkUpdateWithConfirmButton
43+
mutationMode={mutationMode}
44+
data={data}
45+
{...rest}
46+
/>
4347
);
4448
};
4549

@@ -58,7 +62,4 @@ BulkUpdateButton.propTypes = {
5862
icon: PropTypes.element,
5963
};
6064

61-
BulkUpdateButton.defaultProps = {
62-
mutationMode: 'undoable',
63-
data: [],
64-
};
65+
const defaultData = [];

packages/ra-ui-materialui/src/field/ReferenceField.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const ReferenceField = <
6363
>(
6464
props: ReferenceFieldProps<RecordType, ReferenceRecordType>
6565
) => {
66-
const { source, emptyText, ...rest } = props;
66+
const { source, emptyText, link = 'edit', ...rest } = props;
6767
const record = useRecordContext<RecordType>(props);
6868
const id = get(record, source);
6969
const translate = useTranslate();
@@ -77,6 +77,7 @@ export const ReferenceField = <
7777
) : (
7878
<NonEmptyReferenceField<RecordType, ReferenceRecordType>
7979
{...rest}
80+
link={link}
8081
emptyText={emptyText}
8182
record={record}
8283
id={id as Identifier}
@@ -102,11 +103,7 @@ ReferenceField.propTypes = {
102103
PropTypes.string,
103104
PropTypes.bool,
104105
PropTypes.func,
105-
]).isRequired,
106-
};
107-
108-
ReferenceField.defaultProps = {
109-
link: 'edit',
106+
]),
110107
};
111108

112109
export interface ReferenceFieldProps<

packages/ra-ui-materialui/src/field/ReferenceManyField.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ export const ReferenceManyField = <
6767
) => {
6868
const {
6969
children,
70-
filter,
70+
filter = defaultFilter,
7171
page = 1,
7272
pagination = null,
73-
perPage,
73+
perPage = 25,
7474
reference,
7575
resource,
76-
sort,
77-
source,
76+
sort = defaultSort,
77+
source = 'id',
7878
target,
7979
} = props;
8080
const record = useRecordContext(props);
@@ -128,21 +128,14 @@ ReferenceManyField.propTypes = {
128128
resource: PropTypes.string,
129129
sortBy: PropTypes.string,
130130
sortByOrder: fieldPropTypes.sortByOrder,
131-
source: PropTypes.string.isRequired,
131+
source: PropTypes.string,
132132
sort: PropTypes.exact({
133133
field: PropTypes.string,
134134
order: PropTypes.string,
135135
}),
136136
target: PropTypes.string.isRequired,
137137
};
138138

139-
ReferenceManyField.defaultProps = {
140-
filter: {},
141-
perPage: 25,
142-
sort: { field: 'id', order: 'DESC' },
143-
source: 'id',
144-
};
145-
146139
// FIXME kept for backwards compatibility, unused, to be removed in v5
147140
export const ReferenceManyFieldView: FC<ReferenceManyFieldViewProps> = props => {
148141
const { children, pagination } = props;
@@ -181,3 +174,6 @@ ReferenceManyFieldView.propTypes = {
181174
reference: PropTypes.string,
182175
setSort: PropTypes.func,
183176
};
177+
178+
const defaultFilter = {};
179+
const defaultSort = { field: 'id', order: 'DESC' };

packages/ra-ui-materialui/src/field/ReferenceOneField.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const ReferenceOneField = <
3535
const {
3636
children,
3737
reference,
38-
source,
38+
source = 'id',
3939
target,
4040
emptyText,
4141
sort,
@@ -120,13 +120,12 @@ ReferenceOneField.propTypes = {
120120
label: fieldPropTypes.label,
121121
record: PropTypes.any,
122122
reference: PropTypes.string.isRequired,
123-
source: PropTypes.string.isRequired,
123+
source: PropTypes.string,
124124
target: PropTypes.string.isRequired,
125125
queryOptions: PropTypes.any,
126126
};
127127

128128
ReferenceOneField.defaultProps = {
129-
source: 'id',
130129
// disable sorting on this field by default as its default source prop ('id')
131130
// will match the default sort ({ field: 'id', order: 'DESC'})
132131
// leading to an incorrect sort indicator in a datagrid header

packages/ra-ui-materialui/src/field/SelectField.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ const SelectFieldImpl = <
8585
emptyText,
8686
source,
8787
choices,
88-
optionValue,
89-
optionText,
90-
translateChoice,
88+
optionValue = 'id',
89+
optionText = 'name',
90+
translateChoice = true,
9191
...rest
9292
} = props;
9393
const record = useRecordContext(props);
@@ -142,11 +142,6 @@ SelectFieldImpl.propTypes = {
142142
translateChoice: PropTypes.bool,
143143
};
144144

145-
SelectFieldImpl.defaultProps = {
146-
optionText: 'name',
147-
optionValue: 'id',
148-
translateChoice: true,
149-
};
150145
SelectFieldImpl.displayName = 'SelectFieldImpl';
151146

152147
export const SelectField = genericMemo(SelectFieldImpl);

packages/ra-ui-materialui/src/input/ArrayInput/ArrayInput.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,6 @@ export const ArrayInput = (props: ArrayInputProps) => {
204204
);
205205
};
206206

207-
ArrayInput.defaultProps = {
208-
options: {},
209-
};
210-
211207
export const getArrayInputError = error => {
212208
if (Array.isArray(error)) {
213209
return undefined;

packages/ra-ui-materialui/src/input/ArrayInput/SimpleFormIterator.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
4949
resource,
5050
source,
5151
disabled,
52-
disableAdd,
52+
disableAdd = false,
5353
disableClear,
54-
disableRemove,
54+
disableRemove = false,
5555
disableReordering,
5656
inline,
5757
getItemLabel = false,
@@ -229,11 +229,6 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
229229
) : null;
230230
};
231231

232-
SimpleFormIterator.defaultProps = {
233-
disableAdd: false,
234-
disableRemove: false,
235-
};
236-
237232
SimpleFormIterator.propTypes = {
238233
addButton: PropTypes.element,
239234
removeButton: PropTypes.element,

packages/ra-ui-materialui/src/input/BooleanInput.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const BooleanInput = (props: BooleanInputProps) => {
3030
resource,
3131
source,
3232
validate,
33-
options,
33+
options = defaultOptions,
3434
sx,
3535
...rest
3636
} = props;
@@ -114,12 +114,10 @@ BooleanInput.propTypes = {
114114
disabled: PropTypes.bool,
115115
};
116116

117-
BooleanInput.defaultProps = {
118-
options: {},
119-
};
120-
121117
export type BooleanInputProps = CommonInputProps &
122118
SwitchProps &
123119
Omit<FormGroupProps, 'defaultValue' | 'onChange' | 'onBlur' | 'onFocus'> & {
124-
options: SwitchProps;
120+
options?: SwitchProps;
125121
};
122+
123+
const defaultOptions = {};

packages/ra-ui-materialui/src/input/FileInputPreview.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ FileInputPreview.propTypes = {
5353
removeIcon: PropTypes.element,
5454
};
5555

56-
FileInputPreview.defaultProps = {
57-
file: undefined,
58-
};
59-
6056
const PREFIX = 'RaFileInputPreview';
6157

6258
const FileInputPreviewClasses = {

0 commit comments

Comments
 (0)