Browse Source

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 <hfhbd@users.noreply.github.com>
pull/2121/head
Philip Wedemann 2 years ago committed by GitHub
parent
commit
ad757a8f0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt
  2. 15
      web/core/src/jsTest/kotlin/elements/AttributesTests.kt

20
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt

@ -51,7 +51,15 @@ interface AttrsScope<TElement : Element> : EventsListenerScope {
* *
* `attr("class", ...)` overrides everything added using `classes(...)` calls * `attr("class", ...)` overrides everything added using `classes(...)` calls
*/ */
fun classes(vararg classes: String) fun classes(classes: Collection<String>)
/**
* [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 id(value: String) = attr(ID, value)
fun hidden() = attr(HIDDEN, true.toString()) fun hidden() = attr(HIDDEN, true.toString())
@ -136,6 +144,16 @@ open class AttrsScopeBuilder<TElement : Element>(
internal var refEffect: (DisposableEffectScope.(TElement) -> DisposableEffectResult)? = null internal var refEffect: (DisposableEffectScope.(TElement) -> DisposableEffectResult)? = null
internal val classes: MutableList<String> = mutableListOf() internal val classes: MutableList<String> = 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<String>) {
this.classes.addAll(classes)
}
/** /**
* [classes] adds all values passed as params to the element's classList. * [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. * This method acts cumulatively, that is, each call adds values to the classList.

15
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 @Test
fun attrClassOverridesClassesCall() = runTest { fun attrClassOverridesClassesCall() = runTest {
composition { composition {

Loading…
Cancel
Save