Skip to content

Moved ColorSliderGroup to FC. #2833

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

Merged
merged 8 commits into from
Dec 13, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 62 additions & 59 deletions src/components/slider/ColorSliderGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
import React, {PureComponent, GetDerivedStateFromProps} from 'react';
import React, {useState, useEffect} from 'react';
import {StyleProp, ViewStyle, TextStyle} from 'react-native';
import {asBaseComponent} from '../../commons/new';
import GradientSlider, {GradientSliderTypes} from './GradientSlider';
Expand Down Expand Up @@ -42,79 +42,82 @@ export type ColorSliderGroupProps = {
* If true the component will have accessibility features enabled
*/
accessible?: boolean;
/**
/**
* Whether to use the new Slider implementation using Reanimated
*/
migrate?: boolean;
};

interface ColorSliderGroupState {
initialColor: string;
}
type ColorSliderProps = Pick<
ColorSliderGroupProps,
'sliderContainerStyle' | 'showLabels' | 'labelsStyle' | 'accessible' | 'labels' | 'migrate' | 'initialColor'
> & {
type: GradientSliderTypes;
};

const ColorSlider = (props: ColorSliderProps) => {
const {type, sliderContainerStyle, showLabels, labelsStyle, accessible, labels, migrate, initialColor} = props;
return (
<>
{showLabels && labels && (
<Text recorderTag={'unmask'} $textNeutral text80 style={labelsStyle} accessible={accessible}>
{labels[type]}
</Text>
)}
<GradientSlider
color={initialColor}
type={type}
containerStyle={sliderContainerStyle}
accessible={accessible}
migrate={migrate}
/>
</>
);
};

/**
* @description: A Gradient Slider component
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorSliderGroup/ColorSliderGroup.gif?raw=true
*/
class ColorSliderGroup extends PureComponent<ColorSliderGroupProps, ColorSliderGroupState> {
static displayName = 'ColorSliderGroup';

static defaultProps = {
labels: {hue: 'Hue', lightness: 'Lightness', saturation: 'Saturation'}
};
const ColorSliderGroup = (props: ColorSliderGroupProps) => {
const {
labels = {hue: 'Hue', lightness: 'Lightness', saturation: 'Saturation', default: ''},
initialColor,
containerStyle,
showLabels,
labelsStyle,
accessible,
migrate,
sliderContainerStyle
} = props;
const [color, setColor] = useState(initialColor);
useEffect(() => {
setColor(initialColor);
}, [initialColor]);

state = {
initialColor: this.props.initialColor
const onValueChange = (value: string) => {
_.invoke(props, 'onValueChange', value);
};

static getDerivedStateFromProps: GetDerivedStateFromProps<ColorSliderGroupProps, ColorSliderGroupState> = (nextProps,
prevState) => {
if (prevState.initialColor !== nextProps.initialColor) {
return {
initialColor: nextProps.initialColor
};
}
return null;
const sliderProps = {
initialColor,
sliderContainerStyle,
showLabels,
labelsStyle,
accessible,
labels,
migrate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you could pick up the props you need for the group (container) and pass the others as "others" to the ColorSlider, no?

Copy link
Collaborator Author

@nitzanyiz nitzanyiz Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of them, Yeah. Fixed.

};

onValueChange = (value: string) => {
_.invoke(this.props, 'onValueChange', value);
};

renderSlider = (type: GradientSliderTypes) => {
const {sliderContainerStyle, showLabels, labelsStyle, accessible, labels, migrate} = this.props;

return (
<>
{showLabels && labels && (
<Text recorderTag={'unmask'} $textNeutral text80 style={labelsStyle} accessible={accessible}>
{labels[type]}
</Text>
)}
<GradientSlider
color={this.props.initialColor}
type={type}
containerStyle={sliderContainerStyle}
accessible={accessible}
migrate={migrate}
/>
</>
);
};

render() {
const {containerStyle} = this.props;
const {initialColor} = this.state;

return (
<SliderGroup style={containerStyle} color={initialColor} onValueChange={this.onValueChange}>
{this.renderSlider(GradientSlider.types.HUE)}
{this.renderSlider(GradientSlider.types.LIGHTNESS)}
{this.renderSlider(GradientSlider.types.SATURATION)}
</SliderGroup>
);
}
}
return (
<SliderGroup style={containerStyle} color={color} onValueChange={onValueChange}>
<ColorSlider type={GradientSlider.types.HUE} {...sliderProps}/>
<ColorSlider type={GradientSlider.types.LIGHTNESS} {...sliderProps}/>
<ColorSlider type={GradientSlider.types.SATURATION} {...sliderProps}/>
</SliderGroup>
);
};

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