Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: initialValue prop not used when input value is null #7133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions packages/ra-core/src/form/useInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,38 @@ describe('useInput', () => {
expect(queryByDisplayValue('foo')).not.toBeNull();
});

it('applies the initialValue when input has null value', () => {
const { queryByDisplayValue } = renderWithRedux(
<FormWithRedirect
record={{
title: null,
}}
onSubmit={jest.fn()}
render={() => {
return (
<Input
source="title"
resource="posts"
initialValue="foo"
>
{({ id, input }) => {
return (
<input
type="text"
id={id}
aria-label="Title"
{...input}
/>
);
}}
</Input>
);
}}
/>
);
expect(queryByDisplayValue('foo')).not.toBeNull();
});

it('does not apply the initialValue when input has a value of 0', () => {
const { queryByDisplayValue } = renderWithRedux(
<FormWithRedirect
Expand Down
6 changes: 5 additions & 1 deletion packages/ra-core/src/form/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ const useInput = ({
// This ensure dynamically added inputs have their value set correctly (ArrayInput for example).
// We don't do this for the form level initialValues so that it works as it should in final-form
// (ie. field level initialValue override form level initialValues for this field).
let inputInitialValue = get(record, source);
if (inputInitialValue === null || inputInitialValue === undefined) {
inputInitialValue = initialValue;
}
const { input, meta } = useFinalFormField(finalName, {
initialValue: get(record, source, initialValue),
thdk marked this conversation as resolved.
Show resolved Hide resolved
initialValue: inputInitialValue,
defaultValue,
validate: sanitizedValidate,
...options,
Expand Down