@@ -232,24 +232,45 @@ Try changing the input value to to `monalisa` to show the `success` state.
232232
233233``` javascript live noinline
234234const App = () => {
235- const [value , setValue ] = React .useState (' mona lisa' )
235+ const [value , setValue ] = React .useState ()
236+ const [validationState , setValidationState ] = React .useState ()
237+
238+ React .useEffect (() => {
239+ const defaultHandle = ' mona lisa'
240+ setValue (defaultHandle)
241+ validate (defaultHandle)
242+ }, [])
243+
244+ const validate = (inputValue ) => {
245+ if (/ \s / g .test (inputValue)) {
246+ setValidationState (' error' )
247+ } else {
248+ setValidationState (' success' )
249+ }
250+ }
236251
237252 const handleChange = (event ) => {
253+ event .preventDefault ()
254+ if (! event .target .value ) {
255+ setValue (undefined )
256+ setValidationState (undefined )
257+ return
258+ }
238259 setValue (event .target .value )
260+ validate (event .target .value )
239261 }
240262
241- const isInvalid = / \s / g .test (value)
242-
243263 return (
244- < FormControl validationStatus= {isInvalid ? ' error ' : ' success ' } fullWidth>
264+ < FormControl validationStatus= {validationState } fullWidth>
245265 < FormControl .Label > GitHub handle< / FormControl .Label >
246266 < TextInput onChange= {handleChange} value= {value} fullWidth / >
247- {isInvalid ? (
267+ {validationState && validationState === ' error ' && (
248268 < FormControl .Validation >
249269 GitHub handles cannot contain spaces.{' ' }
250270 {value && ` Did you mean "${ value .replaceAll (' ' , ' ' )} "` }
251271 < / FormControl .Validation >
252- ) : (
272+ )}
273+ {validationState && validationState === ' success' && (
253274 < FormControl .Validation > Valid name< / FormControl .Validation >
254275 )}
255276 < / FormControl>
0 commit comments