fix search button

This commit is contained in:
Pete Matsyburka committed 2026-09-23 17:00:17 +03:00
1 parent 56bba60d3f
commit e7fd1e4006
1 file changed
+12 -8
+12 -8
View File
@@ -1,16 +1,10 @@
export default class extends HTMLElement { export default class extends HTMLElement {
connectedCallback () { connectedCallback () {
this.button.addEventListener('click', (event) => { this.addEventListener('click', this.onClick)
if (!this.input.value && document.activeElement !== this.input) {
event.preventDefault()
this.input.focus()
}
})
document.addEventListener('turbo:before-cache', this.onBeforeCache) document.addEventListener('turbo:before-cache', this.onBeforeCache)
if (this.input.value) { if (this.input?.value) {
this.input.focus() this.input.focus()
this.input.setSelectionRange(this.input.value.length, this.input.value.length) this.input.setSelectionRange(this.input.value.length, this.input.value.length)
} }
@@ -20,6 +14,16 @@ export default class extends HTMLElement {
document.removeEventListener('turbo:before-cache', this.onBeforeCache) document.removeEventListener('turbo:before-cache', this.onBeforeCache)
} }
onClick = (event) => {
if (!event.target.closest('button')) return
if (!this.input.value && document.activeElement !== this.input) {
event.preventDefault()
this.input.focus()
}
}
onBeforeCache = () => { onBeforeCache = () => {
this.input.value = this.input.getAttribute('value') || '' this.input.value = this.input.getAttribute('value') || ''
} }