From f8c718301e6c3015dd53e684469f05cb9665110c Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 22 Sep 2023 00:53:16 +0800 Subject: [PATCH] feat: support null/undefined & add `hue` props (#128). --- packages/color-saturation/README.md | 16 +++++++++++ packages/color-saturation/src/index.tsx | 36 ++++++++++++++----------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/packages/color-saturation/README.md b/packages/color-saturation/README.md index 94a0d6d62..f28985da7 100644 --- a/packages/color-saturation/README.md +++ b/packages/color-saturation/README.md @@ -34,6 +34,22 @@ export default function Demo() { } ``` +The value of `hsva` does not exist + +```jsx mdx:preview +import React from 'react'; +import Saturation from '@uiw/react-color-saturation'; + +export default function Demo() { + return ( +
+ + + +
+ ); +} +``` ## Props ```ts diff --git a/packages/color-saturation/src/index.tsx b/packages/color-saturation/src/index.tsx index 83da28f27..66f64ef92 100644 --- a/packages/color-saturation/src/index.tsx +++ b/packages/color-saturation/src/index.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useMemo } from 'react'; import { HsvaColor, hsvaToHslaString } from '@uiw/color-convert'; import Interactive, { Interaction } from '@uiw/react-drag-event-interactive'; import { Pointer, PointerProps } from './Pointer'; @@ -6,7 +6,8 @@ import { Pointer, PointerProps } from './Pointer'; export interface SaturationProps extends Omit, 'onChange'> { prefixCls?: string; /** hsva => `{ h: 0, s: 75, v: 82, a: 1 }` */ - hsva: HsvaColor; + hsva?: HsvaColor; + hue?: number; radius?: React.CSSProperties['borderRadius']; /** React Component, Custom pointer component */ pointer?: ({ prefixCls, left, top, color }: PointerProps) => JSX.Element; @@ -14,7 +15,7 @@ export interface SaturationProps extends Omit((props, ref) => { - const { prefixCls = 'w-color-saturation', radius = 0, pointer, className, style, hsva, onChange, ...other } = props; + const { prefixCls = 'w-color-saturation', radius = 0, pointer, className, hue = 0, style, hsva, onChange, ...other } = props; const containerStyle: React.CSSProperties = { width: 200, height: 200, @@ -25,6 +26,7 @@ const Saturation = React.forwardRef((props, ref const handleChange = (interaction: Interaction, event: MouseEvent | TouchEvent) => { onChange && + hsva && onChange({ h: hsva.h, s: interaction.left * 100, @@ -34,17 +36,19 @@ const Saturation = React.forwardRef((props, ref }); }; - const comProps = { - top: `${100 - hsva.v}%`, - left: `${hsva.s}%`, - color: hsvaToHslaString(hsva), - }; - const pointerElement = - pointer && typeof pointer === 'function' ? ( - pointer({ prefixCls, ...comProps }) - ) : ( - - ); + const pointerElement = useMemo(() => { + if (!hsva) return null; + const comProps = { + top: `${100 - hsva.v}%`, + left: `${hsva.s}%`, + color: hsvaToHslaString(hsva), + }; + if (pointer && typeof pointer === 'function') { + return pointer({ prefixCls, ...comProps }); + } + return ; + }, [hsva, pointer, prefixCls]); + return ( ((props, ref position: 'absolute', inset: 0, cursor: 'crosshair', - backgroundImage: `linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, hsl(${hsva.h}, 100%, 50%))`, + backgroundImage: `linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, hsl(${ + hsva?.h ?? hue + }, 100%, 50%))`, ...containerStyle, }} ref={ref}