import { query, hasClass, on, next, prev, hasType, parent, queryAll } from "../utils"; function updatePlaceholder(element: HTMLInputElement | HTMLTextAreaElement) { if (!element.placeholder) element.placeholder = " "; } function onClickLabel(e: Event) { const label = e.currentTarget as HTMLLabelElement; const field = parent(label); const input = query("input:not([type=file], [type=checkbox], [type=radio]), select, textarea", field) as HTMLElement; if (input) input.focus(); } function onFocusInput(e: Event) { const input = e.currentTarget as HTMLInputElement; updateInput(input); } function onBlurInput(e: Event) { const input = e.currentTarget as HTMLInputElement; updateInput(input); } function onChangeFile(e: Event) { const input = e.currentTarget as HTMLInputElement; updateFile(input); } function onChangeColor(e: Event) { const input = e.currentTarget as HTMLInputElement; updateColor(input); } function onKeydownFile(e: KeyboardEvent) { const input = e.currentTarget as HTMLInputElement; updateFile(input, e); } function onKeydownColor(e: KeyboardEvent) { const input = e.currentTarget as HTMLInputElement; updateColor(input, e); } function onInputTextarea(e: Event) { const textarea = e.currentTarget as HTMLTextAreaElement; updateTextarea(textarea); } function onPasswordIconClick(e: Event) { const icon = e.currentTarget as HTMLElement; const input = query("input", parent(icon)) as HTMLInputElement; if (input && icon.textContent?.includes("visibility")) input.type = input.type === "password" ? "text" : "password"; } function updateAllLabels() { const labels = queryAll(".field > label"); for (let i=0; i input:not([type=file], [type=color], [type=range])") as NodeListOf; for (let i=0; i select") as NodeListOf; for (let i=0; i input[type=file]") as NodeListOf; for (let i=0; i input[type=color]") as NodeListOf; for (let i=0; i textarea") as NodeListOf; for (let i=0; i x.name).join(", ") : ""; nextInput.readOnly = true; on(nextInput, "keydown", onKeydownFile, false); updateInput(nextInput); } function updateColor(input: HTMLInputElement, e?: KeyboardEvent) { if (e?.key === "Enter") { const previousInput = prev(input) as HTMLInputElement; if (!hasType(previousInput, "color")) return; previousInput.click(); return; } const nextInput = next(input) as HTMLInputElement; if (!hasType(nextInput, "text")) return; nextInput.readOnly = true; nextInput.value = input.value; on(nextInput, "keydown", onKeydownColor, false); updateInput(nextInput); } function updateTextarea(textarea: HTMLTextAreaElement) { updatePlaceholder(textarea); const field = parent(textarea) as HTMLElement; field.removeAttribute("style"); if (hasClass(field, "min")) field.style.setProperty("--_size", `${Math.min(192, Math.max(textarea.scrollHeight, field.offsetHeight))}px`); } export function updateAllFields() { updateAllLabels(); updateAllInputs(); updateAllSelects(); updateAllFiles(); updateAllColors(); updateAllTextareas(); updateAllPasswordIcons(); }