TheSpyder / rescript-webapi

ReScript bindings to the DOM and other Web APIs
http://tinymce.github.io/rescript-webapi/api/Webapi/
Other
151 stars 37 forks source link

Add module functor to make CustomEvent with typed detail #93

Closed mununki closed 2 years ago

mununki commented 2 years ago

This PR is related to https://github.com/reasonml-community/bs-webapi-incubator/pull/204, https://github.com/reasonml-community/bs-webapi-incubator/issues/203.

This PR adds a module functor to make a CustomEvent module with typed detail.

Example

module OnChangeDetail = {
  type t = {
    component: string,
    valueAsDate: Js.Date.t,
    value: string,
  }
}

let bar: OnChangeDetail.t = {
  component: "date-picker",
  valueAsDate: Js.Date.make(),
  value: "2022-01-28",
}

module NewOnChangeEvent = Webapi.Dom.CustomEvent.Make(OnChangeDetail)
let eventWithDetail = NewOnChangeEvent.make("event-with-detail")

Js.log(eventWithDetail->Webapi.Dom.CustomEvent.preventDefault) // still can use as before.
Js.log(eventWithDetail->NewOnChangeEvent.preventDefault) // can use with new CustomEvent
Js.log2("make", NewOnChangeEvent.makeWithOptions("foo", bar)) // can make with options..
Js.log((eventWithDetail->NewOnChangeEvent.detail).valueAsDate) // typed detail!
TheSpyder commented 2 years ago

Interesting. I hadn't considered doing it this way. I'll give it some thought, thanks!

mununki commented 2 years ago

committed the suggestions and fix the test.

mununki commented 2 years ago

@TheSpyder I don't know why exactly, but does my rebase to reword the commits cause the task-list-complete check running loop maybe?

TheSpyder commented 2 years ago

No, it's because resolving a comment thread doesn't automatically resolve all tasks within it: https://github.com/tinymce/rescript-webapi/pull/93#discussion_r796623111

I normally take care of that before merging if it's clear the task was done.

I hope to find time to finish reviewing and merge this in the next day or two.

mununki commented 2 years ago

@TheSpyder I got it. Thx.

TheSpyder commented 2 years ago

published as 0.6.1