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 SimpleFormIterator label function #6305

Merged
merged 5 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 34 additions & 7 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { fireEvent, waitFor, getByText } from '@testing-library/react';
import * as React from 'react';
import { ThemeProvider } from '@material-ui/core';
import { createMuiTheme } from '@material-ui/core/styles';
import { fireEvent, getByText, waitFor } from '@testing-library/react';
import expect from 'expect';
import { SaveContextProvider, SideEffectContextProvider } from 'ra-core';
import { renderWithRedux } from 'ra-test';
import { ThemeProvider } from '@material-ui/core';
import { createMuiTheme } from '@material-ui/core/styles';

import SimpleFormIterator from './SimpleFormIterator';
import TextInput from '../input/TextInput';
import * as React from 'react';
import { ArrayInput } from '../input';
import TextInput from '../input/TextInput';
import SimpleForm from './SimpleForm';
import SimpleFormIterator from './SimpleFormIterator';

const theme = createMuiTheme();

Expand Down Expand Up @@ -499,6 +498,34 @@ describe('<SimpleFormIterator />', () => {
expect(getByText('Custom Remove Button')).not.toBeNull();
});

it('should display custom row label', () => {
const { getByText } = renderWithRedux(
<ThemeProvider theme={theme}>
<SaveContextProvider value={saveContextValue}>
<SideEffectContextProvider value={sideEffectValue}>
<SimpleForm
record={{
id: 'whatever',
emails: [{ email: 'foo' }, { email: 'bar' }],
}}
>
<ArrayInput source="emails">
<SimpleFormIterator
labelFn={index => `3.${index}`}
iamstiil marked this conversation as resolved.
Show resolved Hide resolved
>
<TextInput source="email" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</SideEffectContextProvider>
</SaveContextProvider>
</ThemeProvider>
);

expect(getByText('3.0')).toBeDefined();
expect(getByText('3.1')).toBeDefined();
});

it('should call the onClick method when the custom add button is clicked', async () => {
const onClick = jest.fn();
const { getByText } = renderWithRedux(
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-ui-materialui/src/form/SimpleFormIterator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const SimpleFormIterator: FC<SimpleFormIteratorProps> = props => {
margin,
TransitionProps,
defaultValue,
labelFn = index => index + 1,
iamstiil marked this conversation as resolved.
Show resolved Hide resolved
} = props;
const classes = useStyles(props);
const nodeRef = useRef(null);
Expand Down Expand Up @@ -197,7 +198,7 @@ const SimpleFormIterator: FC<SimpleFormIteratorProps> = props => {
variant="body1"
className={classes.index}
>
{index + 1}
{labelFn(index)}
iamstiil marked this conversation as resolved.
Show resolved Hide resolved
</Typography>
<section className={classes.form}>
{Children.map(
Expand Down Expand Up @@ -326,6 +327,7 @@ export interface SimpleFormIteratorProps
disabled?: boolean;
disableAdd?: boolean;
disableRemove?: boolean | DisableRemoveFunction;
labelFn?: (index: number) => string;
iamstiil marked this conversation as resolved.
Show resolved Hide resolved
margin?: 'none' | 'normal' | 'dense';
meta?: {
// the type defined in FieldArrayRenderProps says error is boolean, which is wrong.
Expand Down