@@ -51,8 +51,6 @@ The form value for the source must be the selected value, e.g.
5151
5252** Tip** : If you need to let users select more than one item in the list, check out the [ ` <AutocompleteArrayInput> ` ] ( ./AutocompleteArrayInput.md ) component.
5353
54- ** Tip** : ` <AutocompleteInput> ` is a stateless component, so it only allows to * filter* the list of choices, not to * extend* it. If you need to populate the list of choices based on the result from a ` fetch ` call (and if [ ` <ReferenceInput> ` ] ( ./ReferenceInput.md ) doesn't cover your need), you'll have to [ write your own Input component] ( ./Inputs.md#writing-your-own-input-component ) based on Material UI ` <Autocomplete> ` component.
55-
5654## Props
5755
5856| Prop | Required | Type | Default | Description |
@@ -153,65 +151,70 @@ To allow users to add new options, pass a React element as the `create` prop. `<
153151
154152{% raw %}
155153``` jsx
156- import { CreateCategory } from ' ./CreateCategory ' ;
154+ import { CreateAuthor } from ' ./CreateAuthor ' ;
157155
158- const PostCreate = () => (
156+ const BookCreate = () => (
159157 < Create>
160158 < SimpleForm>
161- < TextInput source= " title" / >
162- < ReferenceInput source= " category_id" reference= " categories" >
163- < AutocompleteInput create= {< CreateCategory / > } / >
159+ < ReferenceInput reference= " authors" source= " author" >
160+ < AutocompleteInput
161+ create= {< CreateAuthor / > }
162+ / >
164163 < / ReferenceInput>
165164 < / SimpleForm>
166165 < / Create>
167166);
168167
169- // in ./CreateCategory .js
168+ // in ./CreateAuthor .js
170169import React from ' react' ;
171- import { useCreate , useCreateSuggestionContext } from ' react-admin' ;
170+ import { CreateBase , SimpleForm , TextInput , useCreateSuggestionContext } from ' react-admin' ;
171+ import CloseIcon from ' @mui/icons-material/Close' ;
172172import {
173173 Button ,
174174 Dialog ,
175- DialogActions ,
176175 DialogContent ,
177- TextField ,
176+ DialogTitle ,
177+ IconButton ,
178178} from ' @mui/material' ;
179179
180- const CreateCategory = () => {
180+ const CreateAuthor = () => {
181181 const { filter , onCancel , onCreate } = useCreateSuggestionContext ();
182- const [create ] = useCreate ();
183- const [value , setValue ] = React .useState (filter || ' ' );
184-
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- );
182+
183+ const onAuthorCreate = author => {
184+ onCreate (author);
197185 };
198186
199187 return (
200188 < 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>
189+ < DialogTitle sx= {{ m: 0 , p: 2 }}> Create Author< / DialogTitle>
190+ < IconButton
191+ aria- label= " close"
192+ onClick= {onCancel}
193+ sx= {theme => ({
194+ position: ' absolute' ,
195+ right: 8 ,
196+ top: 8 ,
197+ color: theme .palette .grey [500 ],
198+ })}
199+ >
200+ < CloseIcon / >
201+ < / IconButton>
202+ < DialogContent sx= {{ p: 0 }}>
203+ < CreateBase
204+ redirect= {false }
205+ resource= " authors"
206+ mutationOptions= {{
207+ onSuccess : author => {
208+ onAuthorCreate (author);
209+ },
210+ }}
211+ >
212+ < SimpleForm defaultValues= {{ name: filter }}>
213+ < TextInput source= " name" helperText= {false } / >
214+ < TextInput source= " language" helperText= {false } autoFocus / >
215+ < / SimpleForm>
216+ < / CreateBase>
217+ < / DialogContent>
215218 < / Dialog>
216219 );
217220};
@@ -871,21 +874,22 @@ Use the `create` prop when you want a more polished or complex UI. For example a
871874import {
872875 AutocompleteInput ,
873876 Create ,
877+ CreateBase ,
874878 ReferenceInput ,
875879 SimpleForm ,
876880 TextInput ,
877881 useCreate ,
878- useCreateSuggestionContext
882+ useCreateSuggestionContext ,
879883} from ' react-admin' ;
880884
885+ import CloseIcon from ' @mui/icons-material/Close' ;
881886import {
882887 Box ,
883888 BoxProps ,
884889 Button ,
885890 Dialog ,
886- DialogActions ,
887891 DialogContent ,
888- TextField ,
892+ IconButton ,
889893} from ' @mui/material' ;
890894
891895const PostCreate = () => {
@@ -903,43 +907,42 @@ const PostCreate = () => {
903907
904908const CreateCategory = () => {
905909 const { filter , onCancel , onCreate } = useCreateSuggestionContext ();
906- const [value , setValue ] = React .useState (filter || ' ' );
907- const [create ] = useCreate ();
908-
909- const handleSubmit = event => {
910- event .preventDefault ();
911- create (
912- ' categories' ,
913- {
914- data: {
915- title: value,
916- },
917- },
918- {
919- onSuccess : (data ) => {
920- setValue (' ' );
921- onCreate (data);
922- },
923- }
924- );
910+
911+ const onCategoryCreate = category => {
912+ onCreate (category);
925913 };
926914
915+
927916 return (
928917 < Dialog open onClose= {onCancel}>
929- < form onSubmit= {handleSubmit}>
930- < DialogContent>
931- < TextField
932- label= " New category name"
933- value= {value}
934- onChange= {event => setValue (event .target .value )}
935- autoFocus
936- / >
937- < / DialogContent>
938- < DialogActions>
939- < Button type= " submit" > Save< / Button>
940- < Button onClick= {onCancel}> Cancel< / Button>
941- < / DialogActions>
942- < / form>
918+ < DialogTitle sx= {{ m: 0 , p: 2 }}> Create Category< / DialogTitle>
919+ < IconButton
920+ aria- label= " close"
921+ onClick= {onCancel}
922+ sx= {theme => ({
923+ position: ' absolute' ,
924+ right: 8 ,
925+ top: 8 ,
926+ color: theme .palette .grey [500 ],
927+ })}
928+ >
929+ < CloseIcon / >
930+ < / IconButton>
931+ < DialogContent sx= {{ p: 0 }}>
932+ < CreateBase
933+ redirect= {false }
934+ resource= " categories"
935+ mutationOptions= {{
936+ onSuccess : category => {
937+ onCategoryCreate (category);
938+ },
939+ }}
940+ >
941+ < SimpleForm defaultValues= {{ title: filter }}>
942+ < TextInput source= " name" helperText= {false } autoFocus/ >
943+ < / SimpleForm>
944+ < / CreateBase>
945+ < / DialogContent>
943946 < / Dialog>
944947 );
945948};
0 commit comments