import React, { CSSProperties, PropsWithChildren } from 'react'; import ColorPickerFoundation from '@douyinfe/semi-foundation/colorPicker/foundation'; import { cssClasses } from '@douyinfe/semi-foundation/colorPicker/constants'; import cls from 'classnames'; import ColorSliderFoundation, { ColorSliderAdapter, ColorSliderBaseProps, ColorSliderBaseState } from "@douyinfe/semi-foundation/colorPicker/ColorSliderFoundation"; import BaseComponent from "../../_base/baseComponent"; export interface ColorSliderProps extends ColorSliderBaseProps { className?: string; style?: CSSProperties } export interface ColorSliderState extends ColorSliderBaseState { } class ColorSlider extends BaseComponent, ColorSliderState> { private readonly ref: React.RefObject; constructor(props: ColorSliderProps) { super(props); this.foundation = new ColorSliderFoundation(this.adapter); this.state = { handlePosition: props.hue / 360 * props.width - props.handleSize / 2, isHandleGrabbing: false }; this.ref = React.createRef(); } get adapter(): ColorSliderAdapter { return { ...super.adapter, handleMouseDown: (e: any) => { this.setState({ isHandleGrabbing: true }); window.addEventListener('mousemove', this.foundation.setHandlePositionByMousePosition); window.addEventListener('mouseup', this.foundation.handleMouseUp); }, handleMouseUp: (e: MouseEvent) => { this.setState({ isHandleGrabbing: false }); window.removeEventListener('mousemove', this.foundation.setHandlePositionByMousePosition); window.removeEventListener('mouseup', this.foundation.handleMouseUp); }, getColorPickerFoundation: () => this.props.foundation, getDOM: () => this.ref.current }; } componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any) { if (prevProps.hue !== this.props.hue) { this.setState({ handlePosition: this.props.hue / 360 * this.props.width - this.props.handleSize / 2 }); } } handleClick = (e: React.MouseEvent) => { this.foundation.setHandlePositionByMousePosition(e); this.foundation.handleMouseDown(e); } render() { return
this.foundation.handleMouseDown(e)}>
; } } export default ColorSlider;