@@ -6,15 +6,19 @@ import {TOGGLE_SIZES} from '../utils/constants';
66export class Toggle extends React . Component {
77 constructor ( props ) {
88 super ( props ) ;
9- this . state = { checked : props . checked } ;
9+ this . state = { checked : props . checked ? props . checked : false } ;
1010 }
1111
12- handleChange = ( ) => {
13- this . setState ( { checked : ! this . state . checked } ) ;
14- } ;
12+ handleChange = ( e ) => {
13+ this . setState ( {
14+ checked : ! this . state . checked
15+ } , ( ) => {
16+ this . props . onChange ( e ) ;
17+ } ) ;
18+ }
1519
1620 render ( ) {
17- const { size, id, disabled, children, className, labelProps, inputProps, ...rest } = this . props ;
21+ const { size, id, checked , disabled, children, className, labelProps, inputProps, onChange , ...rest } = this . props ;
1822
1923 const toggleClasses = classnames (
2024 'fd-form__item' ,
@@ -58,10 +62,20 @@ export class Toggle extends React.Component {
5862}
5963
6064Toggle . propTypes = {
65+ checked : PropTypes . bool ,
6166 className : PropTypes . string ,
6267 disabled : PropTypes . bool ,
6368 id : PropTypes . string ,
6469 inputProps : PropTypes . object ,
6570 labelProps : PropTypes . object ,
66- size : PropTypes . oneOf ( TOGGLE_SIZES )
71+ size : PropTypes . oneOf ( TOGGLE_SIZES ) ,
72+ onChange : PropTypes . func
73+ } ;
74+
75+ Toggle . defaultProps = {
76+ onChange : ( ) => { }
77+ } ;
78+
79+ Toggle . propDescriptions = {
80+ checked : 'Set to true for component to be checked on render.'
6781} ;
0 commit comments