1
1
import React , { Fragment , useState } from 'react' ;
2
2
import TextField from '@mui/material/TextField' ;
3
3
import Select from '@mui/material/Select' ;
4
- import FormControl from '@mui/material/FormControl' ;
5
- import { createFilterOptions } from '@mui/material/Autocomplete' ;
4
+ import Snackbar from '@mui/material/Snackbar' ;
6
5
import Button from '@mui/material/Button' ;
7
6
import Box from '@mui/material/Box' ;
7
+ import IconButton from '@mui/material/IconButton' ;
8
+ import CloseIcon from '@mui/icons-material/Close' ;
9
+ import MuiAlert , { AlertProps } from '@mui/material/Alert' ;
8
10
import { MenuItem , Typography } from '@mui/material' ;
9
11
import { useSelector } from 'react-redux' ;
10
12
import { RootState } from '../../../../redux/store' ;
11
- import { error } from 'console' ;
12
-
13
- const filter = createFilterOptions ( ) ;
14
13
15
14
const AddContextForm = ( {
16
15
contextStore,
17
16
handleClickSelectContext,
18
17
handleDeleteContextClick,
19
- renderTable,
20
18
contextInput,
21
19
setContextInput,
22
20
currentContext,
23
- setCurrentContext
21
+ setCurrentContext,
22
+ errorMsg,
23
+ errorStatus,
24
+ setErrorStatus
24
25
} ) => {
25
26
const { allContext } = contextStore ;
26
27
console . log ( 'all contexts' , allContext ) ;
27
28
const [ btnDisabled , setBtnDisabled ] = useState ( false ) ;
29
+ const [ open , setOpen ] = useState ( false ) ;
28
30
const { state, isDarkMode } = useSelector ( ( store : RootState ) => ( {
29
31
isDarkMode : store . darkMode . isDarkMode ,
30
32
state : store . appState
31
33
} ) ) ;
32
34
const color = isDarkMode ? 'white' : 'black' ;
33
- const [ selection , setSelection ] = React . useState ( '' ) ;
34
35
35
36
const handleClick = ( ) => {
36
- if ( contextInput === '' || contextInput === null ) {
37
- window . alert ( 'must enter context name' ) ;
38
- return ;
39
- }
40
37
handleClickSelectContext ( ) ;
38
+ setOpen ( true ) ;
41
39
} ;
42
40
43
- // const onChange = (event, newValue) => {
44
- // if (typeof newValue === 'string') {
45
- // setContextInput({
46
- // name: newValue
47
- // });
48
- // } else if (newValue && newValue.inputValue) {
49
- // // Create a new contextInput from the user input
50
- // setContextInput({
51
- // name: newValue.inputValue,
52
- // values: []
53
- // });
54
- // renderTable(newValue);
55
- // } else {)
56
- // setContextInput(newValue);
57
- // renderTable(newValue);
58
- // }
59
- // };
60
-
61
41
const handleChange = ( e ) => {
62
- setContextInput ( { name : e . target . value } ) ;
42
+ setErrorStatus ( false ) ;
43
+ setOpen ( false ) ;
44
+ setContextInput ( e . target . value ) ;
63
45
} ;
64
46
65
- const filterOptions = ( options , params ) => {
66
- // setBtnDisabled(true);
67
- const filtered = filter ( options , params ) ;
68
- const { inputValue } = params ;
69
- // Suggest the creation of a new contextInput
70
- const isExisting = options . some ( ( option ) => inputValue === option . name ) ;
71
- if ( inputValue !== '' && ! isExisting ) {
72
- filtered . push ( {
73
- inputValue,
74
- name : `Add "${ inputValue } "`
75
- } ) ;
76
-
77
- // setBtnDisabled(false);
47
+ const handleClose = (
48
+ event : React . SyntheticEvent | Event ,
49
+ reason ?: string
50
+ ) => {
51
+ if ( reason === 'clickaway' ) {
52
+ return ;
78
53
}
79
54
80
- return filtered ;
81
- } ;
82
-
83
- const getOptionLabel = ( option ) => {
84
- // Value selected with enter, right from the input
85
- if ( typeof option === 'string' ) {
86
- return option ;
87
- }
88
- // Add "xxx" option created dynamically
89
- if ( option . inputValue ) {
90
- return option . inputValue ;
91
- }
92
- // Regular option
93
- return option . name ;
55
+ setOpen ( false ) ;
94
56
} ;
95
57
96
- const renderOption = ( props , option ) => (
97
- < li style = { { color : 'black' } } { ...props } >
98
- { option . name }
99
- </ li >
100
- ) ;
58
+ const Alert = React . forwardRef < HTMLDivElement , AlertProps > ( function Alert (
59
+ props ,
60
+ ref
61
+ ) {
62
+ return < MuiAlert elevation = { 6 } ref = { ref } variant = "filled" { ...props } /> ;
63
+ } ) ;
101
64
102
65
const contexts = allContext . map ( ( context ) => {
103
66
return (
@@ -113,56 +76,59 @@ const AddContextForm = ({
113
76
Create Context
114
77
</ Typography >
115
78
< Box sx = { { display : 'flex' , gap : 2 , mb : 4 } } >
116
- { /* <Autocomplete
117
- id="autoCompleteContextField"
118
- value={contextInput}
119
- onChange={onChange}
120
- filterOptions={filterOptions}
121
- selectOnFocus
122
- clearOnBlur
123
- handleHomeEndKeys
124
- options={allContext || []}
125
- getOptionLabel={getOptionLabel}
126
- renderOption={renderOption}
127
- sx={{ width: 425, border: '1px solid black' }}
128
- freeSolo
129
- renderInput={(params) => (
130
- <TextField
131
- {...params}
132
- InputProps={{
133
- ...params.InputProps,
134
- style: { color: color }
135
- }}
136
- variant="filled"
137
- label="Create/Select Context"
138
- />
139
- )}
140
- /> */ }
141
79
< TextField
142
80
InputProps = { {
143
81
style : { color : 'black' }
144
82
} }
145
- // {...params}
146
- // InputProps={{
147
- // ...params.InputProps,
148
- // style: { color: color }
149
- // }}
150
83
onChange = { handleChange }
151
84
sx = { {
152
85
width : 425 ,
153
- border : '1px solid black' ,
154
- color : 'black !important'
86
+ border : '1px solid black'
155
87
} }
156
88
variant = "filled"
157
89
label = "Create Context"
90
+ value = { contextInput }
91
+ helperText = { errorStatus ? errorMsg : null }
92
+ error = { errorStatus }
158
93
/>
94
+ < Snackbar
95
+ open = { open && ! errorStatus }
96
+ autoHideDuration = { 6000 }
97
+ onClose = { handleClose }
98
+ >
99
+ < Alert
100
+ onClose = { handleClose }
101
+ severity = "success"
102
+ sx = { { width : '100%' , color : 'white' } }
103
+ >
104
+ Context Created
105
+ </ Alert >
106
+ </ Snackbar >
159
107
< Button
160
108
variant = "contained"
161
109
onClick = { handleClick }
162
110
disabled = { btnDisabled }
163
111
>
164
112
Create
165
113
</ Button >
114
+ </ Box >
115
+ < Typography style = { { color : color } } variant = "h6" gutterBottom = { true } >
116
+ Select Context
117
+ </ Typography >
118
+ < Box sx = { { display : 'flex' , gap : 2 , mb : 4 } } >
119
+ < Select
120
+ sx = { { width : 425 } }
121
+ style = { { border : '1px solid #0099e6' , color : 'black' } }
122
+ value = { currentContext }
123
+ label = "Select Context"
124
+ MenuProps = { { disablePortal : true } }
125
+ onChange = { ( e ) => {
126
+ console . log ( e ) ;
127
+ setCurrentContext ( e . target . value ) ;
128
+ } }
129
+ >
130
+ { contexts }
131
+ </ Select >
166
132
< Button
167
133
color = "error"
168
134
variant = "contained"
@@ -171,25 +137,6 @@ const AddContextForm = ({
171
137
Delete
172
138
</ Button >
173
139
</ Box >
174
- < Typography style = { { color : color } } variant = "h6" gutterBottom = { true } >
175
- Select Context
176
- </ Typography >
177
-
178
- < Select
179
- style = { { border : '1px solid #0099e6' , color : 'black' } }
180
- value = { currentContext }
181
- label = "Select Context"
182
- MenuProps = { { disablePortal : true } }
183
- onChange = { ( e ) => {
184
- console . log ( e ) ;
185
- setCurrentContext ( e . target . value ) ;
186
- } }
187
-
188
- // value={props.selectValue}
189
- // onChange={props.handleChange}
190
- >
191
- { contexts }
192
- </ Select >
193
140
</ Fragment >
194
141
) ;
195
142
} ;
0 commit comments