@@ -2,7 +2,8 @@ import { BaseEventOrig, Label, Switch, SwitchProps, View } from '@tarojs/compone
22import { createBEM } from '@zarm-design/bem' ;
33import { Minus as MinusIcon , Success as SuccessIcon } from '@zarm-design/icons' ;
44import includes from 'lodash/includes' ;
5- import React , { ReactNode , useContext , useEffect , useMemo , useState } from 'react' ;
5+ import React , { ReactNode , useContext , useMemo , } from 'react' ;
6+ import { useControllableEventValue } from '../utils/hooks' ;
67import Button from '../button/index.mini' ;
78import { ConfigContext } from '../config-provider' ;
89import List from '../list' ;
@@ -19,19 +20,37 @@ export type CheckboxProps = BaseCheckboxProps &
1920 onChange ?: ( value : BaseEventOrig < SwitchProps . onChangeEventDetail > ) => void ;
2021 } ;
2122
22- const getChecked = ( props : CheckboxProps , defaultChecked ?: boolean ) => {
23- return props . checked ?? props . defaultChecked ?? defaultChecked ;
24- } ;
23+ // const getChecked = (props: CheckboxProps, defaultChecked?: boolean) => {
24+ // return props.checked ?? props.defaultChecked ?? defaultChecked;
25+ // };
2526
2627const Checkbox = ( props : CheckboxProps ) => {
27- let [ checked , setChecked ] = useState ( getChecked ( props , false ) ) ;
28+ // const defaultVal: Partial<{value: boolean, defaultValue: boolean}> = {};
29+ // if ('checked' in props) {
30+ // defaultVal.value = props.checked;
31+ // }
32+ // if ('defaultChecked' in props) {
33+ // defaultVal.defaultValue = props.defaultChecked;
34+ // }
35+ // let [checked, setChecked] = useControllableEventValue({
36+ // ...props,
37+ // ...defaultVal,
38+ // });
39+
40+ // console.log(checked);
41+ let [ checked , setChecked ] = useControllableEventValue ( props , {
42+ valuePropName : 'checked' ,
43+ defaultValuePropName : 'defaultChecked'
44+ } ) ;
45+ checked = checked ?? false ;
46+
2847 let { disabled } = props ;
2948
3049 const groupContext = useContext ( CheckboxGroupContext ) ;
3150 if ( groupContext && props . value !== undefined ) {
3251 checked = includes ( groupContext . value , props . value ) ;
33- setChecked = ( changedChecked : boolean ) => {
34- if ( changedChecked ) {
52+ setChecked = ( e : any ) => {
53+ if ( e . target . checked === true ) {
3554 groupContext . check ( props . value ) ;
3655 } else {
3756 groupContext . uncheck ( props . value ) ;
@@ -84,22 +103,17 @@ const Checkbox = (props: CheckboxProps) => {
84103 className = { bem ( 'input' ) }
85104 disabled = { disabled }
86105 checked = { checked }
87- onChange = { ( e : BaseEventOrig < SwitchProps . onChangeEventDetail > ) => {
106+ onChange = { ( ) => {
88107 if ( disabled ) return ;
89-
90- if ( ! ( 'checked' in props ) ) {
91- setChecked ( e . detail . value ) ;
92- }
93- props . onChange ?.( e ) ;
108+ setChecked ( {
109+ target : {
110+ checked : ! checked ,
111+ }
112+ } as any ) ;
113+ // props.onChange?.(e);
94114 } }
95115 />
96116 ) ;
97- useEffect ( ( ) => {
98- if ( props . checked === undefined ) return ;
99- if ( props . checked === checked ) return ;
100-
101- setChecked ( getChecked ( { checked : props . checked , defaultChecked : props . defaultChecked } , false ) ) ;
102- } , [ props . checked , props . defaultChecked ] ) ;
103117
104118 if ( groupContext ?. type === 'button' ) {
105119 return (
0 commit comments