@@ -45,35 +45,53 @@ const defaultProps = {
45
45
class AppSwitch extends Component {
46
46
constructor ( props ) {
47
47
super ( props ) ;
48
- this . onChange = this . onChange . bind ( this ) ;
48
+ this . handleChange = this . handleChange . bind ( this ) ;
49
+ this . handleKeyDown = this . handleKeyDown . bind ( this ) ;
50
+ this . handleKeyUp = this . handleKeyUp . bind ( this ) ;
49
51
this . state = {
50
52
checked : this . props . defaultChecked || this . props . checked ,
51
53
selected : [ ]
52
54
} ;
53
55
}
54
56
55
- onChange ( event ) {
56
- const target = event . target ;
57
+ toggleState ( check ) {
57
58
this . setState ( {
58
- checked : target . checked ,
59
+ checked : check
59
60
} )
61
+ }
62
+
63
+ handleChange ( event ) {
64
+ const target = event . target ;
65
+ this . toggleState ( target . checked )
60
66
61
67
if ( this . props . onChange ) {
62
68
this . props . onChange ( event ) ;
63
69
}
64
70
}
65
71
66
- componentDidUpdate ( prevProps ) {
67
- if ( this . props . checked !== prevProps . checked ) {
68
- this . setState ( {
69
- checked : this . props . checked
70
- } )
72
+ handleKeyDown ( event ) {
73
+ if ( event . key === ' ' ) {
74
+ event . preventDefault ( ) ;
75
+ }
76
+ }
77
+
78
+ handleKeyUp ( event ) {
79
+ if ( event . key === 'Enter' || event . key === ' ' ) {
80
+ this . toggleState ( ! this . state . checked ) ;
81
+ }
82
+ }
83
+
84
+ componentDidUpdate ( prevProps , prevState ) {
85
+ if ( this . props . checked !== prevState . checked ) {
86
+ this . toggleState ( this . props . checked )
71
87
}
72
88
}
73
89
74
90
render ( ) {
75
91
const { className, disabled, color, name, label, outline, size, required, type, value, dataOn, dataOff, variant, ...attributes } = this . props ;
76
92
93
+ const tabindex = attributes . tabIndex
94
+ delete attributes . tabIndex
77
95
delete attributes . checked
78
96
delete attributes . defaultChecked
79
97
delete attributes . onChange
@@ -98,8 +116,19 @@ class AppSwitch extends Component {
98
116
) ;
99
117
100
118
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 } />
119
+ < label className = { classes } tabIndex = { tabindex } onKeyUp = { this . handleKeyUp } onKeyDown = { this . handleKeyDown } >
120
+ < input type = { type }
121
+ className = { inputClasses }
122
+ onChange = { this . handleChange }
123
+ checked = { this . state . checked }
124
+ name = { name }
125
+ required = { required }
126
+ disabled = { disabled }
127
+ value = { value }
128
+ aria-checked = { this . state . checked }
129
+ aria-disabled = { disabled }
130
+ aria-readonly = { disabled }
131
+ { ...attributes } />
103
132
< span className = { sliderClasses } data-checked = { dataOn } data-unchecked = { dataOff } > </ span >
104
133
</ label >
105
134
) ;
0 commit comments