1
- import React , { useState } from 'react' ;
1
+ import React , { useState , ReactElement , useEffect } from 'react' ;
2
2
3
3
import classnames from '../../utils/classNames' ;
4
4
@@ -14,14 +14,19 @@ export interface IProps {
14
14
labelText ?: string ;
15
15
disabled ?: boolean ;
16
16
labelDisabled ?: boolean ;
17
+ changed ?: Function ;
18
+ clicked ?: Function ;
17
19
}
18
20
19
21
const baseClass = 'vant-checkbox' ;
20
22
21
23
// TODO: Round/Square checkbox
24
+ // TODO: Checkbox groups
22
25
23
26
const Checkbox = ( {
24
27
checked,
28
+ changed,
29
+ clicked,
25
30
name,
26
31
activeIcon = 'checked' ,
27
32
checkedColor = '#1989fa' ,
@@ -32,20 +37,29 @@ const Checkbox = ({
32
37
} : IProps ) => {
33
38
const [ isChecked , handleCheck ] = useState ( checked ) ;
34
39
40
+ useEffect ( ( ) => {
41
+ changed ( isChecked ) ;
42
+ } , [ isChecked ] ) ;
43
+
35
44
const handleContainerClick = ( e ) => {
36
45
e . preventDefault ( ) ;
37
46
if ( ! disabled && ! labelDisabled ) {
38
47
handleCheck ( ! isChecked ) ;
48
+ clicked ( e ) ;
39
49
}
40
50
} ;
41
51
42
52
const handleIconClick = ( e ) => {
43
53
e . preventDefault ( ) ;
44
54
if ( ! disabled ) {
45
55
handleCheck ( ! isChecked ) ;
56
+ clicked ( e ) ;
46
57
}
47
58
} ;
48
59
60
+ const iconName = isChecked ? activeIcon : inactiveIcon ;
61
+ const iconColor = disabled ? '#c8c9cc' : checkedColor ;
62
+
49
63
return (
50
64
< div
51
65
className = { classnames ( baseClass , [
@@ -56,11 +70,7 @@ const Checkbox = ({
56
70
onClick = { handleContainerClick }
57
71
>
58
72
< div className = { `${ baseClass } __icon-container` } onClick = { handleIconClick } >
59
- < Icon
60
- color = { disabled ? '#c8c9cc' : checkedColor }
61
- name = { isChecked ? activeIcon : inactiveIcon }
62
- size = '20px'
63
- />
73
+ < Icon color = { iconColor } name = { iconName } size = '20px' />
64
74
</ div >
65
75
< label htmlFor = { name } > { labelText } </ label >
66
76
</ div >
0 commit comments