JSUIKitKBindings/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Table.kt

64 lines
1.9 KiB
Kotlin
Raw Normal View History

2022-01-12 10:27:49 +00:00
package dev.inmo.jsuikit.elements
2021-12-22 08:38:12 +00:00
import androidx.compose.runtime.Composable
import androidx.compose.runtime.snapshots.SnapshotStateList
2022-01-12 13:58:52 +00:00
import dev.inmo.jsuikit.modifiers.*
import org.jetbrains.compose.web.attributes.AttrsBuilder
2021-12-22 08:38:12 +00:00
import org.jetbrains.compose.web.dom.*
2022-01-13 15:36:39 +00:00
import org.jetbrains.compose.web.dom.Text
import org.w3c.dom.*
2021-12-22 08:38:12 +00:00
@Composable
fun <T> DefaultTable(
heading: List<String>,
dataList: SnapshotStateList<T>,
vararg tableModifiers: UIKitModifier,
attributesCustomizer: AttrBuilderContext<HTMLTableElement> = {},
headerCellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int, title: String) -> Unit = { _, _ -> },
rowAttributes: AttrsBuilder<HTMLTableRowElement>.(t: T) -> Unit = {},
cellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int, t: T) -> Unit = { _, _ -> },
cellFiller: @Composable ElementScope<HTMLTableCellElement>.(i: Int, t: T) -> Unit
2021-12-22 08:38:12 +00:00
) {
val headingIndexes = heading.indices
Table(
{
classes("uk-table")
include(*tableModifiers)
attributesCustomizer()
2021-12-22 08:38:12 +00:00
}
) {
Thead {
Tr {
heading.forEachIndexed { i, t ->
2021-12-22 08:38:12 +00:00
Th(
{
headerCellCustomizer(i, t)
2021-12-22 08:38:12 +00:00
}
) {
Text(t)
2021-12-22 08:38:12 +00:00
}
}
}
}
Tbody {
dataList.forEach {
Tr(
{
rowAttributes(it)
}
) {
2021-12-22 08:38:12 +00:00
headingIndexes.forEach { i ->
Td(
{
cellCustomizer(i, it)
}
) {
2021-12-22 08:38:12 +00:00
cellFiller(i, it)
}
}
}
}
}
}
}