index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* eslint-disable no-param-reassign */
  2. import React from 'react';
  3. import { CustomPicker } from 'react-color';
  4. import {
  5. EditableInput,
  6. Hue,
  7. Saturation,
  8. } from 'react-color/lib/components/common';
  9. import Button from '../Button';
  10. import {
  11. Wrapper,
  12. HueWrapper,
  13. SaturationWrapper,
  14. Label,
  15. InputWrapper,
  16. Swatch,
  17. } from './styled';
  18. type Props = {
  19. hex: string;
  20. hsl: unknown;
  21. hsv: unknown;
  22. onChange: (info: ColorType) => void;
  23. onSubmit: (color: unknown) => void;
  24. };
  25. const ColorPicker: React.FC<Props> = ({
  26. hex,
  27. hsl,
  28. hsv,
  29. onChange,
  30. onSubmit,
  31. }: Props) => {
  32. const styles = {
  33. input: {
  34. height: 28,
  35. width: '80px',
  36. border: `1px solid white`,
  37. color: 'white',
  38. backgroundColor: 'transparent',
  39. paddingLeft: 10,
  40. borderRadius: '4px',
  41. },
  42. };
  43. const handleClick = () => {
  44. onSubmit({ hex, hsl, hsv });
  45. };
  46. return (
  47. <Wrapper>
  48. <SaturationWrapper>
  49. <Saturation hsl={hsl} hsv={hsv} onChange={onChange} />
  50. </SaturationWrapper>
  51. <HueWrapper>
  52. <Hue hsl={hsl} onChange={onChange} />
  53. </HueWrapper>
  54. <InputWrapper>
  55. <Swatch bg={hex} />
  56. <Label>Hex</Label>
  57. <EditableInput
  58. label="hex"
  59. style={{ input: styles.input }}
  60. value={hex}
  61. onChange={onChange}
  62. />
  63. <Button
  64. appearance="primary-hollow"
  65. style={{ marginLeft: '100px' }}
  66. onClick={handleClick}
  67. >
  68. OK
  69. </Button>
  70. </InputWrapper>
  71. </Wrapper>
  72. );
  73. };
  74. const areEqual = (
  75. prevProps: Record<string, unknown>,
  76. nextProps: Record<string, unknown>,
  77. ) => {
  78. if (prevProps.color !== nextProps.color) {
  79. return false;
  80. }
  81. return true;
  82. };
  83. export default React.memo(CustomPicker(ColorPicker), areEqual);