@@ -7,10 +7,13 @@ const template = document.createElement('template');
77
88template . innerHTML = `
99 <style>${ shadowStyles } </style>
10- <div id="container">
11- <hx-icon type="checkmark" id="tick"></hx-icon>
12- <hx-icon type="minus" id="minus"></hx-icon>
13- </div>
10+ <label id="container">
11+ <input type="checkbox" id="nativeControl"/>
12+ <div id="customControl">
13+ <hx-icon type="checkmark" id="tick"></hx-icon>
14+ <hx-icon type="minus" id="minus"></hx-icon>
15+ </div>
16+ </label>
1417` ;
1518
1619export class HXCheckboxElement extends HXElement {
@@ -19,57 +22,44 @@ export class HXCheckboxElement extends HXElement {
1922 }
2023
2124 static get observedAttributes ( ) {
22- return super . observedAttributes . concat ( [
25+ return [
2326 'checked' ,
27+ 'disabled' ,
2428 'indeterminate' ,
25- ] ) ;
29+ ] ;
2630 }
2731
2832 constructor ( ) {
2933 super ( tagName , template ) ;
34+ this . _input = this . shadowRoot . getElementById ( 'nativeControl' ) ;
35+ this . _onChange = this . _onChange . bind ( this ) ;
3036 }
3137
3238 connectedCallback ( ) {
33- super . connectedCallback ( ) ;
34-
35- this . $defaultAttribute ( 'role' , 'checkbox' ) ;
36- if ( ! this . hasAttribute ( 'tabindex' ) && ! this . disabled ) {
37- this . setAttribute ( 'tabindex' , 0 ) ;
38- }
39-
4039 this . $upgradeProperty ( 'checked' ) ;
40+ this . $upgradeProperty ( 'disabled' ) ;
4141 this . $upgradeProperty ( 'indeterminate' ) ;
42-
43- this . addEventListener ( 'keydown' , this . $preventScroll ) ;
44- this . addEventListener ( 'keyup' , this . _onKeyUp ) ;
45- this . addEventListener ( 'click' , this . _onClick ) ;
42+ this . _input . addEventListener ( 'change' , this . _onChange ) ;
4643 }
4744
4845 disconnectedCallback ( ) {
49- this . removeEventListener ( 'keydown' , this . $preventScroll ) ;
50- this . removeEventListener ( 'keyup' , this . _onKeyUp ) ;
51- this . removeEventListener ( 'click' , this . _onClick ) ;
46+ this . _input . removeEventListener ( 'change' , this . _onChange ) ;
5247 }
5348
5449 attributeChangedCallback ( attr , oldVal , newVal ) {
55- super . attributeChangedCallback ( attr , oldVal , newVal ) ;
56-
5750 const hasValue = ( newVal !== null ) ;
5851 switch ( attr ) {
5952 case 'indeterminate' :
60- if ( hasValue ) {
61- this . setAttribute ( 'aria-checked' , 'mixed' ) ;
62- }
53+ this . _input . indeterminate = hasValue ;
6354 break ;
64-
6555 case 'checked' :
66- if ( ! this . indeterminate ) {
67- this . setAttribute ( 'aria-checked' , hasValue ) ;
68- this . $emit ( 'change' , {
69- checked : this . checked ,
70- } ) ;
56+ if ( this . _input . checked !== hasValue ) {
57+ this . _input . checked = hasValue ;
7158 }
7259 break ;
60+ case 'disabled' :
61+ this . _input . disabled = hasValue ;
62+ break ;
7363 }
7464 } //attributeChangedCallback()
7565
@@ -97,26 +87,24 @@ export class HXCheckboxElement extends HXElement {
9787 return this . hasAttribute ( 'indeterminate' ) ;
9888 }
9989
100- _onKeyUp ( event ) {
101- if ( event . altKey ) {
102- return ;
103- }
104-
105- if ( event . keyCode === KEYS . Space ) {
106- event . preventDefault ( ) ;
107- this . _toggleChecked ( ) ;
90+ set disabled ( value ) {
91+ if ( value ) {
92+ this . setAttribute ( 'disabled' , '' ) ;
93+ } else {
94+ this . removeAttribute ( 'disabled' ) ;
10895 }
10996 }
11097
111- _onClick ( ) {
112- this . _toggleChecked ( ) ;
98+ get disabled ( ) {
99+ return this . hasAttribute ( 'disabled' ) ;
113100 }
114101
115- _toggleChecked ( ) {
116- if ( this . disabled ) {
117- return ;
118- }
119- this . indeterminate = false ;
120- this . checked = ! this . checked ;
102+ _onChange ( evt ) {
103+ this . checked = evt . target . checked ;
104+
105+ // Chrome doesn't emit 'input' events for checkboxes and native 'change'
106+ // events do not bubble out of the ShadowDOM. Emit a custom event to
107+ // ensure a 'change' event makes it out of the ShadowDOM.
108+ this . $emit ( 'change' ) ;
121109 }
122110} //HXCheckboxElement
0 commit comments