Created
June 7, 2022 17:07
-
-
Save josecarneiro/4b21ee4aaab903b1ceddee6771ba876c to your computer and use it in GitHub Desktop.
React Training lab iteration 16 solution proposal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ColorSquare = (props) => { | |
return ( | |
<div | |
style={{ | |
display: 'block', | |
width: '2em', | |
height: '2em', | |
backgroundColor: `rgb(${props.r}, ${props.g}, ${props.b})`, | |
}} | |
></div> | |
); | |
}; | |
export default ColorSquare; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react'; | |
import ColorSquare from './ColorSquare'; | |
import SingleColorPicker from './SingleColorPicker'; | |
const RGBColorPicker = () => { | |
const [rValue, setRValue] = useState(255); | |
const [gValue, setGValue] = useState(255); | |
const [bValue, setBValue] = useState(255); | |
return ( | |
<div> | |
<SingleColorPicker color="r" value={rValue} onChange={setRValue} /> | |
<SingleColorPicker color="g" value={gValue} onChange={setGValue} /> | |
<SingleColorPicker color="b" value={bValue} onChange={setBValue} /> | |
<ColorSquare r={rValue} g={gValue} b={bValue} /> | |
<span> | |
rgb({rValue}, {gValue}, {bValue}) | |
</span> | |
</div> | |
); | |
}; | |
export default RGBColorPicker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ColorSquare from './ColorSquare'; | |
const SingleColorPicker = (props) => { | |
const handleValueChange = (event) => { | |
const value = event.target.valueAsNumber; | |
props.onChange(Number.isNaN(value) ? 0 : value); | |
}; | |
return ( | |
<div> | |
<ColorSquare | |
r={props.color === 'r' ? props.value : 0} | |
g={props.color === 'g' ? props.value : 0} | |
b={props.color === 'b' ? props.value : 0} | |
/> | |
<span>{props.color.toUpperCase()}:</span> | |
<input | |
type="number" | |
min="0" | |
max="255" | |
value={props.value} | |
onChange={handleValueChange} | |
/> | |
</div> | |
); | |
}; | |
export default SingleColorPicker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment