Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module functor to make CustomEvent with typed detail #93

Merged
merged 5 commits into from
Mar 9, 2022
Merged
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
30 changes: 30 additions & 0 deletions lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,35 @@ $$event.stopImmediatePropagation();

$$event.stopPropagation();

var Detail = {};

var EventWithDetail = Webapi__Dom__CustomEvent.Make(Detail);

var eventWithDetail = new CustomEvent("event-with-detail");

var eventWithOptions = new CustomEvent("event-with-detail", {
component: "test-component"
});

Curry._1(EventWithDetail.eventPhase, eventWithDetail);

Curry._1(EventWithDetail.eventPhase, eventWithOptions);

eventWithDetail.preventDefault();

eventWithDetail.stopImmediatePropagation();

eventWithDetail.stopPropagation();

eventWithOptions.preventDefault();

eventWithOptions.stopImmediatePropagation();

eventWithOptions.stopPropagation();

exports.$$event = $$event;
exports.Detail = Detail;
exports.EventWithDetail = EventWithDetail;
exports.eventWithDetail = eventWithDetail;
exports.eventWithOptions = eventWithOptions;
/* event Not a pure module */
14 changes: 14 additions & 0 deletions lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var Webapi__Dom__CustomEvent = require("../../../src/Webapi/Dom/Webapi__Dom__CustomEvent.js");

var target = document.createElement("strong");

Expand Down Expand Up @@ -39,8 +40,21 @@ var customEvent = new CustomEvent("custom-event", {

target.dispatchEvent(customEvent);

var Detail = {};

var EventWithDetail = Webapi__Dom__CustomEvent.Make(Detail);

var typedCustomEvent = new CustomEvent("event-with-detail", {
test: "test"
});

target.dispatchEvent(typedCustomEvent);

exports.target = target;
exports.$$event = $$event;
exports.handleClick = handleClick;
exports.customEvent = customEvent;
exports.Detail = Detail;
exports.EventWithDetail = EventWithDetail;
exports.typedCustomEvent = typedCustomEvent;
/* target Not a pure module */
18 changes: 18 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__CustomEvent.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ include Webapi__Dom__Event.Impl({

@new external make: string => t = "CustomEvent"
@new external makeWithOptions: (string, {..}) => t = "CustomEvent"

type _typedCustomEvent<'a>

module Make = (
Detail: {
type t
},
) => {
type t = Dom.event_like<_typedCustomEvent<Detail.t>>

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

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

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

module EventWithDetail = Make(Detail)
let eventWithDetail = EventWithDetail.make("event-with-detail")
let eventWithOptions = EventWithDetail.makeWithOptions(
"event-with-detail",
{component: "test-component"},
)

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

let _ = EventWithDetail.bubbles(eventWithOptions)
let _ = EventWithDetail.cancelable(eventWithOptions)
let _ = EventWithDetail.composed(eventWithOptions)
let _ = EventWithDetail.currentTarget(eventWithOptions)
let _ = EventWithDetail.defaultPrevented(eventWithOptions)
let _ = EventWithDetail.eventPhase(eventWithOptions)
let _ = EventWithDetail.target(eventWithOptions)
let _ = EventWithDetail.timeStamp(eventWithOptions)
let _ = EventWithDetail.type_(eventWithOptions)
let _ = EventWithDetail.isTrusted(eventWithOptions)

EventWithDetail.preventDefault(eventWithDetail)
EventWithDetail.stopImmediatePropagation(eventWithDetail)
EventWithDetail.stopPropagation(eventWithDetail)

EventWithDetail.preventDefault(eventWithOptions)
EventWithDetail.stopImmediatePropagation(eventWithOptions)
EventWithDetail.stopPropagation(eventWithOptions)

let _ = (eventWithDetail->EventWithDetail.detail).component
9 changes: 9 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ let customEvent = CustomEvent.makeWithOptions(
},
)
let _ = target->dispatchEvent(customEvent)

/* dispatch custom event with typed detail */
module Detail = {
type t = {test: string}
}

module EventWithDetail = CustomEvent.Make(Detail)
let typedCustomEvent = EventWithDetail.makeWithOptions("event-with-detail", {test: "test"})
let _ = target->dispatchEvent(typedCustomEvent)