-
Notifications
You must be signed in to change notification settings - Fork 734
ColorSliderGroup - refactor #3009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
bb122b6
6b17aae
84ada5a
2deb860
68f059c
b581f6b
f72aa08
18f5a11
1bb51fa
d8da80a
151c256
d312608
9314e71
f3be548
b28f09a
b0e60f1
ceda3f9
249bd8e
83746fc
e9fc135
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
import _ from 'lodash'; | ||
import React, {useCallback, useEffect, useState, useMemo} from 'react'; | ||
import React, {useCallback, useMemo, useContext} from 'react'; | ||
import {asBaseComponent, forwardRef, ForwardRefInjectedProps} from '../../commons/new'; | ||
import {ComponentStatics} from '../../typings/common'; | ||
import {Colors} from '../../style'; | ||
import {Slider as NewSlider} from '../../incubator'; | ||
import Gradient from '../gradient'; | ||
import {GradientSliderProps, GradientSliderTypes, HSLA} from './types'; | ||
import Slider from './index'; | ||
import {SliderContextProps} from './context/SliderContext'; | ||
import asSliderGroupChild from './context/asSliderGroupChild'; | ||
import SliderContext from './SliderContext'; | ||
|
||
type GradientSliderComponentProps<T> = { | ||
sliderContext: SliderContextProps; | ||
} & GradientSliderProps<T>; | ||
|
||
type Props<T> = GradientSliderComponentProps<T> & ForwardRefInjectedProps; | ||
type Props<T> = GradientSliderProps<T> & ForwardRefInjectedProps; | ||
|
||
/** | ||
* @description: A Gradient Slider component | ||
|
@@ -25,29 +21,25 @@ const GradientSlider = <T extends string | HSLA = string>(props: Props<T>) => { | |
const { | ||
type = GradientSliderTypes.DEFAULT, | ||
gradientSteps = 120, | ||
color: propsColors = Colors.$backgroundPrimaryHeavy, | ||
sliderContext, | ||
onValueChange: _onValueChange, | ||
color: propsColor = Colors.$backgroundPrimaryHeavy, // initialColor | ||
onValueChange, | ||
migrate, | ||
containerStyle, | ||
disabled, | ||
accessible, | ||
forwardedRef, | ||
...others | ||
} = props; | ||
const sliderContext = useContext(SliderContext); | ||
|
||
|
||
const initialColor = useMemo((): HSLA => { | ||
return _.isString(propsColors) ? Colors.getHSL(propsColors) : propsColors; | ||
}, [propsColors]); | ||
const [color, setColor] = useState(initialColor); | ||
|
||
useEffect(() => { | ||
setColor(initialColor); | ||
}, [initialColor]); | ||
return _.isString(propsColor) ? Colors.getHSL(propsColor) : propsColor; | ||
}, [propsColor]); | ||
|
||
const getColor = useCallback(() => { | ||
return color || sliderContext.value; | ||
}, [color, sliderContext.value]); | ||
return initialColor || sliderContext.value; | ||
}, [initialColor, sliderContext.value]); | ||
|
||
const hueColor = useMemo(() => { | ||
const color = getColor(); | ||
|
@@ -75,19 +67,12 @@ const GradientSlider = <T extends string | HSLA = string>(props: Props<T>) => { | |
return <Gradient type={Gradient.types.SATURATION} color={hueColor} numberOfSteps={gradientSteps}/>; | ||
}, [hueColor, gradientSteps]); | ||
|
||
const onValueChange = useCallback((value: string, alpha: number) => { | ||
// alpha returns for type.DEFAULT | ||
_onValueChange?.(value, alpha); | ||
}, | ||
[_onValueChange]); | ||
|
||
const updateColor = useCallback((color: HSLA) => { | ||
if (!_.isEmpty(sliderContext)) { | ||
sliderContext.setValue?.(color); | ||
} else { | ||
setColor(color); | ||
const hex = Colors.getHexString(color); | ||
onValueChange(hex, color.a); | ||
const hex = Colors.getHexString(color); // alpha returns for type.DEFAULT | ||
onValueChange?.(hex, color.a); | ||
} | ||
}, | ||
[sliderContext, onValueChange]); | ||
|
@@ -122,7 +107,7 @@ const GradientSlider = <T extends string | HSLA = string>(props: Props<T>) => { | |
|
||
let step = 0.01; | ||
let maximumValue = 1; | ||
let value = color.a; | ||
let value = initialColor.a; | ||
let renderTrack = renderDefaultGradient; | ||
let sliderOnValueChange = updateAlpha; | ||
|
||
|
@@ -172,6 +157,5 @@ GradientSlider.displayName = 'GradientSlider'; | |
GradientSlider.types = GradientSliderTypes; | ||
// @ts-expect-error | ||
export default asBaseComponent<GradientSliderProps, ComponentStatics<typeof GradientSlider>>( | ||
// @ts-expect-error | ||
forwardRef(asSliderGroupChild(forwardRef(GradientSlider))) | ||
forwardRef(forwardRef(GradientSlider)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have forwardRef twice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import React from 'react'; | ||
import {HSLA} from '../types'; | ||
import {HSLA} from './types'; | ||
|
||
export interface SliderContextProps { | ||
export interface SliderContextType { | ||
value?: HSLA; | ||
setValue?: (value: HSLA) => void; | ||
} | ||
|
||
const SliderContext: React.Context<SliderContextProps> = React.createContext({}); | ||
const SliderContext: React.Context<SliderContextType> = React.createContext({}); | ||
SliderContext.displayName = 'IGNORE'; | ||
export default SliderContext; |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add another simplification
Replace asBaseComponent HOC with the
useThemeProps
(see usages in View component)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got issues with the statics and the forwardRef so I decided to leave it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works for me or at least It didn't throw any errors...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I didn't move the forwardRef... I'll make the change