FormData and Object bindings

Hi,

I’m have a meta question about bindings. I’m getting the hang of creating them but when I write something like:

module FormData = {
  type t

  @new
  external make: 'form => t = "FormData"

  @send
  external entries: t => Iterator.t<_> = "entries"
}

module Object = {
  @val @scope("Object")
  external fromEntries: Iterator.t<_> => Js.Dict.t<string> = "fromEntries"
}

@react.component
let make = () => {
  let onSubmit = ev => {
    ev->ReactEvent.Form.preventDefault
    let target = ev->ReactEvent.Form.target
    let formData = FormData.make(target)
    Console.log(formData->FormData.entries->Object.fromEntries)
  }

  <form onSubmit>
    <input type_="number" placeholder="x" name="x" value={"2"} />
    <input type_="text" placeholder="y" name="y" value={"k"} />
    <button type_="submit"> {React.string(`Submit`)} </button>
  </form>
}

I always wonder: “I can’t be the first one that needs this right?”.
Do bindings for FormData and Object.fromEntries exist?
I was expecting Object.fromEntries to exist in Core (Core.Object | ReScript API). This is not the case, is this by design? Can I contribute this binding?

What is the deal with target in ev->ReactEvent.Form.target. Is there no htmlElement type for this? What am I expected to cast this to?

And what is _ in Iterator.t<_>? I feel like I avoid certain questions by using _ or 'form when I don’t know the answer. What would the correct values be for this?

Object.fromEntries is bound by both Dict.fromArray and Dict.fromIterator.

About FormData, there is a fairly recent thread: Using FormData in rescript react, but I agree it should be easier to find and rescript-webapi could be modernized.

The _ makes the type system allow any type. It’s similar to 'a, but it also works for multiple arguments as long as they are all generic.

Example:

type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
let make = ({a, b}: props<_>) => Console.log2(a, b)
3 Likes