Skip to content

fix: support onXxx event name for TextInput event checks #1404

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

Merged
merged 2 commits into from
May 4, 2023
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
4 changes: 4 additions & 0 deletions src/__tests__/fireEvent-textInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ test('should fire only non-touch-related events on non-editable TextInput with n

const subject = view.getByText('Nested Text');
fireEvent(subject, 'focus');
fireEvent(subject, 'onFocus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'onSubmitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'layout', layoutEvent);
fireEvent(subject, 'onLayout', layoutEvent);

expect(onFocus).not.toHaveBeenCalled();
expect(onChangeText).not.toHaveBeenCalled();
expect(onSubmitEditing).not.toHaveBeenCalled();
expect(onLayout).toHaveBeenCalledTimes(2);
expect(onLayout).toHaveBeenCalledWith(layoutEvent);
});

Expand Down
37 changes: 24 additions & 13 deletions src/fireEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,28 @@ function isPointerEventEnabled(
return isPointerEventEnabled(parent, true);
}

// Due to accepting both `press` and `onPress` for event names, we need to
// cover both forms.
const touchEventNames = ['press', 'onPress'];

function isTouchEvent(eventName: string) {
return touchEventNames.includes(eventName);
}

// Experimentally checked which events are called on non-editable TextInput
const textInputEventsIgnoringEditableProp = [
/**
* List of events affected by `pointerEvents` prop.
*
* Note: `fireEvent` is accepting both `press` and `onPress` for event names,
* so we need cover both forms.
*/
const eventsAffectedByPointerEventsProp = new Set(['press', 'onPress']);

/**
* List of `TextInput` events not affected by `editable` prop.
*
* Note: `fireEvent` is accepting both `press` and `onPress` for event names,
* so we need cover both forms.
*/
const textInputEventsIgnoringEditableProp = new Set([
'contentSizeChange',
'onContentSizeChange',
'layout',
'onLayout',
'scroll',
];
'onScroll',
]);

function isEventEnabled(
element: ReactTestInstance,
Expand All @@ -63,11 +71,14 @@ function isEventEnabled(
if (isHostTextInput(nearestTouchResponder)) {
return (
nearestTouchResponder?.props.editable !== false ||
textInputEventsIgnoringEditableProp.includes(eventName)
textInputEventsIgnoringEditableProp.has(eventName)
);
}

if (isTouchEvent(eventName) && !isPointerEventEnabled(element)) {
if (
eventsAffectedByPointerEventsProp.has(eventName) &&
!isPointerEventEnabled(element)
) {
return false;
}

Expand Down