Skip to content

Moved SliderGroup to FC. #2832

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 5 commits into from
Dec 6, 2023
Merged
Changes from 4 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
53 changes: 19 additions & 34 deletions src/components/slider/context/SliderGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {Component} from 'react';
import React, {useCallback, useMemo, useState} from 'react';
import {StyleProp, ViewStyle} from 'react-native';
import {Colors} from '../../../style';
import SliderContext from './SliderContext';
import View from '../../view';
import tinycolor from 'tinycolor2';

interface SliderGroupProps {
color: string;
Expand All @@ -11,40 +12,24 @@ interface SliderGroupProps {
children?: React.ReactNode;
}

interface SliderGroupState {
value: tinycolor.ColorFormats.HSLA;
}

export default class SliderGroup extends Component<SliderGroupProps, SliderGroupState> {
static displayName = 'IGNORE';
const SliderGroup = (props: SliderGroupProps) => {
const {color, onValueChange, children} = props;
const [value, setValue] = useState(Colors.getHSL(color));

constructor(props: SliderGroupProps) {
super(props);
const valueSetter = useCallback((value: tinycolor.ColorFormats.HSLA) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

'valueSetter' is not a proper name for a callback. We usually use the name of the prop it is passed to or as an action describing what this method does

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is _setValue better?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes

setValue(value);
onValueChange?.(Colors.getHexString(value));
},
[onValueChange]);

this.state = {
value: Colors.getHSL(props.color)
};
}
const providerValue = useMemo(() => ({value, setValue: valueSetter}), [value, valueSetter]);

getContextProviderValue() {
return {
value: this.state.value,
setValue: this.setValue
};
}
return (
<View {...props}>
<SliderContext.Provider value={providerValue}>{children}</SliderContext.Provider>
</View>
);
};

setValue = (value: tinycolor.ColorFormats.HSLA) => {
this.setState({value});
this.props.onValueChange?.(Colors.getHexString(value));
};

render() {
return (
<View {...this.props}>
<SliderContext.Provider value={this.getContextProviderValue()}>
{this.props.children}
</SliderContext.Provider>
</View>
);
}
}
SliderGroup.displayName = 'IGNORE';
export default SliderGroup;