Skip to content

Commit f3e00eb

Browse files
authored
Moved ColorSliderGroup to FC. (#2833)
* Moved ColorSliderGroup to FC * Changed slider props * Changed slider props passing * Added usecallback * Chnaged the way props were passed to the colorslider * Changed invoke to optional chaining * Fixed lint problems * Handle props might be undefined
1 parent 92fb3a5 commit f3e00eb

File tree

2 files changed

+65
-65
lines changed

2 files changed

+65
-65
lines changed

src/components/slider/ColorSlider.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
3+
import Text from '../text';
4+
import GradientSlider, {GradientSliderTypes} from './GradientSlider';
5+
import {ColorSliderGroupProps} from './ColorSliderGroup';
6+
7+
type ColorSliderProps = Pick<
8+
ColorSliderGroupProps,
9+
'sliderContainerStyle' | 'showLabels' | 'labelsStyle' | 'accessible' | 'labels' | 'migrate' | 'initialColor'
10+
> & {
11+
type: GradientSliderTypes;
12+
};
13+
14+
const ColorSlider = (props: ColorSliderProps) => {
15+
const {
16+
labels = {hue: 'Hue', lightness: 'Lightness', saturation: 'Saturation', default: ''},
17+
type,
18+
sliderContainerStyle,
19+
showLabels,
20+
labelsStyle,
21+
accessible,
22+
migrate,
23+
initialColor
24+
} = props;
25+
return (
26+
<>
27+
{showLabels && labels && (
28+
<Text recorderTag={'unmask'} $textNeutral text80 style={labelsStyle} accessible={accessible}>
29+
{labels[type]}
30+
</Text>
31+
)}
32+
<GradientSlider
33+
color={initialColor}
34+
type={type}
35+
containerStyle={sliderContainerStyle}
36+
accessible={accessible}
37+
migrate={migrate}
38+
/>
39+
</>
40+
);
41+
};
42+
43+
export default ColorSlider;
Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import _ from 'lodash';
2-
import React, {PureComponent, GetDerivedStateFromProps} from 'react';
1+
import React, {useState, useEffect, useCallback} from 'react';
32
import {StyleProp, ViewStyle, TextStyle} from 'react-native';
43
import {asBaseComponent} from '../../commons/new';
54
import GradientSlider, {GradientSliderTypes} from './GradientSlider';
65
import SliderGroup from './context/SliderGroup';
7-
import Text from '../text';
6+
import ColorSlider from './ColorSlider';
87

98
type SliderOnValueChange = (value: string) => void;
109

@@ -42,79 +41,37 @@ export type ColorSliderGroupProps = {
4241
* If true the component will have accessibility features enabled
4342
*/
4443
accessible?: boolean;
45-
/**
44+
/**
4645
* Whether to use the new Slider implementation using Reanimated
4746
*/
4847
migrate?: boolean;
4948
};
5049

51-
interface ColorSliderGroupState {
52-
initialColor: string;
53-
}
54-
5550
/**
5651
* @description: A Gradient Slider component
5752
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx
5853
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorSliderGroup/ColorSliderGroup.gif?raw=true
5954
*/
60-
class ColorSliderGroup extends PureComponent<ColorSliderGroupProps, ColorSliderGroupState> {
61-
static displayName = 'ColorSliderGroup';
62-
63-
static defaultProps = {
64-
labels: {hue: 'Hue', lightness: 'Lightness', saturation: 'Saturation'}
65-
};
66-
67-
state = {
68-
initialColor: this.props.initialColor
69-
};
55+
const ColorSliderGroup = (props: ColorSliderGroupProps) => {
56+
const {initialColor, containerStyle, ...others} = props;
57+
const [color, setColor] = useState(initialColor);
58+
useEffect(() => {
59+
setColor(initialColor);
60+
}, [initialColor]);
7061

71-
static getDerivedStateFromProps: GetDerivedStateFromProps<ColorSliderGroupProps, ColorSliderGroupState> = (nextProps,
72-
prevState) => {
73-
if (prevState.initialColor !== nextProps.initialColor) {
74-
return {
75-
initialColor: nextProps.initialColor
76-
};
77-
}
78-
return null;
79-
};
62+
const onValueChange = useCallback((value: string) => {
63+
props?.onValueChange?.(value);
64+
},
65+
[props]);
8066

81-
onValueChange = (value: string) => {
82-
_.invoke(this.props, 'onValueChange', value);
83-
};
84-
85-
renderSlider = (type: GradientSliderTypes) => {
86-
const {sliderContainerStyle, showLabels, labelsStyle, accessible, labels, migrate} = this.props;
87-
88-
return (
89-
<>
90-
{showLabels && labels && (
91-
<Text recorderTag={'unmask'} $textNeutral text80 style={labelsStyle} accessible={accessible}>
92-
{labels[type]}
93-
</Text>
94-
)}
95-
<GradientSlider
96-
color={this.props.initialColor}
97-
type={type}
98-
containerStyle={sliderContainerStyle}
99-
accessible={accessible}
100-
migrate={migrate}
101-
/>
102-
</>
103-
);
104-
};
105-
106-
render() {
107-
const {containerStyle} = this.props;
108-
const {initialColor} = this.state;
109-
110-
return (
111-
<SliderGroup style={containerStyle} color={initialColor} onValueChange={this.onValueChange}>
112-
{this.renderSlider(GradientSlider.types.HUE)}
113-
{this.renderSlider(GradientSlider.types.LIGHTNESS)}
114-
{this.renderSlider(GradientSlider.types.SATURATION)}
115-
</SliderGroup>
116-
);
117-
}
118-
}
67+
return (
68+
<SliderGroup style={containerStyle} color={color} onValueChange={onValueChange}>
69+
<ColorSlider type={GradientSlider.types.HUE} initialColor={initialColor} {...others}/>
70+
<ColorSlider type={GradientSlider.types.LIGHTNESS} initialColor={initialColor} {...others}/>
71+
<ColorSlider type={GradientSlider.types.SATURATION} initialColor={initialColor} {...others}/>
72+
</SliderGroup>
73+
);
74+
};
11975

76+
ColorSliderGroup.displayName = 'ColorSliderGroup';
12077
export default asBaseComponent<ColorSliderGroupProps, typeof ColorSliderGroup>(ColorSliderGroup);

0 commit comments

Comments
 (0)