Skip to content

Commit d3a29a6

Browse files
authored
Merge pull request #10773 from marmelab/fix-disabled-boolean-input
Fix disabled boolean input
2 parents 9f04aa3 + 91c5a83 commit d3a29a6

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/ra-core/src/form/useApplyInputDefaultValues.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const useApplyInputDefaultValues = ({
3232
isArrayInput,
3333
fieldArrayInputControl,
3434
}: Props) => {
35-
const { defaultValue, source } = inputProps;
35+
const { defaultValue, source, disabled } = inputProps;
3636
const finalSource = useWrappedSource(source);
3737

3838
const record = useRecordContext(inputProps);
@@ -46,6 +46,9 @@ export const useApplyInputDefaultValues = ({
4646
if (
4747
defaultValue == null ||
4848
formValue != null ||
49+
// When the input is disabled, its value may always be undefined, no matter the default value.
50+
// This prevents from trying to reset the value indefinitely.
51+
disabled ||
4952
// We check strictly for undefined to avoid setting default value
5053
// when the field is null
5154
recordValue !== undefined ||

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,35 @@ export const Basic = () => (
1818
</Wrapper>
1919
);
2020

21-
export const Disabled = () => (
21+
export const Disabled = ({
22+
defaultValue,
23+
disabled,
24+
}: {
25+
defaultValue: boolean;
26+
disabled: boolean;
27+
}) => (
2228
<Wrapper>
23-
<BooleanInput source="published" disabled />
29+
<BooleanInput
30+
source="published"
31+
defaultValue={defaultValue}
32+
disabled={disabled}
33+
/>
2434
</Wrapper>
2535
);
2636

37+
Disabled.argTypes = {
38+
defaultValue: {
39+
control: 'boolean',
40+
},
41+
disabled: {
42+
control: 'boolean',
43+
},
44+
};
45+
Disabled.args = {
46+
defaultValue: true,
47+
disabled: true,
48+
};
49+
2750
export const ReadOnly = () => (
2851
<Wrapper>
2952
<BooleanInput source="published" readOnly />

0 commit comments

Comments
 (0)