aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/pl/treksoft/kvision/form/SelectOptGroup.kt
blob: 41eb951e79b071fdb367801e7a0b79fbf3ebcb57 (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
60
package pl.treksoft.kvision.form

import com.github.snabbdom.VNode
import pl.treksoft.kvision.panel.SimplePanel
import pl.treksoft.kvision.snabbdom.StringPair

class SelectOptGroup(label: String, options: List<StringPair>? = null, maxOptions: Int? = null,
                     disabled: Boolean = false, classes: Set<String> = setOf()) : SimplePanel(classes) {

    var label: String = label
        set(value) {
            field = value
            refresh()
        }
    private var options = options
        set(value) {
            field = value
            setChildrenFromOptions()
        }
    var maxOptions: Int? = maxOptions
        set(value) {
            field = value
            refresh()
        }
    var disabled: Boolean = disabled
        set(value) {
            field = value
            refresh()
        }

    init {
        setChildrenFromOptions()
    }

    override fun render(): VNode {
        return kvh("optgroup", childrenVNodes())
    }

    private fun setChildrenFromOptions() {
        this.removeAll()
        options?.let {
            val c = it.map {
                SelectOption(it.first, it.second)
            }
            this.addAll(c)
        }
    }

    override fun getSnAttrs(): List<StringPair> {
        val sn = super.getSnAttrs().toMutableList()
        sn.add("label" to label)
        maxOptions?.let {
            sn.add("data-max-options" to "" + it)
        }
        if (disabled) {
            sn.add("disabled" to "true")
        }
        return sn
    }
}