Files
docuseal/app/javascript/elements/search_input.js
T

61 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-08-14 16:34:30 +03:00
export default class extends HTMLElement {
connectedCallback () {
this.input.addEventListener('focus', () => {
if (this.title) {
2026-07-22 20:32:50 +03:00
this.titleContainer.classList.add('max-md:hidden')
2024-08-14 16:34:30 +03:00
}
})
this.input.addEventListener('blur', (e) => {
if (this.title && !e.target.value) {
this.title.classList.remove('hidden')
2026-07-22 20:32:50 +03:00
this.titleContainer.classList.remove('max-md:hidden')
2024-08-14 16:34:30 +03:00
}
})
2025-09-24 12:07:54 +03:00
this.button.addEventListener('click', (event) => {
2025-09-25 09:01:04 +03:00
if (!this.input.value && document.activeElement !== this.input) {
event.preventDefault()
2025-09-24 12:07:54 +03:00
2025-09-25 09:01:04 +03:00
this.input.focus()
2025-09-24 12:07:54 +03:00
}
})
2026-07-22 20:32:50 +03:00
document.addEventListener('turbo:before-cache', this.onBeforeCache)
}
disconnectedCallback () {
document.removeEventListener('turbo:before-cache', this.onBeforeCache)
}
onBeforeCache = () => {
this.input.value = this.input.getAttribute('value') || ''
if (this.title) {
this.titleContainer.classList.remove('max-md:hidden')
}
2024-08-14 16:34:30 +03:00
}
get input () {
return this.querySelector('input')
}
get title () {
return document.querySelector(this.dataset.title)
}
2025-09-24 12:07:54 +03:00
2026-07-22 20:32:50 +03:00
get titleContainer () {
const parent = this.title.parentElement
if (parent && !parent.contains(this)) {
return parent
}
return this.title
}
2025-09-24 12:07:54 +03:00
get button () {
return this.querySelector('button')
}
2024-08-14 16:34:30 +03:00
}