(false);
const { append, fields, move, remove, replace } = useArrayInput(props);
+ const { resetField } = useFormContext();
const translate = useTranslate();
const record = useRecordContext(props);
const initialDefaultValue = useRef({});
@@ -117,8 +118,10 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
}
}
append(defaultValue);
+ // Make sure the newly added inputs are not considered dirty by react-hook-form
+ resetField(`${source}.${fields.length}`, { defaultValue });
},
- [append, children]
+ [append, children, resetField, source, fields.length]
);
// add field and call the onClick event of the button passed as addButton prop
diff --git a/packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx b/packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
index 9f8e5180b8a..6b429e7fec5 100644
--- a/packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
+++ b/packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
@@ -7,7 +7,11 @@ import { AdminContext } from '../AdminContext';
import { SimpleForm } from '../form';
import { SelectArrayInput } from './SelectArrayInput';
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
-import { DifferentIdTypes, TranslateChoice } from './SelectArrayInput.stories';
+import {
+ DifferentIdTypes,
+ TranslateChoice,
+ InsideArrayInput,
+} from './SelectArrayInput.stories';
describe('', () => {
const defaultProps = {
@@ -636,4 +640,23 @@ describe('', () => {
);
expect(screen.queryByTestId('selectArray')).toBeDefined();
});
+
+ it('should always apply its default value inside an ArrayInput', async () => {
+ render();
+ await screen.findByText('Foo');
+ fireEvent.click(screen.getByLabelText('Remove'));
+ await waitFor(() => {
+ expect(screen.queryByText('Foo')).toBeNull();
+ });
+ fireEvent.click(screen.getByLabelText('Add'));
+ await screen.findByText('Foo');
+ fireEvent.click(screen.getByLabelText('Remove'));
+ await waitFor(() => {
+ expect(screen.queryByText('Foo')).toBeNull();
+ });
+ fireEvent.click(screen.getByLabelText('Add'));
+ await screen.findByText('Foo');
+ fireEvent.click(screen.getByLabelText('Add'));
+ expect(await screen.findAllByText('Foo')).toHaveLength(2);
+ });
});
diff --git a/packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx b/packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx
index a0bb155293e..6ecd1def302 100644
--- a/packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx
+++ b/packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx
@@ -17,11 +17,26 @@ import { SelectArrayInput } from './SelectArrayInput';
import { ReferenceArrayInput } from './ReferenceArrayInput';
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
import { TextInput } from './TextInput';
+import { ArrayInput, SimpleFormIterator } from './ArrayInput';
+import { FormDataConsumer } from 'ra-core';
+import { useWatch } from 'react-hook-form';
export default { title: 'ra-ui-materialui/input/SelectArrayInput' };
const i18nProvider = polyglotI18nProvider(() => englishMessages);
+const FormInspector = ({ source }) => {
+ const value = useWatch({ name: source });
+ return (
+
+ {source} value in form:
+
+ {JSON.stringify(value)} ({typeof value})
+
+
+ );
+};
+
export const Basic = () => (
(
);
+export const DefaultValue = () => (
+
+
+
+
+
+
+
+);
+
+export const InsideArrayInput = () => (
+
+
+
+
+
+
+ {({ getSource }) => {
+ const source = getSource!('data');
+ return (
+ <>
+
+ >
+ );
+ }}
+
+
+
+
+
+
+
+);
+
const choices = [
{ id: 'admin', name: 'Admin' },
{ id: 'u001', name: 'Editor' },