|
1 | | -import React from 'react'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
2 | 2 | import { |
3 | 3 | TouchableOpacity, |
4 | 4 | TouchableOpacityProps, |
@@ -29,77 +29,70 @@ export interface CheckBoxState { |
29 | 29 | controlChecked: 'props' | 'state'; |
30 | 30 | } |
31 | 31 |
|
32 | | -export default class CheckBox extends React.Component<CheckBoxProps, CheckBoxState> { |
33 | | - constructor(props: CheckBoxProps) { |
34 | | - super(props); |
35 | | - this.state = { |
36 | | - checked: !!props.checked, |
37 | | - controlChecked: 'props', |
38 | | - }; |
39 | | - } |
40 | | - static defaultProps = { |
41 | | - checkedIcon: 'circle-check', |
42 | | - unCheckedIcon: 'circle-o', |
43 | | - color: '#008EF0', |
44 | | - size: 16, |
45 | | - }; |
46 | | - static getDerivedStateFromProps(props: CheckBoxProps, state: CheckBoxState) { |
47 | | - if (props.checked === state.checked && state.controlChecked === 'props') { |
48 | | - return null; |
49 | | - } |
| 32 | +function CheckBox(props: CheckBoxProps) { |
| 33 | + const [state, setState] = useState({ |
| 34 | + checked: !!props.checked, |
| 35 | + controlChecked: 'props', |
| 36 | + }); |
| 37 | + |
| 38 | + useEffect(() => { |
50 | 39 | if (state.controlChecked === 'props') { |
51 | | - return { |
52 | | - checked: props.checked, |
53 | | - }; |
| 40 | + setState({ ...state, checked: !!props.checked }); |
54 | 41 | } |
55 | | - return { |
56 | | - controlChecked: 'props', |
57 | | - }; |
58 | | - } |
| 42 | + setState({ ...state, controlChecked: 'props' }); |
| 43 | + }, []); |
| 44 | + |
| 45 | + const onPress = () => { |
| 46 | + const { onChange } = props; |
| 47 | + setState({ checked: !state.checked, controlChecked: 'state' }); |
59 | 48 |
|
60 | | - onPress = () => { |
61 | | - const { onChange } = this.props; |
62 | | - this.setState({ checked: !this.state.checked, controlChecked: 'state' }, () => { |
63 | | - onChange && onChange(this.state.checked); |
64 | | - }); |
| 49 | + onChange && onChange(state.checked); |
65 | 50 | }; |
66 | | - render() { |
67 | | - const { |
68 | | - children, |
69 | | - style, |
70 | | - textStyle, |
71 | | - checkedIcon, |
72 | | - unCheckedIcon, |
73 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
74 | | - checked, |
75 | | - disabled, |
76 | | - color: $color, |
77 | | - size, |
78 | | - ...otherProps |
79 | | - } = this.props; |
80 | | - const { checked: $checked } = this.state; |
81 | | - const iconName = ($checked ? checkedIcon : unCheckedIcon) as IconsName; |
82 | | - const styIcon: ViewProps['style'] = {}; |
83 | | - if (children) { |
84 | | - styIcon.marginRight = 6; |
85 | | - } |
86 | | - let colorIcon = $color; |
87 | | - let divStyl: ViewProps['style'] = {}; |
88 | | - if (disabled) { |
89 | | - colorIcon = color(colorIcon).alpha(0.52).rgb().string(); |
90 | | - divStyl.opacity = 0.5; |
91 | | - } |
92 | | - return ( |
93 | | - <TouchableOpacity disabled={disabled} {...otherProps} style={[styles.default, style]} onPress={this.onPress}> |
94 | | - <View style={[styIcon]}> |
95 | | - {typeof iconName === 'string' ? <Icon size={size} fill={colorIcon} name={iconName} /> : iconName} |
96 | | - </View> |
97 | | - {children && <Div children={children} style={[divStyl, textStyle]} />} |
98 | | - </TouchableOpacity> |
99 | | - ); |
| 51 | + |
| 52 | + const { |
| 53 | + children, |
| 54 | + style, |
| 55 | + textStyle, |
| 56 | + checkedIcon, |
| 57 | + unCheckedIcon, |
| 58 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 59 | + checked, |
| 60 | + disabled, |
| 61 | + color: $color, |
| 62 | + size, |
| 63 | + ...otherProps |
| 64 | + } = props; |
| 65 | + const { checked: $checked } = state; |
| 66 | + const iconName = ($checked ? checkedIcon : unCheckedIcon) as IconsName; |
| 67 | + const styIcon: ViewProps['style'] = {}; |
| 68 | + if (children) { |
| 69 | + styIcon.marginRight = 6; |
100 | 70 | } |
| 71 | + let colorIcon = $color; |
| 72 | + let divStyl: ViewProps['style'] = {}; |
| 73 | + if (disabled) { |
| 74 | + colorIcon = color(colorIcon).alpha(0.52).rgb().string(); |
| 75 | + divStyl.opacity = 0.5; |
| 76 | + } |
| 77 | + return ( |
| 78 | + <TouchableOpacity disabled={disabled} {...otherProps} style={[styles.default, style]} onPress={onPress}> |
| 79 | + <View style={[styIcon]}> |
| 80 | + {typeof iconName === 'string' ? <Icon size={size} fill={colorIcon} name={iconName} /> : iconName} |
| 81 | + </View> |
| 82 | + {children && <Div children={children} style={[divStyl, textStyle]} />} |
| 83 | + </TouchableOpacity> |
| 84 | + ); |
101 | 85 | } |
102 | 86 |
|
| 87 | +CheckBox.defaultProps = { |
| 88 | + checkedIcon: 'circle-check', |
| 89 | + unCheckedIcon: 'circle-o', |
| 90 | + color: '#008EF0', |
| 91 | + size: 16, |
| 92 | +}; |
| 93 | + |
| 94 | +export default CheckBox; |
| 95 | + |
103 | 96 | const styles = StyleSheet.create({ |
104 | 97 | default: { |
105 | 98 | flexDirection: 'row', |
|
0 commit comments