@@ -2,27 +2,68 @@ import React, { useState, useContext } from 'react';
2
2
import Grid from '@material-ui/core/Grid' ;
3
3
import { stateContext } from '../../context/context' ;
4
4
import HTMLItem from './HTMLItem' ;
5
+ import { makeStyles } from '@material-ui/core/styles' ;
5
6
6
7
const HTMLPanel = ( ) : JSX . Element => {
8
+ const classes = useStyles ( ) ;
9
+
7
10
const [ tag , setTag ] = useState ( '' ) ;
8
11
const [ name , setName ] = useState ( '' ) ;
9
12
const [ currentID , setCurrentID ] = useState ( 12 ) ;
10
13
const [ state , dispatch ] = useContext ( stateContext ) ;
14
+ const [ errorMsg , setErrorMsg ] = useState ( '' ) ;
15
+ const [ errorStatus , setErrorStatus ] = useState ( false ) ;
16
+
11
17
12
18
const handleTagChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
19
+ resetError ( ) ;
13
20
setTag ( e . target . value ) ;
14
- }
21
+ } ;
15
22
16
23
const handleNameChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
24
+ resetError ( ) ;
17
25
setName ( e . target . value ) ;
18
- }
26
+ } ;
19
27
20
- const handleSubmit = ( e ) => {
21
- e . preventDefault ( ) ;
28
+ const checkNameDupe = ( inputName : String ) => {
29
+ let checkList = state . HTMLTypes . slice ( ) ;
30
+
31
+ // checks to see if inputted comp name already exists
32
+ let dupe = false ;
33
+ checkList . forEach ( HTMLTag => {
34
+ if ( ( HTMLTag . name . toLowerCase ( ) === inputName . toLowerCase ( ) ) || ( HTMLTag . tag . toLowerCase ( ) === inputName . toLowerCase ( ) ) ) {
35
+ dupe = true ;
36
+ }
37
+ } ) ;
38
+ return dupe ;
39
+ } ;
40
+
41
+ const triggerError = ( type : String ) => {
42
+ setErrorStatus ( true ) ;
43
+ if ( type === 'empty' ) {
44
+ setErrorMsg ( 'Tag/ Tag name cannot be blank.' ) ;
45
+ } else if ( type === 'dupe' ) {
46
+ setErrorMsg ( 'Tag/ Tag name already exists.' ) ;
47
+ } else if ( type === 'letters' ) {
48
+ setErrorMsg ( 'Tag/ Tag name must start with a letter.' ) ;
49
+ } else if ( type === 'symbolsDetected' ) {
50
+ setErrorMsg ( 'Tag/ Tag name must not contain symbols.' ) ;
51
+ }
52
+ } ;
53
+
54
+ const resetError = ( ) => {
55
+ setErrorStatus ( false ) ;
56
+ } ;
57
+
58
+ const createOption = ( inputTag : String , inputName : String ) => {
59
+ // format name so first letter is capitalized and there are no whitespaces
60
+ let inputNameClean = inputName . replace ( / \s + / g, '' ) ;
61
+ const formattedName = inputNameClean . charAt ( 0 ) . toUpperCase ( ) + inputNameClean . slice ( 1 ) ;
62
+ // add new component to state
22
63
const newElement = {
23
64
id : currentID ,
24
- tag,
25
- name,
65
+ tag : inputTag ,
66
+ name : formattedName ,
26
67
style : { } ,
27
68
placeHolderShort : name ,
28
69
placeHolderLong : '' ,
@@ -35,25 +76,56 @@ const HTMLPanel = (): JSX.Element => {
35
76
setCurrentID ( currentID + 1 ) ;
36
77
setTag ( '' ) ;
37
78
setName ( '' ) ;
38
- console . log ( "SUBMIT BUTTON CLICKED: " , newElement ) ;
79
+ } ;
80
+
81
+ const alphanumeric = input => {
82
+ let letterNumber = / ^ [ 0 - 9 a - z A - Z ] + $ / ;
83
+ if ( input . match ( letterNumber ) ) return true ;
84
+ return false ;
85
+ }
86
+
87
+ const handleSubmit = ( e ) => {
88
+ e . preventDefault ( ) ;
89
+ let letters = / [ a - z A - Z ] / ;
90
+ if ( ( ! tag . charAt ( 0 ) . match ( letters ) ) || ( ! name . charAt ( 0 ) . match ( letters ) ) ) {
91
+ triggerError ( 'letters' ) ;
92
+ return ;
93
+ } else if ( ! alphanumeric ( tag ) || ! alphanumeric ( name ) ) {
94
+ triggerError ( 'symbolsDetected' ) ;
95
+ return ;
96
+ } else if ( ( tag . trim ( ) === '' ) || ( name . trim ( ) === '' ) ) {
97
+ triggerError ( 'empty' ) ;
98
+ return ;
99
+ } else if ( checkNameDupe ( tag ) || checkNameDupe ( name ) ) {
100
+ triggerError ( 'dupe' ) ;
101
+ return ;
102
+ }
103
+ createOption ( tag , name ) ;
104
+ resetError ( ) ;
39
105
}
40
106
41
107
return (
42
108
< div >
43
109
< h4 > HTML Elements</ h4 >
44
- < div >
45
- < form onSubmit = { handleSubmit } >
46
- < h4 > Create New Element: </ h4 >
47
- < label >
48
- Tag:
49
- < input type = "text" name = "Tag" value = { tag } onChange = { handleTagChange } />
50
- </ label >
51
- < label >
52
- Tag Name:
53
- < input type = "text" name = "Tag Name" value = { name } onChange = { handleNameChange } />
54
- </ label >
55
- < input type = "submit" value = "Add Element" />
56
- </ form >
110
+ < div className = { classes . addComponentWrapper } >
111
+ < div className = { classes . inputWrapper } >
112
+ < form onSubmit = { handleSubmit } >
113
+ < h4 > Create New Element: </ h4 >
114
+ < label className = { classes . inputLabel } >
115
+ Tag:
116
+ < input color = { 'primary' } type = "text" name = "Tag" value = { tag } onChange = { handleTagChange } className = { classes . input } />
117
+ { errorStatus &&
118
+ < span > { errorMsg } </ span > }
119
+ </ label >
120
+ < label className = { classes . inputLabel } >
121
+ Tag Name:
122
+ < input color = { 'primary' } type = "text" name = "Tag Name" value = { name } onChange = { handleNameChange } className = { classes . input } />
123
+ { errorStatus &&
124
+ < span > { errorMsg } </ span > }
125
+ </ label >
126
+ < input className = { classes . button } color = "primary" type = "submit" value = "Add Element" />
127
+ </ form >
128
+ </ div >
57
129
</ div >
58
130
< Grid
59
131
container
@@ -75,4 +147,78 @@ const HTMLPanel = (): JSX.Element => {
75
147
) ;
76
148
} ;
77
149
150
+ const useStyles = makeStyles ( {
151
+ inputField : {
152
+ marginTop : '15px'
153
+ } ,
154
+ inputWrapper : {
155
+ // height: '115px',
156
+ textAlign : 'center' ,
157
+ display : 'flex' ,
158
+ alignItems : 'center' ,
159
+ justifyContent : 'space-between' ,
160
+ // paddingLeft: '35px',
161
+ marginBottom : '15px'
162
+ } ,
163
+ addComponentWrapper : {
164
+ border : '1px solid rgba(70,131,83)' ,
165
+ padding : '20px' ,
166
+ margin : '20px'
167
+ } ,
168
+ rootCheckBox : { } ,
169
+ rootCheckBoxLabel : {
170
+ color : 'white'
171
+ } ,
172
+ panelWrapper : {
173
+ width : '100%' ,
174
+ marginTop : '15px'
175
+ } ,
176
+ panelWrapperList : {
177
+ // maxHeight: '400px',
178
+ minHeight : '120px' ,
179
+ // overflowY: 'auto',
180
+ marginLeft : '-15px' ,
181
+ marginRight : '-15px'
182
+ } ,
183
+ panelSubheader : {
184
+ textAlign : 'center' ,
185
+ color : '#fff'
186
+ } ,
187
+ input : {
188
+ color : '#fff' ,
189
+ borderRadius : '5px' ,
190
+ paddingLeft : '15px' ,
191
+ paddingRight : '10px' ,
192
+ whiteSpace : 'nowrap' ,
193
+ overflowX : 'hidden' ,
194
+ textOverflow : 'ellipsis' ,
195
+ border : '1px solid rgba(51,235,145,0.75)' ,
196
+ backgroundColor : 'rgba(255,255,255,0.15)'
197
+ } ,
198
+ inputLabel : {
199
+ fontSize : '14px' ,
200
+ zIndex : 20 ,
201
+ color : '#fff' ,
202
+ marginTop : '-10px'
203
+ } ,
204
+ btnGroup : {
205
+ display : 'flex' ,
206
+ flexDirection : 'column' ,
207
+ paddingTop : '10px' ,
208
+ marginLeft : '10px'
209
+ } ,
210
+ button : {
211
+ fontSize : '1rem' ,
212
+ height : '40px' ,
213
+ maginTop : '10px' ,
214
+ width : '100%' ,
215
+ // border: '1px solid rgba(70,131,83)',
216
+ backgroundColor : 'rgba(1,212,109,0.1)'
217
+ } ,
218
+ rootToggle : {
219
+ color : '#01d46d' ,
220
+ fontSize : '0.85rem'
221
+ }
222
+ } ) ;
223
+
78
224
export default HTMLPanel ;
0 commit comments