Changes for page CLB Search Component
Last modified by bougault on 2023/01/17 11:42
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -9,17 +9,24 @@ 9 9 this.attachShadow({ mode: 'open'}); 10 10 } 11 11 12 - createTemplate(htmlString) {12 + get template() { 13 13 const template = document.createElement('template'); 14 14 template.innerHTML = ` 15 15 <style> 16 16 ${this.styles} 17 17 </style> 18 - ${html String}18 + ${this.html} 19 19 `; 20 20 return template; 21 21 } 22 22 23 + get styles() { 24 + return ''; 25 + } 26 + 27 + get html() { 28 + return ''; 29 + } 23 23 $(selector) { 24 24 return this.shadowRoot.querySelector(selector); 25 25 } ... ... @@ -39,23 +39,41 @@ 39 39 } 40 40 41 41 connectedCallback() { 42 - // this.attachShadow({ mode: 'open' }); 43 43 super.connectedCallback(); 44 44 this.render(); 45 - this.$('#search').addEventListener('click', () => this.handleSearch()); 46 - this.$('#prev').addEventListener('click', () => { 47 - if(this.offset > 0) { 48 - this.offset--; 49 - this.handleSearch(); 50 - } 51 - }); 52 - this.$('#next').addEventListener('click', () => { 53 - this.offset++; 54 - this.handleSearch(); 55 - }); 51 + const searchInput = this.$('input[type="text"]'); 52 + this.$('#search').addEventListener('click', this.handleSearch); 53 + this.$('#prev').addEventListener('click', this.handlePrevPage); 54 + this.$('#next').addEventListener('click', this.handleNextPage); 55 + searchInput.addEventListener('keypress', this.handleKeyPress); 56 + 56 56 } 57 57 59 + disconnectedCallback() { 60 + this.$('#search').removeEventListener('click', this.handleSearch); 61 + this.$('#prev').removeEventListener('click', this.handlePrevPage); 62 + this.$('#next').removeEventListener('click', this.handleNextPage); 63 + this.$('input[type="text"]').addEventListener('keypress', this.handleKeyPress); 64 + } 58 58 66 + get styles() { 67 + return ` 68 + input[type="text"] { 69 + border: 1px solid red; 70 + } 71 + ` 72 + } 73 + handlePrevPage = () => { 74 + if(this.offset > 0) { 75 + this.offset--; 76 + this.handleSearch(); 77 + } 78 + } 79 + handleNextPage = () => { 80 + this.offset++; 81 + this.handleSearch(); 82 + } 83 + 59 59 handleSearch = () => { 60 60 const value = this.$('input[type="text"]').value; 61 61 fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}`) ... ... @@ -63,9 +63,12 @@ 63 63 .then(this.renderResults); 64 64 } 65 65 91 + handleKeyPress = (e) => { 92 + console.log(e.code); 93 + } 66 66 67 - get searchInputTemplate() {68 - return this.createTemplate(`95 + get html() { 96 + return ` 69 69 <div> 70 70 <input type="text"><button id="search">Search</button> 71 71 <div id="results"></div> ... ... @@ -72,7 +72,7 @@ 72 72 <button id="prev">prev</button> 73 73 <button id="next">next</button> 74 74 </div> 75 - ` );103 + `; 76 76 } 77 77 78 78 renderResults = (results) => { ... ... @@ -89,7 +89,7 @@ 89 89 } 90 90 91 91 render() { 92 - this.shadowRoot.appendChild(this. searchInputTemplate.content.cloneNode(true));120 + this.shadowRoot.appendChild(this.template.content.cloneNode(true)); 93 93 } 94 94 } 95 95 ); ... ... @@ -108,19 +108,26 @@ 108 108 return this.getAttribute('data-href'); 109 109 } 110 110 111 - get resultTemplate() { 112 - return this.createTemplate(` 139 + get styles() { 140 + return ` 141 + a { 142 + color: var(--color-brand-primary); 143 + } 144 + `; 145 + } 146 + get html() { 147 + return ` 113 113 <div> 114 114 <a href=${this.hrefAttr}>${this.titleAttr}</a> 115 115 <div> 116 116 <slot></slot> 117 117 </div> 118 - </div> 119 - `); 153 + </div> ` 120 120 } 155 + 121 121 connectedCallback() { 122 122 this.attachShadow({ mode: 'open' }); 123 - this.shadowRoot.appendChild(this. resultTemplate.content.cloneNode(true));158 + this.shadowRoot.appendChild(this.template.content.cloneNode(true)); 124 124 } 125 125 } 126 126 );