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 doesn't support scalar values #8090

Merged
merged 2 commits into from
Aug 22, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ const dataProvider = {
role: 'co_writer',
},
],
tags: ['novel', 'war', 'classic'],
},
}),
update: (resource, params) => Promise.resolve(params),
@@ -96,3 +97,34 @@ export const AutocompleteFirst = () => (
<Resource name="books" edit={BookEditWithAutocomplete} />
</Admin>
);

export const Scalar = () => (
<Admin dataProvider={dataProvider} history={history}>
<Resource
name="books"
edit={() => (
<Edit
mutationMode="pessimistic"
mutationOptions={{
onSuccess: data => {
console.log(data);
},
}}
>
<SimpleForm>
<TextInput source="title" />
<ArrayInput source="tags" fullWidth>
<SimpleFormIterator disableReordering>
<TextInput
source=""
label="tag"
helperText={false}
/>
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
)}
/>
</Admin>
);
Original file line number Diff line number Diff line change
@@ -59,16 +59,29 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
(item: any = undefined) => {
let defaultValue = item;
if (item == null) {
defaultValue = {} as Record<string, unknown>;
Children.forEach(children, input => {
if (
React.isValidElement(input) &&
input.type !== FormDataConsumer
) {
defaultValue[input.props.source] =
input.props.defaultValue ?? '';
}
});
if (
Children.count(children) === 1 &&
React.isValidElement(Children.only(children)) &&
// @ts-ignore
!Children.only(children).props.source
) {
// ArrayInput used for an array of scalar values
// (e.g. tags: ['foo', 'bar'])
defaultValue = '';
} else {
// ArrayInput used for an array of objects
// (e.g. authors: [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Doe' }])
defaultValue = {} as Record<string, unknown>;
Children.forEach(children, input => {
if (
React.isValidElement(input) &&
input.type !== FormDataConsumer
) {
defaultValue[input.props.source] =
input.props.defaultValue ?? '';
}
});
}
}
append(defaultValue);
},