add opportunity to customize table header cells

This commit is contained in:
InsanusMokrassar 2022-01-25 19:35:08 +06:00
parent a4d4ec3db8
commit 13556a293c
2 changed files with 42 additions and 6 deletions

View File

@ -2,6 +2,8 @@
## 0.0.22 ## 0.0.22
* Add opportunity to customize table header cells
## 0.0.21 ## 0.0.21
* Upfill `UIKitWidth` * Upfill `UIKitWidth`

View File

@ -10,16 +10,16 @@ import org.w3c.dom.*
@Composable @Composable
fun <T> DefaultTable( fun <T> DefaultTable(
heading: List<String>, headerBuilders: List<ContentBuilder<HTMLTableCellElement>>,
dataList: SnapshotStateList<T>, dataList: SnapshotStateList<T>,
vararg tableModifiers: UIKitModifier, vararg tableModifiers: UIKitModifier,
attributesCustomizer: AttrBuilderContext<HTMLTableElement> = {}, attributesCustomizer: AttrBuilderContext<HTMLTableElement> = {},
headerCellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int, title: String) -> Unit = { _, _ -> }, headerCellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int) -> Unit = { },
rowAttributes: AttrsBuilder<HTMLTableRowElement>.(t: T) -> Unit = {}, rowAttributes: AttrsBuilder<HTMLTableRowElement>.(t: T) -> Unit = {},
cellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int, t: T) -> Unit = { _, _ -> }, cellCustomizer: AttrsBuilder<HTMLTableCellElement>.(i: Int, t: T) -> Unit = { _, _ -> },
cellFiller: @Composable ElementScope<HTMLTableCellElement>.(i: Int, t: T) -> Unit cellFiller: @Composable ElementScope<HTMLTableCellElement>.(i: Int, t: T) -> Unit
) { ) {
val headingIndexes = heading.indices val headingIndexes = headerBuilders.indices
Table( Table(
{ {
classes("uk-table") classes("uk-table")
@ -29,13 +29,13 @@ fun <T> DefaultTable(
) { ) {
Thead { Thead {
Tr { Tr {
heading.forEachIndexed { i, t -> headerBuilders.forEachIndexed { i, t ->
Th( Th(
{ {
headerCellCustomizer(i, t) headerCellCustomizer(i)
} }
) { ) {
Text(t) t()
} }
} }
} }
@ -61,3 +61,37 @@ fun <T> DefaultTable(
} }
} }
} }
@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
) {
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,
headerCellCustomizer,
rowAttributes,
cellCustomizer,
cellFiller
)
}