ml-in-barcelona / jsoo-react

js_of_ocaml bindings for ReactJS. Based on ReasonReact.
https://ml-in-barcelona.github.io/jsoo-react
MIT License
138 stars 19 forks source link

Can't pass optional values to dom elements #81

Closed jchavarri closed 2 years ago

jchavarri commented 2 years ago

Repro:

[@@@react.dom]

let href = None in
let element = a ?href ()
(*               ^^^^
This expression has type string option but an expression was expected of type
  string *)
davesnx commented 2 years ago

Ops, that's because the type is enforced by HTML. Should we allow to be wrapped with option?

It's relatively easy to do!

jchavarri commented 2 years ago

I think it'd be useful :) The sample code I was working on had either href or onClick depending on some optional value. We can work around it by building two elements, one with href and one with onClick, but it's kind of sad to not be able to leverage optional labelled args, no? (if you say it's easy...)

jchavarri commented 2 years ago

@davesnx it seems this is still not possible.

Input:

let href = None
let element = a ?href ()
              ^^^^^^^^^^
Error: This expression has type string option
       but an expression was expected of type string
make: *** [build] Error 1

If I expand the expression:

let element =
  React.Dom.createDOMElementVariadic "a"
    ~props:(Js_of_ocaml.Js.Unsafe.obj
              [|("href",
                  (Js_of_ocaml.Js.Unsafe.inject
                     (Js_of_ocaml.Js.Optdef.option
                         (Js_of_ocaml.Js.string (href : string option)))))|] :
                                                ^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type string option
       but an expression was expected of type string
make: *** [build] Error 1
    React.Dom.domProps) []

(we should prob add a test to test_jsoo_react suite)

jchavarri commented 2 years ago

This was fixed in https://github.com/ml-in-barcelona/jsoo-react/pull/92. Thanks @davesnx !