Skip to content

Commit 6a7095b

Browse files
fix(color-picker): Disable submit button when custom hex is invalid (#2328)
Fixes: #887 [category:Components]
1 parent 55c53bf commit 6a7095b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

modules/preview-react/color-picker/lib/ColorPicker.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,21 @@ export const ColorPicker = ({
172172
...elemProps
173173
}: ColorPickerProps) => {
174174
const [validHexValue, setValidHexValue] = React.useState('');
175+
const [disabled, setDisabled] = React.useState(true);
175176
const [customHexValue, setCustomHexValue] = React.useState(
176177
isCustomColor(colorSet, value) ? value : ''
177178
);
178179

179180
const onCustomHexChange = (event: React.ChangeEvent<HTMLInputElement>) => {
180181
setCustomHexValue(event.target.value);
181182
setValidHexValue('');
183+
setDisabled(true);
182184
};
183185

184-
const onValidCustomHexChange = (validHexValue: string) => setValidHexValue(validHexValue);
186+
const onValidCustomHexChange = (validHexValue: string) => {
187+
setValidHexValue(validHexValue);
188+
setDisabled(false);
189+
};
185190

186191
const onSubmit = (event: React.FormEvent) => {
187192
if (onSubmitClick) {
@@ -207,7 +212,12 @@ export const ColorPicker = ({
207212
showCheck={value === validHexValue || value === customHexValue}
208213
/>
209214
</ColorInputAndLabel>
210-
<CheckButton aria-label={submitLabel} icon={checkIcon} type="submit" />
215+
<CheckButton
216+
aria-label={submitLabel}
217+
icon={checkIcon}
218+
type="submit"
219+
disabled={disabled}
220+
/>
211221
</ColorInputWrapper>
212222
)}
213223
</ColorPickerContainer>

0 commit comments

Comments
 (0)