index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React, { CSSProperties, PropsWithChildren, ReactNode } from 'react';
  2. import ColorPickerFoundation, {
  3. ColorPickerProps,
  4. ColorPickerState
  5. } from '@douyinfe/semi-foundation/colorPicker/foundation';
  6. import BaseComponent from '../_base/baseComponent';
  7. import { PopoverProps } from '../popover';
  8. import ColorChooseArea from './ColorChooseArea';
  9. import { ColorPickerAdapter, ColorValue } from '@douyinfe/semi-foundation/colorPicker/foundation';
  10. import AlphaSlider from './AlphaSlider';
  11. import ColorSlider from './ColorSlider';
  12. import DataPart from './DataPart';
  13. import cls from 'classnames';
  14. import "@douyinfe/semi-foundation/colorPicker/colorPicker.scss";
  15. import { cssClasses } from '@douyinfe/semi-foundation/colorPicker/constants';
  16. import Popover from '../popover';
  17. import {
  18. hexToHsva,
  19. hexToRgba, hsvaStringToHsva, hsvaToHex, hsvaToRgba,
  20. rgbaStringToHsva,
  21. rgbaStringToRgba, rgbaToHex, rgbStringToHsva, rgbStringToRgba,
  22. } from '@douyinfe/semi-foundation/colorPicker/utils/convert';
  23. export interface ColorPickerReactProps extends ColorPickerProps {
  24. usePopover?: boolean;
  25. popoverProps?: PopoverProps;
  26. className?: string;
  27. style?: CSSProperties;
  28. bottomSlot?: ReactNode;
  29. topSlot?: ReactNode
  30. }
  31. export interface ColorPickerReactState extends ColorPickerState {
  32. }
  33. class ColorPicker extends BaseComponent<PropsWithChildren<ColorPickerReactProps>, ColorPickerReactState> {
  34. static __SemiComponentName__ = "ColorPicker";
  35. public foundation: ColorPickerFoundation;
  36. constructor(props: ColorPickerReactProps) {
  37. super(props);
  38. this.foundation = new ColorPickerFoundation(this.adapter);
  39. const initValue = (props.value ?? props.defaultValue);
  40. this.state = {
  41. currentColor: initValue,
  42. };
  43. }
  44. static defaultProps = {
  45. defaultValue: {
  46. hsva: { h: 176, s: 71, v: 77, a: 1 },
  47. rgba: { r: 57, g: 197, b: 187, a: 1 },
  48. hex: '#39c5bb'
  49. },
  50. eyeDropper: true,
  51. defaultFormat: 'hex'
  52. }
  53. get adapter(): ColorPickerAdapter<ColorPickerReactProps, ColorPickerReactState> {
  54. return {
  55. ...super.adapter,
  56. notifyChange: (value) => {
  57. this.props.onChange?.(value);
  58. }
  59. };
  60. }
  61. static colorStringToValue = (raw: string) => {
  62. if (raw.startsWith("#")) {
  63. return {
  64. hsva: hexToHsva(raw),
  65. rgba: hexToRgba(raw),
  66. hex: raw
  67. };
  68. } else if (raw.startsWith('rgba')) {
  69. const rgba = rgbaStringToRgba(raw);
  70. return {
  71. hsva: rgbaStringToHsva(raw),
  72. rgba: rgba,
  73. hex: rgbaToHex(rgba)
  74. };
  75. } else if (raw.startsWith("rgb")) {
  76. const rgba = rgbStringToRgba(raw);
  77. return {
  78. hsva: rgbStringToHsva(raw),
  79. rgba: rgba,
  80. hex: rgbaToHex(rgba)
  81. };
  82. } else if (raw.startsWith("hsv")) {
  83. const hsva = hsvaStringToHsva(raw);
  84. const rgba = hsvaToRgba(hsva);
  85. const hex = hsvaToHex(hsva);
  86. return {
  87. hsva,
  88. rgba,
  89. hex
  90. };
  91. } else {
  92. throw new Error("Semi ColorPicker: error on static colorStringToValue method, input value is invalid: " + raw);
  93. }
  94. }
  95. renderPicker() {
  96. const { className: userClassName } = this.props;
  97. const className = cls(`${cssClasses.PREFIX}`, userClassName);
  98. const currentColor = this.foundation.getCurrentColor();
  99. return <div className={className}>
  100. {this.props.topSlot}
  101. <ColorChooseArea hsva={currentColor.hsva} foundation={this.foundation} onChange={({ s, v }) => {
  102. this.foundation.handleChange({
  103. s,
  104. v,
  105. a: currentColor.hsva.a,
  106. h: currentColor.hsva.h
  107. }, 'hsva');
  108. }} handleSize={20} width={this.props.width ?? 280} height={this.props.height ?? 280}/>
  109. <ColorSlider width={this.props.width ?? 280}
  110. height={10}
  111. handleSize={18}
  112. hue={currentColor.hsva.h}
  113. className={'colorSliderWrapper'}
  114. foundation={this.foundation}
  115. />
  116. {this.props.alpha && <AlphaSlider width={this.props.width ?? 280}
  117. height={10}
  118. handleSize={18}
  119. hsva={currentColor.hsva}
  120. className={'alphaSliderWrapper'}
  121. foundation={this.foundation}
  122. />}
  123. <DataPart currentColor={currentColor}
  124. eyeDropper={this.props.eyeDropper}
  125. alpha={this.props.alpha}
  126. width={this.props.width ?? 280}
  127. foundation={this.foundation}
  128. defaultFormat={this.props.defaultFormat}/>
  129. {this.props.bottomSlot}
  130. </div>;
  131. }
  132. render() {
  133. const currentColor = this.foundation.getCurrentColor();
  134. if (this.props.usePopover) {
  135. return <Popover {...this.props.popoverProps}
  136. className={cls(`${cssClasses.PREFIX}-popover`, this.props.popoverProps?.className)}
  137. content={this.renderPicker()}>
  138. {this.props.children ?? <div style={{ backgroundColor: currentColor.hex }}
  139. className={cls(`${cssClasses.PREFIX}-popover-defaultChildren`)}></div>}
  140. </Popover>;
  141. } else {
  142. return this.renderPicker();
  143. }
  144. }
  145. }
  146. export type { ColorValue };
  147. export * from "@douyinfe/semi-foundation/colorPicker/interface";
  148. export default ColorPicker;