Skip to content

Commit 4f51f4f

Browse files
authored
Moved ColorPicker to FC. (#2822)
* ColorPicker changed to FC * Change to show toggle functions * Removed extra import * Removed extra import * Added usecallbacks * Changed animatedIndex to a ternary * Changed animatedIndex to a fallback value
1 parent 07df924 commit 4f51f4f

File tree

1 file changed

+55
-67
lines changed

1 file changed

+55
-67
lines changed

src/components/colorPicker/index.tsx

Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {PureComponent} from 'react';
1+
import React, {useCallback, useState} from 'react';
22
import {StyleSheet} from 'react-native';
33
import {asBaseComponent} from '../../commons/new';
44
import Assets from '../../assets';
@@ -58,77 +58,65 @@ const ACCESSIBILITY_LABELS = {
5858
* @notes: This is a screen width component
5959
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorPicker/ColorPicker.gif?raw=true
6060
*/
61-
class ColorPicker extends PureComponent<Props> {
62-
static displayName = 'ColorPicker';
61+
const ColorPicker = (props: Props) => {
62+
const {
63+
accessibilityLabels = ACCESSIBILITY_LABELS,
64+
backgroundColor = Colors.$backgroundDefault,
65+
initialColor,
66+
colors,
67+
value,
68+
testID,
69+
onValueChange,
70+
animatedIndex
71+
} = props;
72+
const [show, setShow] = useState(false);
6373

64-
static defaultProps = {
65-
accessibilityLabels: ACCESSIBILITY_LABELS,
66-
backgroundColor: Colors.$backgroundDefault
67-
};
68-
69-
state = {
70-
show: false
71-
};
74+
const showDialog = useCallback(() => setShow(true), []);
7275

73-
get animatedIndex() {
74-
const {animatedIndex, colors} = this.props;
75-
if (animatedIndex === undefined) {
76-
return colors.length - 1;
77-
}
78-
return animatedIndex;
79-
}
76+
const hideDialog = useCallback(() => setShow(false), []);
8077

81-
showDialog = () => {
82-
this.setState({show: true});
83-
};
84-
85-
hideDialog = () => {
86-
this.setState({show: false});
87-
};
88-
89-
render() {
90-
const {initialColor, colors, value, testID, accessibilityLabels, backgroundColor, onValueChange} = this.props;
91-
const {show} = this.state;
92-
return (
93-
<View row testID={testID} style={{backgroundColor}}>
94-
<ColorPalette
95-
value={value}
96-
colors={colors}
97-
style={styles.palette}
98-
usePagination={false}
99-
animatedIndex={this.animatedIndex}
100-
onValueChange={onValueChange}
101-
testID={`${testID}-palette`}
102-
backgroundColor={backgroundColor}
103-
/>
104-
<View style={[styles.buttonContainer, {backgroundColor}]}>
105-
<Button
106-
color={Colors.$textDefault}
107-
outlineColor={Colors.$textDefault}
108-
style={styles.button}
109-
round
110-
outline
111-
iconSource={Assets.icons.plusSmall}
112-
onPress={this.showDialog}
113-
testID={`${testID}-button`}
114-
accessibilityLabel={accessibilityLabels?.addButton}
115-
/>
116-
</View>
117-
<ColorPickerDialog
118-
{...this.props}
119-
key={initialColor}
120-
visible={show}
121-
onDismiss={this.hideDialog}
122-
accessibilityLabels={{
123-
dismissButton: accessibilityLabels?.dismissButton,
124-
doneButton: accessibilityLabels?.doneButton,
125-
input: accessibilityLabels?.input
126-
}}
78+
return (
79+
<View row testID={testID} style={{backgroundColor}}>
80+
<ColorPalette
81+
value={value}
82+
colors={colors}
83+
style={styles.palette}
84+
usePagination={false}
85+
animatedIndex={animatedIndex ?? colors.length - 1}
86+
onValueChange={onValueChange}
87+
testID={`${testID}-palette`}
88+
backgroundColor={backgroundColor}
89+
/>
90+
<View style={[styles.buttonContainer, {backgroundColor}]}>
91+
<Button
92+
color={Colors.$textDefault}
93+
outlineColor={Colors.$textDefault}
94+
style={styles.button}
95+
round
96+
outline
97+
iconSource={Assets.icons.plusSmall}
98+
onPress={showDialog}
99+
testID={`${testID}-button`}
100+
accessibilityLabel={accessibilityLabels?.addButton}
127101
/>
128102
</View>
129-
);
130-
}
131-
}
103+
<ColorPickerDialog
104+
{...props}
105+
key={initialColor}
106+
visible={show}
107+
onDismiss={hideDialog}
108+
accessibilityLabels={{
109+
dismissButton: accessibilityLabels?.dismissButton,
110+
doneButton: accessibilityLabels?.doneButton,
111+
input: accessibilityLabels?.input
112+
}}
113+
migrate
114+
/>
115+
</View>
116+
);
117+
};
118+
119+
ColorPicker.displayName = 'ColorPicker';
132120

133121
export default asBaseComponent<Props>(ColorPicker);
134122

0 commit comments

Comments
 (0)