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

Add support for SimpleFormIterator inline and sx props #8067

Merged
merged 1 commit into from
Aug 22, 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
15 changes: 14 additions & 1 deletion docs/ArrayInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ import { ArrayInput, SimpleFormIterator, DateInput, TextInput } from 'react-admi

`<ArrayInput>` expects a single child, which must be a *form iterator* component. A form iterator is a component accepting a `fields` object as passed by [react-hook-form](https://react-hook-form.com/api/usefieldarray), and defining a layout for an array of fields. It also receives several functions to manipulate the array values. For instance, the `<SimpleFormIterator>` component displays an array of react-admin Inputs in an unordered list (`<ul>`), one sub-form by list item (`<li>`). It also provides controls for adding and removing a sub-record (a backlink in this example).

By using `<SimpleFormIterator inline>`, child inputs apper inline.

```jsx
import { ArrayInput, SimpleFormIterator, DateInput, TextInput } from 'react-admin';

<ArrayInput source="backlinks">
<SimpleFormIterator inline>
<DateInput source="date" />
<TextInput source="url" />
</SimpleFormIterator>
</ArrayInput>
```
Comment on lines +45 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be even better to add your screenshot here to showcase how this looks like 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SimpleFormIterator component and docs need more love, let's do that later.


You can pass `disableAdd`, `disableRemove` and `disableReordering` as props of `SimpleFormIterator`, to disable `ADD`, `REMOVE` and the `UP/DOWN` button(s) respectively. Default value of each is `false`.

```jsx
import { ArrayInput, SimpleFormIterator, DateInput, TextInput } from 'react-admin';

<ArrayInput source="backlinks">
<SimpleFormIterator disableRemove >
<SimpleFormIterator disableRemove>
<DateInput source="date" />
<TextInput source="url" />
</SimpleFormIterator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as React from 'react';

import { Edit } from '../../detail';
import { SimpleForm } from '../../form';
import { ArrayInput } from './ArrayInput';
import { SimpleFormIterator } from './SimpleFormIterator';
import { TextInput } from '../TextInput';
import { AdminContext } from '../../AdminContext';

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

const dataProvider = {
getOne: () =>
Promise.resolve({
data: {
id: 1,
title: 'War and Peace',
authors: [
{
name: 'Leo Tolstoy',
role: 'head_writer',
},
{
name: 'Alexander Pushkin',
role: 'co_writer',
},
],
},
}),
} as any;

export const Basic = () => (
<AdminContext dataProvider={dataProvider}>
<Edit resource="books" id="1">
<SimpleForm>
<ArrayInput source="authors" fullWidth>
<SimpleFormIterator>
<TextInput source="name" />
<TextInput source="role" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
</AdminContext>
);

export const Inline = () => (
<AdminContext dataProvider={dataProvider}>
<Edit resource="books" id="1">
<SimpleForm>
<ArrayInput source="authors" fullWidth>
<SimpleFormIterator inline>
<TextInput source="name" />
<TextInput source="role" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
</AdminContext>
);

export const Sx = () => (
<AdminContext dataProvider={dataProvider}>
<Edit resource="books" id="1">
<SimpleForm>
<ArrayInput source="authors" fullWidth>
<SimpleFormIterator
sx={{
border: 'solid lightgrey 1px',
borderRadius: 2,
mt: 3,
p: 1,
'& .RaSimpleFormIterator-form': {
flexDirection: 'row',
gap: '1em',
},
}}
>
<TextInput source="name" />
<TextInput source="role" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
</AdminContext>
);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useCallback,
useMemo,
} from 'react';
import { styled } from '@mui/material';
import { styled, SxProps } from '@mui/material';
import clsx from 'clsx';
import get from 'lodash/get';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -43,7 +43,9 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
disableAdd,
disableRemove,
disableReordering,
inline,
getItemLabel = DefaultLabelFn,
sx,
} = props;
const { append, fields, move, remove } = useArrayInput(props);
const record = useRecordContext(props);
Expand Down Expand Up @@ -106,7 +108,7 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
);
return fields ? (
<SimpleFormIteratorContext.Provider value={context}>
<Root className={className}>
<Root className={className} sx={sx}>
{fields.map((member, index) => (
<SimpleFormIteratorItem
key={member.id}
Expand All @@ -124,6 +126,7 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
reOrderButtons={reOrderButtons}
resource={resource}
source={source}
inline={inline}
>
{children}
</SimpleFormIteratorItem>
Expand Down Expand Up @@ -163,6 +166,7 @@ SimpleFormIterator.propTypes = {
fields: PropTypes.array,
fieldState: PropTypes.object,
formState: PropTypes.object,
inline: PropTypes.bool,
record: PropTypes.object,
source: PropTypes.string,
resource: PropTypes.string,
Expand All @@ -182,6 +186,7 @@ export interface SimpleFormIteratorProps extends Partial<UseFieldArrayReturn> {
disableRemove?: boolean | DisableRemoveFunction;
disableReordering?: boolean;
getItemLabel?: (index: number) => string;
inline?: boolean;
meta?: {
// the type defined in FieldArrayRenderProps says error is boolean, which is wrong.
error?: any;
Expand All @@ -192,6 +197,7 @@ export interface SimpleFormIteratorProps extends Partial<UseFieldArrayReturn> {
reOrderButtons?: ReactElement;
resource?: string;
source?: string;
sx?: SxProps;
}

const Root = styled('ul', {
Expand Down Expand Up @@ -225,6 +231,10 @@ const Root = styled('ul', {
flexDirection: 'column',
flex: 2,
},
[`& .${SimpleFormIteratorClasses.inline}`]: {
flexDirection: 'row',
gap: '1em',
},
[`& .${SimpleFormIteratorClasses.action}`]: {
paddingTop: '0.5em',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const SimpleFormIteratorItem = React.forwardRef(
disableRemove,
getItemLabel,
index,
inline = false,
member,
record,
removeButton,
Expand Down Expand Up @@ -97,7 +98,12 @@ export const SimpleFormIteratorItem = React.forwardRef(
})}
</div>
</div>
<section className={SimpleFormIteratorClasses.form}>
<section
className={clsx(
SimpleFormIteratorClasses.form,
inline && SimpleFormIteratorClasses.inline
)}
>
{Children.map(
children,
(input: ReactElement, index2) => {
Expand Down Expand Up @@ -146,6 +152,7 @@ export type SimpleFormIteratorItemProps = Partial<ArrayInputContextValue> & {
disableReordering?: boolean;
getItemLabel?: (index: number) => string;
index: number;
inline?: boolean;
member: string;
onRemoveField: (index: number) => void;
onReorder: (origin: number, destination: number) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const SimpleFormIteratorPrefix = 'RaSimpleFormIterator';
export const SimpleFormIteratorClasses = {
line: `${SimpleFormIteratorPrefix}-line`,
index: `${SimpleFormIteratorPrefix}-index`,
inline: `${SimpleFormIteratorPrefix}-inline`,
indexContainer: `${SimpleFormIteratorPrefix}-indexContainer`,
form: `${SimpleFormIteratorPrefix}-form`,
action: `${SimpleFormIteratorPrefix}-action`,
Expand Down