@@ -21,7 +21,7 @@ import { Typography } from '@mui/material';
21
21
22
22
const filter = createFilterOptions ( ) ;
23
23
24
- //START - Table styling
24
+ //START - Table styling ------------
25
25
const StyledTableCell = styled ( TableCell ) ( ( { theme } ) => ( {
26
26
[ `&.${ tableCellClasses . head } ` ] : {
27
27
backgroundColor : theme . palette . common . black ,
@@ -41,7 +41,7 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
41
41
border : 0
42
42
}
43
43
} ) ) ;
44
- //END - table styling
44
+ //END - table styling --------------------------
45
45
46
46
const CreatorForm = ( {
47
47
contextStore,
@@ -69,6 +69,7 @@ const CreatorForm = ({
69
69
setTableState ( targetContext . values ) ;
70
70
} ;
71
71
72
+ // START - autocomplete functionality ----------------
72
73
const handleChange = e => {
73
74
setDataContext ( prevDataContext => {
74
75
return {
@@ -85,6 +86,59 @@ const CreatorForm = ({
85
86
handleClickSelectContext ( temp ) ;
86
87
} ;
87
88
89
+ const autoOnChange = ( event , newValue ) => {
90
+ if ( typeof newValue === 'string' ) {
91
+ setContextInput ( {
92
+ name : newValue
93
+ } ) ;
94
+ } else if ( newValue && newValue . inputValue ) {
95
+ // Create a new contextInput from the user input
96
+ setContextInput ( {
97
+ name : newValue . inputValue
98
+ } ) ;
99
+ } else {
100
+ setContextInput ( newValue ) ;
101
+ renderTable ( newValue ) ;
102
+ }
103
+ } ;
104
+
105
+ const autoFitler = ( options , params ) => {
106
+ const filtered = filter ( options , params ) ;
107
+
108
+ const { inputValue } = params ;
109
+ // Suggest the creation of a new contextInput
110
+ const isExisting = options . some (
111
+ option => inputValue === option . name
112
+ // console.log(inputValue)
113
+ ) ;
114
+ if ( inputValue !== '' && ! isExisting ) {
115
+ filtered . push ( {
116
+ inputValue,
117
+ name : `Add "${ inputValue } "`
118
+ } ) ;
119
+ }
120
+
121
+ return filtered ;
122
+ } ;
123
+
124
+ const autoGetOptions = option => {
125
+ // Value selected with enter, right from the input
126
+ if ( typeof option === 'string' ) {
127
+ return option ;
128
+ }
129
+ // Add "xxx" option created dynamically
130
+ if ( option . inputValue ) {
131
+ return option . inputValue ;
132
+ }
133
+ // Regular option
134
+ return option . name ;
135
+ } ;
136
+
137
+ const autoRenderOptions = ( props , option ) => (
138
+ < li { ...props } > { option . name } </ li >
139
+ ) ;
140
+ // END - autocomplete --------------------------
141
+
88
142
return (
89
143
< Fragment >
90
144
{ /* Input box for context */ }
@@ -95,66 +149,18 @@ const CreatorForm = ({
95
149
< Autocomplete
96
150
id = "autoCompleteContextField"
97
151
value = { contextInput }
98
- onChange = { ( event , newValue ) => {
99
- if ( typeof newValue === 'string' ) {
100
- setContextInput ( {
101
- name : newValue
102
- } ) ;
103
- } else if ( newValue && newValue . inputValue ) {
104
- // Create a new contextInput from the user input
105
- setContextInput ( {
106
- name : newValue . inputValue
107
- } ) ;
108
- } else {
109
- setContextInput ( newValue ) ;
110
- renderTable ( newValue ) ;
111
- }
112
- } }
113
- filterOptions = { ( options , params ) => {
114
- const filtered = filter ( options , params ) ;
115
-
116
- const { inputValue } = params ;
117
- // Suggest the creation of a new contextInput
118
- const isExisting = options . some (
119
- option => inputValue === option . name
120
- // console.log(inputValue)
121
- ) ;
122
- if ( inputValue !== '' && ! isExisting ) {
123
- filtered . push ( {
124
- inputValue,
125
- name : `Add "${ inputValue } "`
126
- } ) ;
127
- }
128
-
129
- return filtered ;
130
- } }
152
+ onChange = { autoOnChange }
153
+ filterOptions = { autoFitler }
131
154
selectOnFocus
132
155
clearOnBlur
133
156
handleHomeEndKeys
134
- id = "free-solo-with-text-demo"
135
157
options = { allContext || [ ] }
136
- getOptionLabel = { option => {
137
- // Value selected with enter, right from the input
138
- if ( typeof option === 'string' ) {
139
- return option ;
140
- }
141
- // Add "xxx" option created dynamically
142
- if ( option . inputValue ) {
143
- return option . inputValue ;
144
- }
145
- // Regular option
146
- return option . name ;
147
- } }
148
- renderOption = { ( props , option ) => < li { ...props } > { option . name } </ li > }
158
+ getOptionLabel = { autoGetOptions }
159
+ renderOption = { autoRenderOptions }
149
160
sx = { { width : 300 } }
150
161
freeSolo
151
162
renderInput = { params => (
152
163
< TextField { ...params } label = "Create/Select Context" />
153
- // <TextField
154
- // {...params}
155
- // label="Free solo with text demo"
156
- // onKeyUp={() => console.log(params.inputProps.value)}
157
- // />
158
164
) }
159
165
/>
160
166
< Button variant = "contained" onClick = { handleClick } >
0 commit comments