mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add getDefinedEventHandlers API (#55861)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55861 Add `Fantom.getDefinedEventHandlers(element)` which returns the names of event handlers registered on a component. It Reads committed memoizedProps from the React fiber, so it works for any component type — including component-specific events like ScrollView's onScroll. The alternative we considered was reading the ViewEvents bitmap from `BaseViewProps` in the ShadowNode (C++). That approach only covers View-specific touch/pointer events and onLayout; it misses component-specific events dispatched imperatively (e.g. ScrollView's onScroll), requiring a separate codepath per component type. Changelog: [Internal] Reviewed By: javache Differential Revision: D94936370 fbshipit-source-id: f48b6e37a9e7cf252f396db69e1a43ecca82db96
This commit is contained in:
1 parent
7f1a1e6c93
commit
9ebb3259f0
3 files changed
+114
-4
No files matched your search
+80
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import * as Fantom from '@react-native/fantom';
|
||||
import * as React from 'react';
|
||||
import {createRef} from 'react';
|
||||
import {ScrollView, View} from 'react-native';
|
||||
|
||||
describe('Fantom.getDefinedEventHandlers', () => {
|
||||
it('returns event handler names registered on a view', () => {
|
||||
const root = Fantom.createRoot();
|
||||
const ref = createRef<React.ElementRef<typeof View>>();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(
|
||||
<View ref={ref} onLayout={() => {}} onTouchStart={() => {}} />,
|
||||
);
|
||||
});
|
||||
|
||||
const handlers = Fantom.getDefinedEventHandlers(ref);
|
||||
expect(handlers).toContain('onLayout');
|
||||
expect(handlers).toContain('onTouchStart');
|
||||
});
|
||||
|
||||
it('returns an empty array when no event handlers are registered', () => {
|
||||
const root = Fantom.createRoot();
|
||||
const ref = createRef<React.ElementRef<typeof View>>();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<View ref={ref} />);
|
||||
});
|
||||
|
||||
expect(Fantom.getDefinedEventHandlers(ref)).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns multiple event handler names when multiple are registered', () => {
|
||||
const root = Fantom.createRoot();
|
||||
const ref = createRef<React.ElementRef<typeof View>>();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(
|
||||
<View
|
||||
ref={ref}
|
||||
onLayout={() => {}}
|
||||
onTouchStart={() => {}}
|
||||
onTouchEnd={() => {}}
|
||||
onTouchCancel={() => {}}
|
||||
onClick={() => {}}
|
||||
/>,
|
||||
);
|
||||
});
|
||||
|
||||
const handlers = Fantom.getDefinedEventHandlers(ref);
|
||||
expect(handlers).toContain('onLayout');
|
||||
expect(handlers).toContain('onTouchStart');
|
||||
expect(handlers).toContain('onTouchEnd');
|
||||
expect(handlers).toContain('onTouchCancel');
|
||||
expect(handlers).toContain('onClick');
|
||||
});
|
||||
|
||||
it('detects onScroll on a ScrollView', () => {
|
||||
const root = Fantom.createRoot();
|
||||
const ref = createRef<React.ElementRef<typeof ScrollView>>();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<ScrollView ref={ref} onScroll={() => {}} />);
|
||||
});
|
||||
|
||||
expect(Fantom.getDefinedEventHandlers(ref)).toContain('onScroll');
|
||||
});
|
||||
});
|
||||
+30
-1
@@ -23,7 +23,10 @@ import {LogBox} from 'react-native';
|
||||
import NativeFantom, {
|
||||
NativeEventCategory,
|
||||
} from 'react-native/src/private/testing/fantom/specs/NativeFantom';
|
||||
import {getNativeNodeReference} from 'react-native/src/private/webapis/dom/nodes/internals/NodeInternals';
|
||||
import {
|
||||
getInstanceHandle,
|
||||
getNativeNodeReference,
|
||||
} from 'react-native/src/private/webapis/dom/nodes/internals/NodeInternals';
|
||||
import ReadOnlyNode from 'react-native/src/private/webapis/dom/nodes/ReadOnlyNode';
|
||||
|
||||
const nativeRuntimeScheduler = global.nativeRuntimeScheduler;
|
||||
@@ -237,6 +240,32 @@ export function unstable_getFabricUpdateProps(nodeOrRef: NodeOrRef): Readonly<{
|
||||
return NativeFantom.getFabricUpdateProps(shadowNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the names of event handlers registered on a component's shadow node.
|
||||
*
|
||||
* @param nodeOrRef - The node or ref for which to retrieve event handlers.
|
||||
* @returns Array of event handler prop names (e.g. `['onLayout', 'onTouchStart']`)
|
||||
*/
|
||||
export function getDefinedEventHandlers(
|
||||
nodeOrRef: NodeOrRef,
|
||||
): ReadonlyArray<string> {
|
||||
const node = getNode(nodeOrRef);
|
||||
const instanceHandle = getInstanceHandle(node);
|
||||
if (typeof instanceHandle !== 'object' || instanceHandle == null) {
|
||||
return [];
|
||||
}
|
||||
// WARNING: This uses React private API (fiber internals).
|
||||
// $FlowExpectedError[incompatible-type]
|
||||
const memoizedProps = (instanceHandle as {memoizedProps?: {[string]: mixed}})
|
||||
.memoizedProps;
|
||||
if (memoizedProps == null) {
|
||||
return [];
|
||||
}
|
||||
return Object.keys(memoizedProps).filter(
|
||||
key => key.startsWith('on') && typeof memoizedProps[key] === 'function',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulates running a task on the UI thread and forces side effect to drain
|
||||
* the event queue, scheduling events to be dispatched to JavaScript.
|
||||
|
||||
@@ -86,7 +86,8 @@ std::string NativeFantom::getRenderedOutput(
|
||||
SurfaceId surfaceId,
|
||||
NativeFantomGetRenderedOutputRenderFormatOptions options) {
|
||||
RenderFormatOptions formatOptions{
|
||||
options.includeRoot, options.includeLayoutMetrics};
|
||||
.includeRoot = options.includeRoot,
|
||||
.includeLayoutMetrics = options.includeLayoutMetrics};
|
||||
|
||||
auto viewTree = appDelegate_.mountingManager_->getViewTree(surfaceId);
|
||||
return appDelegate_.mountingManager_->renderer()->render(
|
||||
@@ -124,10 +125,10 @@ void NativeFantom::enqueueNativeEvent(
|
||||
std::optional<bool> isUnique) {
|
||||
if (isUnique.value_or(false)) {
|
||||
shadowNode->getEventEmitter()->dispatchUniqueEvent(
|
||||
std::move(type), payload.value_or(folly::dynamic::object()));
|
||||
type, payload.value_or(folly::dynamic::object()));
|
||||
} else {
|
||||
shadowNode->getEventEmitter()->dispatchEvent(
|
||||
std::move(type),
|
||||
type,
|
||||
payload.value_or(folly::dynamic::object()),
|
||||
category.value_or(RawEvent::Category::Unspecified));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user