Skip to content

Commit 6fb28fb

Browse files
authored
Merge pull request #10700 from marmelab/rewrite-autocompletearray-input
[DOC] Rewrite autocompletearray input create doc example
2 parents 901e109 + 7220b15 commit 6fb28fb

File tree

2 files changed

+91
-94
lines changed

2 files changed

+91
-94
lines changed

docs/AutocompleteArrayInput.md

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -714,20 +714,23 @@ Use the `create` prop when you want a more polished or complex UI. For example a
714714
import {
715715
AutocompleteArrayInput,
716716
Create,
717+
CreateBase,
717718
ReferenceArrayInput,
718719
SimpleForm,
719720
TextInput,
720721
useCreate,
721722
useCreateSuggestionContext
722723
} from 'react-admin';
723724

725+
import CloseIcon from '@mui/icons-material/Close';
724726
import {
725727
Box,
726728
BoxProps,
727729
Button,
728730
Dialog,
729-
DialogActions,
730731
DialogContent,
732+
DialogTitle,
733+
IconButton,
731734
TextField,
732735
} from '@mui/material';
733736

@@ -746,43 +749,45 @@ const PostCreate = () => {
746749

747750
const CreateTag = () => {
748751
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
749-
const [value, setValue] = React.useState(filter || '');
750-
const [create] = useCreate();
751-
752-
const handleSubmit = event => {
753-
event.preventDefault();
754-
create(
755-
'tags',
756-
{
757-
data: {
758-
title: value,
759-
},
760-
},
761-
{
762-
onSuccess: (data) => {
763-
setValue('');
764-
onCreate(data);
765-
},
766-
}
767-
);
752+
753+
const onTagCreate = tag => {
754+
onCreate(tag);
768755
};
769756

770757
return (
771758
<Dialog open onClose={onCancel}>
772-
<form onSubmit={handleSubmit}>
773-
<DialogContent>
774-
<TextField
775-
label="New tag"
776-
value={value}
777-
onChange={event => setValue(event.target.value)}
778-
autoFocus
779-
/>
780-
</DialogContent>
781-
<DialogActions>
782-
<Button type="submit">Save</Button>
783-
<Button onClick={onCancel}>Cancel</Button>
784-
</DialogActions>
785-
</form>
759+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Author</DialogTitle>
760+
<IconButton
761+
aria-label="close"
762+
onClick={onCancel}
763+
sx={theme => ({
764+
position: 'absolute',
765+
right: 8,
766+
top: 8,
767+
color: theme.palette.grey[500],
768+
})}
769+
>
770+
<CloseIcon />
771+
</IconButton>
772+
<DialogContent sx={{ p: 0 }}>
773+
<CreateBase
774+
redirect={false}
775+
resource="tags"
776+
mutationOptions={{
777+
onSuccess: tag => {
778+
onTagCreate(tag);
779+
},
780+
}}
781+
>
782+
<SimpleForm defaultValues={{ name: filter }}>
783+
<TextInput
784+
source="name"
785+
helperText={false}
786+
autoFocus
787+
/>
788+
</SimpleForm>
789+
</CreateBase>
790+
</DialogContent>
786791
</Dialog>
787792
);
788793
};

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

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
import * as React from 'react';
22
import { Admin } from 'react-admin';
3+
4+
import CloseIcon from '@mui/icons-material/Close';
5+
import {
6+
Button,
7+
Dialog,
8+
DialogActions,
9+
DialogContent,
10+
DialogTitle,
11+
IconButton,
12+
TextField,
13+
} from '@mui/material';
14+
315
import {
16+
CreateBase,
417
Resource,
5-
required,
6-
useCreate,
7-
useRecordContext,
818
TestMemoryRouter,
19+
required,
920
testDataProvider,
21+
useRecordContext,
1022
} from 'ra-core';
1123
import polyglotI18nProvider from 'ra-i18n-polyglot';
1224
import englishMessages from 'ra-language-english';
13-
import {
14-
Dialog,
15-
DialogContent,
16-
TextField,
17-
DialogActions,
18-
Button,
19-
Stack,
20-
} from '@mui/material';
25+
2126
import { useFormContext } from 'react-hook-form';
2227

2328
import { AdminContext } from '../AdminContext';
2429
import { Create, Edit } from '../detail';
2530
import { SimpleForm } from '../form';
31+
import { ArrayInput, SimpleFormIterator } from './ArrayInput';
2632
import {
2733
AutocompleteArrayInput,
2834
AutocompleteArrayInputProps,
2935
} from './AutocompleteArrayInput';
3036
import { ReferenceArrayInput } from './ReferenceArrayInput';
31-
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
3237
import { TextInput } from './TextInput';
33-
import { ArrayInput, SimpleFormIterator } from './ArrayInput';
38+
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
3439

3540
export default { title: 'ra-ui-materialui/input/AutocompleteArrayInput' };
3641

@@ -686,56 +691,46 @@ export const InsideReferenceArrayInputOnChange = ({
686691

687692
const CreateAuthor = () => {
688693
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
689-
const [name, setName] = React.useState(filter || '');
690-
const [language, setLanguage] = React.useState('');
691-
const [create] = useCreate();
692694

693-
const handleSubmit = event => {
694-
event.preventDefault();
695-
create(
696-
'authors',
697-
{
698-
data: {
699-
name,
700-
language,
701-
},
702-
},
703-
{
704-
onSuccess: ({ data }) => {
705-
setName('');
706-
setLanguage('');
707-
onCreate(data);
708-
},
709-
}
710-
);
695+
const onAuthorCreate = author => {
696+
onCreate(author);
711697
};
712698

713699
return (
714700
<Dialog open onClose={onCancel}>
715-
<form onSubmit={handleSubmit}>
716-
<DialogContent>
717-
<Stack gap={4}>
718-
<TextField
719-
name="name"
720-
label="The author name"
721-
value={name}
722-
onChange={event => setName(event.target.value)}
723-
autoFocus
724-
/>
725-
<TextField
726-
name="language"
727-
label="The author language"
728-
value={language}
729-
onChange={event => setLanguage(event.target.value)}
701+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Author</DialogTitle>
702+
<IconButton
703+
aria-label="close"
704+
onClick={onCancel}
705+
sx={theme => ({
706+
position: 'absolute',
707+
right: 8,
708+
top: 8,
709+
color: theme.palette.grey[500],
710+
})}
711+
>
712+
<CloseIcon />
713+
</IconButton>
714+
<DialogContent sx={{ p: 0 }}>
715+
<CreateBase
716+
redirect={false}
717+
resource="authors"
718+
mutationOptions={{
719+
onSuccess: author => {
720+
onAuthorCreate(author);
721+
},
722+
}}
723+
>
724+
<SimpleForm defaultValues={{ name: filter }}>
725+
<TextInput source="name" helperText={false} />
726+
<TextInput
727+
source="language"
728+
helperText={false}
730729
autoFocus
731730
/>
732-
</Stack>
733-
</DialogContent>
734-
<DialogActions>
735-
<Button type="submit">Save</Button>
736-
<Button onClick={onCancel}>Cancel</Button>
737-
</DialogActions>
738-
</form>
731+
</SimpleForm>
732+
</CreateBase>
733+
</DialogContent>
739734
</Dialog>
740735
);
741736
};
@@ -751,10 +746,7 @@ const BookEditWithReferenceAndCreationSupport = () => (
751746
>
752747
<SimpleForm>
753748
<ReferenceArrayInput reference="authors" source="author">
754-
<AutocompleteArrayInput
755-
create={<CreateAuthor />}
756-
optionText="name"
757-
/>
749+
<AutocompleteArrayInput create={<CreateAuthor />} />
758750
</ReferenceArrayInput>
759751
</SimpleForm>
760752
</Edit>

0 commit comments

Comments
 (0)