-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path02-solution.js
More file actions
35 lines (30 loc) · 862 Bytes
/
02-solution.js
File metadata and controls
35 lines (30 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React, { useState } from 'react'
export const validateColor = (value) => {
const div = document.createElement('div')
div.style.color = value
return !!div.style.color
}
const Main = () => {
const defaultColor = 'orange'
const [inputColor, setInputColor] = useState('')
const [validColor, setValidColor] = useState(defaultColor)
const handleChange = (event) => {
const { value } = event.target
setInputColor(value)
if (!value) {
setValidColor(defaultColor)
} else if (validateColor(value)) {
setValidColor(value)
}
}
return (
<div className="Main">
<h3>TDD</h3>
<input placeholder={defaultColor} value={inputColor} onChange={handleChange} />
<div className="Main-box" style={{ backgroundColor: validColor }}>
{validColor}
</div>
</div>
)
}
export default Main