Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/ra-core/src/form/useApplyInputDefaultValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ||
Expand Down
27 changes: 25 additions & 2 deletions packages/ra-ui-materialui/src/input/BooleanInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,35 @@ export const Basic = () => (
</Wrapper>
);

export const Disabled = () => (
export const Disabled = ({
defaultValue,
disabled,
}: {
defaultValue: boolean;
disabled: boolean;
}) => (
<Wrapper>
<BooleanInput source="published" disabled />
<BooleanInput
source="published"
defaultValue={defaultValue}
disabled={disabled}
/>
</Wrapper>
);

Disabled.argTypes = {
defaultValue: {
control: 'boolean',
},
disabled: {
control: 'boolean',
},
};
Disabled.args = {
defaultValue: true,
disabled: true,
};

export const ReadOnly = () => (
<Wrapper>
<BooleanInput source="published" readOnly />
Expand Down
Loading