Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException with native event when using autocomplete #765

Closed
thubalek opened this issue Jun 9, 2021 · 3 comments
Closed

ClassCastException with native event when using autocomplete #765

thubalek opened this issue Jun 9, 2021 · 3 comments
Assignees
Labels

Comments

@thubalek
Copy link

thubalek commented Jun 9, 2021

Hi, I'm working with JetPack Compose for Web and I found that following snippet

@Composable
fun ClassCastExceptionTestCaseView() {
    Input(attrs = {
        onInput {
            console.log("New value is", it.nativeEvent.target.asDynamic().value as String)
        }
        attr("type", "email")
        attr("autocomplete", "email")
    }, value = "")
}

causes ClassCastException when using autocomplete. When typing then there is no problem.

In debugger it looks like this:

image

I'm not sure but it may be a bug in JetPack Compose for Web.

@thubalek
Copy link
Author

thubalek commented Jun 9, 2021

According suggestion on Kotlin Slack I implemented this workaround:

@Composable
fun ClassCastExceptionTestCaseView() {
    Input(attrs = {
        // onInput {
        //     console.log("New value is", it.nativeEvent.target.asDynamic().value as String)
        // }
        attr("type", "email")
        attr("autocomplete", "email")
    }, value = "") {
        DomSideEffect {
          val callback : ((Event)->Unit) = { event ->
                val newText = event.target.asDynamic().value
                console.log("New value is", newText)
            }
            it.addEventListener("input", callback)
            onDispose { element ->
                element.removeEventListener("input", callback)
            }
         }
    }
} 

@hfhbd
Copy link
Contributor

hfhbd commented Jun 11, 2021

This works too

Input(...
  attrs = {
    addEventListener("input") {
      val target = it.nativeEvent.target as HTMLInputElement
      onChange(target)
    }
  }
)

@eymar
Copy link
Collaborator

eymar commented Jun 29, 2021

fixed in 0.5.0-build228

@eymar eymar closed this as completed Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants