Skip to content

Commit 43bc724

Browse files
authored
Merge pull request marmelab#10636 from marmelab/fix/apply_a_default_defalutValue_on_ArrayInput
Fix: Apply a default `defalutValue` on `ArrayInput`
2 parents 238c1ad + d9d29fb commit 43bc724

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

packages/ra-ui-materialui/src/input/ArrayInput/ArrayInput.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
NestedInlineNoTranslation,
2424
Validation,
2525
Focus,
26+
Reset,
2627
} from './ArrayInput.stories';
2728
import { useArrayInput } from './useArrayInput';
2829

@@ -457,4 +458,23 @@ describe('<ArrayInput />', () => {
457458
);
458459
});
459460
});
461+
462+
it('should empty the input on form reset', async () => {
463+
render(<Reset />);
464+
fireEvent.click(await screen.findByRole('button', { name: 'Add' }));
465+
fireEvent.change(screen.getByLabelText('Name'), {
466+
target: { value: 'Leo Tolstoy' },
467+
});
468+
fireEvent.change(screen.getByLabelText('Role'), {
469+
target: { value: 'Writer' },
470+
});
471+
fireEvent.click(screen.getByRole('button', { name: 'Reset' }));
472+
await waitFor(() => {
473+
expect(screen.queryByDisplayValue('Leo Tolstoy')).toBeNull();
474+
expect(screen.queryByDisplayValue('Writer')).toBeNull();
475+
expect(
476+
screen.queryByRole('button', { name: 'Clear the list' })
477+
).toBeNull();
478+
});
479+
});
460480
});

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import * as React from 'react';
2-
import { Admin, DateTimeInput } from 'react-admin';
2+
import {
3+
Admin,
4+
DateTimeInput,
5+
DeleteButton,
6+
SaveButton,
7+
Toolbar,
8+
} from 'react-admin';
39
import {
410
minLength,
511
required,
612
Resource,
713
testI18nProvider,
814
TestMemoryRouter,
15+
useCreatePath,
916
useSourceContext,
1017
} from 'ra-core';
11-
import { Button, InputAdornment } from '@mui/material';
18+
import { Button, InputAdornment, Stack } from '@mui/material';
1219

1320
import { Edit, Create } from '../../detail';
1421
import { SimpleForm, TabbedForm } from '../../form';
@@ -925,6 +932,55 @@ export const SetValue = () => (
925932
</TestMemoryRouter>
926933
);
927934

935+
const ResetButton = () => {
936+
const { reset } = useFormContext();
937+
938+
return (
939+
<>
940+
<Button onClick={() => reset()}>Reset</Button>
941+
</>
942+
);
943+
};
944+
945+
const BookCreateReset = () => {
946+
return (
947+
<Create>
948+
<SimpleForm
949+
toolbar={
950+
<Toolbar>
951+
<div className="RaToolbar-defaultToolbar">
952+
<Stack direction="row" gap={2}>
953+
<SaveButton />
954+
<ResetButton />
955+
</Stack>
956+
<DeleteButton />
957+
</div>
958+
</Toolbar>
959+
}
960+
>
961+
<ArrayInput source="authors">
962+
<SimpleFormIterator inline>
963+
<TextInput source="name" helperText={false} />
964+
<TextInput source="role" helperText={false} />
965+
</SimpleFormIterator>
966+
</ArrayInput>
967+
</SimpleForm>
968+
</Create>
969+
);
970+
};
971+
972+
export const Reset = () => {
973+
const createPath = useCreatePath();
974+
const initialEntrie = createPath({ resource: 'books', type: 'create' });
975+
return (
976+
<TestMemoryRouter initialEntries={[initialEntrie]}>
977+
<Admin dataProvider={dataProvider}>
978+
<Resource name="books" create={BookCreateReset} />
979+
</Admin>
980+
</TestMemoryRouter>
981+
);
982+
};
983+
928984
export const Focus = ({
929985
input,
930986
}: {

packages/ra-ui-materialui/src/input/ArrayInput/ArrayInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import { ArrayInputContext } from './ArrayInputContext';
7474
export const ArrayInput = (props: ArrayInputProps) => {
7575
const {
7676
className,
77-
defaultValue,
77+
defaultValue = [],
7878
label,
7979
isFetching,
8080
isLoading,
@@ -131,7 +131,7 @@ export const ArrayInput = (props: ArrayInputProps) => {
131131
}, [finalSource, formGroups, formGroupName]);
132132

133133
useApplyInputDefaultValues({
134-
inputProps: props,
134+
inputProps: { ...props, defaultValue },
135135
isArrayInput: true,
136136
fieldArrayInputControl: fieldProps,
137137
});

0 commit comments

Comments
 (0)