Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Add customevent functor #204

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.19.3

* Add a functor `MakeEmittedCustomEvent` to emit a detail of `CustomEvent`

### 0.19.2

* Removed peer dependency on `bsdoc`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bs-webapi",
"version": "0.19.2",
"version": "0.19.3",
"description": "Reason + BuckleScript bindings to DOM",
"repository": {
"type": "git",
Expand Down
12 changes: 12 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__CustomEvent.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ include Webapi__Dom__Event.Impl({ type nonrec t = t; });

[@bs.new] external make : string => t = "CustomEvent";
[@bs.new] external makeWithOptions : (string, Js.t({..})) => t = "CustomEvent";

module MakeCustomEvent = (Detail: {type t;}) => {
type t = Dom.customEvent;

include Webapi__Dom__Event.Impl({
type nonrec t = t;
});

[@bs.new] external make: string => t = "CustomEvent";
[@bs.new] external makeWithOptions: (string, Detail.t) => t = "CustomEvent";
[@bs.get] external detail: t => Detail.t = "detail";
};
34 changes: 34 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,37 @@ let _ = isTrusted(event);
preventDefault(event);
stopImmediatePropagation(event);
stopPropagation(event);

/* Event with type detail */
module Detail = {
type t = {
component:string
}
}

let t: Detail.t = {
component: "test-component"
};

module EventWithDetail = MakeCustomEvent(Detail);
let eventWithDetail = EventWithDetail.make("event-with-detail");
let eventWithOptions = EventWithDetail.makeWithOptions("event-with-detail", t);
eventWithOptions->Webapi.Dom.EventTarget.dispatchEvent;

/* Event */
let _ = bubbles(eventWithDetail);
let _ = cancelable(eventWithDetail);
let _ = composed(eventWithDetail);
let _ = currentTarget(eventWithDetail);
let _ = defaultPrevented(eventWithDetail);
let _ = eventPhase(eventWithDetail);
let _ = target(eventWithDetail);
let _ = timeStamp(eventWithDetail);
let _ = type_(eventWithDetail);
let _ = isTrusted(eventWithDetail);

preventDefault(eventWithDetail);
stopImmediatePropagation(eventWithDetail);
stopPropagation(eventWithDetail);

let _ = eventWithDetail->EventWithDetail.detail.component;
16 changes: 16 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__EventTarget__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ let _ = dispatchEvent(event, target);
/* https://github.com/reasonml-community/bs-webapi-incubator/issues/103 */
let customEvent = CustomEvent.makeWithOptions("custom-event", {"detail": {"test": "test"}});
dispatchEvent(customEvent, target);


/* another way */
module Detail = {
type t = {
test: string
}
};

let t: Detail.t = {
test: "test"
};

module EventWithDetail = CustomEvent.MakeCustomEvent(Detail);
let typedCustomEvent = EventWithDetail.makeWithOptions("event-with-detail", t);
typedCustomEvent->Webapi.Dom.EventTarget.dispatchEvent;