Search code examples
reactjskotlinkotlin-multiplatformkotlin-js

KotlinJS and React, get change value from TextArea


Hello everyone I am trying to get value from textArea DOM but it doesn't seem to work like regular react

val chat = fc<ChatProps> { props ->
    val (mess, setMess) = useState("")

        div("flex flex-row bg-gray-400 m-4 py-2 px-1 mt-auto") {
            textarea(classes = "bg-transparent border-transparent w-full h-full border-opacity-0 " +
                    "scrollbar-hide w-full h-full") {
                attrs.value = mess
                attrs.onChange = {
                    console.log(it.target.nodeValue ?: "")
                }
            }

it.target.value is in React, it.currentTarget doesn't work either,casting the event T to be HTMLInputElement doesn't work either. It's always null. The classes are tailwind classes


Solution

  • This seems to work, will wait for someone to submit a better answer

     textarea() {
          attrs.value = mess
          attrs.onChangeFunction = {
             val ev = it as ChangeEvent<HTMLInputElement>
             console.log(it.target.value)
             setMess(it.target.value)
        }
     }