Greg Steckman
07/20/2021, 2:02 AMGreg Steckman
07/20/2021, 2:03 AM@Composable
fun FilteredInput() {
var text by remember { mutableStateOf("0") }
var text2 by remember { mutableStateOf("0") }
Div {
Text("Using event.target.value")
TextInput(value = text) {
onInput { event ->
text = event.value.filterToIntegerChars()
event.target.value = text
}
}
}
Div {
Text("Not using event.target.value")
TextInput(value = text2) {
onInput { event ->
text2 = event.value.filterToIntegerChars()
}
}
}
}
fun String.filterToIntegerChars(): String {
return this.filter {
it in '0'..'9'
}
}
hfhbd
07/20/2021, 8:54 AMInput
after onChange
, ignoring the (not managed) value state...
Interesting, even neverEqualsPolicy()
does not work tooOleksandr Karpovich [JB]
07/20/2021, 4:36 PMI think it works - it makes the recomposable scope get recomposed. But text2 value remains the same, so TextInput does't get recomposed (even though the outer scope is recomposing) because it has its own recomposable scope. Philip, you're right that the main issue here is that Input has its own internal state. here is an option, which is rather imperative than declarative:does not work tooneverEqualsPolicy()
Div {
Text("Not using event.target.value")
TextInput(value = text2) {
onBeforeInput {
val char = it.data?.get(0)
if(char != null && char !in '0'..'9') {
it.preventDefault()
}
}
onInput { event ->
text2 = event.value//.filterToIntegerChars()
}
}
}
its advantage is that you can avoid using event.target.text=...
which also was imperative.
I think ideally this should work:
onInput { event ->
text2 = event.value.filterToIntegerChars()
}
so we'll think what we can do about it
just in case: NumberInput would work as you expect.
But as for general use cases, you have the point.Greg Steckman
07/24/2021, 5:00 PMGreg Steckman
07/24/2021, 5:01 PMGreg Steckman
07/25/2021, 1:15 AMonChange {
it.target.files?.let { fileList ->
fileList[0]?.let { file ->
<http://console.info|console.info>("selected file: ${file.name}")
}
}
}
Greg Steckman
07/25/2021, 1:17 AMonInput{
it.dataTransfer?.apply{
val file: File? = files[0]
if (file != null) {
<http://console.info|console.info>("selected file: ${file.name}")
} else {
<http://console.info|console.info>("null file")
}
}
}
Greg Steckman
07/25/2021, 1:22 AMhfhbd
07/25/2021, 1:28 AMref { inputElement = it; onDispose { inputElement = null }}
. See https://github.com/hfhbd/bootstrap-compose/blob/0102af3e59dfe2238273eafe6615bfd1b0bf6453/showcase/src/jsMain/kotlin/app.softwork.bootstrapcompose.showcase/InputGroupView.kt#L364 😄