slinky icon indicating copy to clipboard operation
slinky copied to clipboard

support Option ref type in forwardRef

Open evbo opened this issue 2 years ago • 2 comments

Hi, some very reputable JS libraries rely on forwardRef being able to take an optional ref, for instance here: https://react-table.tanstack.com/docs/examples/row-selection

So is it possible to support Option[ReactRef[_]] types?

The only way I could get this code equivalent in slinky was to hack with a null check:

val component = React.forwardRef(FunctionalComponent(
    fn = (props: RowSelectorProps, tableRef: /*TODO: needs to be Option Type*/ReactRef[js.Any]) => {
      val defaultRef = useRef[js.Any](null)

      // TODO: faking it for now...
      val resolvedRef = if (tableRef != null) tableRef else defaultRef

      useEffect(
        () => {
          resolvedRef.current.asInstanceOf[js.Dynamic].indeterminate = props.indeterminate
        },
        Seq(resolvedRef, props.indeterminate)
      )

      println(s"checked: ${props.checked}, ind: ${props.indeterminate}, table: ${tableRef}")

      div(
        input(
          `type` := "checkbox",
          checked := props.checked,
          onChange := props.onChange,
          ref := resolvedRef,
          style := props.style
        )
      )
    }
  ))

evbo avatar Apr 25 '22 16:04 evbo

Note to self, this is documented at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L543

shadaj avatar Apr 30 '22 21:04 shadaj

This is going to be a breaking change, so unfortunately we may have to wait until v0.8.0.

shadaj avatar Apr 30 '22 21:04 shadaj