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 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
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 _setValue = useCallback((value: tinycolor.ColorFormats.HSLA) => {
setValue(value);
onValueChange?.(Colors.getHexString(value));
},
[onValueChange]);

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

getContextProviderValue() {
return {
value: this.state.value,
setValue: this.setValue
};
}
return (
<View {...props}>
<SliderContext.Provider value={contextProviderValue}>{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;