aboutsummaryrefslogtreecommitdiff
path: root/static/components/input-list.ts
blob: 326cb2b589bb06b4ddc1631ff473bbd9601789a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
export { InputList }

/* This file replaces input_list.js */

class InputList extends HTMLElement {

    el: HTMLInputElement;

    values: [HTMLInputElement, any][] = [];

    constructor() {
        super();
        this.el = this.children[0].cloneNode(true) as HTMLInputElement;
    }

    connectedCallback() {
        this.addInstance();
    }

    addInstance() {
        let new_el = this.el.cloneNode(true) as HTMLInputElement
        let that = this;
        new_el.addEventListener('input', function() {
            if (new_el.value === '') {
                let sibling = this.previousElementSibling || this.nextElementSibling;
                // this.remove();
                // that.values = that.values.filter((p) => p[0] == this)
                that.values = that.values.filter((p) => p[0] != this);
                this.remove();
                (sibling as HTMLInputElement).focus();
            } else {
                if (!this.nextElementSibling) {
                    that.addInstance();
                    // window.setTimeout(() => this.focus())
                    this.focus();
                }
            }
        });
        this.values.push([new_el, ''])
        // this.appendChild(new_el);
        this.replaceChildren(... this.values.map((p) => p[0]))
    }

    get value(): any[] {
        return []
    }

    set value(new_value: any[]) {
        let els = [];
        for (let value of new_value) {
            let new_el = this.el.cloneNode() as HTMLInputElement;
            new_el.value = value;
            els.push(new_el);
        }
        /* Final element (empty) */
        els.push(this.el.cloneNode() as HTMLInputElement);
        this.replaceChildren(...els);
    }
}