196 lines
6.3 KiB
JavaScript
196 lines
6.3 KiB
JavaScript
/**
|
|
* Copyright (c) Nicolas Gallagher.
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
*
|
|
*/
|
|
import * as React from 'react';
|
|
import { forwardRef, useContext, useRef } from 'react';
|
|
import createElement from '../createElement';
|
|
import css from '../StyleSheet/css';
|
|
import pick from '../../modules/pick';
|
|
import useElementLayout from '../../hooks/useElementLayout';
|
|
import useMergeRefs from '../../modules/useMergeRefs';
|
|
import usePlatformMethods from '../../hooks/usePlatformMethods';
|
|
import useResponderEvents from '../../hooks/useResponderEvents';
|
|
import StyleSheet from '../StyleSheet';
|
|
import TextAncestorContext from './TextAncestorContext';
|
|
var forwardPropsList = {
|
|
accessibilityLabel: true,
|
|
accessibilityLiveRegion: true,
|
|
accessibilityRole: true,
|
|
accessibilityState: true,
|
|
accessibilityValue: true,
|
|
accessible: true,
|
|
children: true,
|
|
classList: true,
|
|
dir: true,
|
|
importantForAccessibility: true,
|
|
lang: true,
|
|
nativeID: true,
|
|
onBlur: true,
|
|
onClick: true,
|
|
onClickCapture: true,
|
|
onContextMenu: true,
|
|
onFocus: true,
|
|
onKeyDown: true,
|
|
onKeyUp: true,
|
|
onTouchCancel: true,
|
|
onTouchCancelCapture: true,
|
|
onTouchEnd: true,
|
|
onTouchEndCapture: true,
|
|
onTouchMove: true,
|
|
onTouchMoveCapture: true,
|
|
onTouchStart: true,
|
|
onTouchStartCapture: true,
|
|
pointerEvents: true,
|
|
ref: true,
|
|
style: true,
|
|
testID: true,
|
|
// unstable
|
|
dataSet: true,
|
|
onMouseDown: true,
|
|
onMouseEnter: true,
|
|
onMouseLeave: true,
|
|
onMouseMove: true,
|
|
onMouseOver: true,
|
|
onMouseOut: true,
|
|
onMouseUp: true,
|
|
onScroll: true,
|
|
onWheel: true,
|
|
href: true,
|
|
rel: true,
|
|
target: true
|
|
};
|
|
|
|
var pickProps = function pickProps(props) {
|
|
return pick(props, forwardPropsList);
|
|
};
|
|
|
|
var Text = forwardRef(function (props, forwardedRef) {
|
|
var dir = props.dir,
|
|
numberOfLines = props.numberOfLines,
|
|
onClick = props.onClick,
|
|
onLayout = props.onLayout,
|
|
onPress = props.onPress,
|
|
onMoveShouldSetResponder = props.onMoveShouldSetResponder,
|
|
onMoveShouldSetResponderCapture = props.onMoveShouldSetResponderCapture,
|
|
onResponderEnd = props.onResponderEnd,
|
|
onResponderGrant = props.onResponderGrant,
|
|
onResponderMove = props.onResponderMove,
|
|
onResponderReject = props.onResponderReject,
|
|
onResponderRelease = props.onResponderRelease,
|
|
onResponderStart = props.onResponderStart,
|
|
onResponderTerminate = props.onResponderTerminate,
|
|
onResponderTerminationRequest = props.onResponderTerminationRequest,
|
|
onScrollShouldSetResponder = props.onScrollShouldSetResponder,
|
|
onScrollShouldSetResponderCapture = props.onScrollShouldSetResponderCapture,
|
|
onSelectionChangeShouldSetResponder = props.onSelectionChangeShouldSetResponder,
|
|
onSelectionChangeShouldSetResponderCapture = props.onSelectionChangeShouldSetResponderCapture,
|
|
onStartShouldSetResponder = props.onStartShouldSetResponder,
|
|
onStartShouldSetResponderCapture = props.onStartShouldSetResponderCapture,
|
|
selectable = props.selectable;
|
|
var hasTextAncestor = useContext(TextAncestorContext);
|
|
var hostRef = useRef(null);
|
|
var classList = [classes.text, hasTextAncestor === true && classes.textHasAncestor, numberOfLines === 1 && classes.textOneLine, numberOfLines != null && numberOfLines > 1 && classes.textMultiLine];
|
|
var style = [props.style, numberOfLines != null && numberOfLines > 1 && {
|
|
WebkitLineClamp: numberOfLines
|
|
}, selectable === true && styles.selectable, selectable === false && styles.notSelectable, onPress && styles.pressable];
|
|
useElementLayout(hostRef, onLayout);
|
|
useResponderEvents(hostRef, {
|
|
onMoveShouldSetResponder: onMoveShouldSetResponder,
|
|
onMoveShouldSetResponderCapture: onMoveShouldSetResponderCapture,
|
|
onResponderEnd: onResponderEnd,
|
|
onResponderGrant: onResponderGrant,
|
|
onResponderMove: onResponderMove,
|
|
onResponderReject: onResponderReject,
|
|
onResponderRelease: onResponderRelease,
|
|
onResponderStart: onResponderStart,
|
|
onResponderTerminate: onResponderTerminate,
|
|
onResponderTerminationRequest: onResponderTerminationRequest,
|
|
onScrollShouldSetResponder: onScrollShouldSetResponder,
|
|
onScrollShouldSetResponderCapture: onScrollShouldSetResponderCapture,
|
|
onSelectionChangeShouldSetResponder: onSelectionChangeShouldSetResponder,
|
|
onSelectionChangeShouldSetResponderCapture: onSelectionChangeShouldSetResponderCapture,
|
|
onStartShouldSetResponder: onStartShouldSetResponder,
|
|
onStartShouldSetResponderCapture: onStartShouldSetResponderCapture
|
|
});
|
|
|
|
function handleClick(e) {
|
|
if (onClick != null) {
|
|
onClick(e);
|
|
}
|
|
|
|
if (onClick == null && onPress != null) {
|
|
e.stopPropagation();
|
|
onPress(e);
|
|
}
|
|
}
|
|
|
|
var component = hasTextAncestor ? 'span' : 'div';
|
|
var supportedProps = pickProps(props);
|
|
supportedProps.classList = classList;
|
|
supportedProps.dir = dir; // 'auto' by default allows browsers to infer writing direction (root elements only)
|
|
|
|
if (!hasTextAncestor) {
|
|
supportedProps.dir = dir != null ? dir : 'auto';
|
|
}
|
|
|
|
supportedProps.onClick = handleClick;
|
|
supportedProps.style = style;
|
|
var platformMethodsRef = usePlatformMethods(supportedProps);
|
|
var setRef = useMergeRefs(hostRef, platformMethodsRef, forwardedRef);
|
|
supportedProps.ref = setRef;
|
|
var element = createElement(component, supportedProps);
|
|
return hasTextAncestor ? element : React.createElement(TextAncestorContext.Provider, {
|
|
value: true
|
|
}, element);
|
|
});
|
|
Text.displayName = 'Text';
|
|
var classes = css.create({
|
|
text: {
|
|
border: '0 solid black',
|
|
boxSizing: 'border-box',
|
|
color: 'black',
|
|
display: 'inline',
|
|
font: '14px System',
|
|
margin: 0,
|
|
padding: 0,
|
|
whiteSpace: 'pre-wrap',
|
|
wordWrap: 'break-word'
|
|
},
|
|
textHasAncestor: {
|
|
color: 'inherit',
|
|
font: 'inherit',
|
|
whiteSpace: 'inherit'
|
|
},
|
|
textOneLine: {
|
|
maxWidth: '100%',
|
|
overflow: 'hidden',
|
|
textOverflow: 'ellipsis',
|
|
whiteSpace: 'nowrap'
|
|
},
|
|
// See #13
|
|
textMultiLine: {
|
|
display: '-webkit-box',
|
|
maxWidth: '100%',
|
|
overflow: 'hidden',
|
|
textOverflow: 'ellipsis',
|
|
WebkitBoxOrient: 'vertical'
|
|
}
|
|
});
|
|
var styles = StyleSheet.create({
|
|
notSelectable: {
|
|
userSelect: 'none'
|
|
},
|
|
selectable: {
|
|
userSelect: 'text'
|
|
},
|
|
pressable: {
|
|
cursor: 'pointer'
|
|
}
|
|
});
|
|
export default Text; |