Skip to content

Commit 9705dac

Browse files
committed
[Doc] improve AutocompleteInput create example
1 parent 7a322df commit 9705dac

File tree

2 files changed

+91
-95
lines changed

2 files changed

+91
-95
lines changed

docs/AutocompleteInput.md

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -153,65 +153,71 @@ To allow users to add new options, pass a React element as the `create` prop. `<
153153

154154
{% raw %}
155155
```jsx
156-
import { CreateCategory } from './CreateCategory';
156+
import { CreateAuthor } from './CreateAuthor';
157157

158-
const PostCreate = () => (
158+
const BookCreate = () => (
159159
<Create>
160160
<SimpleForm>
161-
<TextInput source="title" />
162-
<ReferenceInput source="category_id" reference="categories">
163-
<AutocompleteInput create={<CreateCategory />} />
161+
<ReferenceInput reference="authors" source="author">
162+
<AutocompleteInput
163+
create={<CreateAuthor />}
164+
optionText="name"
165+
/>
164166
</ReferenceInput>
165167
</SimpleForm>
166168
</Create>
167169
);
168170

169-
// in ./CreateCategory.js
171+
// in ./CreateAuthor.js
170172
import React from 'react';
171-
import { useCreate, useCreateSuggestionContext } from 'react-admin';
173+
import { CreateBase, SimpleForm, TextInput, useCreateSuggestionContext } from 'react-admin';
174+
import CloseIcon from '@mui/icons-material/Close';
172175
import {
173176
Button,
174177
Dialog,
175-
DialogActions,
176178
DialogContent,
177-
TextField,
179+
DialogTitle,
180+
IconButton,
178181
} from '@mui/material';
179182

180-
const CreateCategory = () => {
183+
const CreateAuthor = () => {
181184
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
182-
const [create] = useCreate();
183-
const [value, setValue] = React.useState(filter || '');
184185

185-
const handleSubmit = event => {
186-
event.preventDefault();
187-
create(
188-
'categories',
189-
{ data: { title: value } },
190-
{
191-
onSuccess: (data) => {
192-
setValue('');
193-
onCreate(data);
194-
},
195-
}
196-
);
186+
const onAuthorCreate = author => {
187+
onCreate(author);
197188
};
198189

199190
return (
200191
<Dialog open onClose={onCancel}>
201-
<form onSubmit={handleSubmit}>
202-
<DialogContent>
203-
<TextField
204-
label="New category name"
205-
value={value}
206-
onChange={event => setValue(event.target.value)}
207-
autoFocus
208-
/>
209-
</DialogContent>
210-
<DialogActions>
211-
<Button type="submit">Save</Button>
212-
<Button onClick={onCancel}>Cancel</Button>
213-
</DialogActions>
214-
</form>
192+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Author</DialogTitle>
193+
<IconButton
194+
aria-label="close"
195+
onClick={onCancel}
196+
sx={theme => ({
197+
position: 'absolute',
198+
right: 8,
199+
top: 8,
200+
color: theme.palette.grey[500],
201+
})}
202+
>
203+
<CloseIcon />
204+
</IconButton>
205+
<DialogContent sx={{ p: 0 }}>
206+
<CreateBase
207+
redirect={false}
208+
resource="author"
209+
mutationOptions={{
210+
onSuccess: author => {
211+
onAuthorCreate(author);
212+
},
213+
}}
214+
>
215+
<SimpleForm defaultValues={{ name: filter }}>
216+
<TextInput source="name" helperText={false} />
217+
<TextInput source="language" helperText={false} />
218+
</SimpleForm>
219+
</CreateBase>
220+
</DialogContent>
215221
</Dialog>
216222
);
217223
};

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

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,45 @@ import * as React from 'react';
22
import { Admin, AdminContext } from 'react-admin';
33

44
import {
5+
CreateBase,
6+
ListBase,
7+
RecordContextProvider,
58
Resource,
9+
TestMemoryRouter,
610
required,
711
useCreate,
8-
useRecordContext,
9-
ListBase,
1012
useListContext,
11-
RecordContextProvider,
12-
TestMemoryRouter,
13+
useRecordContext,
1314
} from 'ra-core';
1415

16+
import AttributionIcon from '@mui/icons-material/Attribution';
17+
import CloseIcon from '@mui/icons-material/Close';
18+
import ExpandCircleDownIcon from '@mui/icons-material/ExpandCircleDown';
1519
import {
20+
Box,
21+
Button,
1622
Dialog,
17-
DialogContent,
1823
DialogActions,
19-
Button,
24+
DialogContent,
25+
DialogTitle,
26+
IconButton,
27+
InputAdornment,
2028
Stack,
2129
TextField,
2230
Typography,
23-
Box,
24-
InputAdornment,
2531
} from '@mui/material';
26-
import { useFormContext } from 'react-hook-form';
2732
import fakeRestProvider from 'ra-data-fakerest';
2833
import polyglotI18nProvider from 'ra-i18n-polyglot';
2934
import englishMessages from 'ra-language-english';
30-
import ExpandCircleDownIcon from '@mui/icons-material/ExpandCircleDown';
31-
import AttributionIcon from '@mui/icons-material/Attribution';
35+
import { useFormContext } from 'react-hook-form';
3236

37+
import { useState } from 'react';
3338
import { Create, Edit } from '../detail';
3439
import { SimpleForm } from '../form';
3540
import { AutocompleteInput, AutocompleteInputProps } from './AutocompleteInput';
3641
import { ReferenceInput } from './ReferenceInput';
3742
import { TextInput } from './TextInput';
3843
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
39-
import { useState } from 'react';
4044

4145
export default { title: 'ra-ui-materialui/input/AutocompleteInput' };
4246

@@ -826,56 +830,42 @@ export const InsideReferenceInputWithError = () => (
826830

827831
const CreateAuthor = () => {
828832
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
829-
const [name, setName] = React.useState(filter || '');
830-
const [language, setLanguage] = React.useState('');
831-
const [create] = useCreate();
832833

833-
const handleSubmit = event => {
834-
event.preventDefault();
835-
create(
836-
'authors',
837-
{
838-
data: {
839-
name,
840-
language,
841-
},
842-
},
843-
{
844-
onSuccess: data => {
845-
setName('');
846-
setLanguage('');
847-
onCreate(data);
848-
},
849-
}
850-
);
834+
const onAuthorCreate = author => {
835+
onCreate(author);
851836
};
852837

853838
return (
854839
<Dialog open onClose={onCancel}>
855-
<form onSubmit={handleSubmit}>
856-
<DialogContent>
857-
<Stack gap={4}>
858-
<TextField
859-
name="name"
860-
label="The author name"
861-
value={name}
862-
onChange={event => setName(event.target.value)}
863-
autoFocus
864-
/>
865-
<TextField
866-
name="language"
867-
label="The author language"
868-
value={language}
869-
onChange={event => setLanguage(event.target.value)}
870-
autoFocus
871-
/>
872-
</Stack>
873-
</DialogContent>
874-
<DialogActions>
875-
<Button type="submit">Save</Button>
876-
<Button onClick={onCancel}>Cancel</Button>
877-
</DialogActions>
878-
</form>
840+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Author</DialogTitle>
841+
<IconButton
842+
aria-label="close"
843+
onClick={onCancel}
844+
sx={theme => ({
845+
position: 'absolute',
846+
right: 8,
847+
top: 8,
848+
color: theme.palette.grey[500],
849+
})}
850+
>
851+
<CloseIcon />
852+
</IconButton>
853+
<DialogContent sx={{ p: 0 }}>
854+
<CreateBase
855+
redirect={false}
856+
resource="author"
857+
mutationOptions={{
858+
onSuccess: author => {
859+
onAuthorCreate(author);
860+
},
861+
}}
862+
>
863+
<SimpleForm defaultValues={{ name: filter }}>
864+
<TextInput source="name" helperText={false} />
865+
<TextInput source="language" helperText={false} />
866+
</SimpleForm>
867+
</CreateBase>
868+
</DialogContent>
879869
</Dialog>
880870
);
881871
};

0 commit comments

Comments
 (0)