Skip to content

Commit aa2abdf

Browse files
authored
Merge pull request marmelab#10710 from marmelab/rewrite-selectInput-create-example
[DOC] selectInput create example + storie
2 parents 914d5df + 746de8e commit aa2abdf

File tree

2 files changed

+312
-177
lines changed

2 files changed

+312
-177
lines changed

docs/SelectInput.md

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -158,66 +158,70 @@ To allow users to add new options, pass a React element as the `create` prop. `<
158158

159159
{% raw %}
160160
```jsx
161-
import { CreateCategory } from './CreateCategory';
161+
import { CreateAuthor } from './CreateAuthor';
162162

163-
const PostCreate = () => (
163+
const BookCreate = () => (
164164
<Create>
165165
<SimpleForm>
166-
<TextInput source="title" />
167-
<ReferenceInput source="category_id" reference="categories">
168-
<SelectInput create={<CreateCategory />} />
166+
<ReferenceInput reference="authors" source="author">
167+
<SelectInput
168+
create={<CreateAuthor />}
169+
/>
169170
</ReferenceInput>
170171
</SimpleForm>
171172
</Create>
172173
);
173174

174-
// in ./CreateCategory.js
175-
import { useCreate, useCreateSuggestionContext } from 'react-admin';
175+
// in ./CreateAuthor.js
176+
import React from 'react';
177+
import { CreateBase, SimpleForm, TextInput, useCreateSuggestionContext } from 'react-admin';
178+
import CloseIcon from '@mui/icons-material/Close';
176179
import {
177-
Box,
178-
BoxProps,
179180
Button,
180181
Dialog,
181-
DialogActions,
182182
DialogContent,
183-
TextField,
183+
DialogTitle,
184+
IconButton,
184185
} from '@mui/material';
185186

186-
const CreateCategory = () => {
187-
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
188-
const [create] = useCreate();
189-
const [value, setValue] = React.useState(filter || '');
190-
191-
const handleSubmit = event => {
192-
event.preventDefault();
193-
create(
194-
'categories',
195-
{ data: { title: value } },
196-
{
197-
onSuccess: (data) => {
198-
setValue('');
199-
onCreate(data);
200-
},
201-
}
202-
);
187+
const CreateAuthor = () => {
188+
const { onCancel, onCreate } = useCreateSuggestionContext();
189+
190+
const onAuthorCreate = author => {
191+
onCreate(author);
203192
};
204193

205194
return (
206195
<Dialog open onClose={onCancel}>
207-
<form onSubmit={handleSubmit}>
208-
<DialogContent>
209-
<TextField
210-
label="New category name"
211-
value={value}
212-
onChange={event => setValue(event.target.value)}
213-
autoFocus
214-
/>
215-
</DialogContent>
216-
<DialogActions>
217-
<Button type="submit">Save</Button>
218-
<Button onClick={onCancel}>Cancel</Button>
219-
</DialogActions>
220-
</form>
196+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Author</DialogTitle>
197+
<IconButton
198+
aria-label="close"
199+
onClick={onCancel}
200+
sx={theme => ({
201+
position: 'absolute',
202+
right: 8,
203+
top: 8,
204+
color: theme.palette.grey[500],
205+
})}
206+
>
207+
<CloseIcon />
208+
</IconButton>
209+
<DialogContent sx={{ p: 0 }}>
210+
<CreateBase
211+
redirect={false}
212+
resource="authors"
213+
mutationOptions={{
214+
onSuccess: author => {
215+
onAuthorCreate(author);
216+
},
217+
}}
218+
>
219+
<SimpleForm>
220+
<TextInput source="name" helperText={false} />
221+
<TextInput source="language" helperText={false} autoFocus />
222+
</SimpleForm>
223+
</CreateBase>
224+
</DialogContent>
221225
</Dialog>
222226
);
223227
};
@@ -695,43 +699,42 @@ const PostCreate = () => {
695699

696700
const CreateCategory = () => {
697701
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
698-
const [value, setValue] = React.useState(filter || '');
699-
const [create] = useCreate();
700-
701-
const handleSubmit = event => {
702-
event.preventDefault();
703-
create(
704-
'categories',
705-
{
706-
data: {
707-
title: value,
708-
},
709-
},
710-
{
711-
onSuccess: (data) => {
712-
setValue('');
713-
onCreate(data);
714-
},
715-
}
716-
);
702+
703+
const onCategoryCreate = category => {
704+
onCreate(category);
717705
};
718706

707+
719708
return (
720709
<Dialog open onClose={onCancel}>
721-
<form onSubmit={handleSubmit}>
722-
<DialogContent>
723-
<TextField
724-
label="New category name"
725-
value={value}
726-
onChange={event => setValue(event.target.value)}
727-
autoFocus
728-
/>
729-
</DialogContent>
730-
<DialogActions>
731-
<Button type="submit">Save</Button>
732-
<Button onClick={onCancel}>Cancel</Button>
733-
</DialogActions>
734-
</form>
710+
<DialogTitle sx={{ m: 0, p: 2 }}>Create Category</DialogTitle>
711+
<IconButton
712+
aria-label="close"
713+
onClick={onCancel}
714+
sx={theme => ({
715+
position: 'absolute',
716+
right: 8,
717+
top: 8,
718+
color: theme.palette.grey[500],
719+
})}
720+
>
721+
<CloseIcon />
722+
</IconButton>
723+
<DialogContent sx={{ p: 0 }}>
724+
<CreateBase
725+
redirect={false}
726+
resource="categories"
727+
mutationOptions={{
728+
onSuccess: category => {
729+
onCategoryCreate(category);
730+
},
731+
}}
732+
>
733+
<SimpleForm>
734+
<TextInput source="name" helperText={false} autoFocus/>
735+
</SimpleForm>
736+
</CreateBase>
737+
</DialogContent>
735738
</Dialog>
736739
);
737740
};

0 commit comments

Comments
 (0)