11import * as React from 'react' ;
22import { 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+
315import {
16+ CreateBase ,
417 Resource ,
5- required ,
6- useCreate ,
7- useRecordContext ,
818 TestMemoryRouter ,
19+ required ,
920 testDataProvider ,
21+ useRecordContext ,
1022} from 'ra-core' ;
1123import polyglotI18nProvider from 'ra-i18n-polyglot' ;
1224import englishMessages from 'ra-language-english' ;
13- import {
14- Dialog ,
15- DialogContent ,
16- TextField ,
17- DialogActions ,
18- Button ,
19- Stack ,
20- } from '@mui/material' ;
25+
2126import { useFormContext } from 'react-hook-form' ;
2227
2328import { AdminContext } from '../AdminContext' ;
2429import { Create , Edit } from '../detail' ;
2530import { SimpleForm } from '../form' ;
31+ import { ArrayInput , SimpleFormIterator } from './ArrayInput' ;
2632import {
2733 AutocompleteArrayInput ,
2834 AutocompleteArrayInputProps ,
2935} from './AutocompleteArrayInput' ;
3036import { ReferenceArrayInput } from './ReferenceArrayInput' ;
31- import { useCreateSuggestionContext } from './useSupportCreateSuggestion' ;
3237import { TextInput } from './TextInput' ;
33- import { ArrayInput , SimpleFormIterator } from './ArrayInput ' ;
38+ import { useCreateSuggestionContext } from './useSupportCreateSuggestion ' ;
3439
3540export default { title : 'ra-ui-materialui/input/AutocompleteArrayInput' } ;
3641
@@ -686,56 +691,46 @@ export const InsideReferenceArrayInputOnChange = ({
686691
687692const 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