Arjan van Wieringen
07/09/2022, 6:23 AMInput
which on recomposition doesn't update the UI with the latest value, but it is visible in the DOM tree. The code is a bit like this
@Composable fun EditableTitle(value: String, onInputChange: (String) -> Unit = {}) {
var editMode by remember { mutableStateOf(false) }
val nativeElement = remember { mutableStateOf<HTMLInputElement?>(null) }
// I need to use this as a hack to overwrite the value so that it works
LaunchedEffect(value) {
nativeElement.value?.value = value
}
LaunchedEffect(editMode, nativeElement.value) {
when {
editMode -> nativeElement.value?.select()
!editMode && nativeElement.value != null -> {
nativeElement.value?.value?.also(onInputChange)
nativeElement.value?.blur()
}
}
}
Input(InputType.Text) {
placeholder("Enter list name...")
defaultValue(value) // when I use value(value) it blocks my UI
refState(nativeElement) // this one uses DisposableEffect to fetch the nativeElement
if (editMode && evt.key == "Enter") {
editMode = false
}
}
}
I do some stuff with some other state variables to make it more UI friendly. The value
parameter can be updated externally as well and when it has been modified at least once in the above Composable, the UI doesn't update if the input value into the composable is changed. When I use the LaunchedEffect(value)
it works. Is this logical?Oleksandr Karpovich [JB]
07/11/2022, 11:02 AMdefaultValue
sets only the initial value of the input. Subsequent changes of defaultValue
won't do anything.
Ideally, no LaunchedEffect(value) should be needed and using value
should not block the UI.
Do you have a minimal reproducer when value(value)
leads to UI freeze?Arjan van Wieringen
07/11/2022, 3:17 PMArjan van Wieringen
07/11/2022, 4:17 PMfun main() {
renderComposable("root") {
MySimpleInput("Foo")
}
}
@Composable fun MySimpleInput(
value: String,
) {
Input(InputType.Text) {
value(value)
}
}
I can't type anything in the input.Arjan van Wieringen
07/11/2022, 4:24 PMvalue(value)
I can type.Arjan van Wieringen
07/11/2022, 4:28 PMattr("value", value)
Arjan van Wieringen
07/11/2022, 4:32 PMfun defaultValue(value: Number): InputAttrsScope<ValueType> {
attr("value", value.toString())
return this
}
I don't know what to say.....hfhbd
07/11/2022, 4:34 PMArjan van Wieringen
07/11/2022, 4:51 PMvalue
somehow right? So how would I do that then? Because even a minimal example freezes my input element.Arjan van Wieringen
07/11/2022, 4:52 PMprop({ el: HTMLInputElement, v: String -> el.value = v}, value)
This also workshfhbd
07/11/2022, 4:53 PMfun main() {
renderComposable("root") {
var input by remember { mutableStateOf("Foo") }
MySimpleInput(input) { input = it }
}
}
@Composable fun MySimpleInput(
value: String,
onInput: (String) -> Unit
) {
Input(InputType.Text) {
value(value)
onInput { onInput(it.value) }
}
}
Arjan van Wieringen
07/11/2022, 4:59 PMhfhbd
07/11/2022, 5:02 PMArjan van Wieringen
07/11/2022, 6:29 PMArjan van Wieringen
07/14/2022, 12:12 PM