aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-bootstrap-select
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2020-05-20 17:12:56 +0200
committerRobert Jaros <rjaros@finn.pl>2020-05-20 17:12:56 +0200
commitef1abc3b975e1c77fc8663c5e5e95769fc934551 (patch)
tree343236378e2f24514635fb5e892043a054c86170 /kvision-modules/kvision-bootstrap-select
parent35628c364d9768a5589559b99cb698ebc2b56eba (diff)
downloadkvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.tar.gz
kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.tar.bz2
kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.zip
Make all stateful components implement ObservableState interface
Diffstat (limited to 'kvision-modules/kvision-bootstrap-select')
-rw-r--r--kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/Select.kt9
-rw-r--r--kvision-modules/kvision-bootstrap-select/src/main/kotlin/pl/treksoft/kvision/form/select/SelectInput.kt17
2 files changed, 23 insertions, 3 deletions
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<StringPair>? = 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<String?> {
/**
* 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<StringPair>? = null, value: String? = null,
multiple: Boolean = false, ajaxOptions: AjaxOptions? = null,
classes: Set<String> = setOf()
-) : SimplePanel(classes), FormInput {
+) : SimplePanel(classes), FormInput, ObservableState<String?> {
+
+ 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
+ }
+ }
}
/**