From ef1abc3b975e1c77fc8663c5e5e95769fc934551 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 20 May 2020 17:12:56 +0200 Subject: Make all stateful components implement ObservableState interface --- .../kotlin/pl/treksoft/kvision/form/select/Select.kt | 9 ++++++++- .../pl/treksoft/kvision/form/select/SelectInput.kt | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'kvision-modules/kvision-bootstrap-select/src') diff --git a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt index 4ffece7d..5fb92ab8 100644 --- a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt +++ b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt @@ -30,6 +30,7 @@ import pl.treksoft.kvision.form.FieldLabel import pl.treksoft.kvision.form.InvalidFeedback import pl.treksoft.kvision.form.StringFormControl import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.SnOn /** @@ -52,7 +53,7 @@ open class Select( options: List? = null, value: String? = null, name: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, label: String? = null, rich: Boolean = false -) : SimplePanel(setOf("form-group")), StringFormControl { +) : SimplePanel(setOf("form-group")), StringFormControl, ObservableState { /** * A list of options (value to label pairs) for the select control. @@ -272,6 +273,12 @@ open class Select( input.blur() } + override fun getState(): String? = input.getState() + + override fun subscribe(observer: (String?) -> Unit): () -> Unit { + return input.subscribe(observer) + } + companion object { internal var counter = 0 } diff --git a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt index ced0945d..5cba791c 100644 --- a/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt +++ b/kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt @@ -33,6 +33,7 @@ import pl.treksoft.kvision.form.InputSize import pl.treksoft.kvision.form.ValidationStatus import pl.treksoft.kvision.html.ButtonStyle import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.asString import pl.treksoft.kvision.utils.obj import pl.treksoft.kvision.utils.set @@ -72,7 +73,9 @@ open class SelectInput( options: List? = null, value: String? = null, multiple: Boolean = false, ajaxOptions: AjaxOptions? = null, classes: Set = setOf() -) : SimplePanel(classes), FormInput { +) : SimplePanel(classes), FormInput, ObservableState { + + protected val observers = mutableListOf<(String?) -> Unit>() /** * A list of options (value to label pairs) for the select control. @@ -82,7 +85,7 @@ open class SelectInput( /** * A value of the selected option. */ - var value by refreshOnUpdate(value) { refreshState() } + var value by refreshOnUpdate(value) { refreshState(); observers.forEach { ob -> ob(it) } } /** * The name attribute of the generated HTML select element. @@ -396,6 +399,16 @@ open class SelectInput( override fun blur() { getElementJQuery()?.blur() } + + override fun getState(): String? = value + + override fun subscribe(observer: (String?) -> Unit): () -> Unit { + observers += observer + observer(value) + return { + observers -= observer + } + } } /** -- cgit