Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

FormData and Object bindings

$
0
0

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?


Viewing all articles
Browse latest Browse all 2592

Trending Articles