@@ -20,6 +20,7 @@ const propTypes = {
20
20
name : PropTypes . string ,
21
21
required : PropTypes . bool ,
22
22
onChange : PropTypes . func ,
23
+ role : PropTypes . oneOf ( [ 'switch' , 'checkbox' , 'radio' ] ) ,
23
24
type : PropTypes . oneOf ( [ 'checkbox' , 'radio' ] ) ,
24
25
variant : PropTypes . oneOf ( [ '' , '3d' , 'pill' ] ) ,
25
26
className : PropTypes . string ,
@@ -36,6 +37,7 @@ const defaultProps = {
36
37
defaultChecked : false ,
37
38
disabled : false ,
38
39
required : false ,
40
+ role : 'switch' ,
39
41
type : 'checkbox' ,
40
42
variant : '' ,
41
43
dataOn : 'On' ,
@@ -46,6 +48,8 @@ class AppSwitch extends Component {
46
48
constructor ( props ) {
47
49
super ( props ) ;
48
50
this . onChange = this . onChange . bind ( this ) ;
51
+ this . onKeyboard = this . onKeyboard . bind ( this ) ;
52
+ this . input = React . createRef ( ) ;
49
53
this . state = {
50
54
checked : this . props . defaultChecked || this . props . checked ,
51
55
selected : [ ]
@@ -71,8 +75,23 @@ class AppSwitch extends Component {
71
75
}
72
76
}
73
77
78
+ onKeyboard ( event ) {
79
+ if ( event . key === "Enter" || event . key === " " ) {
80
+ event . preventDefault ( )
81
+ event . stopPropagation ( )
82
+ const target = event . target . firstChild ;
83
+ this . setState ( {
84
+ checked : ! target . checked ,
85
+ } )
86
+
87
+ if ( this . props . onChange ) {
88
+ this . props . onChange ( event ) ;
89
+ }
90
+ }
91
+ }
92
+
74
93
render ( ) {
75
- const { className, disabled, color, name, label, outline, size, required, type, value, dataOn, dataOff, variant, ...attributes } = this . props ;
94
+ const { className, disabled, color, name, label, outline, size, required, role , type, value, dataOn, dataOff, variant, ...attributes } = this . props ;
76
95
77
96
delete attributes . checked
78
97
delete attributes . defaultChecked
@@ -98,8 +117,20 @@ class AppSwitch extends Component {
98
117
) ;
99
118
100
119
return (
101
- < label className = { classes } >
102
- < input type = { type } className = { inputClasses } onChange = { this . onChange } checked = { this . state . checked } name = { name } required = { required } disabled = { disabled } value = { value } { ...attributes } />
120
+ < label className = { classes } onKeyPress = { this . onKeyboard } tabIndex = { disabled ? "-1" : "0" } >
121
+ < input ref = { this . input }
122
+ type = { type }
123
+ role = { role }
124
+ aria-checked = { this . state . checked }
125
+ className = { inputClasses }
126
+ onChange = { this . onChange }
127
+ checked = { this . state . checked }
128
+ name = { name }
129
+ value = { value }
130
+ required = { required }
131
+ disabled = { disabled }
132
+ { ...attributes }
133
+ />
103
134
< span className = { sliderClasses } data-checked = { dataOn } data-unchecked = { dataOff } > </ span >
104
135
</ label >
105
136
) ;
0 commit comments