Fork onPress callbacks for ios highlighting (#44909)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44909

Today we wrap all `onPressIn` and `onPressOut` callbacks we pass to pressability so we can set the `highlighted` state. However highlighted state is only ever set to anything other that false on iOS. This change not only skips calling `setHighlighted(false)` on every press event but also skips wrapping the callback.

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D58391419

fbshipit-source-id: e79f51469609a59063098501f015f8078e3db79f
This commit is contained in:
Pieter Vanderwerff 2024-06-12 19:12:15 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 0ad107b988
Коммит f7aea0c8e1
1 изменённых файлов: 39 добавлений и 33 удалений

72
packages/react-native/Libraries/Text/Text.js поставляемый
Просмотреть файл

@ -101,41 +101,47 @@ const Text: React.AbstractComponent<
_disabled !== true;
const initialized = useLazyInitialization(isPressable);
const config = useMemo(
() =>
initialized
? {
disabled: !isPressable,
pressRectOffset: pressRetentionOffset,
onLongPress,
onPress,
onPressIn(event: PressEvent) {
// Updating isHighlighted causes unnecessary re-renders for platforms that don't use it
// in the best case, and cause issues with text selection in the worst case. Forcing
// the isHighlighted prop to false on all platforms except iOS.
setHighlighted(
(suppressHighlighting == null || !suppressHighlighting) &&
Platform.OS === 'ios',
);
onPressIn?.(event);
},
onPressOut(event: PressEvent) {
setHighlighted(false);
onPressOut?.(event);
},
}
: null,
[
initialized,
isPressable,
pressRetentionOffset,
const config = useMemo(() => {
if (!initialized) {
return null;
}
let _onPressIn = onPressIn;
let _onPressOut = onPressOut;
// Updating isHighlighted causes unnecessary re-renders for platforms that don't use it
// in the best case, and cause issues with text selection in the worst case. Forcing
// the isHighlighted prop to false on all platforms except iOS.
if (Platform.OS === 'ios') {
_onPressIn = (event: PressEvent) => {
setHighlighted(suppressHighlighting == null || !suppressHighlighting);
onPressIn?.(event);
};
_onPressOut = (event: PressEvent) => {
setHighlighted(false);
onPressOut?.(event);
};
}
return {
disabled: !isPressable,
pressRectOffset: pressRetentionOffset,
onLongPress,
onPress,
onPressIn,
onPressOut,
suppressHighlighting,
],
);
onPressIn: _onPressIn,
onPressOut: _onPressOut,
};
}, [
initialized,
isPressable,
pressRetentionOffset,
onLongPress,
onPress,
onPressIn,
onPressOut,
suppressHighlighting,
]);
const eventHandlers = usePressability(config);
const eventHandlersForText = useMemo(