mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
feat: add userSelect style equivalent to selectable (#34575)
Summary: This adds support for the `userSelect` style attribute, mapping the already existing selectable attribute as requested on https://github.com/facebook/react-native/issues/34425. This PR also updates the TextExample.android and TestExample.ios on the RNTester in order to facilitate the manual QA of this. ## Changelog [General] [Added] - Add support for `userSelect` style Pull Request resolved: https://github.com/facebook/react-native/pull/34575 Test Plan: - open the RNTester app and navigate to the Text page - Check the `Selectable Text` through the Selectable text section <Image src="https://user-images.githubusercontent.com/22423684/188112863-65acd145-76b0-47ba-8bc6-f72298077096.png" height="600" width="300" /> Reviewed By: yungsters Differential Revision: D39252798 Pulled By: jacdebug fbshipit-source-id: f7fabf20ee68778d75461f511c56f94d0d756d9c
This commit is contained in:
committed by
Facebook GitHub Bot
parent
62f83a9fad
commit
fc42d5bbb9
@@ -134,6 +134,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
|
|||||||
textShadowOffset: true,
|
textShadowOffset: true,
|
||||||
textShadowRadius: true,
|
textShadowRadius: true,
|
||||||
textTransform: true,
|
textTransform: true,
|
||||||
|
userSelect: true,
|
||||||
writingDirection: true,
|
writingDirection: true,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -632,6 +632,7 @@ export type ____TextStyle_InternalCore = $ReadOnly<{
|
|||||||
textDecorationStyle?: 'solid' | 'double' | 'dotted' | 'dashed',
|
textDecorationStyle?: 'solid' | 'double' | 'dotted' | 'dashed',
|
||||||
textDecorationColor?: ____ColorValue_Internal,
|
textDecorationColor?: ____ColorValue_Internal,
|
||||||
textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase',
|
textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase',
|
||||||
|
userSelect?: 'auto' | 'text' | 'none' | 'contain' | 'all',
|
||||||
writingDirection?: 'auto' | 'ltr' | 'rtl',
|
writingDirection?: 'auto' | 'ltr' | 'rtl',
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
|
|||||||
+18
-1
@@ -20,6 +20,7 @@ import {NativeText, NativeVirtualText} from './TextNativeComponent';
|
|||||||
import {type TextProps} from './TextProps';
|
import {type TextProps} from './TextProps';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import {useContext, useMemo, useState} from 'react';
|
import {useContext, useMemo, useState} from 'react';
|
||||||
|
import flattenStyle from '../StyleSheet/flattenStyle';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text is the fundamental component for displaying text.
|
* Text is the fundamental component for displaying text.
|
||||||
@@ -152,7 +153,13 @@ const Text: React.AbstractComponent<
|
|||||||
? null
|
? null
|
||||||
: processColor(restProps.selectionColor);
|
: processColor(restProps.selectionColor);
|
||||||
|
|
||||||
let style = restProps.style;
|
let style = flattenStyle(restProps.style);
|
||||||
|
|
||||||
|
let _selectable = restProps.selectable;
|
||||||
|
if (style?.userSelect != null) {
|
||||||
|
_selectable = userSelectToSelectableMap[style.userSelect];
|
||||||
|
}
|
||||||
|
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
if (PressabilityDebug.isEnabled() && onPress != null) {
|
if (PressabilityDebug.isEnabled() && onPress != null) {
|
||||||
style = StyleSheet.compose(restProps.style, {
|
style = StyleSheet.compose(restProps.style, {
|
||||||
@@ -182,6 +189,7 @@ const Text: React.AbstractComponent<
|
|||||||
{...eventHandlersForText}
|
{...eventHandlersForText}
|
||||||
isHighlighted={isHighlighted}
|
isHighlighted={isHighlighted}
|
||||||
isPressable={isPressable}
|
isPressable={isPressable}
|
||||||
|
selectable={_selectable}
|
||||||
numberOfLines={numberOfLines}
|
numberOfLines={numberOfLines}
|
||||||
selectionColor={selectionColor}
|
selectionColor={selectionColor}
|
||||||
style={style}
|
style={style}
|
||||||
@@ -193,6 +201,7 @@ const Text: React.AbstractComponent<
|
|||||||
{...restProps}
|
{...restProps}
|
||||||
{...eventHandlersForText}
|
{...eventHandlersForText}
|
||||||
disabled={_disabled}
|
disabled={_disabled}
|
||||||
|
selectable={_selectable}
|
||||||
accessible={_accessible}
|
accessible={_accessible}
|
||||||
accessibilityState={_accessibilityState}
|
accessibilityState={_accessibilityState}
|
||||||
allowFontScaling={allowFontScaling !== false}
|
allowFontScaling={allowFontScaling !== false}
|
||||||
@@ -222,4 +231,12 @@ function useLazyInitialization(newValue: boolean): boolean {
|
|||||||
return oldValue;
|
return oldValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const userSelectToSelectableMap = {
|
||||||
|
auto: true,
|
||||||
|
text: true,
|
||||||
|
none: false,
|
||||||
|
contain: true,
|
||||||
|
all: true,
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Text;
|
module.exports = Text;
|
||||||
|
|||||||
@@ -982,4 +982,14 @@ exports.examples = [
|
|||||||
return <TextBaseLineLayoutExample />;
|
return <TextBaseLineLayoutExample />;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: 'Selectable Text',
|
||||||
|
render: function (): React.Node {
|
||||||
|
return (
|
||||||
|
<View>
|
||||||
|
<Text style={{userSelect: 'auto'}}>Text element is selectable</Text>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1227,4 +1227,14 @@ exports.examples = [
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: 'Selectable Text',
|
||||||
|
render: function (): React.Node {
|
||||||
|
return (
|
||||||
|
<View>
|
||||||
|
<Text style={{userSelect: 'auto'}}>Text element is selectable</Text>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user