From 69abefbab382a5189c340412ee0b3d2345a4a521 Mon Sep 17 00:00:00 2001 From: Matthieu Hochlander Date: Mon, 9 Jun 2025 18:10:14 +0200 Subject: [PATCH 1/2] Fix infinite rerenders for a disabled boolean input. --- packages/ra-core/src/form/useApplyInputDefaultValues.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/ra-core/src/form/useApplyInputDefaultValues.ts b/packages/ra-core/src/form/useApplyInputDefaultValues.ts index 4ad17d6ee2b..1857964f42d 100644 --- a/packages/ra-core/src/form/useApplyInputDefaultValues.ts +++ b/packages/ra-core/src/form/useApplyInputDefaultValues.ts @@ -32,7 +32,7 @@ export const useApplyInputDefaultValues = ({ isArrayInput, fieldArrayInputControl, }: Props) => { - const { defaultValue, source } = inputProps; + const { defaultValue, source, disabled } = inputProps; const finalSource = useWrappedSource(source); const record = useRecordContext(inputProps); @@ -46,6 +46,9 @@ export const useApplyInputDefaultValues = ({ if ( defaultValue == null || formValue != null || + // When the input is disabled, its value may always be undefined, no matter the default value. + // This prevents from trying to reset the value indefinitely. + disabled || // We check strictly for undefined to avoid setting default value // when the field is null recordValue !== undefined || From 91c5a836b75c2616377c866a12d460c4261cfdd8 Mon Sep 17 00:00:00 2001 From: Matthieu Hochlander Date: Tue, 10 Jun 2025 11:17:12 +0200 Subject: [PATCH 2/2] Improve Disabled story to test default value when boolean input is disabled. --- .../src/input/BooleanInput.stories.tsx | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/BooleanInput.stories.tsx b/packages/ra-ui-materialui/src/input/BooleanInput.stories.tsx index e3e7b675584..0a46a94dfcb 100644 --- a/packages/ra-ui-materialui/src/input/BooleanInput.stories.tsx +++ b/packages/ra-ui-materialui/src/input/BooleanInput.stories.tsx @@ -18,12 +18,35 @@ export const Basic = () => ( ); -export const Disabled = () => ( +export const Disabled = ({ + defaultValue, + disabled, +}: { + defaultValue: boolean; + disabled: boolean; +}) => ( - + ); +Disabled.argTypes = { + defaultValue: { + control: 'boolean', + }, + disabled: { + control: 'boolean', + }, +}; +Disabled.args = { + defaultValue: true, + disabled: true, +}; + export const ReadOnly = () => (