11import React , { Component } from 'react' ;
22import classNames from 'classnames' ;
3+ import DefaultProps from '../../defaultProps' ;
34
45export default class Checkbox extends Component {
56 static propTypes = {
7+ ...DefaultProps . propTypes ,
8+
9+ /**
10+ * State checked
11+ */
612 checked : React . PropTypes . bool ,
7- children : React . PropTypes . node ,
8- className : React . PropTypes . any ,
9- component : React . PropTypes . oneOfType ( [
10- React . PropTypes . element ,
11- React . PropTypes . string
12- ] ) ,
13- defaultClasses : React . PropTypes . bool ,
13+ /**
14+ * Does not allow user interaction
15+ */
1416 disabled : React . PropTypes . bool ,
15- fitted : React . PropTypes . bool ,
16- indeterminate : React . PropTypes . bool ,
17- name : React . PropTypes . string ,
18- onClick : React . PropTypes . func ,
19- radio : React . PropTypes . bool ,
17+ /**
18+ * It does disabled, but does not allow user interaction
19+ */
2020 readOnly : React . PropTypes . bool ,
21- slider : React . PropTypes . bool ,
22- toggle : React . PropTypes . bool
21+ /**
22+ * Callback handler to click checkbox
23+ */
24+ onClick : React . PropTypes . func ,
25+ /**
26+ * Attr name
27+ */
28+ name : React . PropTypes . string ,
29+ /**
30+ * Checkbox - appearance
31+ */
32+ type : React . PropTypes . oneOf ( [ 'default' , 'radio' , 'toggle' , 'slider' ] ) ,
33+ /**
34+ * A fitted checkbox does not leave padding for a label
35+ */
36+ fitted : React . PropTypes . bool
2337 } ;
2438
2539 static defaultProps = {
26- component : 'div' ,
27- defaultClasses : true
40+ ...DefaultProps . defaultProps ,
41+ type : 'default' ,
42+ onClick : ( ) => { }
2843 } ;
2944
30- constructor ( props ) {
31- super ( props ) ;
45+ onClick = ( event ) => {
46+ if ( this . props . disabled || this . props . readOnly ) return ;
3247
33- this . state = {
34- active : this . props . checked
35- }
36- }
37-
38- onClick ( ) {
39- if ( ! this . state . active || ( this . state . active && ! this . props . radio ) ) {
40- this . setState ( {
41- active : ! this . state . active
42- } ) ;
43- }
44- }
48+ this . props . onClick ( event ) ;
49+ } ;
4550
4651 renderChildren ( ) {
4752 /* eslint-disable no-use-before-define */
48- let { children, defaultClasses, className, onClick,
49- radio, slider, toggle, component, readOnly, checked,
50- disabled, ...other } = this . props ;
53+ let { children, defaultClasses, className, onClick, type,
54+ component, readOnly, checked, ...other } = this . props ;
5155 /* eslint-enable no-use-before-define */
5256
5357 let childElements = [
54- React . DOM . input ( {
55- type : 'checkbox' ,
56- key : 'input' ,
57- className : 'hidden' ,
58- checked : this . state . active || this . props . checked ,
59- readOnly : true ,
60- ...other
61- } ) ,
62- React . DOM . label ( {
63- key : 'label'
64- } , this . props . children )
58+ < input
59+ { ...other }
60+ type = "checkbox"
61+ key = "input"
62+ className = "hidden"
63+ readOnly
64+ checked = { checked } /> ,
65+ < label key = "label" >
66+ { children }
67+ </ label >
6568 ] ;
6669
6770 return childElements ;
6871 }
6972
7073 render ( ) {
7174 /* eslint-disable no-use-before-define */
72- let { component, defaultClasses, name, ...other } = this . props ;
75+ let { component, defaultClasses, checked , type , onClick , name, ...other } = this . props ;
7376 /* eslint-enable no-use-before-define */
7477
7578 other . className = classNames ( this . props . className , this . getClasses ( ) ) ;
76- other . onClick = typeof this . props . onClick === 'function' ? this . props . onClick : this . onClick . bind ( this ) ;
79+ other . onClick = this . onClick ;
7780
7881 return React . createElement (
7982 this . props . component ,
@@ -91,16 +94,16 @@ export default class Checkbox extends Component {
9194 // positioning
9295
9396 // types
94- radio : this . props . radio ,
97+ radio : this . props . type == 'radio' ,
98+ slider : this . props . type == 'slider' ,
99+ toggle : this . props . type == 'toggle' ,
95100
96101 // variations
97102 fitted : this . props . fitted ,
98- slider : this . props . slider ,
99- toggle : this . props . toggle ,
100103
101104 // state
102105 'read-only' : this . props . readOnly ,
103- checked : this . state . active || this . props . checked ,
106+ checked : this . props . checked ,
104107 disabled : this . props . disabled ,
105108 indeterminate : this . props . indeterminate
106109 } ;
0 commit comments