Reuben Firmin
11/29/2023, 10:06 PMcreateHTMLDocument
consumer (i.e. the original render) but fails when it's part of the appendHTML
consumer. source in thread. why is this happening?Reuben Firmin
11/29/2023, 10:07 PMclass TabBar(private val tabs: List<Tab>, consumer: TagConsumer<*>): DIV(mapOf(
"class" to "text-xl font-medium text-center text-gray-500 border-b border-gray-200 dark:text-gray-400 " +
"dark:border-gray-700 pb-8"), consumer) {
private val activeClasses = "inline-block p-4 text-blue-600 border-b-2 border-blue-600 rounded-t-lg " +
"active dark:text-blue-500 dark:border-blue-500"
private val inactiveClasses = "inline-block p-4 border-b-2 border-transparent rounded-t-lg " +
"hover:text-gray-600 hover:border-gray-300 dark:hover:text-gray-300"
fun render(block: TabBar.() -> Unit) {
ul("flex flex-wrap -mb-px") {
this@TabBar.tabs.forEach { tab ->
li("me-2") {
form {
a(classes = if (tab.current) {
this@TabBar.activeClasses
} else {
this@TabBar.inactiveClasses
}) {
href = "#"
htmxClick(this@form, tab.path, tab.target)
.params(tab.params)
if (tab.current) {
this@a.attributes["aria-current"] = "page"
}
+tab.label
}
}
}
}
}
block()
}
}
fun FlowContent.tabBar(tabs: List<Tab>, block: TabBar.() -> Unit = {}) {
TabBar(tabs, consumer).visit {
render(block)
}
}
data class Tab(val label: String,
val current: Boolean,
val path: String,
val target: String,
val params: Map<String, String>)
Reuben Firmin
11/29/2023, 10:07 PMthis@a.attributes["aria-current"]
line, but again only when under appendHTML
Reuben Firmin
11/29/2023, 10:08 PMcreateHTMLDocument
consumerCies
11/30/2023, 12:06 PMthis@a.attributes
? why not just
if (tab.current) { attributes["aria-current"] = "page"}
directly under the href = "#"
Cies
11/30/2023, 12:07 PMReuben Firmin
11/30/2023, 12:08 PMthis@a
was just superstition to make sure I was adding attributes to the right tag.
What constitutes content in this case, href? Or the label?Reuben Firmin
11/30/2023, 12:08 PMfun HTMLTag.htmxClick(path: String, target: String): HTMLTag {
attributes["hx-post"] = path
attributes["hx-trigger"] = "click"
attributes["hx-target"] = target
attributes["hx-swap"] = "innerHTML"
return this
}
Reuben Firmin
11/30/2023, 12:23 PMfun render(block: TabBar.() -> Unit) {
ul("flex flex-wrap -mb-px") {
this@TabBar.tabs.forEach { tab ->
li("me-2") {
// form {
a(classes = if (tab.current) {
this@TabBar.activeClasses
} else {
this@TabBar.inactiveClasses
}) {
href = "#"
htmxClick(tab.path, tab.target)
// htmxClick(this@form, tab.path, tab.target)
// .params(tab.params)
if (tab.current) {
attributes["aria-current"] = "page"
}
+tab.label
// }
}
}
}
}
block()
}
So it's something about the form, and the form version of htmxClick, which is:
fun HTMLTag.htmxClick(form: FORM, path: String, target: String): FORM {
attributes["hx-post"] = path
attributes["hx-trigger"] = "click"
attributes["hx-target"] = target
attributes["hx-swap"] = "innerHTML"
return form
}
fun FORM.params(params: Map<String, String>) {
params.entries.forEach {
input {
type = InputType.hidden
name = it.key
value = it.value
}
}
}
So I think what you're saying is because I'm adding these hidden inputs into the a tag itself (which is admittedly janky) then the subsequent setting of attributes fails.Cies
11/30/2023, 12:37 PMCies
11/30/2023, 12:38 PMCies
11/30/2023, 12:41 PMthis
, we do not do that... also if htmxClick only works on a tags, put it on A
instead of on all htmlTagsReuben Firmin
11/30/2023, 12:47 PMthis
just so i could stick params on in a builder pattern. with some lightweight refactoring, the form approach now works:
fun render(block: TabBar.() -> Unit) {
ul("flex flex-wrap -mb-px") {
this@TabBar.tabs.forEach { tab ->
li("me-2") {
form {
htmxParams(tab.params) // <<<<<<
a(classes = if (tab.current) {
this@TabBar.activeClasses
} else {
this@TabBar.inactiveClasses
}) {
href = "#"
htmxClick(this@form, tab.path, tab.target)
if (tab.current) {
attributes["aria-current"] = "page"
}
+tab.label
}
}
}
}
}
block()
}
so now the hidden inputs go into the form above the a
Reuben Firmin
11/30/2023, 12:48 PMCies
12/04/2023, 1:50 PM