|
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'; |
4 | 2 | import { |
| 3 | + Box, |
5 | 4 | Button, |
6 | 5 | Dialog, |
7 | 6 | DialogActions, |
8 | 7 | DialogContent, |
| 8 | + DialogTitle, |
| 9 | + IconButton, |
9 | 10 | Stack, |
10 | | - Box, |
11 | 11 | TextField, |
12 | 12 | } from '@mui/material'; |
13 | 13 | 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'; |
14 | 17 |
|
| 18 | +import { CreateBase, Resource, TestMemoryRouter } from 'ra-core'; |
| 19 | +import { Admin } from 'react-admin'; |
15 | 20 | import { AdminContext } from '../AdminContext'; |
16 | 21 | import { Create, Edit } from '../detail'; |
17 | 22 | import { SimpleForm } from '../form'; |
18 | | -import { SelectArrayInput } from './SelectArrayInput'; |
19 | | -import { ReferenceArrayInput } from './ReferenceArrayInput'; |
20 | | -import { useCreateSuggestionContext } from './useSupportCreateSuggestion'; |
21 | | -import { TextInput } from './TextInput'; |
22 | 23 | import { ArrayInput, SimpleFormIterator } from './ArrayInput'; |
23 | | -import { Resource, TestMemoryRouter } from 'ra-core'; |
24 | | -import { Admin } from 'react-admin'; |
25 | 24 | import { FormInspector } from './common'; |
| 25 | +import { ReferenceArrayInput } from './ReferenceArrayInput'; |
| 26 | +import { SelectArrayInput } from './SelectArrayInput'; |
| 27 | +import { TextInput } from './TextInput'; |
| 28 | +import { useCreateSuggestionContext } from './useSupportCreateSuggestion'; |
26 | 29 |
|
27 | 30 | export default { title: 'ra-ui-materialui/input/SelectArrayInput' }; |
28 | 31 |
|
@@ -677,3 +680,96 @@ export const InsideReferenceArrayInputWithError = () => ( |
677 | 680 | </Admin> |
678 | 681 | </TestMemoryRouter> |
679 | 682 | ); |
| 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