Reuben Firmin
11/16/2023, 11:34 AMclass Column(heading: String, consumer : TagConsumer<*>): DIV(mapOf(), consumer) {
init {
h1 {
+heading
}
}
}
and then within the dsl call it as:
appendHTML().div("p-4") {
...
Column("Column 1", consumer)
Column("Column 2", consumer)
Column("Column 3", consumer)
Column("Column 4", consumer)
Column("Column 5", consumer)
this is pretty nice, because now I can create components in a typesafe way. what I don't like is having to explicitly pass in consumer. is there a better pattern for this?Reuben Firmin
11/16/2023, 11:48 AMBoard("My Board",
listOf(Column("Column 1", consumer),
Column("Column 2", consumer),
Column("Column 3", consumer),
Column("Column 4", consumer),
Column("Column 5", consumer)), consumer)
Reuben Firmin
11/16/2023, 11:48 AMclass Board(heading: String, columns: List<Column>, consumer : TagConsumer<*>): DIV(mapOf(), consumer) {
init {
h1 {
+heading
}
div {
columns.forEach {
it.visit { }
}
}
}
}
e5l
11/28/2023, 11:50 AMReuben Firmin
11/28/2023, 12:02 PMclass Board(val heading: String, consumer: TagConsumer<*>): DIV(mapOf(), consumer) {
fun render() {
h1 {
+this@Board.heading
}
}
}
fun FlowContent.board(heading: String, block : Board.() -> Unit = {})
= Board(heading, consumer).visit {
render()
block()
}
class Column(val heading: String, consumer : TagConsumer<*>): DIV(mapOf(), consumer) {
fun render() {
h1 {
+this@Column.heading
}
}
companion object {
inline fun Board.column(heading: String, crossinline block : Column.() -> Unit = {}) : Unit
= Column(heading, consumer).visit {
render()
block()
}
}
}
This does the right thing and lets me do this:
board("My Board") {
column("Column 1")
column("Column 2")
column("Column 3")
column("Column 4")
column("Column 5")
}
...but I'm not 100% sure if that's what was intended (the docs on github don't cover this); also not sure if this'll scale to more complex components.
(my issue with the inits above was that Column(s) got instantiated before Board and so rendered first, because their inits rendered first)e5l
11/28/2023, 12:03 PM