|
1 |
| -import _ from 'lodash'; |
2 |
| -import React, {PureComponent, GetDerivedStateFromProps} from 'react'; |
| 1 | +import React, {useState, useEffect, useCallback} from 'react'; |
3 | 2 | import {StyleProp, ViewStyle, TextStyle} from 'react-native';
|
4 | 3 | import {asBaseComponent} from '../../commons/new';
|
5 | 4 | import GradientSlider, {GradientSliderTypes} from './GradientSlider';
|
6 | 5 | import SliderGroup from './context/SliderGroup';
|
7 |
| -import Text from '../text'; |
| 6 | +import ColorSlider from './ColorSlider'; |
8 | 7 |
|
9 | 8 | type SliderOnValueChange = (value: string) => void;
|
10 | 9 |
|
@@ -42,79 +41,37 @@ export type ColorSliderGroupProps = {
|
42 | 41 | * If true the component will have accessibility features enabled
|
43 | 42 | */
|
44 | 43 | accessible?: boolean;
|
45 |
| - /** |
| 44 | + /** |
46 | 45 | * Whether to use the new Slider implementation using Reanimated
|
47 | 46 | */
|
48 | 47 | migrate?: boolean;
|
49 | 48 | };
|
50 | 49 |
|
51 |
| -interface ColorSliderGroupState { |
52 |
| - initialColor: string; |
53 |
| -} |
54 |
| - |
55 | 50 | /**
|
56 | 51 | * @description: A Gradient Slider component
|
57 | 52 | * @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx
|
58 | 53 | * @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorSliderGroup/ColorSliderGroup.gif?raw=true
|
59 | 54 | */
|
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]); |
70 | 61 |
|
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]); |
80 | 66 |
|
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 | +}; |
119 | 75 |
|
| 76 | +ColorSliderGroup.displayName = 'ColorSliderGroup'; |
120 | 77 | export default asBaseComponent<ColorSliderGroupProps, typeof ColorSliderGroup>(ColorSliderGroup);
|
0 commit comments