Skip to content

Commit ea8372f

Browse files
author
Ryan A. Johnson
committed
fix(events): Correct 'change' event logic
* `<hx-search>` now emits a 'clear' event instead of a 'change' event (BREAKING) * prevent 'change' event listeners from firing twice on `<hx-checkbox>` * turn off autocomplete for `<hx-search>` Shadow DOM input Closes #174
1 parent 00e0e29 commit ea8372f

File tree

8 files changed

+23
-28
lines changed

8 files changed

+23
-28
lines changed

docs/components/search/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h3>Basic Search Box</h3>
1717
:placeholder="placeholder"
1818
:value="searchValue"
1919
@input="onSearchUpdate"
20-
@change="onSearchUpdate"
20+
@clear="onSearchUpdate"
2121
></hx-search>
2222
</div>
2323

docs/components/search/search-demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (document.getElementById('vue-searchDemo')) {
1010
placeholder: '',
1111
},
1212
methods: {
13-
// Used on "input" and "change" events
13+
// fires on 'input' and 'clear' events
1414
onSearchUpdate: function (evt) {
1515
this.searchValue = evt.target.value;
1616
},

docs/elements/hx-search/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Any of the following:
2626
<ul>
2727
<li><code>input</code> as user types</li>
28-
<li><code>change</code> on clear</li>
28+
<li><code>clear</code> on clear</li>
2929
</ul>
3030
</hx-dd>
3131
</hx-def>

src/helix-ui/elements/HXCheckboxElement.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ export class HXCheckboxElement extends HXElement {
100100
}
101101

102102
_onChange (evt) {
103+
// Update internal state
103104
this.checked = evt.target.checked;
104105

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.
106+
// Prevent 'change' listeners from firing twice in polyfilled browsers.
107+
evt.stopImmediatePropagation();
108+
109+
// Emit a new 'change' event from the custom element
108110
this.$emit('change');
109111
}
110112
}//HXCheckboxElement

src/helix-ui/elements/HXElement.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ export class HXElement extends HTMLElement {
8585
}//$preventScroll()
8686

