2
2
//it will have another object within to hold the key contextInput pairs
3
3
4
4
import React , { Fragment , useState , useEffect } from 'react' ;
5
- import { useDispatch } from 'react-redux'
5
+
6
6
import TextField from '@mui/material/TextField' ;
7
7
import Autocomplete , { createFilterOptions } from '@mui/material/Autocomplete' ;
8
8
import Button from '@mui/material/Button' ;
@@ -17,30 +17,29 @@ import TableCell, { tableCellClasses } from '@mui/material/TableCell';
17
17
import Box from '@mui/material/Box' ;
18
18
19
19
import ContextTable from './ContextTable' ;
20
-
20
+ import { Typography } from '@mui/material' ;
21
21
22
22
const filter = createFilterOptions ( ) ;
23
23
24
- const contextArr = [
25
- {
26
- name : 'theme' ,
27
- contextInputs : [
28
- { key : 'dark' , contextInput : 'black' } ,
29
- { key : 'light' , contextInput : 'white' }
30
- ]
31
-
32
- } ,
33
-
34
- {
35
- name : 'mood' ,
36
- contextInputs : [
37
- { key : 'happy' , contextInput : 'rainbow' } ,
38
- { key : 'sad' , contextInput : 'blue' } ,
39
- ]
40
- }
41
- ]
42
-
43
- //START - Table styling
24
+ // const contextArr = [
25
+ // {
26
+ // name: 'theme',
27
+ // contextInputs: [
28
+ // { key: 'dark', contextInput: 'black' },
29
+ // { key: 'light', contextInput: 'white' }
30
+ // ]
31
+ // },
32
+
33
+ // {
34
+ // name: 'mood',
35
+ // contextInputs: [
36
+ // { key: 'happy', contextInput: 'rainbow' },
37
+ // { key: 'sad', contextInput: 'blue' }
38
+ // ]
39
+ // }
40
+ // ];
41
+
42
+ //START - Table styling
44
43
const StyledTableCell = styled ( TableCell ) ( ( { theme } ) => ( {
45
44
[ `&.${ tableCellClasses . head } ` ] : {
46
45
backgroundColor : theme . palette . common . black ,
@@ -62,95 +61,139 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
62
61
} ) ) ;
63
62
//END - table styling
64
63
65
-
66
-
67
- const CreatorForm = ( { contextStore } ) => {
68
- // const [contextInput, setContextInput] = useState('');
69
-
64
+ const CreatorForm = ( {
65
+ contextStore ,
66
+ handleClickSelectContext ,
67
+ handleClickInputData
68
+ } ) => {
70
69
//array storing all contexts
71
- const { allContext} = contextStore
72
-
73
- const [ contextInput , setContextInput ] = React . useState ( null ) ;
74
-
75
-
76
- const dispatch = useDispatch ( )
77
-
78
- const handleClickSelectContext = ( ) => {
79
- console . log ( document . getElementById ( "autoCompleteContextField" ) )
80
- console . log ( 'COMING FROM CLICK EVENT' , contextInput ) ;
81
-
82
- // dispatch({type: })
83
- }
70
+ const { allContext } = contextStore ;
84
71
72
+ const [ contextInput , setContextInput ] = React . useState ( null ) ;
73
+ const [ dataContext , setDataContext ] = React . useState ( {
74
+ inputKey : '' ,
75
+ inputValue : ''
76
+ } ) ;
77
+
78
+ const handleChange = e => {
79
+ setDataContext ( prevDataContext => {
80
+ return {
81
+ ...prevDataContext ,
82
+ [ e . target . name ] : e . target . value
83
+ } ;
84
+ } ) ;
85
+ } ;
86
+
87
+ const handleClick = ( ) => {
88
+ if ( contextInput === '' || contextInput === null ) return ;
89
+ const temp = contextInput ;
90
+ setContextInput ( '' ) ;
91
+ handleClickSelectContext ( temp ) ;
92
+ } ;
85
93
86
94
return (
87
95
< Fragment >
88
- < Box
89
- sx = { { display : 'flex' } } >
90
- < Autocomplete
91
- id = "autoCompleteContextField"
92
- value = { contextInput }
93
- onChange = { ( event , newValue ) => {
94
- if ( typeof newValue === 'string' ) {
95
- setContextInput ( {
96
- name : newValue ,
97
- } ) ;
98
- } else if ( newValue && newValue . inputValue ) {
99
- // Create a new contextInput from the user input
100
- setContextInput ( {
101
- name : newValue . inputValue ,
102
- } ) ;
103
- } else {
104
- setContextInput ( newValue ) ;
105
- }
106
- } }
107
- filterOptions = { ( options , params ) => {
108
- const filtered = filter ( options , params ) ;
109
-
110
- const { inputValue } = params ;
111
- // Suggest the creation of a new contextInput
112
- const isExisting = options . some ( ( option ) => inputValue === option . name ) ;
113
- if ( inputValue !== '' && ! isExisting ) {
114
- filtered . push ( {
115
- inputValue,
116
- name : `Add "${ inputValue } "` ,
117
- } ) ;
118
- }
119
-
120
- return filtered ;
121
- } }
122
- selectOnFocus
123
- clearOnBlur
124
- handleHomeEndKeys
125
- id = "free-solo-with-text-demo"
126
- options = { allContext || [ ] }
127
- getOptionLabel = { ( option ) => {
128
- // Value selected with enter, right from the input
129
- if ( typeof option === 'string' ) {
130
- return option ;
131
- }
132
- // Add "xxx" option created dynamically
133
- if ( option . inputValue ) {
134
- return option . inputValue ;
135
- }
136
- // Regular option
137
- return option . name ;
138
- } }
139
- renderOption = { ( props , option ) => < li { ...props } > { option . name } </ li > }
140
- sx = { { width : 300 } }
141
- freeSolo
142
- renderInput = { ( params ) => (
143
- < TextField { ...params } label = "Free solo with text demo" />
144
- ) }
145
- />
146
- < Button variant = "contained" onClick = { handleClickSelectContext } > Select/Create</ Button >
147
- { /* <Button variant="contained">Delete</Button> */ }
148
- </ Box >
149
- < ContextTable target = { contextArr [ 0 ] . contextInputs } />
150
-
151
- </ Fragment >
96
+ { /* Input box for context */ }
97
+ < Typography style = { { color : 'black' } } variant = "h6" gutterBottom = { true } >
98
+ Context Input
99
+ </ Typography >
100
+ < Box sx = { { display : 'flex' , gap : 2 , mb : 4 } } >
101
+ < Autocomplete
102
+ id = "autoCompleteContextField"
103
+ value = { contextInput }
104
+ onChange = { ( event , newValue ) => {
105
+ if ( typeof newValue === 'string' ) {
106
+ setContextInput ( {
107
+ name : newValue
108
+ } ) ;
109
+ } else if ( newValue && newValue . inputValue ) {
110
+ // Create a new contextInput from the user input
111
+ setContextInput ( {
112
+ name : newValue . inputValue
113
+ } ) ;
114
+ } else {
115
+ setContextInput ( newValue ) ;
116
+ }
117
+ } }
118
+ filterOptions = { ( options , params ) => {
119
+ const filtered = filter ( options , params ) ;
120
+
121
+ const { inputValue } = params ;
122
+ // Suggest the creation of a new contextInput
123
+ const isExisting = options . some (
124
+ option => inputValue === option . name
125
+ ) ;
126
+ if ( inputValue !== '' && ! isExisting ) {
127
+ filtered . push ( {
128
+ inputValue,
129
+ name : `Add "${ inputValue } "`
130
+ } ) ;
131
+ }
132
+
133
+ return filtered ;
134
+ } }
135
+ selectOnFocus
136
+ clearOnBlur
137
+ handleHomeEndKeys
138
+ id = "free-solo-with-text-demo"
139
+ options = { allContext || [ ] }
140
+ getOptionLabel = { option => {
141
+ // Value selected with enter, right from the input
142
+ if ( typeof option === 'string' ) {
143
+ return option ;
144
+ }
145
+ // Add "xxx" option created dynamically
146
+ if ( option . inputValue ) {
147
+ return option . inputValue ;
148
+ }
149
+ // Regular option
150
+ return option . name ;
151
+ } }
152
+ renderOption = { ( props , option ) => < li { ...props } > { option . name } </ li > }
153
+ sx = { { width : 300 } }
154
+ freeSolo
155
+ renderInput = { params => (
156
+ < TextField { ...params } label = "Create/Select Context" />
157
+ ) }
158
+ />
159
+ < Button variant = "contained" onClick = { handleClick } >
160
+ Create
161
+ </ Button >
162
+ { /* <Button variant="contained">Delete</Button> */ }
163
+ </ Box >
164
+
165
+ { /* Input box for context data */ }
166
+ < Typography style = { { color : 'black' } } variant = "h6" gutterBottom = { true } >
167
+ Add context data
168
+ </ Typography >
169
+ < Box sx = { { display : 'flex' , gap : 2 } } >
170
+ < TextField
171
+ id = "outlined-basic"
172
+ label = "Key"
173
+ variant = "outlined"
174
+ value = { dataContext . inputKey }
175
+ name = "inputKey"
176
+ onChange = { e => handleChange ( e ) }
177
+ />
178
+ < TextField
179
+ id = "outlined-basic"
180
+ label = "Value"
181
+ variant = "outlined"
182
+ value = { dataContext . inputValue }
183
+ name = "inputValue"
184
+ onChange = { e => handleChange ( e ) }
185
+ />
186
+ < Button
187
+ variant = "contained"
188
+ onClick = { ( ) => handleClickInputData ( contextInput , dataContext ) }
189
+ >
190
+ Save
191
+ </ Button >
192
+ </ Box >
193
+
194
+ { /* <ContextTable target={contextArr[0].contextInputs}/> */ }
195
+ </ Fragment >
152
196
) ;
153
- }
197
+ } ;
154
198
155
199
export default CreatorForm ;
156
-
0 commit comments