index.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React from 'react';
  2. import Icon from '../Icon';
  3. import Typography from '../Typography';
  4. import ColorPicker from '../ColorPicker';
  5. import Portal from '../Portal';
  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' | 'freehand' | 'watermark';
  19. isCollapse: boolean;
  20. pickerToggle: (e?: React.MouseEvent<HTMLElement>) => void;
  21. throughPortal: boolean;
  22. };
  23. type ColorItem = {
  24. key: string | number | undefined;
  25. color: string;
  26. icon?: string;
  27. };
  28. const ColorSelector: React.FC<Props> = ({
  29. title = '',
  30. mode = 'normal',
  31. selectedColor = '',
  32. onClick,
  33. pickerToggle,
  34. isCollapse,
  35. throughPortal,
  36. }: Props) => {
  37. const colorList: ColorItem[] = data[mode];
  38. let ColorPickerGenerator = (
  39. <PickerContainer>
  40. <Blanket onMouseDown={pickerToggle} />
  41. <ColorPicker
  42. color={selectedColor}
  43. onSubmit={(color: { hex: string }): void => {
  44. pickerToggle();
  45. onClick(color.hex);
  46. }}
  47. />
  48. </PickerContainer>
  49. );
  50. if (throughPortal) {
  51. ColorPickerGenerator = <Portal>{ColorPickerGenerator}</Portal>;
  52. }
  53. return (
  54. <>
  55. {title ? (
  56. <Typography
  57. variant="subtitle"
  58. style={{ marginTop: '8px' }}
  59. align="left"
  60. >
  61. {title}
  62. </Typography>
  63. ) : null}
  64. <Group>
  65. {colorList.map((ele: ColorItem) => (
  66. <Item
  67. key={ele.key}
  68. selected={selectedColor === ele.color}
  69. onMouseDown={(): void => {
  70. onClick(ele.color);
  71. }}
  72. >
  73. {ele.icon ? (
  74. <Icon glyph={ele.icon} />
  75. ) : (
  76. <Circle color={ele.color} />
  77. )}
  78. </Item>
  79. ))}
  80. <Item
  81. onMouseDown={pickerToggle}
  82. selected={!colorList.find((ele) => selectedColor === ele.color)}
  83. >
  84. <Icon glyph="color-picker" />
  85. </Item>
  86. {!isCollapse ? ColorPickerGenerator : null}
  87. </Group>
  88. </>
  89. );
  90. };
  91. export default ColorSelector;