import React, { CSSProperties, PropsWithChildren } from 'react'; import { hsvaToHslString, hsvaToRgba } from "@douyinfe/semi-foundation/colorPicker/utils/convert"; import { cssClasses } from '@douyinfe/semi-foundation/colorPicker/constants'; import cls from 'classnames'; import ColorChooseAreaFoundation, { ColorChooseAreaAdapter, ColorChooseAreaBaseProps, ColorChooseAreaBaseState } from "@douyinfe/semi-foundation/colorPicker/ColorChooseAreaFoundation"; import BaseComponent from "../../_base/baseComponent"; import { round } from "@douyinfe/semi-foundation/colorPicker/utils/round"; export interface ColorChooseAreaProps extends ColorChooseAreaBaseProps{ className?: string; style?: CSSProperties } export interface ColorChooseAreaState extends ColorChooseAreaBaseState{ } class ColorChooseArea extends BaseComponent, ColorChooseAreaState> { ref: React.RefObject; constructor(props) { super(props); this.foundation = new ColorChooseAreaFoundation(this.adapter); this.state = { handlePosition: this.foundation.getHandlePositionByHSVA(), isHandleGrabbing: false, }; this.ref = React.createRef(); } get adapter(): ColorChooseAreaAdapter { return { ...super.adapter, getColorPickerFoundation: ()=>this.props.foundation, handleMouseDown: (e: React.MouseEvent) => { this.setState({ isHandleGrabbing: true }); this.ref.current.addEventListener('mousemove', this.foundation.setHandlePositionByMousePosition); window.addEventListener('mouseup', this.foundation.handleMouseUp); }, handleMouseUp: () => { this.ref.current.removeEventListener('mousemove', this.foundation.setHandlePositionByMousePosition); window.removeEventListener('mouseup', this.foundation.handleMouseUp); this.setState({ isHandleGrabbing: false }); }, getDOM: ()=>this.ref.current, notifyChange: (newColor)=>this.props.onChange(newColor) }; } componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any) { if (JSON.stringify(prevProps.hsva) !== JSON.stringify(this.props.hsva)) { this.setState({ handlePosition: this.foundation.getHandlePositionByHSVA() }); } } handleClick = (e: React.MouseEvent)=>{ this.foundation.setHandlePositionByMousePosition(e); this.foundation.handleMouseDown(e); } render() { const areaBgStyle = hsvaToHslString({ h: this.props.hsva.h, s: 100, v: 100, a: 1 }); const currentColor = hsvaToRgba(this.props.hsva); return
this.foundation.handleMouseDown(e)}>
; } } export default ColorChooseArea;