Skip to content

Commit a87933c

Browse files
committed
fix:调整 checkbox 选中值处理
1 parent 9e4ef3a commit a87933c

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

packages/core/src/CheckBox/index.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,23 @@ export interface CheckBoxProps extends TouchableOpacityProps {
2626
onChange?: (checked: boolean) => void;
2727
}
2828

29-
export interface CheckBoxState {
30-
checked: boolean;
31-
controlChecked: 'props' | 'state';
32-
}
33-
3429
function CheckBox(props: CheckBoxProps) {
3530
const theme = useTheme<Theme>();
36-
const [state, setState] = useState({
37-
checked: !!props.checked,
38-
controlChecked: 'props',
39-
});
31+
32+
const [state, setState] = useState({ checked: !!props.checked });
4033

4134
useEffect(() => {
42-
if (state.controlChecked === 'props') {
43-
setState({ ...state, checked: !!props.checked });
44-
}
45-
setState({ ...state, controlChecked: 'props' });
46-
}, []);
35+
setState({ checked: !!props.checked });
36+
}, [props.checked]);
4737

4838
const onPress = () => {
4939
const { onChange } = props;
50-
setState({ checked: !state.checked, controlChecked: 'state' });
51-
52-
onChange && onChange(state.checked);
40+
if (Reflect.has(props, 'checked')) {
41+
onChange && onChange(state.checked);
42+
} else {
43+
setState({ checked: !state.checked });
44+
onChange && onChange(state.checked);
45+
}
5346
};
5447

5548
const {

0 commit comments

Comments
 (0)