All files / color-editable-input-rgba/src index.tsx

100% Statements 41/41
100% Branches 62/62
100% Functions 8/8
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                              1x                         13x 13x   3x 3x 1x   3x 2x     13x 3x 3x 2x   3x 1x     13x 9x 6x 3x 3x 3x   6x 1x 1x   6x 2x 2x   6x 1x   6x 1x   6x 1x       13x 13x                               2x                 1x                 1x                   5x                 1x      
import React from 'react';
import EditableInput, { EditableInputProps } from '@uiw/react-color-editable-input';
import { HsvaColor, color as handleColor, RgbaColor, ColorResult, hsvaToRgba, rgbaToHsva } from '@uiw/color-convert';
 
export interface EditableInputRGBAProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
  prefixCls?: string;
  hsva: HsvaColor;
  placement?: 'top' | 'left' | 'bottom' | 'right';
  rProps?: EditableInputProps;
  gProps?: EditableInputProps;
  bProps?: EditableInputProps;
  aProps?: false | EditableInputProps;
  onChange?: (color: ColorResult) => void;
}
 
const EditableInputRGBA = React.forwardRef<HTMLDivElement, EditableInputRGBAProps>((props, ref) => {
  const {
    prefixCls = 'w-color-editable-input-rgba',
    hsva,
    placement = 'bottom',
    rProps = {},
    gProps = {},
    bProps = {},
    aProps = {},
    className,
    style,
    onChange,
    ...other
  } = props;
  const rgba = (hsva ? hsvaToRgba(hsva) : {}) as RgbaColor;
  function handleBlur(evn: React.FocusEvent<HTMLInputElement>) {
    const value = Number(evn.target.value);
    if (value && value > 255) {
      evn.target.value = '255';
    }
    if (value && value < 0) {
      evn.target.value = '0';
    }
  }
  const handleAlphaBlur = (evn: React.FocusEvent<HTMLInputElement>) => {
    const value = Number(evn.target.value);
    if (value && value > 100) {
      evn.target.value = '100';
    }
    if (value && value < 0) {
      evn.target.value = '0';
    }
  };
  const handleChange = (value: string | number, type: 'r' | 'g' | 'b' | 'a', evn: React.ChangeEvent<HTMLInputElement>) => {
    if (typeof value === 'number') {
      if (type === 'a') {
        if (value < 0) value = 0;
        if (value > 100) value = 100;
        onChange && onChange(handleColor(rgbaToHsva({ ...rgba, a: value / 100 })));
      }
      if (value > 255) {
        value = 255;
        evn.target.value = '255';
      }
      if (value < 0) {
        value = 0;
        evn.target.value = '0';
      }
      if (type === 'r') {
        onChange && onChange(handleColor(rgbaToHsva({ ...rgba, r: value })));
      }
      if (type === 'g') {
        onChange && onChange(handleColor(rgbaToHsva({ ...rgba, g: value })));
      }
      if (type === 'b') {
        onChange && onChange(handleColor(rgbaToHsva({ ...rgba, b: value })));
      }
    }
  };
  const roundedAlpha = rgba.a ? Math.round(rgba.a * 100) / 100 : 0;
  return (
    <div
      ref={ref}
      className={[prefixCls, className || ''].filter(Boolean).join(' ')}
      {...other}
      style={{
        fontSize: 11,
        display: 'flex',
        ...style,
      }}
    >
      <EditableInput
        label="R"
        value={rgba.r || 0}
        onBlur={handleBlur}
        placement={placement}
        onChange={(evn, val) => handleChange(val, 'r', evn)}
        {...rProps}
        style={{ ...rProps.style }}
      />
      <EditableInput
        label="G"
        value={rgba.g || 0}
        onBlur={handleBlur}
        placement={placement}
        onChange={(evn, val) => handleChange(val, 'g', evn)}
        {...gProps}
        style={{ marginLeft: 5, ...rProps.style }}
      />
      <EditableInput
        label="B"
        value={rgba.b || 0}
        onBlur={handleBlur}
        placement={placement}
        onChange={(evn, val) => handleChange(val, 'b', evn)}
        {...bProps}
        style={{ marginLeft: 5, ...bProps.style }}
      />
      {aProps && (
        <EditableInput
          label="A"
          value={parseInt(String(roundedAlpha * 100), 10)}
          onBlur={handleAlphaBlur}
          placement={placement}
          onChange={(evn, val) => handleChange(val, 'a', evn)}
          {...aProps}
          style={{ marginLeft: 5, ...aProps.style }}
        />
      )}
    </div>
  );
});
 
EditableInputRGBA.displayName = 'EditableInputRGBA';
 
export default EditableInputRGBA;