Skip to content

Commit 8bec546

Browse files
committed
Add an error state to the color picker for invalid values
1 parent 6bcc4a1 commit 8bec546

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/components/ColorPicker.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const ColorPicker = React.forwardRef(
2828
ref
2929
) => {
3030
const [color, setColor] = useState({ input: '#000000', picker: '#000000' });
31+
const [hasError, setHasError] = useState(false);
3132

3233
useEffect(() => {
3334
if (value && color.picker !== value) {
@@ -102,11 +103,17 @@ const ColorPicker = React.forwardRef(
102103
'margin-right-lg': alignment === 'right',
103104
'margin-left-md': alignment === 'right',
104105
})}
106+
error={hasError}
105107
onChange={(event) => {
106108
setColor({ ...color, input: event.target.value });
107109
}}
108110
onKeyDown={enterKeyHandler((event) => {
109-
event.target.blur();
111+
try {
112+
new Color(event.target.value.toLowerCase());
113+
event.target.blur();
114+
} catch {
115+
setHasError(true);
116+
}
110117
})}
111118
onBlur={(event) => {
112119
try {
@@ -115,6 +122,7 @@ const ColorPicker = React.forwardRef(
115122
} catch {
116123
setColor({ ...color, input: value });
117124
}
125+
setHasError(false);
118126
}}
119127
value={color.input}
120128
/>

0 commit comments

Comments
 (0)