-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-bar.js
49 lines (36 loc) · 1.39 KB
/
search-bar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(function() {
"use strict";
const template = `
<input type="text">
<button id="search">Submit</button>
`;
class MySearchBar extends HTMLElement {
get value() {
const searchValue = this.shadowRoot.querySelector('input');
return searchValue.value;
}
set value(value){
const setSearchVal = this.shadowRoot.querySelector('input')
setSearchVal.value = value;
this.setAttribute('value', value)
}
constructor() {
super();
let myShadowRoot = this.attachShadow({mode: 'open'});
myShadowRoot.innerHTML = template;
const myComponent = this
this.shadowRoot.querySelector('button').addEventListener('click', function(e) {
// watch for click events in the context of the button of the shadowRoot to console log the search
console.log(myComponent.value);
});
this.shadowRoot.querySelector('input').addEventListener('keydown', function(e) {
// watch for submit events to console log the search
if(e.keyCode === 13)
{
console.log(myComponent.value);
}
});
}
}
window.customElements.define('search-input', MySearchBar);
})();