8787
$emit (evtName, details) {
88-
if (window.ShadyCSS && evtName === 'change') {
89-
// Let native 'change' events bubble naturally.
90-
return;
91-
}
92-
9388
let evt = new CustomEvent(evtName, {
9489
bubbles: true,
9590
detail: details,

src/helix-ui/elements/HXSearchElement.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const template = document.createElement('template');
77
template.innerHTML = `
88
<style>${shadowStyles}</style>
99
<label id="wrapper">
10-
<input type="text" role="search" id="search" />
10+
<input type="text" role="search" id="search" autocomplete="off" />
1111
<button id="clear" hidden aria-label="Clear search">
1212
<hx-icon type="times"></hx-icon>
1313
</button>
@@ -144,10 +144,8 @@ export class HXSearchElement extends HXElement {
144144
_clearValue () {
145145
this.value = '';
146146

147-
// Modifying the value property of the native input doesn't trigger
148-
// an event. So, we have to emit our own 'change' event to communicate
149-
// the change of internal state.
150-
this.$emit('change');
147+
// Emit a 'clear' event to communicate state change.
148+
this.$emit('clear');
151149

152150
this._elSearch.focus();
153151
}

test/regression/generateRegressionTests.js.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ Generated by [AVA](https://ava.li).
695695
<hx-search>␊
696696
697697
<label id="wrapper" class="style-scope hx-search">␊
698-
<input role="search" id="search" class="style-scope hx-search" type="text">␊
698+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" type="text">␊
699699
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
700700
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
701701
</button>␊
@@ -710,7 +710,7 @@ Generated by [AVA](https://ava.li).
710710
<hx-search placeholder="Search...">␊
711711
712712
<label id="wrapper" class="style-scope hx-search">␊
713-
<input role="search" id="search" class="style-scope hx-search" placeholder="Search..." type="text">␊
713+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" placeholder="Search..." type="text">␊
714714
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
715715
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
716716
</button>␊
@@ -725,7 +725,7 @@ Generated by [AVA](https://ava.li).
725725
<hx-search value="kittens">␊
726726
727727
<label id="wrapper" class="style-scope hx-search">␊
728-
<input role="search" id="search" class="style-scope hx-search" type="text">␊
728+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" type="text">␊
729729
<button id="clear" aria-label="Clear search" class="style-scope hx-search">␊
730730
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
731731
</button>␊
@@ -743,7 +743,7 @@ Generated by [AVA](https://ava.li).
743743
<hx-search invalid="">␊
744744
745745
<label id="wrapper" class="style-scope hx-search">␊
746-
<input role="search" id="search" class="style-scope hx-search" type="text">␊
746+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" type="text">␊
747747
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
748748
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
749749
</button>␊
@@ -758,7 +758,7 @@ Generated by [AVA](https://ava.li).
758758
<hx-search invalid="" placeholder="Search...">␊
759759
760760
<label id="wrapper" class="style-scope hx-search">␊
761-
<input role="search" id="search" class="style-scope hx-search" placeholder="Search..." type="text">␊
761+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" placeholder="Search..." type="text">␊
762762
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
763763
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
764764
</button>␊
@@ -773,7 +773,7 @@ Generated by [AVA](https://ava.li).
773773
<hx-search invalid="" value="kittens">␊
774774
775775
<label id="wrapper" class="style-scope hx-search">␊
776-
<input role="search" id="search" class="style-scope hx-search" type="text">␊
776+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" type="text">␊
777777
<button id="clear" aria-label="Clear search" class="style-scope hx-search">␊
778778
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
779779
</button>␊
@@ -791,7 +791,7 @@ Generated by [AVA](https://ava.li).
791791
<hx-search disabled="">␊
792792
793793
<label id="wrapper" class="style-scope hx-search">␊
794-
<input role="search" id="search" class="style-scope hx-search" disabled="" type="text">␊
794+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" disabled="" type="text">␊
795795
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
796796
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
797797
</button>␊
@@ -806,7 +806,7 @@ Generated by [AVA](https://ava.li).
806806
<hx-search disabled="" placeholder="Search...">␊
807807
808808
<label id="wrapper" class="style-scope hx-search">␊
809-
<input role="search" id="search" class="style-scope hx-search" disabled="" placeholder="Search..." type="text">␊
809+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" disabled="" placeholder="Search..." type="text">␊
810810
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
811811
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
812812
</button>␊
@@ -821,7 +821,7 @@ Generated by [AVA](https://ava.li).
821821
<hx-search disabled="" value="kittens">␊
822822
823823
<label id="wrapper" class="style-scope hx-search">␊
824-
<input role="search" id="search" class="style-scope hx-search" disabled="" type="text">␊
824+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" disabled="" type="text">␊
825825
<button id="clear" aria-label="Clear search" class="style-scope hx-search">␊
826826
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
827827
</button>␊
@@ -839,7 +839,7 @@ Generated by [AVA](https://ava.li).
839839
<hx-search invalid="" disabled="">␊
840840
841841
<label id="wrapper" class="style-scope hx-search">␊
842-
<input role="search" id="search" class="style-scope hx-search" disabled="" type="text">␊
842+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" disabled="" type="text">␊
843843
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
844844
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
845845
</button>␊
@@ -854,7 +854,7 @@ Generated by [AVA](https://ava.li).
854854
<hx-search invalid="" disabled="" placeholder="Search...">␊
855855
856856
<label id="wrapper" class="style-scope hx-search">␊
857-
<input role="search" id="search" class="style-scope hx-search" disabled="" placeholder="Search..." type="text">␊
857+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" disabled="" placeholder="Search..." type="text">␊
858858
<button id="clear" aria-label="Clear search" class="style-scope hx-search" hidden="">␊
859859
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
860860
</button>␊
@@ -869,7 +869,7 @@ Generated by [AVA](https://ava.li).
869869
<hx-search invalid="" disabled="" value="kittens">␊
870870
871871
<label id="wrapper" class="style-scope hx-search">␊
872-
<input role="search" id="search" class="style-scope hx-search" disabled="" type="text">␊
872+
<input role="search" id="search" autocomplete="off" class="style-scope hx-search" disabled="" type="text">␊
873873
<button id="clear" aria-label="Clear search" class="style-scope hx-search">␊
874874
<hx-icon type="times" class="style-scope hx-search" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="style-scope hx-search"><path d="M9.414 8l4.293-4.293a.999.999 0 1 0-1.414-1.414L8 6.586 3.707 2.293a.999.999 0 1 0-1.414 1.414L6.586 8l-4.293 4.293a.999.999 0 1 0 1.414 1.414L8 9.414l4.293 4.293a.997.997 0 0 0 1.414 0 .999.999 0 0 0 0-1.414L9.414 8z" class="style-scope hx-search"></path></svg></hx-icon>␊
875875
</button>␊
13 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)