Skip to content

Commit 8054cc7

Browse files
authored
Merge pull request marmelab#11101 from AarishMansur/fix/simpleformiterator-disable-autofocus
Allow disabling auto focusing the first input of newly added row with `<SimpleFormIterator disableAutoFocus>`
2 parents b4b6f53 + 036d6f9 commit 8054cc7

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

docs/SimpleFormIterator.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,19 @@ const OrderEdit = () => (
7777
| `children` | Optional | `ReactElement` | - | List of inputs to display for each row |
7878
| `className` | Optional | `string` | - | Applied to the root element (`<ul>`) |
7979
| `disableAdd` | Optional | `boolean` | `false` | When true, the user cannot add new rows |
80+
| `disableAutoFocus` | Optional | `boolean` | `false` | Prevent focusing the first input when adding a new row |
8081
| `disableClear` | Optional | `boolean` | `false` | When true, the user cannot clear the array |
82+
| `disabled` | Optional | `boolean` | `false` | If true, all buttons are disabled. |
8183
| `disableRemove` | Optional | `boolean` | `false` | When true, the user cannot remove rows |
8284
| `disableReordering` | Optional | `boolean` | `false` | When true, the user cannot reorder rows |
8385
| `fullWidth` | Optional | `boolean` | `true` | Set to false to glue the actions to last input |
8486
| `getItemLabel` | Optional | `function` | `x => x` | Callback to render the label displayed in each row |
8587
| `inline` | Optional | `boolean` | `false` | When true, inputs are put on the same line |
8688
| `removeButton` | Optional | `ReactElement` | - | Component to render for the remove button |
8789
| `reOrderButtons` | Optional | `ReactElement` | - | Component to render for the up / down button |
88-
| `disabled` | Optional | `boolean` | `false` | If true, all buttons are disabled. |
8990
| `sx` | Optional | `SxProps` | - | Material UI shortcut for defining custom styles |
9091

92+
9193
## `addButton`
9294

9395
This prop lets you pass a custom element to replace the default Add button.
@@ -211,6 +213,23 @@ When true, the Add button isn't rendered, so users cannot add new rows.
211213
</SimpleFormIterator>
212214
```
213215
216+
## `disableAutoFocus`
217+
218+
By default, `<SimpleFormIterator>` focuses the first input of a newly added row.
219+
This behavior comes from `react-hook-form`'s [`useFieldArray`](https://react-hook-form.com/docs/usefieldarray) `append()` method.
220+
221+
You can disable this behavior by setting the `disableAutoFocus` prop.
222+
223+
```jsx
224+
<ArrayInput source="items">
225+
<SimpleFormIterator disableAutoFocus>
226+
<TextInput source="name" />
227+
<NumberInput source="price" />
228+
<NumberInput source="quantity" />
229+
</SimpleFormIterator>
230+
</ArrayInput>
231+
```
232+
214233
## `disableClear`
215234
216235
When true, the array clear button isn't rendered, so the user cannot clear the array.
@@ -223,6 +242,19 @@ When true, the array clear button isn't rendered, so the user cannot clear the a
223242
</SimpleFormIterator>
224243
```
225244
245+
## `disabled`
246+
247+
The `disabled` prop set to true makes the children input not mutable, focusable, or even submitted with the form.
248+
249+
```jsx
250+
<SimpleFormIterator disabled>
251+
<TextInput source="name" />
252+
<NumberInput source="price" />
253+
<NumberInput source="quantity" />
254+
</SimpleFormIterator>
255+
```
256+
257+
Contrary to read-only controls, disabled controls can not receive focus and are not submitted with the form.
226258
227259
## `disableRemove`
228260
@@ -401,20 +433,6 @@ The `readOnly` prop set to true makes the children input not mutable, meaning th
401433
402434
Contrary to disabled controls, read-only controls are still focusable and are submitted with the form.
403435
404-
## `disabled`
405-
406-
The `disabled` prop set to true makes the children input not mutable, focusable, or even submitted with the form.
407-
408-
```jsx
409-
<SimpleFormIterator disabled>
410-
<TextInput source="name" />
411-
<NumberInput source="price" />
412-
<NumberInput source="quantity" />
413-
</SimpleFormIterator>
414-
```
415-
416-
Contrary to read-only controls, disabled controls can not receive focus and are not submitted with the form.
417-
418436
## `sx`
419437
420438
You can override the style of the root element (a `<div>` element) as well as those of the inner components thanks to the `sx` property (see [the `sx` documentation](./SX.md) for syntax and examples).

docs_headless/src/content/docs/SimpleFormIteratorBase.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ const AddItemButton = () => {
6565

6666
## Props
6767

68-
| Prop | Required | Type | Default | Description |
69-
|-------------------|----------|----------------|-----------------------|-----------------------------------------------|
70-
| `children` | Optional | `ReactNode` | - | List of inputs to display for each array item |
68+
| Prop | Required | Type | Default | Description |
69+
| ------------------ | -------- | ----------- | ------- | ------------------------------------------------------- |
70+
| `children` | Optional | `ReactNode` | - | List of inputs to display for each array item |
71+
| `disableAutoFocus` | Optional | `boolean` | `false` | Prevent focusing the first input when adding a new item |
72+
73+
## `disableAutoFocus`
74+
75+
When true, will pass `{ shouldFocus: false }` to `react-hook-form`'s [`useFieldArray`](https://react-hook-form.com/docs/usefieldarray) `append()` method when adding a new item, preventing the first input of the newly added item from being focused automatically.

packages/ra-core/src/controller/input/SimpleFormIteratorBase.stories.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const AddItemButton = () => {
7575
);
7676
};
7777

78-
export const Basic = () => (
78+
export const Basic = (props: Partial<SimpleFormIteratorBaseProps>) => (
7979
<TestMemoryRouter initialEntries={['/posts/1']}>
8080
<Admin
8181
dataProvider={fakeRestDataProvider({
@@ -118,7 +118,7 @@ export const Basic = () => (
118118
<div>
119119
<div>Tags:</div>
120120
<ArrayInputBase source="tags">
121-
<SimpleFormIterator>
121+
<SimpleFormIterator {...props}>
122122
<TextInput source="name" />
123123
<TextInput source="color" />
124124
</SimpleFormIterator>
@@ -131,3 +131,9 @@ export const Basic = () => (
131131
</Admin>
132132
</TestMemoryRouter>
133133
);
134+
Basic.args = {
135+
disableAutoFocus: false,
136+
};
137+
Basic.argTypes = {
138+
disableAutoFocus: { control: 'boolean' },
139+
};

packages/ra-core/src/controller/input/SimpleFormIteratorBase.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const SimpleFormIteratorBase = (props: SimpleFormIteratorBaseProps) => {
1313
const {
1414
children,
1515
getItemDefaults: getItemDefaultsProp = DefaultGetItemDefaults,
16+
disableAutoFocus = false,
1617
} = props;
1718
const getItemDefaults = useEvent(getItemDefaultsProp);
1819

@@ -39,7 +40,7 @@ export const SimpleFormIteratorBase = (props: SimpleFormIteratorBaseProps) => {
3940
});
4041

4142
const addField = useEvent((item: any = undefined) => {
42-
append(getItemDefaults(item));
43+
append(getItemDefaults(item), { shouldFocus: !disableAutoFocus });
4344
});
4445

4546
const handleReorder = useEvent((origin: number, destination: number) => {
@@ -92,4 +93,5 @@ export interface SimpleFormIteratorBaseProps
9293
record?: RaRecord;
9394
resource?: string;
9495
source?: string;
96+
disableAutoFocus?: boolean;
9597
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,12 @@ export const Large = () => (
328328
</Edit>
329329
</AdminContext>
330330
);
331+
332+
export const DisableAutoFocus = () => (
333+
<Wrapper>
334+
<SimpleFormIterator disableAutoFocus>
335+
<TextInput source="name" />
336+
<TextInput source="role" />
337+
</SimpleFormIterator>
338+
</Wrapper>
339+
);

0 commit comments

Comments
 (0)