Skip to content

Instantly share code, notes, and snippets.

@gsteckman
Created July 20, 2021 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsteckman/0c94952560d04776646fbf93e6eee8fe to your computer and use it in GitHub Desktop.
Save gsteckman/0c94952560d04776646fbf93e6eee8fe to your computer and use it in GitHub Desktop.
compose-web 0.5.0-build228 example demonstrating that the checkbox state cannot be controlled using the checked parameter
import androidx.compose.runtime.*
import org.jetbrains.compose.web.css.em
import org.jetbrains.compose.web.css.padding
import org.jetbrains.compose.web.css.width
import org.jetbrains.compose.web.dom.*
import org.jetbrains.compose.web.renderComposable
fun main() {
renderComposable(rootElementId = "root") {
Presidents()
}
}
data class President(val name: String, val alive: Boolean)
@Composable
fun Presidents() {
val presidents: MutableList<President> = remember {
mutableStateListOf(
President("George Washington", false),
President("Thomas Jefferson", false),
President("Bill Clinton", true)
)
}
var selectedIndex by remember { mutableStateOf(0) }
MasterView(presidents) {
selectedIndex = it
}
Hr { }
DetailView(presidents[selectedIndex]) { president ->
presidents[selectedIndex] = president
}
}
@Composable
fun MasterView(presidents: List<President>, onClick: (Int) -> Unit) {
Div {
presidents.forEachIndexed { index, it ->
Div(attrs = {
onClick {
onClick(index)
}
}) {
Text(it.name)
Span(attrs = { style { padding(2.em) } })
Text(it.alive.toString())
}
}
}
}
@Composable
fun DetailView(president: President, save: (President) -> Unit) {
var tempPresident by remember(president) { mutableStateOf(president) }
Div {
Text("Name: ")
TextInput(value = tempPresident.name) {
onInput {
tempPresident = tempPresident.copy(name = it.value)
}
}
}
Div {
Text("Is Alive: ")
CheckboxInput(checked = tempPresident.alive) {
onInput {
tempPresident = tempPresident.copy(alive = it.value)
}
}
}
Button(attrs = {
style { width(20.em) }
onClick {
save(tempPresident)
}
}) {
Text("Save")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment