Skip to content

Commit 2ab1b02

Browse files
committed
add receiver to cell lambda, and appropriate DslMarker annotations
1 parent b23d5de commit 2ab1b02

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

data-table/src/commonMain/kotlin/com/seanproctor/datatable/BasicDataTable.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ fun BasicDataTable(
9999
}
100100
check(cells.size <= columns.size) { "Row $rowIndex has too many cells." }
101101
check(cells.size >= columns.size) { "Row $rowIndex doesn't have enough cells." }
102-
cells.forEach { cellData ->
102+
cells.forEachIndexed { index, cellData ->
103103
// Must have exactly 1 Composable per cell
104104
Box(Modifier.padding(contentPadding)) {
105105
cellContentProvider.RowCellContent {
106-
cellData()
106+
cellData(TableCellScopeImpl(index))
107107
}
108108
}
109109
}

data-table/src/commonMain/kotlin/com/seanproctor/datatable/DataTableDsl.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
44
import androidx.compose.ui.graphics.Color
55
import androidx.compose.ui.unit.Dp
66

7+
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
78
@DslMarker
89
annotation class DataTableScopeMarker
910

@@ -17,7 +18,7 @@ interface DataTableScope {
1718
* Creates a new row in the [BasicDataTable] with the specified content.
1819
*/
1920
fun row(
20-
content: TableRowScope.() -> Unit
21+
content: @DataTableScopeMarker TableRowScope.() -> Unit
2122
)
2223
}
2324

@@ -29,7 +30,12 @@ interface TableRowScope {
2930
var isFooter: Boolean
3031
var backgroundColor: Color
3132

32-
fun cell(content: @Composable () -> Unit)
33+
fun cell(content: @DataTableScopeMarker @Composable TableCellScope.() -> Unit)
34+
}
35+
36+
@DataTableScopeMarker
37+
interface TableCellScope {
38+
val columnIndex: Int
3339
}
3440

3541
internal class DataTableScopeImpl(
@@ -56,13 +62,17 @@ internal class TableRowScopeImpl(
5662
override var isHeader: Boolean = false
5763
override var isFooter: Boolean = false
5864
override var backgroundColor: Color = Color.Unspecified
59-
val cells = mutableListOf<@Composable () -> Unit>()
65+
val cells = mutableListOf<@Composable TableCellScope.() -> Unit>()
6066

6167
init {
6268
apply(content)
6369
}
6470

65-
override fun cell(content: @Composable () -> Unit) {
71+
override fun cell(content: @Composable TableCellScope.() -> Unit) {
6672
cells += content
6773
}
6874
}
75+
76+
internal class TableCellScopeImpl(
77+
override val columnIndex: Int,
78+
) : TableCellScope

0 commit comments

Comments
 (0)