@@ -21,7 +21,7 @@ import { Typography } from '@mui/material';
2121
2222const filter = createFilterOptions ( ) ;
2323
24- //START - Table styling
24+ //START - Table styling ------------
2525const StyledTableCell = styled ( TableCell ) ( ( { theme } ) => ( {
2626 [ `&.${ tableCellClasses . head } ` ] : {
2727 backgroundColor : theme . palette . common . black ,
@@ -41,7 +41,7 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
4141 border : 0
4242 }
4343} ) ) ;
44- //END - table styling
44+ //END - table styling --------------------------
4545
4646const CreatorForm = ( {
4747 contextStore,
@@ -69,6 +69,7 @@ const CreatorForm = ({
6969 setTableState ( targetContext . values ) ;
7070 } ;
7171
72+ // START - autocomplete functionality ----------------
7273 const handleChange = e => {
7374 setDataContext ( prevDataContext => {
7475 return {
@@ -85,6 +86,59 @@ const CreatorForm = ({
8586 handleClickSelectContext ( temp ) ;
8687 } ;
8788
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+
88142 return (
89143 < Fragment >
90144 { /* Input box for context */ }
@@ -95,66 +149,18 @@ const CreatorForm = ({
95149 < Autocomplete
96150 id = "autoCompleteContextField"
97151 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 }
131154 selectOnFocus
132155 clearOnBlur
133156 handleHomeEndKeys
134- id = "free-solo-with-text-demo"
135157 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 }
149160 sx = { { width : 300 } }
150161 freeSolo
151162 renderInput = { params => (
152163 < 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- // />
158164 ) }
159165 />
160166 < Button variant = "contained" onClick = { handleClick } >
0 commit comments