I can query the Dom with Webapi and get the input element, but how do I read the files so I can pass it to a FileReader
I just spent an hour chasing my tail. asking chatgpt (useless btw) and got no where.
chatgpt :
let handleFileInputChange = (event: Dom.Event.t) => {
let target = Dom.Event.target(event);
switch (Dom.HtmlInputElement.files(target) {
but there’s no such function HtmlInputElement.files. also its missing a closing ) lol
I haven’t used rescript-webapi (if that’s what you’ve referred to in your question) though. You can make a judgment call on whether to integrate the above code with WebApi’s types and bindings or not.
Thanks. That clears up the filereader. However I’m not using react or jsx. If it wasn’t clear by my need for webapi to get the input element. It’s for a chrome extension. I don’t own the input element. So I’m still stuck trying to get at the files to pass to a filereader. Beyond dipping into raw js which is… fine.
Meanwhile, here is an attempt to build minimal bindings to file input, independent of rescript-webapi.
// File APIs. Also borrow the FileReader bindings from my previous post.
module File = {
type t
}
// We want to get to event.target.files
// So we need a binding to event.target.files
// where event.target is our input element.
// A bindign to Event.target should give us our InputElement.
// A binding to InputElement.files should give us our files.
// I've also added a binding to addEventListener specific for InputElement.
type evType = [
| #change
]
module InputElement = {
type t
@get external files: t => array<File.t> = "files"
// You can add other API bindings for properties like .value, .disabled, etc.
module Event = {
type event
@get external target: event => t = "target"
@get external currentTarget: event => t = "currentTarget"
}
@send
external addEventListener: (t, evType, Event.event => unit) => unit =
"addEventListener"
}
// NOTE: FAKE constructor and creation of InputElement, just to verify the compiled JS code for event listener.
@new external make: unit => InputElement.t = "InputElement"
let ele = make()
ele->InputElement.addEventListener(#change, ev => {
let target = ev->InputElement.Event.target
Console.log(target->InputElement.files)
})
The tests, while many of them are incomplete, show a rough guideline of how to start using the HtmlInputElement api (this pattern actually works for all element types):
The binding @bhoot suggested would be fine to PR to the repo, actually.
Also note that there is an ongoing effort to replace webapi, due to it’s importance to the ecosystem and my lack of time to work on what is now a very old codebase.