Skip to content

Commit

Permalink
fix: Disable color picker submit button when custom hex is invalid(#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethjang34 committed Sep 1, 2023
1 parent e0f80f6 commit 3d726c6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions modules/preview-react/color-picker/lib/ColorPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,21 @@ export const ColorPicker = ({
...elemProps
}: ColorPickerProps) => {
const [validHexValue, setValidHexValue] = React.useState('');
const [disabled, setDisabled] = React.useState(true);
const [customHexValue, setCustomHexValue] = React.useState(
isCustomColor(colorSet, value) ? value : ''
);

const onCustomHexChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setCustomHexValue(event.target.value);
setValidHexValue('');
setDisabled(true);
};

const onValidCustomHexChange = (validHexValue: string) => setValidHexValue(validHexValue);
const onValidCustomHexChange = (validHexValue: string) => {
setValidHexValue(validHexValue);
setDisabled(false);
};

const onSubmit = (event: React.FormEvent) => {
if (onSubmitClick) {
Expand All @@ -207,7 +212,12 @@ export const ColorPicker = ({
showCheck={value === validHexValue || value === customHexValue}
/>
</ColorInputAndLabel>
<CheckButton aria-label={submitLabel} icon={checkIcon} type="submit" />
<CheckButton
aria-label={submitLabel}
icon={checkIcon}
type="submit"
disabled={disabled}
/>
</ColorInputWrapper>
)}
</ColorPickerContainer>
Expand Down

0 comments on commit 3d726c6

Please sign in to comment.