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

Fix ArrayInput Always Override Inputs disabled Prop #5876

Merged
merged 1 commit into from
Feb 5, 2021
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
31 changes: 31 additions & 0 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,37 @@ describe('<SimpleFormIterator />', () => {
expect((inputElements[1] as HTMLInputElement).value).toBe('bar');
});

it('should allow to override the disabled prop of each inputs', () => {
const { queryAllByLabelText } = renderWithRedux(
<ThemeProvider theme={theme}>
<SaveContextProvider value={saveContextValue}>
<SideEffectContextProvider value={sideEffectValue}>
<SimpleForm
record={{
id: 'whatever',
emails: [{ email: 'foo' }, { email: 'bar' }],
}}
>
<ArrayInput source="emails">
<SimpleFormIterator>
<TextInput source="email" disabled />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</SideEffectContextProvider>
</SaveContextProvider>
</ThemeProvider>
);
const inputElements = queryAllByLabelText(
'resources.undefined.fields.email'
);
expect(inputElements).toHaveLength(2);
expect((inputElements[0] as HTMLInputElement).disabled).toBeTruthy();
expect((inputElements[0] as HTMLInputElement).value).toBe('foo');
expect((inputElements[1] as HTMLInputElement).disabled).toBeTruthy();
expect((inputElements[1] as HTMLInputElement).value).toBe('bar');
});

it('should display an add item button at least', () => {
const { getByText } = renderWithRedux(
<SaveContextProvider value={saveContextValue}>
Expand Down
25 changes: 17 additions & 8 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,37 @@ const SimpleFormIterator: FC<SimpleFormIteratorProps> = props => {
<section className={classes.form}>
{Children.map(
children,
(input: ReactElement, index2) =>
isValidElement<any>(input) ? (
(input: ReactElement, index2) => {
if (!isValidElement<any>(input)) {
return null;
}
const {
source,
...inputProps
} = input.props;
return (
<FormInput
basePath={
input.props.basePath ||
basePath
}
input={cloneElement(input, {
source: input.props.source
? `${member}.${input.props.source}`
source: source
? `${member}.${source}`
: member,
index: input.props.source
index: source
? undefined
: index2,
label:
typeof input.props
.label ===
'undefined'
? input.props.source
? `resources.${resource}.fields.${input.props.source}`
? source
? `resources.${resource}.fields.${source}`
: undefined
: input.props.label,
disabled,
...inputProps,
})}
record={
(records &&
Expand All @@ -234,7 +242,8 @@ const SimpleFormIterator: FC<SimpleFormIteratorProps> = props => {
variant={variant}
margin={margin}
/>
) : null
);
}
)}
</section>
{!disabled &&
Expand Down