-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/react-native/src/private/webapis/dom/events/CustomEvent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
*/ | ||
|
||
/** | ||
* This module implements the `CustomEvent` interface from the DOM. | ||
* See https://dom.spec.whatwg.org/#interface-customevent. | ||
*/ | ||
|
||
// flowlint unsafe-getters-setters:off | ||
|
||
import type {EventInit} from './Event'; | ||
|
||
import Event from './Event'; | ||
|
||
export type CustomEventInit = { | ||
...EventInit, | ||
detail?: mixed, | ||
}; | ||
|
||
export default class CustomEvent extends Event { | ||
_detail: mixed; | ||
|
||
constructor(type: string, options?: ?CustomEventInit) { | ||
const {detail, ...eventOptions} = options ?? {}; | ||
super(type, eventOptions); | ||
|
||
this._detail = detail; | ||
} | ||
|
||
get detail(): mixed { | ||
return this._detail; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/react-native/src/private/webapis/dom/events/__tests__/CustomEvent-itest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
* @fantom_flags enableAccessToHostTreeInFabric:true | ||
*/ | ||
|
||
import '../../../../../../Libraries/Core/InitializeCore.js'; | ||
|
||
import CustomEvent from '../CustomEvent'; | ||
import Event from '../Event'; | ||
|
||
describe('CustomEvent', () => { | ||
it('extends Event', () => { | ||
const event = new CustomEvent('foo', { | ||
bubbles: true, | ||
cancelable: true, | ||
composed: true, | ||
}); | ||
|
||
expect(event.type).toBe('foo'); | ||
expect(event.bubbles).toBe(true); | ||
expect(event.cancelable).toBe(true); | ||
expect(event.composed).toBe(true); | ||
expect(event).toBeInstanceOf(Event); | ||
}); | ||
|
||
it('allows passing a detail value', () => { | ||
const detail = Symbol('detail'); | ||
|
||
const event = new CustomEvent('foo', {detail}); | ||
|
||
expect(event.detail).toBe(detail); | ||
}); | ||
|
||
it('does NOT allow changing the detail value after construction', () => { | ||
const detail = Symbol('detail'); | ||
|
||
const event = new CustomEvent('foo', {detail}); | ||
|
||
expect(() => { | ||
'use strict'; | ||
// Use strict mode to throw an error instead of silently failing | ||
// $FlowExpectedError[cannot-write] | ||
event.detail = 'bar'; | ||
}).toThrow(); | ||
}); | ||
}); |