mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
b966d29724
Summary: In 2017, React published v15.5 which extracted the built-in `prop types` to a separate package to reflect the fact that not everybody uses them. In 2018, React Native started to remove `PropTypes` from React Native for the same reason. In 0.68 React Native introduced a deprecation warning which notified users that the change was coming, and in 0.69 we removed the PropTypes entirely. The feedback we've received from the community is that there has not been enough time to migrate libraries off of PropTypes. This has resulted in users needing to patch the React Native package `index.js` file directly to add back the PropTypes, instead of migrating off of them. We can empathize with this fix short term (it unblocks the upgrade) but long term this patch will cause users to miss important changes to `index.js`, and add a maintenance cost for users. Part of the reason there was not enough time is that we didn't do a good job surfacing libraries that were using PropTypes. This means, when you got a deprecation warning, it wasn't clear where the source of the usage was (either in your code or in a library). So even if you wanted to migrate, it was difficult to know where to actually make the change. In the next release, we've made it easier to find call sites using deprecated types by [fixing the code frame in errors](https://github.com/react-native-community/cli/pull/1699) reporting in LogBox, and ensuring that [the app doesn't crash without a warning](https://github.com/facebook/react-native/pull/34650). This should make it easier to identify exactly where the deprecated usage is, so you can migrate it. To help users get off of the patch, and allow more time to migrate, we're walking back the removal of PropTypes, and keeping it as a deprecation for a couple more versions. We ask that you either migrate off PropTypes to a type system like TypeScript, or migrate to the `deprecated-react-native-prop-types` package. Once we feel more confident that the community has migrated and will not need to patch React Native in order to fix this issue, we'll remove the PropTypes again. **If you have any trouble finding the source of the PropType usage, please file an issue so we can help track it down with you.** Changelog: [General][Changed] - Add back deprecated PropTypes Reviewed By: yungsters Differential Revision: D40725705 fbshipit-source-id: 8ce61be30343827efd6dc89a012eeef0b6676deb
308 lines
8.8 KiB
JavaScript
308 lines
8.8 KiB
JavaScript
/**
|
|
* 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 type {PressEvent} from '../Types/CoreEventTypes';
|
|
|
|
import * as PressabilityDebug from '../Pressability/PressabilityDebug';
|
|
import usePressability from '../Pressability/usePressability';
|
|
import flattenStyle from '../StyleSheet/flattenStyle';
|
|
import processColor from '../StyleSheet/processColor';
|
|
import StyleSheet from '../StyleSheet/StyleSheet';
|
|
import {getAccessibilityRoleFromRole} from '../Utilities/AcessibilityMapping';
|
|
import Platform from '../Utilities/Platform';
|
|
import TextAncestor from './TextAncestor';
|
|
import {NativeText, NativeVirtualText} from './TextNativeComponent';
|
|
import {type TextProps} from './TextProps';
|
|
import * as React from 'react';
|
|
import {useContext, useMemo, useState} from 'react';
|
|
|
|
/**
|
|
* Text is the fundamental component for displaying text.
|
|
*
|
|
* @see https://reactnative.dev/docs/text
|
|
*/
|
|
const Text: React.AbstractComponent<
|
|
TextProps,
|
|
React.ElementRef<typeof NativeText | typeof NativeVirtualText>,
|
|
> = React.forwardRef((props: TextProps, forwardedRef) => {
|
|
const {
|
|
accessible,
|
|
accessibilityLabel,
|
|
accessibilityRole,
|
|
allowFontScaling,
|
|
'aria-busy': ariaBusy,
|
|
'aria-checked': ariaChecked,
|
|
'aria-disabled': ariaDisabled,
|
|
'aria-expanded': ariaExpanded,
|
|
'aria-label': ariaLabel,
|
|
'aria-selected': ariaSelected,
|
|
ellipsizeMode,
|
|
id,
|
|
nativeID,
|
|
onLongPress,
|
|
onPress,
|
|
onPressIn,
|
|
onPressOut,
|
|
onResponderGrant,
|
|
onResponderMove,
|
|
onResponderRelease,
|
|
onResponderTerminate,
|
|
onResponderTerminationRequest,
|
|
onStartShouldSetResponder,
|
|
pressRetentionOffset,
|
|
role,
|
|
suppressHighlighting,
|
|
...restProps
|
|
} = props;
|
|
|
|
const [isHighlighted, setHighlighted] = useState(false);
|
|
|
|
const _accessibilityState = {
|
|
busy: ariaBusy ?? props.accessibilityState?.busy,
|
|
checked: ariaChecked ?? props.accessibilityState?.checked,
|
|
disabled: ariaDisabled ?? props.accessibilityState?.disabled,
|
|
expanded: ariaExpanded ?? props.accessibilityState?.expanded,
|
|
selected: ariaSelected ?? props.accessibilityState?.selected,
|
|
};
|
|
|
|
const _disabled =
|
|
restProps.disabled != null
|
|
? restProps.disabled
|
|
: _accessibilityState?.disabled;
|
|
|
|
const nativeTextAccessibilityState =
|
|
_disabled !== _accessibilityState?.disabled
|
|
? {..._accessibilityState, disabled: _disabled}
|
|
: _accessibilityState;
|
|
|
|
const isPressable =
|
|
(onPress != null ||
|
|
onLongPress != null ||
|
|
onStartShouldSetResponder != null) &&
|
|
_disabled !== true;
|
|
|
|
const initialized = useLazyInitialization(isPressable);
|
|
const config = useMemo(
|
|
() =>
|
|
initialized
|
|
? {
|
|
disabled: !isPressable,
|
|
pressRectOffset: pressRetentionOffset,
|
|
onLongPress,
|
|
onPress,
|
|
onPressIn(event: PressEvent) {
|
|
setHighlighted(!suppressHighlighting);
|
|
onPressIn?.(event);
|
|
},
|
|
onPressOut(event: PressEvent) {
|
|
setHighlighted(false);
|
|
onPressOut?.(event);
|
|
},
|
|
onResponderTerminationRequest_DEPRECATED:
|
|
onResponderTerminationRequest,
|
|
onStartShouldSetResponder_DEPRECATED: onStartShouldSetResponder,
|
|
}
|
|
: null,
|
|
[
|
|
initialized,
|
|
isPressable,
|
|
pressRetentionOffset,
|
|
onLongPress,
|
|
onPress,
|
|
onPressIn,
|
|
onPressOut,
|
|
onResponderTerminationRequest,
|
|
onStartShouldSetResponder,
|
|
suppressHighlighting,
|
|
],
|
|
);
|
|
|
|
const eventHandlers = usePressability(config);
|
|
const eventHandlersForText = useMemo(
|
|
() =>
|
|
eventHandlers == null
|
|
? null
|
|
: {
|
|
onResponderGrant(event: PressEvent) {
|
|
eventHandlers.onResponderGrant(event);
|
|
if (onResponderGrant != null) {
|
|
onResponderGrant(event);
|
|
}
|
|
},
|
|
onResponderMove(event: PressEvent) {
|
|
eventHandlers.onResponderMove(event);
|
|
if (onResponderMove != null) {
|
|
onResponderMove(event);
|
|
}
|
|
},
|
|
onResponderRelease(event: PressEvent) {
|
|
eventHandlers.onResponderRelease(event);
|
|
if (onResponderRelease != null) {
|
|
onResponderRelease(event);
|
|
}
|
|
},
|
|
onResponderTerminate(event: PressEvent) {
|
|
eventHandlers.onResponderTerminate(event);
|
|
if (onResponderTerminate != null) {
|
|
onResponderTerminate(event);
|
|
}
|
|
},
|
|
onClick: eventHandlers.onClick,
|
|
onResponderTerminationRequest:
|
|
eventHandlers.onResponderTerminationRequest,
|
|
onStartShouldSetResponder: eventHandlers.onStartShouldSetResponder,
|
|
},
|
|
[
|
|
eventHandlers,
|
|
onResponderGrant,
|
|
onResponderMove,
|
|
onResponderRelease,
|
|
onResponderTerminate,
|
|
],
|
|
);
|
|
|
|
// TODO: Move this processing to the view configuration.
|
|
const selectionColor =
|
|
restProps.selectionColor == null
|
|
? null
|
|
: processColor(restProps.selectionColor);
|
|
|
|
let style = flattenStyle(restProps.style);
|
|
|
|
let _selectable = restProps.selectable;
|
|
if (style?.userSelect != null) {
|
|
_selectable = userSelectToSelectableMap[style.userSelect];
|
|
}
|
|
|
|
if (style?.verticalAlign != null) {
|
|
style = StyleSheet.compose(style, {
|
|
textAlignVertical:
|
|
verticalAlignToTextAlignVerticalMap[style.verticalAlign],
|
|
});
|
|
}
|
|
|
|
if (__DEV__) {
|
|
if (PressabilityDebug.isEnabled() && onPress != null) {
|
|
style = StyleSheet.compose(restProps.style, {
|
|
color: 'magenta',
|
|
});
|
|
}
|
|
}
|
|
|
|
let numberOfLines = restProps.numberOfLines;
|
|
if (numberOfLines != null && !(numberOfLines >= 0)) {
|
|
console.error(
|
|
`'numberOfLines' in <Text> must be a non-negative number, received: ${numberOfLines}. The value will be set to 0.`,
|
|
);
|
|
numberOfLines = 0;
|
|
}
|
|
|
|
const hasTextAncestor = useContext(TextAncestor);
|
|
|
|
const _accessible = Platform.select({
|
|
ios: accessible !== false,
|
|
default: accessible,
|
|
});
|
|
|
|
let flattenedStyle = flattenStyle(style);
|
|
|
|
if (typeof flattenedStyle?.fontWeight === 'number') {
|
|
flattenedStyle.fontWeight = flattenedStyle?.fontWeight.toString();
|
|
}
|
|
|
|
const _hasOnPressOrOnLongPress =
|
|
props.onPress != null || props.onLongPress != null;
|
|
|
|
return hasTextAncestor ? (
|
|
<NativeVirtualText
|
|
{...restProps}
|
|
accessibilityState={_accessibilityState}
|
|
{...eventHandlersForText}
|
|
accessibilityLabel={ariaLabel ?? accessibilityLabel}
|
|
accessibilityRole={
|
|
role ? getAccessibilityRoleFromRole(role) : accessibilityRole
|
|
}
|
|
isHighlighted={isHighlighted}
|
|
isPressable={isPressable}
|
|
selectable={_selectable}
|
|
nativeID={id ?? nativeID}
|
|
numberOfLines={numberOfLines}
|
|
selectionColor={selectionColor}
|
|
style={flattenedStyle}
|
|
ref={forwardedRef}
|
|
/>
|
|
) : (
|
|
<TextAncestor.Provider value={true}>
|
|
<NativeText
|
|
{...restProps}
|
|
{...eventHandlersForText}
|
|
disabled={_disabled}
|
|
selectable={_selectable}
|
|
accessible={
|
|
accessible == null && Platform.OS === 'android'
|
|
? _hasOnPressOrOnLongPress
|
|
: _accessible
|
|
}
|
|
accessibilityLabel={ariaLabel ?? accessibilityLabel}
|
|
accessibilityState={nativeTextAccessibilityState}
|
|
accessibilityRole={
|
|
role ? getAccessibilityRoleFromRole(role) : accessibilityRole
|
|
}
|
|
allowFontScaling={allowFontScaling !== false}
|
|
ellipsizeMode={ellipsizeMode ?? 'tail'}
|
|
isHighlighted={isHighlighted}
|
|
nativeID={id ?? nativeID}
|
|
numberOfLines={numberOfLines}
|
|
selectionColor={selectionColor}
|
|
style={flattenedStyle}
|
|
ref={forwardedRef}
|
|
/>
|
|
</TextAncestor.Provider>
|
|
);
|
|
});
|
|
|
|
Text.displayName = 'Text';
|
|
|
|
/**
|
|
* Switch to `deprecated-react-native-prop-types` for compatibility with future
|
|
* releases. This is deprecated and will be removed in the future.
|
|
*/
|
|
Text.propTypes = require('deprecated-react-native-prop-types').TextPropTypes;
|
|
|
|
/**
|
|
* Returns false until the first time `newValue` is true, after which this will
|
|
* always return true. This is necessary to lazily initialize `Pressability` so
|
|
* we do not eagerly create one for every pressable `Text` component.
|
|
*/
|
|
function useLazyInitialization(newValue: boolean): boolean {
|
|
const [oldValue, setValue] = useState(newValue);
|
|
if (!oldValue && newValue) {
|
|
setValue(newValue);
|
|
}
|
|
return oldValue;
|
|
}
|
|
|
|
const userSelectToSelectableMap = {
|
|
auto: true,
|
|
text: true,
|
|
none: false,
|
|
contain: true,
|
|
all: true,
|
|
};
|
|
|
|
const verticalAlignToTextAlignVerticalMap = {
|
|
auto: 'auto',
|
|
top: 'top',
|
|
bottom: 'bottom',
|
|
middle: 'center',
|
|
};
|
|
|
|
module.exports = Text;
|