Skip to content

Commit 5c5d3f7

Browse files
authored
Merge pull request #514 from nullptr-z/dev
refactor(CheckBox): 使用Function组件重构 Class组件
2 parents be95d58 + bf11d5d commit 5c5d3f7

File tree

2 files changed

+58
-82
lines changed

2 files changed

+58
-82
lines changed

packages/core/src/CheckBox/index.tsx

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import {
33
TouchableOpacity,
44
TouchableOpacityProps,
@@ -29,77 +29,70 @@ export interface CheckBoxState {
2929
controlChecked: 'props' | 'state';
3030
}
3131

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(() => {
5039
if (state.controlChecked === 'props') {
51-
return {
52-
checked: props.checked,
53-
};
40+
setState({ ...state, checked: !!props.checked });
5441
}
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' });
5948

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);
6550
};
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;
10070
}
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+
);
10185
}
10286

87+
CheckBox.defaultProps = {
88+
checkedIcon: 'circle-check',
89+
unCheckedIcon: 'circle-o',
90+
color: '#008EF0',
91+
size: 16,
92+
};
93+
94+
export default CheckBox;
95+
10396
const styles = StyleSheet.create({
10497
default: {
10598
flexDirection: 'row',

test-ci/src/__tests__/ChecedBox.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)