Skip to content

Infra/color slider group remove unsafe #944

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 9 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 13 additions & 14 deletions src/components/slider/ColorSliderGroup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import _ from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';
import React, {PureComponent} from 'react';
import {asBaseComponent} from '../../commons';
import GradientSlider from './GradientSlider';
import SliderGroup from './context/SliderGroup';
import Text from '../text';
import {PureBaseComponent} from '../../commons';


/**
* @description: A Gradient Slider component
* @example: https://github.com/wix/react-native-ui-lib/blob/feat/new_components/demo/src/screens/componentScreens/SliderScreen.js
*/
export default class ColorSliderGroup extends PureBaseComponent {
class ColorSliderGroup extends PureComponent {
static displayName = 'ColorSliderGroup';

static propTypes = {
Expand Down Expand Up @@ -54,25 +54,22 @@ export default class ColorSliderGroup extends PureBaseComponent {
initialColor: this.props.initialColor
}

// static getDerivedStateFromProps(nextProps, prevState) {
// if (prevState.initialColor !== nextProps.initialColor) {
// return {initialColor: nextProps.initialColor};
// }
// return null;
// }

UNSAFE_componentWillReceiveProps(nextProps) {
if (this.state.initialColor !== nextProps.initialColor) {
return {initialColor: nextProps.initialColor};
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.initialColor !== nextProps.initialColor) {
return {
initialColor: nextProps.initialColor
};
}
return null;
}

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

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

return (
<>
{showLabels && (
Expand All @@ -98,3 +95,5 @@ export default class ColorSliderGroup extends PureBaseComponent {
);
}
}

export default asBaseComponent(ColorSliderGroup);
23 changes: 14 additions & 9 deletions src/components/slider/GradientSlider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import _ from 'lodash';
import PropTypes from 'prop-types';
import tinycolor from 'tinycolor2';
import React from 'react';
import React, {Component} from 'react';
import {HueGradient, LightnessGradient, SaturationGradient, Gradient} from 'react-native-color';
import {Colors} from '../../style';
import {BaseComponent} from '../../commons';
import {asBaseComponent} from '../../commons';
import Slider from './index';
import asSliderGroupChild from './context/asSliderGroupChild';

Expand All @@ -20,7 +20,7 @@ const GRADIENT_TYPES = {
* @description: A Gradient Slider component
* @example: https://github.com/wix/react-native-ui-lib/blob/feat/new_components/demo/src/screens/componentScreens/SliderScreen.js
*/
class GradientSlider extends BaseComponent {
class GradientSlider extends Component {
static displayName = 'GradientSlider';

static propTypes = {
Expand Down Expand Up @@ -53,14 +53,19 @@ class GradientSlider extends BaseComponent {
super(props);

this.state = {
prevColor: props.color,
color: props.color ? Colors.getHSL(props.color) : undefined
};
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.color !== nextProps.color) {
this.setState({color: Colors.getHSL(nextProps.color)});
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.prevColor !== nextProps.color) {
return {
color: Colors.getHSL(nextProps.color),
prevColor: Colors.getHSL(nextProps.color)
};
}
return null;
}

getColor() {
Expand Down Expand Up @@ -88,7 +93,7 @@ class GradientSlider extends BaseComponent {

renderHueGradient = () => {
const {gradientSteps} = this.props;

return (
<HueGradient gradientSteps={gradientSteps}/>
);
Expand Down Expand Up @@ -180,7 +185,7 @@ class GradientSlider extends BaseComponent {
}

return (
<Slider
<Slider
renderTrack={renderTrack}
step={step}
maximumValue={maximumValue}
Expand All @@ -195,4 +200,4 @@ class GradientSlider extends BaseComponent {
}
}

export default asSliderGroupChild(GradientSlider);
export default asBaseComponent(asSliderGroupChild(GradientSlider));
15 changes: 1 addition & 14 deletions src/components/slider/context/SliderGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,9 @@ export default class SliderGroup extends Component {
};
}

// static getDerivedStateFromProps(nextProps, prevState) {
// if (nextProps.color !== Colors.getHexString(prevState.value)) {
// return {value: Colors.getHSL(nextProps.color)};
// }
// return null;
// }

UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.color !== Colors.getHexString(this.state.value)) {
return {value: Colors.getHSL(nextProps.color)};
}
}

Comment on lines -24 to -36
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't it break functionality?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not when I checked it. Did you have an issue?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No.
But If I understand correctly..
It supported initializing the color value when passing a different value to the prop. No?

Actually, I think it's better when removed....
merging.

getContextProviderValue() {
return {
value: this.state.value,
value: this.state.value,
setValue: this.setValue
};
}
Expand Down