add textarea emails autocomplete

This commit is contained in:
Alex Turchyn
2023-09-23 18:22:48 +03:00
parent cf85bea0df
commit 7ba3a604bd
3 changed files with 23 additions and 10 deletions
@@ -19,21 +19,32 @@ export default class extends HTMLElement {
fields.forEach((field) => {
const input = submitterItemEl.querySelector(`submitters-autocomplete[data-field="${field}"] input`)
const textarea = submitterItemEl.querySelector(`submitters-autocomplete[data-field="${field}"] textarea`)
if (input && item[field]) {
input.value = item[field]
}
if (textarea && item[field]) {
textarea.value = textarea.value.replace(/[^;,\s]+$/, item[field] + ' ')
}
})
}
fetch = (text, resolve) => {
if (text) {
const queryParams = new URLSearchParams({ q: text, field: this.dataset.field })
const q = text.split(/[;,\s]+/).pop().trim()
if (q) {
const queryParams = new URLSearchParams({ q, field: this.dataset.field })
fetch('/api/submitters_autocomplete?' + queryParams).then(async (resp) => {
const items = await resp.json()
resolve(items)
if (q.length < 3) {
resolve(items.filter((e) => e[this.dataset.field].startsWith(q)))
} else {
resolve(items)
}
}).catch(() => {
resolve([])
})
@@ -51,6 +62,6 @@ export default class extends HTMLElement {
}
get input () {
return this.querySelector('input')
return this.querySelector('input') || this.querySelector('textarea')
}
}