index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ColorType = {
  19. h?: string;
  20. s?: string;
  21. l?: string;
  22. v?: string;
  23. a?: string;
  24. hex?: string;
  25. };
  26. type Props = {
  27. hex: string;
  28. hsl: object;
  29. hsv: object;
  30. onChange: (info: ColorType) => void;
  31. onSubmit: (color: any) => void;
  32. };
  33. const ColorPicker: React.FC<Props> = ({
  34. hex,
  35. hsl,
  36. hsv,
  37. onChange,
  38. onSubmit,
  39. }: Props) => {
  40. const styles = {
  41. input: {
  42. height: 28,
  43. width: '80px',
  44. border: `1px solid white`,
  45. color: 'white',
  46. backgroundColor: 'transparent',
  47. paddingLeft: 10,
  48. borderRadius: '4px',
  49. },
  50. };
  51. const handleClick = () => {
  52. onSubmit({ hex, hsl, hsv });
  53. };
  54. return (
  55. <Wrapper>
  56. <SaturationWrapper>
  57. <Saturation hsl={hsl} hsv={hsv} onChange={onChange} />
  58. </SaturationWrapper>
  59. <HueWrapper>
  60. <Hue hsl={hsl} onChange={onChange} />
  61. </HueWrapper>
  62. <InputWrapper>
  63. <Swatch bg={hex} />
  64. <Label>Hex</Label>
  65. <EditableInput
  66. label="hex"
  67. style={{ input: styles.input }}
  68. value={hex}
  69. onChange={onChange}
  70. />
  71. <Button
  72. appearance="primary-hollow"
  73. style={{ marginLeft: '100px' }}
  74. onClick={handleClick}
  75. >
  76. OK
  77. </Button>
  78. </InputWrapper>
  79. </Wrapper>
  80. );
  81. };
  82. const areEqual = (prevProps: any, nextProps: any) => {
  83. if (prevProps.color !== nextProps.color) {
  84. return false;
  85. }
  86. return true;
  87. };
  88. export default React.memo(CustomPicker(ColorPicker), areEqual);