Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(react-color-picker): Added RGB fields to stories. #33469

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ValentinaKozlova
Copy link
Contributor

New Behavior

Added RGB color fields to the ColorPicker
color-picker-rgb-fields

@ValentinaKozlova ValentinaKozlova force-pushed the docs/color-picker-rgb-fields branch from 2974329 to 5c7d961 Compare December 13, 2024 14:43
Copy link

github-actions bot commented Dec 13, 2024

📊 Bundle size report

✅ No changes found

Copy link

Pull request demo site: URL

export const Default = () => {
const hexId = useId('hex-input');

const styles = useStyles();
const [color, setColor] = React.useState(DEFAULT_COLOR_HSV);
const [hex, setHex] = React.useState(tinycolor(color).toHexString());
const [rgb, setRgb] = React.useState(tinycolor(color).toRgb());
const [red, setRed] = React.useState(rgb.r);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to store state for red/green/blue since it's already in rgb.

const parseRGBChannelValue = (value: string, min = 0, max = 255) => {
   if (/^\d+$/.test(data.value)) {
     return null;
   }
    
   const parsedValue = parseInt(value, 10);

   if (isNaN(parsedValue)) {
     return null;
   }
   
   return clamp(parsedValue, min, max);
}

const onRgbChange: InputRgbFieldProps<'onChange'> = (event, data) => {
    const value = parseRGBChannelValue(data.value);
    
    // new value is not valid, we shouldn't update state
    if (value === null) {
      return;
    }
    
    // get color key value from the element attribute 
    const colorKey = event.target.name as RgbKey;

    // create new color
    const newColor = tinycolor({ ...rgb, [colorKey]: value });
    
    // update the new color in the component state
    if (newColor.isValid) {
      setColor(newColor.toHsv());
      setHex(newColor.toHex());
      setRgb(newColor.toRgb());
    }
}


// and then

<InputRgbField label="Red" value={rgb.r} name="r" onChange={onRgbChange} />
<InputRgbField label="Green" value={rgb.g} name="g" onChange={onRgbChange} />
<InputRgbField label="Blue" value={rgb.b} name="b" onChange={onRgbChange} />

Also, you can remove handleRgbKeyPress as it's not needed anymore, because we set to state only valid values in onChange

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseRGBChannelValue is not working. Probably, because SpinButton has value as number and displayValue as string. But I'll try to find a workaround to make it work

Copy link
Contributor

@dmytrokirpa dmytrokirpa Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to get the actual input value (string) in the onChange handler, probably 'event.target.value'?

Copy link
Contributor Author

@ValentinaKozlova ValentinaKozlova Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried it at the beginning. With event.target.value SpinButton stops working at all. I mean, it's possible to type digits but increase/decrease logic is not working

@@ -107,3 +177,17 @@ const handleOnBlur = (e: React.FocusEvent<HTMLInputElement>) => {
e.target.removeAttribute('aria-invalid');
}
};

const handleRgbKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any way to get rid of this handler? I still don't get it why can't we use the controlled input with onChange/onInput only, and don't use prevent default and keys whitelist, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on it. When I use restriction in onChange then SpinButton stops working from keyboard. Or if I put preventDefault to SpinButton onChange it also doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants