import React, { useState, useEffect } from 'react'; import { InputNumber } from 'antd'; import './index.scss'; /** * 渐变颜色拾取器 * @param current 位置 * @param color 颜色--数组(包含两项) * @param angle 角度(-360~360) * @param handleChange 变更回调函数 * @return {*} * @constructor */ const GradientColorPicker = ({ current = 0, color = [], angle = 0, handleChange }: IGradientColorPicker) => { const [curPos, setCurPos] = useState(current); const [gradientAngle, setGradientAngle] = useState(angle); const start = color?.[0] ?? '#5B8FF9'; const end = color?.[1] ?? '#5B8FF9'; const gradientNodes = [ { key: 'start', name: '起点', pos: 0, background: start, active: curPos === 0 }, { key: 'block', name: '背景块', background: `linear-gradient(${gradientAngle + 90}deg, ${start}, ${end})` }, { key: 'end', name: '起点', pos: 1, background: end, active: curPos === 1 } ]; useEffect(() => { setGradientAngle(angle); }, [angle]); // 选择需要修改的起始位置 const choosePos = (val:number) => { setCurPos(val); typeof handleChange === 'function' && handleChange({ position: val }); }; // 修改角度 const changeAngle = (val:number) => { val = val && typeof Number(val) === 'number' ? val : angle; setGradientAngle(val); typeof handleChange === 'function' && handleChange({ angle: val }); }; return (
{gradientNodes.map((item) => ( choosePos(item.pos)} /> ))}
); }; export default GradientColorPicker; export interface IGradientColorPicker { current: number; color: string[]; angle: number; handleChange: Function; }