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.*
|
2022-01-21 14:28:19 +00:00
|
|
|
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(
|
2022-01-25 13:35:08 +00:00
|
|
|
headerBuilders: List<ContentBuilder<HTMLTableCellElement>>,
|
2021-12-22 08:38:12 +00:00
|
|
|
dataList: SnapshotStateList<T>,
|
2022-01-13 13:56:39 +00:00
|
|
|
vararg tableModifiers: UIKitModifier,
|
2022-01-13 05:55:08 +00:00
|
|
|
attributesCustomizer: AttrBuilderContext<HTMLTableElement> = {},
|
2022-01-25 14:13:03 +00:00
|
|
|
headerCustomizer: AttrBuilderContext<HTMLTableSectionElement> = {},
|
|
|
|
headerCellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int) -> Unit = {},
|
2022-01-21 14:28:19 +00:00
|
|
|
rowAttributes: AttrsBuilder<HTMLTableRowElement>.(t: T) -> Unit = {},
|
2022-01-21 16:20:02 +00:00
|
|
|
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
|
|
|
) {
|
2022-01-25 13:35:08 +00:00
|
|
|
val headingIndexes = headerBuilders.indices
|
2021-12-22 08:38:12 +00:00
|
|
|
Table(
|
|
|
|
{
|
|
|
|
classes("uk-table")
|
|
|
|
include(*tableModifiers)
|
2022-01-13 05:55:08 +00:00
|
|
|
attributesCustomizer()
|
2021-12-22 08:38:12 +00:00
|
|
|
}
|
|
|
|
) {
|
2022-01-25 14:13:03 +00:00
|
|
|
Thead(headerCustomizer) {
|
2021-12-22 08:38:12 +00:00
|
|
|
Tr {
|
2022-01-25 13:35:08 +00:00
|
|
|
headerBuilders.forEachIndexed { i, t ->
|
2021-12-22 08:38:12 +00:00
|
|
|
Th(
|
|
|
|
{
|
2022-01-25 13:35:08 +00:00
|
|
|
headerCellCustomizer(i)
|
2021-12-22 08:38:12 +00:00
|
|
|
}
|
|
|
|
) {
|
2022-01-25 13:35:08 +00:00
|
|
|
t()
|
2021-12-22 08:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 14:13:03 +00:00
|
|
|
|
2021-12-22 08:38:12 +00:00
|
|
|
Tbody {
|
|
|
|
dataList.forEach {
|
2022-01-21 14:28:19 +00:00
|
|
|
Tr(
|
|
|
|
{
|
|
|
|
rowAttributes(it)
|
|
|
|
}
|
|
|
|
) {
|
2021-12-22 08:38:12 +00:00
|
|
|
headingIndexes.forEach { i ->
|
2022-01-21 16:20:02 +00:00
|
|
|
Td(
|
|
|
|
{
|
|
|
|
cellCustomizer(i, it)
|
|
|
|
}
|
|
|
|
) {
|
2021-12-22 08:38:12 +00:00
|
|
|
cellFiller(i, it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 13:35:08 +00:00
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun <T> DefaultTable(
|
|
|
|
heading: List<String>,
|
|
|
|
dataList: SnapshotStateList<T>,
|
|
|
|
vararg tableModifiers: UIKitModifier,
|
|
|
|
attributesCustomizer: AttrBuilderContext<HTMLTableElement> = {},
|
2022-01-25 14:13:03 +00:00
|
|
|
headerCustomizer: AttrBuilderContext<HTMLTableSectionElement> = {},
|
2022-01-25 13:35:08 +00:00
|
|
|
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
|
|
|
|
) {
|
|
|
|
val headersByIndex = heading.mapIndexed { index, s -> index to s }.toMap()
|
|
|
|
val headerCellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int) -> Unit = { i ->
|
|
|
|
val header = headersByIndex[i]
|
|
|
|
if (header != null) {
|
|
|
|
headerCellCustomizer(i, header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline fun headerFactory(header: String): ContentBuilder<HTMLTableCellElement> = {
|
|
|
|
Text(header)
|
|
|
|
}
|
|
|
|
val headerBuilders = heading.map(::headerFactory)
|
|
|
|
return DefaultTable(
|
|
|
|
headerBuilders,
|
|
|
|
dataList,
|
|
|
|
tableModifiers = tableModifiers,
|
|
|
|
attributesCustomizer,
|
2022-01-25 14:13:03 +00:00
|
|
|
headerCustomizer,
|
2022-01-25 13:35:08 +00:00
|
|
|
headerCellCustomizer,
|
|
|
|
rowAttributes,
|
|
|
|
cellCustomizer,
|
|
|
|
cellFiller
|
|
|
|
)
|
|
|
|
}
|