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 ArrayInput makes edited form always dirty on input blur #8275

Merged
merged 5 commits into from
Oct 20, 2022
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
40 changes: 39 additions & 1 deletion packages/ra-ui-materialui/src/button/SaveButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
import { SaveButton } from './SaveButton';
import { SimpleForm, Toolbar } from '../form';
import { Edit } from '../detail';
import { TextInput } from '../input';
import {
TextInput,
ArrayInput,
SimpleFormIterator,
NumberInput,
} from '../input';
import { AdminContext } from '../AdminContext';

const invalidButtonDomProps = {
Expand Down Expand Up @@ -400,4 +405,37 @@ describe('<SaveButton />', () => {
)
);
});

it('should not be enabled if no inputs have changed', async () => {
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm
resource="myresource"
onSubmit={jest.fn}
defaultValues={{
test: 'test',
}}
>
<TextInput source="test" />
<ArrayInput resource="foo" source="arr">
<SimpleFormIterator>
<NumberInput source="id" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</AdminContext>
);

const testInput = screen.getByLabelText(
'resources.myresource.fields.test'
);
fireEvent.focus(testInput);
fireEvent.blur(testInput);

await waitFor(() =>
expect(screen.getByLabelText('ra.action.save')['disabled']).toEqual(
true
)
);
});
});
4 changes: 3 additions & 1 deletion packages/ra-ui-materialui/src/button/SaveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export const SaveButton = <RecordType extends RaRecord = any>(
const translate = useTranslate();
const form = useFormContext();
const saveContext = useSaveContext();
const { isDirty, isValidating, isSubmitting } = useFormState();
const { dirtyFields, isValidating, isSubmitting } = useFormState();
// useFormState().isDirty might differ from useFormState().dirtyFields (https://github.com/react-hook-form/react-hook-form/issues/4740)
const isDirty = Object.keys(dirtyFields).length > 0;
// Use form isDirty, isValidating and form context saving to enable or disable the save button
// if alwaysEnable is undefined
const disabled = valueOrDefault(
Expand Down
3 changes: 1 addition & 2 deletions packages/ra-ui-materialui/src/input/DateTimeInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('<DateTimeInput />', () => {
const onSubmit = jest.fn();
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={onSubmit}>
<SimpleForm mode="onBlur" onSubmit={onSubmit}>
<DateTimeInput
{...defaultProps}
validate={required()}
Expand All @@ -245,7 +245,6 @@ describe('<DateTimeInput />', () => {
target: { value: '' },
});
fireEvent.blur(input);
fireEvent.click(screen.getByText('ra.action.save'));
await waitFor(() => {
expect(
screen.queryByText('ra.validation.required')
Expand Down
3 changes: 1 addition & 2 deletions packages/ra-ui-materialui/src/input/TimeInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('<TimeInput />', () => {
const onSubmit = jest.fn();
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={onSubmit}>
<SimpleForm mode="onBlur" onSubmit={onSubmit}>
<TimeInput {...defaultProps} validate={required()} />
</SimpleForm>
</AdminContext>
Expand All @@ -232,7 +232,6 @@ describe('<TimeInput />', () => {
target: { value: '' },
});
fireEvent.blur(input);
fireEvent.click(screen.getByText('ra.action.save'));
await waitFor(() => {
expect(
screen.queryByText('ra.validation.required')
Expand Down