1
- import { Card , Typography } from '@material-ui/core' ;
1
+ import {
2
+ Card ,
3
+ Typography ,
4
+ Dialog ,
5
+ DialogContent ,
6
+ TextField as MuiTextField ,
7
+ DialogActions ,
8
+ Button ,
9
+ } from '@material-ui/core' ;
2
10
import { makeStyles } from '@material-ui/core/styles' ;
3
11
import * as React from 'react' ;
4
12
import {
@@ -14,6 +22,8 @@ import {
14
22
Title ,
15
23
minLength ,
16
24
Record ,
25
+ useCreateSuggestionContext ,
26
+ useCreate ,
17
27
} from 'react-admin' ; // eslint-disable-line import/no-unresolved
18
28
19
29
const LinkToRelatedPost = ( { record } : { record ?: Record } ) => (
@@ -34,13 +44,64 @@ const useEditStyles = makeStyles({
34
44
} ,
35
45
} ) ;
36
46
37
- const OptionRenderer = ( { record } : { record ?: Record } ) => (
38
- < span >
39
- { record ?. title } - { record ?. id }
40
- </ span >
41
- ) ;
47
+ const OptionRenderer = ( { record } : { record ?: Record } ) => {
48
+ return record . id === '@@ra-create' ? (
49
+ < span > { record . name } </ span >
50
+ ) : (
51
+ < span >
52
+ { record ?. title } - { record ?. id }
53
+ </ span >
54
+ ) ;
55
+ } ;
56
+
57
+ const inputText = record =>
58
+ record . id === '@@ra-create'
59
+ ? record . name
60
+ : `${ record . title } - ${ record . id } ` ;
42
61
43
- const inputText = record => `${ record . title } - ${ record . id } ` ;
62
+ const CreatePost = ( ) => {
63
+ const { filter, onCancel, onCreate } = useCreateSuggestionContext ( ) ;
64
+ const [ value , setValue ] = React . useState ( filter || '' ) ;
65
+ const [ create ] = useCreate ( 'posts' ) ;
66
+ const handleSubmit = ( event : React . FormEvent ) => {
67
+ event . preventDefault ( ) ;
68
+ create (
69
+ {
70
+ payload : {
71
+ data : {
72
+ title : value ,
73
+ } ,
74
+ } ,
75
+ } ,
76
+ {
77
+ onSuccess : ( { data } ) => {
78
+ setValue ( '' ) ;
79
+ const choice = data ;
80
+ onCreate ( choice ) ;
81
+ } ,
82
+ }
83
+ ) ;
84
+ return false ;
85
+ } ;
86
+ return (
87
+ < Dialog open onClose = { onCancel } >
88
+ < form onSubmit = { handleSubmit } >
89
+ < DialogContent >
90
+ < MuiTextField
91
+ label = "New post title"
92
+ value = { value }
93
+ onChange = { event => setValue ( event . target . value ) }
94
+ autoFocus
95
+ />
96
+ </ DialogContent >
97
+ < DialogActions >
98
+ < Button type = "submit" > Save</ Button >
99
+ < Button onClick = { onCancel } > Cancel</ Button >
100
+ </ DialogActions >
101
+ </ form >
102
+ </ Dialog >
103
+ ) ;
104
+ } ;
44
105
45
106
const CommentEdit = props => {
46
107
const classes = useEditStyles ( ) ;
@@ -86,6 +147,7 @@ const CommentEdit = props => {
86
147
fullWidth
87
148
>
88
149
< AutocompleteInput
150
+ create = { < CreatePost /> }
89
151
matchSuggestion = { (
90
152
filterValue ,
91
153
suggestion
0 commit comments