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 useInput incorrectly set default value for numbers #6374

Merged
merged 1 commit into from
Jun 21, 2021
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
121 changes: 120 additions & 1 deletion packages/ra-core/src/form/useInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as React from 'react';
import { FunctionComponent, ReactElement } from 'react';
import { render, fireEvent } from '@testing-library/react';
import { Form } from 'react-final-form';

import FormWithRedirect from './FormWithRedirect';
import useInput, { InputProps } from './useInput';
import { required } from './validate';
import { renderWithRedux } from '../../../ra-test/esm';

const Input: FunctionComponent<
{ children: (props: any) => ReactElement } & InputProps
Expand Down Expand Up @@ -116,4 +117,122 @@ describe('useInput', () => {
expect(handleBlur).toHaveBeenCalled();
expect(formApi.getState().active).toBeUndefined();
});

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

it('does not apply the defaultValue when input has a value of 0', () => {
const { queryByDisplayValue } = renderWithRedux(
<FormWithRedirect
onSubmit={jest.fn()}
record={{ id: 1, views: 0 }}
render={() => {
return (
<Input
source="views"
resource="posts"
defaultValue={99}
>
{({ id, input }) => {
return (
<input
type="number"
id={id}
aria-label="Views"
{...input}
/>
);
}}
</Input>
);
}}
/>
);
expect(queryByDisplayValue('99')).toBeNull();
});

it('applies the initialValue when input does not have a value', () => {
const { queryByDisplayValue } = renderWithRedux(
<FormWithRedirect
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
onSubmit={jest.fn()}
record={{ id: 1, views: 0 }}
render={() => {
return (
<Input
source="views"
resource="posts"
initialValue={99}
>
{({ id, input }) => {
return (
<input
type="number"
id={id}
aria-label="Views"
{...input}
/>
);
}}
</Input>
);
}}
/>
);
expect(queryByDisplayValue('99')).toBeNull();
});
});
6 changes: 3 additions & 3 deletions packages/ra-core/src/form/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ const useInput = ({
const form = useForm();
const recordId = record?.id;
useEffect(() => {
if (!!input.value) {
if (input.value != null && input.value !== '') {
return;
}
// Apply the default value if provided
// We use a change here which will make the form dirty but this is expected
// and identical to what FinalForm does (https://final-form.org/docs/final-form/types/FieldConfig#defaultvalue)
if (!!defaultValue) {
if (defaultValue != null) {
form.change(source, defaultValue);
}

if (!!initialValue) {
if (initialValue != null) {
form.batch(() => {
form.change(source, initialValue);
form.resetFieldState(source);
Expand Down