Skip to content

Commit e6cfbd3

Browse files
committed
add selectArrayInput in refereneArrayInput with create story
1 parent e4fd550 commit e6cfbd3

File tree

1 file changed

+106
-10
lines changed

1 file changed

+106
-10
lines changed

packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import * as React from 'react';
2-
import polyglotI18nProvider from 'ra-i18n-polyglot';
3-
import englishMessages from 'ra-language-english';
1+
import CloseIcon from '@mui/icons-material/Close';
42
import {
3+
Box,
54
Button,
65
Dialog,
76
DialogActions,
87
DialogContent,
8+
DialogTitle,
9+
IconButton,
910
Stack,
10-
Box,
1111
TextField,
1212
} from '@mui/material';
1313
import fakeRestProvider from 'ra-data-fakerest';
14+
import polyglotI18nProvider from 'ra-i18n-polyglot';
15+
import englishMessages from 'ra-language-english';
16+
import * as React from 'react';
1417

18+
import { CreateBase, Resource, TestMemoryRouter } from 'ra-core';
19+
import { Admin } from 'react-admin';
1520
import { AdminContext } from '../AdminContext';
1621
import { Create, Edit } from '../detail';
1722
import { SimpleForm } from '../form';
18-
import { SelectArrayInput } from './SelectArrayInput';
19-
import { ReferenceArrayInput } from './ReferenceArrayInput';
20-
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
21-
import { TextInput } from './TextInput';
2223
import { ArrayInput, SimpleFormIterator } from './ArrayInput';
23-
import { Resource, TestMemoryRouter } from 'ra-core';
24-
import { Admin } from 'react-admin';
2524
import { FormInspector } from './common';
25+
import { ReferenceArrayInput } from './ReferenceArrayInput';
26+
import { SelectArrayInput } from './SelectArrayInput';
27+
import { TextInput } from './TextInput';
28+
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
2629

2730
export default { title: 'ra-ui-materialui/input/SelectArrayInput' };
2831

@@ -677,3 +680,96 @@ export const InsideReferenceArrayInputWithError = () => (
677680
</Admin>
678681
</TestMemoryRouter>
679682
);
683+
684+
const CreateAuthor = () => {
685+
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
686+
687+
const onAuthorCreate = author => {
688+
onCreate(author);
689+
};
690+
691+
return (
692+
<Dialog open onClose={onCancel}>
693+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Author</DialogTitle>
694+
<IconButton
695+
aria-label="close"
696+
onClick={onCancel}
697+
sx={theme => ({
698+
position: 'absolute',
699+
right: 8,
700+
top: 8,
701+
color: theme.palette.grey[500],
702+
})}
703+
>
704+
<CloseIcon />
705+
</IconButton>
706+
<DialogContent sx={{ p: 0 }}>
707+
<CreateBase
708+
redirect={false}
709+
resource="authors"
710+
mutationOptions={{
711+
onSuccess: author => {
712+
onAuthorCreate(author);
713+
},
714+
}}
715+
>
716+
<SimpleForm defaultValues={{ first_name: filter }}>
717+
<TextInput
718+
source="first_name"
719+
helperText={false}
720+
autoFocus
721+
/>
722+
<TextInput source="last_name" helperText={false} />
723+
</SimpleForm>
724+
</CreateBase>
725+
</DialogContent>
726+
</Dialog>
727+
);
728+
};
729+
730+
export const InsideReferenceArrayInputAndCreationSupport = () => {
731+
const optionRenderer = choice => {
732+
return choice.first_name && choice.last_name
733+
? `${choice.first_name} ${choice.last_name}`
734+
: `${choice.name}`;
735+
};
736+
return (
737+
<TestMemoryRouter initialEntries={['/books/1']}>
738+
<Admin dataProvider={dataProviderWithAuthors}>
739+
<Resource
740+
name="authors"
741+
recordRepresentation={record =>
742+
`${record.first_name} ${record.last_name}`
743+
}
744+
/>
745+
<Resource
746+
name="books"
747+
edit={() => (
748+
<Edit
749+
mutationMode="pessimistic"
750+
mutationOptions={{
751+
onSuccess: data => {
752+
console.log(data);
753+
},
754+
}}
755+
>
756+
<SimpleForm>
757+
<ReferenceArrayInput
758+
reference="authors"
759+
source="authors"
760+
>
761+
<SelectArrayInput
762+
create={<CreateAuthor />}
763+
createLabel="Create a new Author"
764+
optionText={optionRenderer}
765+
/>
766+
</ReferenceArrayInput>
767+
<FormInspector name="authors" />
768+
</SimpleForm>
769+
</Edit>
770+
)}
771+
/>
772+
</Admin>
773+
</TestMemoryRouter>
774+
);
775+
};

0 commit comments

Comments
 (0)