index.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React from 'react';
  2. import { ChromePicker } from 'react-color';
  3. import Portal from '../Portal';
  4. import Icon from '../Icon';
  5. import Typography from '../Typography';
  6. import {
  7. Group,
  8. Item,
  9. Circle,
  10. PickerContainer,
  11. Blanket,
  12. } from '../../global/toolStyled';
  13. import data from './data';
  14. type Props = {
  15. title?: string;
  16. selectedColor?: string;
  17. onClick: (color: string) => void;
  18. mode?: 'normal' | 'shape' | 'watermark';
  19. isCollapse: boolean;
  20. pickerToggle: () => void;
  21. };
  22. type ColorItem = {
  23. key: string | number | undefined;
  24. color: string;
  25. icon?: string;
  26. };
  27. const ColorSelector: React.FC<Props> = ({
  28. title = '',
  29. mode = 'normal',
  30. selectedColor = '',
  31. onClick,
  32. pickerToggle,
  33. isCollapse,
  34. }: Props) => {
  35. const colorList: ColorItem[] = data[mode];
  36. return (
  37. <>
  38. { title ? (
  39. <Typography variant="subtitle" style={{ marginTop: '8px' }} align="left">
  40. {title}
  41. </Typography>
  42. ) : null}
  43. <Group>
  44. {
  45. colorList.map((ele: ColorItem) => (
  46. <Item
  47. key={ele.key}
  48. selected={selectedColor === ele.color}
  49. onMouseDown={(): void => { onClick(ele.color); }}
  50. >
  51. {
  52. ele.icon ? (
  53. <Icon glyph={ele.icon} />
  54. ) : (
  55. <Circle color={ele.color} />
  56. )
  57. }
  58. </Item>
  59. ))
  60. }
  61. <Item onClick={pickerToggle}>
  62. <Icon glyph="color-picker" />
  63. </Item>
  64. {
  65. !isCollapse ? (
  66. <Portal>
  67. <PickerContainer>
  68. <Blanket onClick={pickerToggle} />
  69. <ChromePicker
  70. color={selectedColor}
  71. disableAlpha
  72. onChange={(color: any): void => { onClick(color.hex); }}
  73. />
  74. </PickerContainer>
  75. </Portal>
  76. ) : null
  77. }
  78. </Group>
  79. </>
  80. );
  81. };
  82. export default ColorSelector;