From 13556a293c93b70f3499a168dc77d1548ef85632 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 25 Jan 2022 19:35:08 +0600 Subject: [PATCH] add opportunity to customize table header cells --- CHANGELOG.md | 2 + .../kotlin/dev/inmo/jsuikit/elements/Table.kt | 46 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afc191c..4a7f160 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.0.22 +* Add opportunity to customize table header cells + ## 0.0.21 * Upfill `UIKitWidth` diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Table.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Table.kt index 77db07e..911eae9 100644 --- a/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Table.kt +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Table.kt @@ -10,16 +10,16 @@ import org.w3c.dom.* @Composable fun DefaultTable( - heading: List, + headerBuilders: List>, dataList: SnapshotStateList, vararg tableModifiers: UIKitModifier, attributesCustomizer: AttrBuilderContext = {}, - headerCellCustomizer: AttrsBuilder.(i: Int, title: String) -> Unit = { _, _ -> }, + headerCellCustomizer: AttrsBuilder.(i: Int) -> Unit = { }, rowAttributes: AttrsBuilder.(t: T) -> Unit = {}, cellCustomizer: AttrsBuilder.(i: Int, t: T) -> Unit = { _, _ -> }, cellFiller: @Composable ElementScope.(i: Int, t: T) -> Unit ) { - val headingIndexes = heading.indices + val headingIndexes = headerBuilders.indices Table( { classes("uk-table") @@ -29,13 +29,13 @@ fun DefaultTable( ) { Thead { Tr { - heading.forEachIndexed { i, t -> + headerBuilders.forEachIndexed { i, t -> Th( { - headerCellCustomizer(i, t) + headerCellCustomizer(i) } ) { - Text(t) + t() } } } @@ -61,3 +61,37 @@ fun DefaultTable( } } } + +@Composable +fun DefaultTable( + heading: List, + dataList: SnapshotStateList, + vararg tableModifiers: UIKitModifier, + attributesCustomizer: AttrBuilderContext = {}, + headerCellCustomizer: AttrsBuilder.(i: Int, title: String) -> Unit = { _, _ -> }, + rowAttributes: AttrsBuilder.(t: T) -> Unit = {}, + cellCustomizer: AttrsBuilder.(i: Int, t: T) -> Unit = { _, _ -> }, + cellFiller: @Composable ElementScope.(i: Int, t: T) -> Unit +) { + val headersByIndex = heading.mapIndexed { index, s -> index to s }.toMap() + val headerCellCustomizer: AttrsBuilder.(i: Int) -> Unit = { i -> + val header = headersByIndex[i] + if (header != null) { + headerCellCustomizer(i, header) + } + } + inline fun headerFactory(header: String): ContentBuilder = { + Text(header) + } + val headerBuilders = heading.map(::headerFactory) + return DefaultTable( + headerBuilders, + dataList, + tableModifiers = tableModifiers, + attributesCustomizer, + headerCellCustomizer, + rowAttributes, + cellCustomizer, + cellFiller + ) +}