import React, { ReactNode } from 'react'; import { HsvaColor, RgbaColor } from '@douyinfe/semi-foundation/colorPicker/types'; import Input from "../../input"; import InputGroup from "../../input/inputGroup"; import InputNumber from "../../inputNumber"; import Select from "../../select"; import Button from "../../button"; import split from "@douyinfe/semi-foundation/colorPicker/utils/split"; import ColorPickerFoundation, { ColorPickerProps } from '@douyinfe/semi-foundation/colorPicker/foundation'; import { isEqual } from 'lodash'; import { IconEyedropper } from '@douyinfe/semi-icons'; import { cssClasses } from '@douyinfe/semi-foundation/colorPicker/constants'; type Value = ColorPickerProps['value'] interface DataPartProps { currentColor: Value; defaultFormat: 'hex'|'rgba'|'hsva'; width: number; alpha?: boolean; foundation: ColorPickerFoundation } interface DataPartState{ format: 'hex'|'rgba'|'hsva'; inputValue: string } class DataPart extends React.Component { constructor(props: DataPartProps) { super(props); this.state = { format: this.props.defaultFormat, inputValue: '' }; } componentDidMount() { this.setState({ inputValue: this.getInputValue() }); } componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any) { if (!isEqual(prevProps.currentColor, this.props.currentColor)|| prevState.format !== this.state.format) { this.setState({ inputValue: this.getInputValue() }); } } getInputValue = ()=>{ const rgba = this.props.currentColor.rgba; const hsva = this.props.currentColor.hsva; const hex = this.props.currentColor.hex; if (this.state.format === 'rgba') { return `${rgba.r},${rgba.g},${rgba.b}`; } else if (this.state.format === 'hsva') { return `${hsva.h},${hsva.s},${hsva.v}`; } else { return hex.slice(0, 7); } } handleChange = (newColor: RgbaColor|HsvaColor|string)=>{ this.props.foundation.handleChange(newColor, this.state.format); } getValueByInputValue = (value: string)=>{ if (this.state.format==='rgba') { const result = split(value, this.state.format); if (result) { return result as RgbaColor; } } else if (this.state.format==='hsva') { const result = split(value, this.state.format); if (result) { return result as HsvaColor; } } else if (this.state.format==='hex') { if (!value.startsWith('#')) { value = '#'+value; } if (/#[\d\w]{6,8}/.test(value)) { return value; } } return false; } handlePickValueWithStraw = async ()=>{ if (!window['EyeDropper']) { return; } //@ts-ignore const eyeDropper = new EyeDropper(); try { const result = await eyeDropper.open(); const color = result['sRGBHex']; if (color.startsWith("#")) { this.props.foundation.handleChange(color, 'hex'); } else if (color.startsWith('rgba')) { const rgba = ColorPickerFoundation.rgbaStringToRgba(color); rgba.a = 1; this.props.foundation.handleChange(rgba, 'rgba'); } } catch (e) { } } render() { const rgba = this.props.currentColor.rgba; return
{ const value = this.getValueByInputValue(v); if (value) { this.handleChange(value); } this.setState({ inputValue: v }); }} /> { this.props.alpha && { if (this.state.format==='rgba') { this.handleChange({ ...this.props.currentColor.rgba, a: Number((v/100).toFixed(2)) }); } else if (this.state.format==='hex') { const rgba = { ...this.props.currentColor.rgba, a: Number((v/100).toFixed(2)) }; const hex = ColorPickerFoundation.rgbaToHex(rgba); this.handleChange(hex); } else if (this.state.format==='hsva') { const rgba = { ...this.props.currentColor.hsva, a: Number((v/100).toFixed(2)) }; this.handleChange(rgba); } }} suffix={%} hideButtons={true} /> }