From b7901e371f148bedd105b1f91c5485ec6e9a2f48 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Tue, 2 Jun 2020 23:45:18 +0200 Subject: Add parameter of bind, for not removing children when binding component to the state --- src/main/kotlin/pl/treksoft/kvision/core/Widget.kt | 3 ++- src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'src/main/kotlin/pl/treksoft/kvision') diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt index 72c5699c..371b5a20 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Widget.kt @@ -1019,11 +1019,12 @@ open class Widget(classes: Set = setOf()) : StyledComponent(), Component */ internal fun W.bindState( observableState: ObservableState, + removeChildren: Boolean = true, factory: (W.(S) -> Unit) ): W { val unsubscribe = observableState.subscribe { this.singleRender { - (this as? Container)?.removeAll() + if (removeChildren) (this as? Container)?.removeAll() this.factory(it) } } diff --git a/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt b/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt index 6c8090b7..457af0e4 100644 --- a/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt +++ b/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt @@ -13,13 +13,15 @@ import pl.treksoft.kvision.core.Widget.Companion.bindState * @param S the state type * @param W the widget type * @param observableState the state + * @param removeChildren remove all children of the component * @param factory a function which re-creates the view based on the given state */ fun W.bind( observableState: ObservableState, + removeChildren: Boolean = true, factory: (W.(S) -> Unit) ): W { - return this.bindState(observableState, factory) + return this.bindState(observableState, removeChildren, factory) } /** -- cgit