|
1 | | -import React, {PureComponent} from 'react'; |
| 1 | +import React, {useCallback, useState} from 'react'; |
2 | 2 | import {StyleSheet} from 'react-native'; |
3 | 3 | import {asBaseComponent} from '../../commons/new'; |
4 | 4 | import Assets from '../../assets'; |
@@ -58,77 +58,65 @@ const ACCESSIBILITY_LABELS = { |
58 | 58 | * @notes: This is a screen width component |
59 | 59 | * @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorPicker/ColorPicker.gif?raw=true |
60 | 60 | */ |
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); |
63 | 73 |
|
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), []); |
72 | 75 |
|
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), []); |
80 | 77 |
|
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} |
127 | 101 | /> |
128 | 102 | </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'; |
132 | 120 |
|
133 | 121 | export default asBaseComponent<Props>(ColorPicker); |
134 | 122 |
|
|
0 commit comments