Skip to content

Moved ColorPicker to FC. #2822

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
Dec 24, 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
122 changes: 55 additions & 67 deletions src/components/colorPicker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {PureComponent} from 'react';
import React, {useCallback, useState} from 'react';
import {StyleSheet} from 'react-native';
import {asBaseComponent} from '../../commons/new';
import Assets from '../../assets';
Expand Down Expand Up @@ -58,77 +58,65 @@ const ACCESSIBILITY_LABELS = {
* @notes: This is a screen width component
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorPicker/ColorPicker.gif?raw=true
*/
class ColorPicker extends PureComponent<Props> {
static displayName = 'ColorPicker';
const ColorPicker = (props: Props) => {
const {
accessibilityLabels = ACCESSIBILITY_LABELS,
backgroundColor = Colors.$backgroundDefault,
initialColor,
colors,
value,
testID,
onValueChange,
animatedIndex
} = props;
const [show, setShow] = useState(false);

static defaultProps = {
accessibilityLabels: ACCESSIBILITY_LABELS,
backgroundColor: Colors.$backgroundDefault
};

state = {
show: false
};
const showDialog = useCallback(() => setShow(true), []);

get animatedIndex() {
const {animatedIndex, colors} = this.props;
if (animatedIndex === undefined) {
return colors.length - 1;
}
return animatedIndex;
}
const hideDialog = useCallback(() => setShow(false), []);

showDialog = () => {
this.setState({show: true});
};

hideDialog = () => {
this.setState({show: false});
};

render() {
const {initialColor, colors, value, testID, accessibilityLabels, backgroundColor, onValueChange} = this.props;
const {show} = this.state;
return (
<View row testID={testID} style={{backgroundColor}}>
<ColorPalette
value={value}
colors={colors}
style={styles.palette}
usePagination={false}
animatedIndex={this.animatedIndex}
onValueChange={onValueChange}
testID={`${testID}-palette`}
backgroundColor={backgroundColor}
/>
<View style={[styles.buttonContainer, {backgroundColor}]}>
<Button
color={Colors.$textDefault}
outlineColor={Colors.$textDefault}
style={styles.button}
round
outline
iconSource={Assets.icons.plusSmall}
onPress={this.showDialog}
testID={`${testID}-button`}
accessibilityLabel={accessibilityLabels?.addButton}
/>
</View>
<ColorPickerDialog
{...this.props}
key={initialColor}
visible={show}
onDismiss={this.hideDialog}
accessibilityLabels={{
dismissButton: accessibilityLabels?.dismissButton,
doneButton: accessibilityLabels?.doneButton,
input: accessibilityLabels?.input
}}
return (
<View row testID={testID} style={{backgroundColor}}>
<ColorPalette
value={value}
colors={colors}
style={styles.palette}
usePagination={false}
animatedIndex={animatedIndex ?? colors.length - 1}
onValueChange={onValueChange}
testID={`${testID}-palette`}
backgroundColor={backgroundColor}
/>
<View style={[styles.buttonContainer, {backgroundColor}]}>
<Button
color={Colors.$textDefault}
outlineColor={Colors.$textDefault}
style={styles.button}
round
outline
iconSource={Assets.icons.plusSmall}
onPress={showDialog}
testID={`${testID}-button`}
accessibilityLabel={accessibilityLabels?.addButton}
/>
</View>
);
}
}
<ColorPickerDialog
{...props}
key={initialColor}
visible={show}
onDismiss={hideDialog}
accessibilityLabels={{
dismissButton: accessibilityLabels?.dismissButton,
doneButton: accessibilityLabels?.doneButton,
input: accessibilityLabels?.input
}}
migrate
/>
</View>
);
};

ColorPicker.displayName = 'ColorPicker';

export default asBaseComponent<Props>(ColorPicker);

Expand Down