1
- import React from 'react' ;
1
+ import React , { useMemo } from 'react' ;
2
2
import { HsvaColor , hsvaToHslaString } from '@uiw/color-convert' ;
3
3
import Interactive , { Interaction } from '@uiw/react-drag-event-interactive' ;
4
4
import { Pointer , PointerProps } from './Pointer' ;
5
5
6
6
export interface SaturationProps extends Omit < React . HTMLAttributes < HTMLDivElement > , 'onChange' > {
7
7
prefixCls ?: string ;
8
8
/** hsva => `{ h: 0, s: 75, v: 82, a: 1 }` */
9
- hsva : HsvaColor ;
9
+ hsva ?: HsvaColor ;
10
+ hue ?: number ;
10
11
radius ?: React . CSSProperties [ 'borderRadius' ] ;
11
12
/** React Component, Custom pointer component */
12
13
pointer ?: ( { prefixCls, left, top, color } : PointerProps ) => JSX . Element ;
13
14
onChange ?: ( newColor : HsvaColor ) => void ;
14
15
}
15
16
16
17
const Saturation = React . forwardRef < HTMLDivElement , SaturationProps > ( ( props , ref ) => {
17
- const { prefixCls = 'w-color-saturation' , radius = 0 , pointer, className, style, hsva, onChange, ...other } = props ;
18
+ const { prefixCls = 'w-color-saturation' , radius = 0 , pointer, className, hue = 0 , style, hsva, onChange, ...other } = props ;
18
19
const containerStyle : React . CSSProperties = {
19
20
width : 200 ,
20
21
height : 200 ,
@@ -25,6 +26,7 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
25
26
26
27
const handleChange = ( interaction : Interaction , event : MouseEvent | TouchEvent ) => {
27
28
onChange &&
29
+ hsva &&
28
30
onChange ( {
29
31
h : hsva . h ,
30
32
s : interaction . left * 100 ,
@@ -34,17 +36,19 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
34
36
} ) ;
35
37
} ;
36
38
37
- const comProps = {
38
- top : `${ 100 - hsva . v } %` ,
39
- left : `${ hsva . s } %` ,
40
- color : hsvaToHslaString ( hsva ) ,
41
- } ;
42
- const pointerElement =
43
- pointer && typeof pointer === 'function' ? (
44
- pointer ( { prefixCls, ...comProps } )
45
- ) : (
46
- < Pointer prefixCls = { prefixCls } { ...comProps } />
47
- ) ;
39
+ const pointerElement = useMemo ( ( ) => {
40
+ if ( ! hsva ) return null ;
41
+ const comProps = {
42
+ top : `${ 100 - hsva . v } %` ,
43
+ left : `${ hsva . s } %` ,
44
+ color : hsvaToHslaString ( hsva ) ,
45
+ } ;
46
+ if ( pointer && typeof pointer === 'function' ) {
47
+ return pointer ( { prefixCls, ...comProps } ) ;
48
+ }
49
+ return < Pointer prefixCls = { prefixCls } { ...comProps } /> ;
50
+ } , [ hsva , pointer , prefixCls ] ) ;
51
+
48
52
return (
49
53
< Interactive
50
54
className = { [ prefixCls , className || '' ] . filter ( Boolean ) . join ( ' ' ) }
@@ -53,7 +57,9 @@ const Saturation = React.forwardRef<HTMLDivElement, SaturationProps>((props, ref
53
57
position : 'absolute' ,
54
58
inset : 0 ,
55
59
cursor : 'crosshair' ,
56
- backgroundImage : `linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, hsl(${ hsva . h } , 100%, 50%))` ,
60
+ backgroundImage : `linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, hsl(${
61
+ hsva ?. h ?? hue
62
+ } , 100%, 50%))`,
57
63
...containerStyle ,
58
64
} }
59
65
ref = { ref }
0 commit comments