Skip to content

Commit 14fb192

Browse files
committed
Fix multiple reset issue
1 parent c309ab6 commit 14fb192

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
Resource,
1313
testI18nProvider,
1414
TestMemoryRouter,
15-
useCreatePath,
1615
useSourceContext,
1716
} from 'ra-core';
1817
import { Button, InputAdornment, Stack } from '@mui/material';
@@ -970,10 +969,8 @@ const BookCreateReset = () => {
970969
};
971970

972971
export const Reset = () => {
973-
const createPath = useCreatePath();
974-
const initialEntrie = createPath({ resource: 'books', type: 'create' });
975972
return (
976-
<TestMemoryRouter initialEntries={[initialEntrie]}>
973+
<TestMemoryRouter initialEntries={['/books/create']}>
977974
<Admin dataProvider={dataProvider}>
978975
<Resource name="books" create={BookCreateReset} />
979976
</Admin>

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,24 @@ describe('<SimpleFormIterator />', () => {
456456
expect(inputElements.length).toBe(1);
457457
});
458458

459-
expect(
460-
screen
461-
.queryAllByLabelText('Email')
462-
.map(inputElement => (inputElement as HTMLInputElement).value)
463-
).toEqual(['']);
464-
expect(
465-
screen
466-
.queryAllByLabelText('Name')
467-
.map(inputElement => (inputElement as HTMLInputElement).value)
468-
).toEqual(['']);
459+
await waitFor(() => {
460+
expect(
461+
screen
462+
.queryAllByLabelText('Email')
463+
.map(
464+
inputElement => (inputElement as HTMLInputElement).value
465+
)
466+
).toEqual(['']);
467+
});
468+
await waitFor(() => {
469+
expect(
470+
screen
471+
.queryAllByLabelText('Name')
472+
.map(
473+
inputElement => (inputElement as HTMLInputElement).value
474+
)
475+
).toEqual(['']);
476+
});
469477

470478
expect(screen.queryAllByLabelText('ra.action.remove').length).toBe(1);
471479
});

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { TextInput } from '../TextInput';
99
import { AdminContext } from '../../AdminContext';
1010
import { defaultTheme } from '../../theme/defaultTheme';
1111
import { useSimpleFormIteratorItem } from './useSimpleFormIteratorItem';
12+
import {
13+
FormDataConsumer,
14+
ResourceContextProvider,
15+
testDataProvider,
16+
} from 'ra-core';
1217

1318
export default { title: 'ra-ui-materialui/input/SimpleFormIterator' };
1419

@@ -231,3 +236,51 @@ export const UseSimpleFormIteratorItem = () => (
231236
</SimpleFormIterator>
232237
</Wrapper>
233238
);
239+
240+
export const DefaultValue = () => (
241+
<AdminContext dataProvider={testDataProvider()} defaultTheme="light">
242+
<ResourceContextProvider value="posts">
243+
<SimpleForm
244+
defaultValues={{
245+
emails: [{ email: '[email protected]', name: 'test' }],
246+
}}
247+
>
248+
<ArrayInput source="emails">
249+
<SimpleFormIterator>
250+
<TextInput source="email" label="Email" />
251+
<TextInput source="name" label="Name" />
252+
</SimpleFormIterator>
253+
</ArrayInput>
254+
</SimpleForm>
255+
</ResourceContextProvider>
256+
</AdminContext>
257+
);
258+
259+
export const DefaultValueWithFormDataConsumer = () => (
260+
<AdminContext dataProvider={testDataProvider()} defaultTheme="light">
261+
<ResourceContextProvider value="posts">
262+
<SimpleForm
263+
defaultValues={{
264+
emails: [{ email: '[email protected]', name: 'test' }],
265+
}}
266+
>
267+
<ArrayInput source="emails">
268+
<SimpleFormIterator>
269+
<FormDataConsumer>
270+
{() => (
271+
<>
272+
<TextInput
273+
source="email"
274+
label="Email"
275+
defaultValue="[email protected]"
276+
/>
277+
<TextInput source="name" label="Name" />
278+
</>
279+
)}
280+
</FormDataConsumer>
281+
</SimpleFormIterator>
282+
</ArrayInput>
283+
</SimpleForm>
284+
</ResourceContextProvider>
285+
</AdminContext>
286+
);

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const SimpleFormIterator = (inProps: SimpleFormIteratorProps) => {
6666

6767
const [confirmIsOpen, setConfirmIsOpen] = useState<boolean>(false);
6868
const { append, fields, move, remove, replace } = useArrayInput(props);
69-
const { resetField, trigger, getValues } = useFormContext();
69+
const { trigger, getValues } = useFormContext();
7070
const translate = useTranslate();
7171
const record = useRecordContext(props);
7272
const initialDefaultValue = useRef({});
@@ -105,7 +105,7 @@ export const SimpleFormIterator = (inProps: SimpleFormIteratorProps) => {
105105
!Children.only(children).props.source &&
106106
// Make sure it's not a FormDataConsumer
107107
// @ts-ignore
108-
!Children.only(children).type !== FormDataConsumer
108+
Children.only(children).type !== FormDataConsumer
109109
) {
110110
// ArrayInput used for an array of scalar values
111111
// (e.g. tags: ['foo', 'bar'])
@@ -128,10 +128,8 @@ export const SimpleFormIterator = (inProps: SimpleFormIteratorProps) => {
128128
}
129129
}
130130
append(defaultValue);
131-
// Make sure the newly added inputs are not considered dirty by react-hook-form
132-
resetField(`${finalSource}.${fields.length}`, { defaultValue });
133131
},
134-
[append, children, resetField, finalSource, fields.length]
132+
[append, children]
135133
);
136134

137135
const handleReorder = useCallback(

0 commit comments

Comments
 (0)