@@ -47,12 +47,24 @@ class Switch extends React.Component {
4747 }
4848
4949 render ( ) {
50- const { onText, offText, disabled, isReadOnly, bsSize, ...props } = this . props ;
50+ const { onText, offText, disabled, isReadOnly, bsSize, formOptions , ...props } = this . props ;
5151 return (
5252 < div >
5353 < label
5454 className = { `pf3-switch${ disabled || isReadOnly ? ' disabled' : '' } ${ bsSize === 'mini' || bsSize === 'mn' ? ' mini' : '' } ` }
5555 style = { { width : this . state . labelWidth + DIVIDER_SIZE + COMBINED_MARGIN } }
56+ tabIndex = { disabled || isReadOnly ? - 1 : 0 }
57+ onKeyDown = { ( e ) => {
58+ const SPACEBAR_CODE = 32 ;
59+ const ENTER_CODE = 13 ;
60+ if ( e . keyCode === SPACEBAR_CODE ) {
61+ e . preventDefault ( ) ;
62+ props . onChange ( { target : { checked : ! props . checked } } ) ;
63+ } else if ( e . keyCode === ENTER_CODE ) {
64+ e . preventDefault ( ) ;
65+ formOptions . handleSubmit ( ) ;
66+ }
67+ } }
5668 >
5769 < input type = "checkbox" { ...props } disabled = { disabled || isReadOnly } />
5870 < span className = { `pf3-switch-slider${ props . checked ? ' checked' : '' } ` } >
@@ -90,6 +102,9 @@ Switch.propTypes = {
90102 checked : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . string ] ) ,
91103 onChange : PropTypes . func . isRequired ,
92104 bsSize : PropTypes . oneOf ( [ 'mn' , 'mini' ] ) ,
105+ formOptions : PropTypes . shape ( {
106+ handleSubmit : PropTypes . func . isRequired ,
107+ } ) . isRequired ,
93108} ;
94109
95110Switch . defaultProps = {
0 commit comments