Skip to content

Commit

Permalink
fix(@clayui/color-picker): RGB inputs should only accept numeric valu…
Browse files Browse the repository at this point in the history
…es from 0 to 255
  • Loading branch information
Carlos Lancha committed Feb 25, 2021
1 parent eaff646 commit 4a7db84
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions packages/clay-color-picker/src/Custom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ const RGBInput: React.FunctionComponent<IRGBInputProps> = ({
const inputRef = React.useRef(null);
const [inputValue, setInputValue] = React.useState(value);

const handleOnChange = (event: any) => {
const value = event.target.value;

if (value === '') {
return;
}

let newVal = Number(value);

if (newVal < 0) {
newVal = 0;
} else if (newVal > 255) {
newVal = 255;
}

setInputValue(newVal);

onChange({[name]: newVal});
};

React.useEffect(() => {
if (document.activeElement !== inputRef.current) {
setInputValue(value);
Expand All @@ -55,15 +75,11 @@ const RGBInput: React.FunctionComponent<IRGBInputProps> = ({
<ClayInput
data-testid={`${name}Input`}
insetBefore
onChange={(event) => {
const newVal = Number(event.target.value);

setInputValue(newVal);

onChange({[name]: newVal});
}}
max="255"
min="0"
onChange={handleOnChange}
ref={inputRef}
type="text"
type="number"
value={inputValue}
/>
<ClayInput.GroupInsetItem before tag="label">
Expand Down

0 comments on commit 4a7db84

Please sign in to comment.