Skip to content

Commit f282baa

Browse files
rubennortefacebook-github-bot
authored andcommitted
Implement CustomEvent (#48922)
Summary: Pull Request resolved: #48922 Changelog: [internal] This is a basic subclass of Event that allows setting custom values (as opposed to `Event` that is meant to be used as a superclass). Reviewed By: yungsters Differential Revision: D67804060 fbshipit-source-id: 9e140cc436f8e230f37275a74df23efd4841071b
1 parent 8445ebc commit f282baa

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
/**
12+
* This module implements the `CustomEvent` interface from the DOM.
13+
* See https://dom.spec.whatwg.org/#interface-customevent.
14+
*/
15+
16+
// flowlint unsafe-getters-setters:off
17+
18+
import type {EventInit} from './Event';
19+
20+
import Event from './Event';
21+
22+
export type CustomEventInit = {
23+
...EventInit,
24+
detail?: mixed,
25+
};
26+
27+
export default class CustomEvent extends Event {
28+
_detail: mixed;
29+
30+
constructor(type: string, options?: ?CustomEventInit) {
31+
const {detail, ...eventOptions} = options ?? {};
32+
super(type, eventOptions);
33+
34+
this._detail = detail;
35+
}
36+
37+
get detail(): mixed {
38+
return this._detail;
39+
}
40+
}

packages/react-native/src/private/webapis/dom/events/Event.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
setStopPropagationFlag,
3737
} from './internals/EventInternals';
3838

39-
type EventInit = {
39+
export type EventInit = {
4040
bubbles?: boolean,
4141
cancelable?: boolean,
4242
composed?: boolean,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
* @fantom_flags enableAccessToHostTreeInFabric:true
11+
*/
12+
13+
import '../../../../../../Libraries/Core/InitializeCore.js';
14+
15+
import CustomEvent from '../CustomEvent';
16+
import Event from '../Event';
17+
18+
describe('CustomEvent', () => {
19+
it('extends Event', () => {
20+
const event = new CustomEvent('foo', {
21+
bubbles: true,
22+
cancelable: true,
23+
composed: true,
24+
});
25+
26+
expect(event.type).toBe('foo');
27+
expect(event.bubbles).toBe(true);
28+
expect(event.cancelable).toBe(true);
29+
expect(event.composed).toBe(true);
30+
expect(event).toBeInstanceOf(Event);
31+
});
32+
33+
it('allows passing a detail value', () => {
34+
const detail = Symbol('detail');
35+
36+
const event = new CustomEvent('foo', {detail});
37+
38+
expect(event.detail).toBe(detail);
39+
});
40+
41+
it('does NOT allow changing the detail value after construction', () => {
42+
const detail = Symbol('detail');
43+
44+
const event = new CustomEvent('foo', {detail});
45+
46+
expect(() => {
47+
'use strict';
48+
// Use strict mode to throw an error instead of silently failing
49+
// $FlowExpectedError[cannot-write]
50+
event.detail = 'bar';
51+
}).toThrow();
52+
});
53+
});

0 commit comments

Comments
 (0)