oyvindberg / st-material-ui

Material UI 5 for Scala 3 (Slinky and Scalajs-React)
32 stars 2 forks source link

Unable to get event.target.value #3

Closed dpere326 closed 1 year ago

dpere326 commented 1 year ago

I'm having difficulties extracting value from events. Any attempt results in a java.lang.ClassCastException where the value can't be cast to String

For example, handling TablePagination onRowsPerPageChange and accessing the corresponding event.target.value results in this issue.

def handleChangeRowsPerPage(event: ReactEventFrom[(HTMLTextAreaElement | HTMLInputElement) & org.scalajs.dom.Element]): Callback = {
  val reducedEvent = event.asInstanceOf[ReactEventFrom[HTMLInputElement]]
  val rpp = reducedEvent.target.value
  rowsPerPage.setState(rpp.toDouble)
}
TableFooter(
  TableRow(
    TablePagination(
      count = rowCount,
      onPageChange = handleChangePage,
      page = pageNumber.value,
      rowsPerPage = rowsPerPage.value)
      .rowsPerPageOptions(js.Array(25.0, 50.0))
      .onRowsPerPageChange(handleChangeRowsPerPage)
  )
)

Console output

Please kindly advise if I'm missing something in the process.

Thanks!

oyvindberg commented 1 year ago

I think this looks like a bug in Material UI, but it's the thing which can easily go unnoticed in Javascript since it won't throw an exception.

.target.value for an input or text area field should be a string, but here it is a number.

To unblock yourself you can say something like val rpp = reducedEvent.target.asInstanceOf[js.Dynamic].selectDynamic("value").asInstanceOf[Double]

dpere326 commented 1 year ago

Thanks for the quick feedback! The suggested workaround worked like a charm 👍

nafg commented 1 year ago

reducedEvent.target.asInstanceOf[js.Dynamic].selectDynamic("value").asInstanceOf[Double]

No need for selectDynamic("value"), the whole point of Dynamic is that you can just do .value

So reducedEvent.target.asInstanceOf[js.Dynamic].value.asInstanceOf[Double] should work.