From ad757a8f0a95e99708b849e1720a874c5d84f49b Mon Sep 17 00:00:00 2001 From: Philip Wedemann <22521688+hfhbd@users.noreply.github.com> Date: Mon, 13 Jun 2022 09:21:34 +0200 Subject: [PATCH] Web: Add list overload for classes (#2094) * Add list overload for classes * Add array and collection overload for classes * Remove array overload Co-authored-by: hfhbd --- .../compose/web/attributes/AttrsScope.kt | 20 ++++++++++++++++++- .../jsTest/kotlin/elements/AttributesTests.kt | 15 ++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt index c8ad7cb4c2..ed0e89f600 100644 --- a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt +++ b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt @@ -51,7 +51,15 @@ interface AttrsScope : EventsListenerScope { * * `attr("class", ...)` overrides everything added using `classes(...)` calls */ - fun classes(vararg classes: String) + fun classes(classes: Collection) + + /** + * [classes] adds all values passed as params to the element's classList. + * This method acts cumulatively, that is, each call adds values to the classList. + * In the ideology of Composable functions and their recomposition one just don't need to remove classes, + * since if your classList is, for instance, condition-dependent, you can always just call this method conditionally. + */ + fun classes(vararg classes: String) = classes(classes.asList()) fun id(value: String) = attr(ID, value) fun hidden() = attr(HIDDEN, true.toString()) @@ -136,6 +144,16 @@ open class AttrsScopeBuilder( internal var refEffect: (DisposableEffectScope.(TElement) -> DisposableEffectResult)? = null internal val classes: MutableList = mutableListOf() + /** + * [classes] adds all values passed as params to the element's classList. + * This method acts cumulatively, that is, each call adds values to the classList. + * In the ideology of Composable functions and their recomposition one just don't need to remove classes, + * since if your classList is, for instance, condition-dependent, you can always just call this method conditionally. + */ + override fun classes(classes: Collection) { + this.classes.addAll(classes) + } + /** * [classes] adds all values passed as params to the element's classList. * This method acts cumulatively, that is, each call adds values to the classList. diff --git a/web/core/src/jsTest/kotlin/elements/AttributesTests.kt b/web/core/src/jsTest/kotlin/elements/AttributesTests.kt index 09e7bb2f93..790e895383 100644 --- a/web/core/src/jsTest/kotlin/elements/AttributesTests.kt +++ b/web/core/src/jsTest/kotlin/elements/AttributesTests.kt @@ -64,6 +64,21 @@ class AttributesTests { } } + @Test + fun attrClassVarargArrayList() = runTest { + composition { + Div(attrs = { + classes("c1", "c2") + classes(listOf("c3", "c4")) + classes(classes = arrayOf("c5", "c6")) + }) + } + + with(nextChild()) { + assertEquals("c1 c2 c3 c4 c5 c6", getAttribute("class")) + } + } + @Test fun attrClassOverridesClassesCall() = runTest { composition {