Skip to content

Commit 8003a76

Browse files
fix: support onXxx event name for TextInput event checks (#1404)
1 parent a110fd8 commit 8003a76

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/__tests__/fireEvent-textInput.test.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ test('should fire only non-touch-related events on non-editable TextInput with n
6262

6363
const subject = view.getByText('Nested Text');
6464
fireEvent(subject, 'focus');
65+
fireEvent(subject, 'onFocus');
6566
fireEvent.changeText(subject, 'Text');
6667
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
68+
fireEvent(subject, 'onSubmitEditing', { nativeEvent: { text: 'Text' } });
6769
fireEvent(subject, 'layout', layoutEvent);
70+
fireEvent(subject, 'onLayout', layoutEvent);
6871

6972
expect(onFocus).not.toHaveBeenCalled();
7073
expect(onChangeText).not.toHaveBeenCalled();
7174
expect(onSubmitEditing).not.toHaveBeenCalled();
75+
expect(onLayout).toHaveBeenCalledTimes(2);
7276
expect(onLayout).toHaveBeenCalledWith(layoutEvent);
7377
});
7478

src/fireEvent.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,28 @@ function isPointerEventEnabled(
4040
return isPointerEventEnabled(parent, true);
4141
}
4242

43-
// Due to accepting both `press` and `onPress` for event names, we need to
44-
// cover both forms.
45-
const touchEventNames = ['press', 'onPress'];
46-
47-
function isTouchEvent(eventName: string) {
48-
return touchEventNames.includes(eventName);
49-
}
50-
51-
// Experimentally checked which events are called on non-editable TextInput
52-
const textInputEventsIgnoringEditableProp = [
43+
/**
44+
* List of events affected by `pointerEvents` prop.
45+
*
46+
* Note: `fireEvent` is accepting both `press` and `onPress` for event names,
47+
* so we need cover both forms.
48+
*/
49+
const eventsAffectedByPointerEventsProp = new Set(['press', 'onPress']);
50+
51+
/**
52+
* List of `TextInput` events not affected by `editable` prop.
53+
*
54+
* Note: `fireEvent` is accepting both `press` and `onPress` for event names,
55+
* so we need cover both forms.
56+
*/
57+
const textInputEventsIgnoringEditableProp = new Set([
5358
'contentSizeChange',
59+
'onContentSizeChange',
5460
'layout',
61+
'onLayout',
5562
'scroll',
56-
];
63+
'onScroll',
64+
]);
5765

5866
function isEventEnabled(
5967
element: ReactTestInstance,
@@ -63,11 +71,14 @@ function isEventEnabled(
6371
if (isHostTextInput(nearestTouchResponder)) {
6472
return (
6573
nearestTouchResponder?.props.editable !== false ||
66-
textInputEventsIgnoringEditableProp.includes(eventName)
74+
textInputEventsIgnoringEditableProp.has(eventName)
6775
);
6876
}
6977

70-
if (isTouchEvent(eventName) && !isPointerEventEnabled(element)) {
78+
if (
79+
eventsAffectedByPointerEventsProp.has(eventName) &&
80+
!isPointerEventEnabled(element)
81+
) {
7182
return false;
7283
}
7384

0 commit comments

Comments
 (0)