2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule Text
|
2015-03-24 23:37:54 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-01-18 20:53:58 +00:00
|
|
|
const ColorPropType = require('ColorPropType');
|
2016-12-19 23:51:01 +00:00
|
|
|
const EdgeInsetsPropType = require('EdgeInsetsPropType');
|
2016-11-04 12:40:26 +00:00
|
|
|
const NativeMethodsMixin = require('NativeMethodsMixin');
|
2015-12-08 07:08:26 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
const PropTypes = require('prop-types');
|
2015-12-08 07:08:26 +00:00
|
|
|
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
|
|
|
const StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
const TextStylePropTypes = require('TextStylePropTypes');
|
|
|
|
const Touchable = require('Touchable');
|
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
const createReactClass = require('create-react-class');
|
2016-11-04 12:40:26 +00:00
|
|
|
const createReactNativeComponentClass = require('createReactNativeComponentClass');
|
2016-09-01 00:26:51 +00:00
|
|
|
const mergeFast = require('mergeFast');
|
2017-04-07 18:47:35 +00:00
|
|
|
const processColor = require('processColor');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-12-08 07:08:26 +00:00
|
|
|
const stylePropType = StyleSheetPropType(TextStylePropTypes);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-12-08 07:08:26 +00:00
|
|
|
const viewConfig = {
|
2016-09-01 00:26:51 +00:00
|
|
|
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, {
|
2015-02-20 04:10:52 +00:00
|
|
|
isHighlighted: true,
|
|
|
|
numberOfLines: true,
|
2016-07-25 20:05:14 +00:00
|
|
|
ellipsizeMode: true,
|
2015-07-31 14:37:12 +00:00
|
|
|
allowFontScaling: true,
|
2017-04-22 00:49:52 +00:00
|
|
|
disabled: true,
|
2016-06-23 02:07:31 +00:00
|
|
|
selectable: true,
|
2017-01-18 20:53:58 +00:00
|
|
|
selectionColor: true,
|
2016-08-10 18:14:36 +00:00
|
|
|
adjustsFontSizeToFit: true,
|
|
|
|
minimumFontScale: true,
|
2016-12-16 09:19:16 +00:00
|
|
|
textBreakStrategy: true,
|
2015-02-20 04:10:52 +00:00
|
|
|
}),
|
|
|
|
uiViewClassName: 'RCTText',
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-06-28 14:34:58 +00:00
|
|
|
* A React component for displaying text.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2016-06-28 14:34:58 +00:00
|
|
|
* `Text` supports nesting, styling, and touch handling.
|
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* In the following example, the nested title and body text will inherit the
|
2017-09-15 01:01:25 +00:00
|
|
|
* `fontFamily` from `styles.baseText`, but the title provides its own
|
2017-10-10 20:02:19 +00:00
|
|
|
* additional styles. The title and body will stack on top of each other on
|
2017-08-04 23:14:25 +00:00
|
|
|
* account of the literal newlines:
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
|
|
|
* ```ReactNativeWebPlayer
|
|
|
|
* import React, { Component } from 'react';
|
|
|
|
* import { AppRegistry, Text, StyleSheet } from 'react-native';
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* export default class TextInANest extends Component {
|
2016-06-28 14:34:58 +00:00
|
|
|
* constructor(props) {
|
|
|
|
* super(props);
|
|
|
|
* this.state = {
|
|
|
|
* titleText: "Bird's Nest",
|
|
|
|
* bodyText: 'This is not really a bird nest.'
|
|
|
|
* };
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <Text style={styles.baseText}>
|
|
|
|
* <Text style={styles.titleText} onPress={this.onPressTitle}>
|
2016-10-11 22:00:44 +00:00
|
|
|
* {this.state.titleText}{'\n'}{'\n'}
|
2016-06-28 14:34:58 +00:00
|
|
|
* </Text>
|
|
|
|
* <Text numberOfLines={5}>
|
|
|
|
* {this.state.bodyText}
|
|
|
|
* </Text>
|
2015-03-09 16:28:51 +00:00
|
|
|
* </Text>
|
2016-06-28 14:34:58 +00:00
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const styles = StyleSheet.create({
|
2015-03-09 16:28:51 +00:00
|
|
|
* baseText: {
|
|
|
|
* fontFamily: 'Cochin',
|
2015-02-20 04:10:52 +00:00
|
|
|
* },
|
2015-03-09 16:28:51 +00:00
|
|
|
* titleText: {
|
|
|
|
* fontSize: 20,
|
|
|
|
* fontWeight: 'bold',
|
|
|
|
* },
|
2016-06-28 14:34:58 +00:00
|
|
|
* });
|
|
|
|
*
|
2017-05-17 06:43:46 +00:00
|
|
|
* // skip this line if using Create React Native App
|
2016-06-28 14:34:58 +00:00
|
|
|
* AppRegistry.registerComponent('TextInANest', () => TextInANest);
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ## Nested text
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* Both iOS and Android allow you to display formatted text by annotating
|
|
|
|
* ranges of a string with specific formatting like bold or colored text
|
|
|
|
* (`NSAttributedString` on iOS, `SpannableString` on Android). In practice,
|
|
|
|
* this is very tedious. For React Native, we decided to use web paradigm for
|
2017-08-04 23:14:25 +00:00
|
|
|
* this where you can nest text to achieve the same effect.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```ReactNativeWebPlayer
|
|
|
|
* import React, { Component } from 'react';
|
|
|
|
* import { AppRegistry, Text } from 'react-native';
|
|
|
|
*
|
|
|
|
* export default class BoldAndBeautiful extends Component {
|
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <Text style={{fontWeight: 'bold'}}>
|
|
|
|
* I am bold
|
|
|
|
* <Text style={{color: 'red'}}>
|
|
|
|
* and red
|
|
|
|
* </Text>
|
|
|
|
* </Text>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* // skip this line if using Create React Native App
|
|
|
|
* AppRegistry.registerComponent('AwesomeProject', () => BoldAndBeautiful);
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* Behind the scenes, React Native converts this to a flat `NSAttributedString`
|
2017-08-04 23:14:25 +00:00
|
|
|
* or `SpannableString` that contains the following information:
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```javascript
|
|
|
|
* "I am bold and red"
|
|
|
|
* 0-9: bold
|
|
|
|
* 9-17: bold, red
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ## Nested views (iOS only)
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* On iOS, you can nest views within your Text component. Here's an example:
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```ReactNativeWebPlayer
|
|
|
|
* import React, { Component } from 'react';
|
|
|
|
* import { AppRegistry, Text, View } from 'react-native';
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* export default class BlueIsCool extends Component {
|
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <Text>
|
|
|
|
* There is a blue square
|
|
|
|
* <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
|
|
|
|
* in between my text.
|
|
|
|
* </Text>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* // skip this line if using Create React Native App
|
|
|
|
* AppRegistry.registerComponent('AwesomeProject', () => BlueIsCool);
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* > In order to use this feature, you must give the view a `width` and a `height`.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ## Containers
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* The `<Text>` element is special relative to layout: everything inside is no
|
|
|
|
* longer using the flexbox layout but using text layout. This means that
|
|
|
|
* elements inside of a `<Text>` are no longer rectangles, but wrap when they
|
2017-08-04 23:14:25 +00:00
|
|
|
* see the end of the line.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```javascript
|
|
|
|
* <Text>
|
|
|
|
* <Text>First part and </Text>
|
|
|
|
* <Text>second part</Text>
|
|
|
|
* </Text>
|
|
|
|
* // Text container: all the text flows as if it was one
|
|
|
|
* // |First part |
|
|
|
|
* // |and second |
|
|
|
|
* // |part |
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* <View>
|
|
|
|
* <Text>First part and </Text>
|
|
|
|
* <Text>second part</Text>
|
|
|
|
* </View>
|
|
|
|
* // View container: each text is its own block
|
|
|
|
* // |First part |
|
|
|
|
* // |and |
|
|
|
|
* // |second part|
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ## Limited Style Inheritance
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* On the web, the usual way to set a font family and size for the entire
|
2017-08-04 23:14:25 +00:00
|
|
|
* document is to take advantage of inherited CSS properties like so:
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```css
|
|
|
|
* html {
|
|
|
|
* font-family: 'lucida grande', tahoma, verdana, arial, sans-serif;
|
|
|
|
* font-size: 11px;
|
|
|
|
* color: #141823;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2017-09-15 01:01:25 +00:00
|
|
|
* All elements in the document will inherit this font unless they or one of
|
2017-08-04 23:14:25 +00:00
|
|
|
* their parents specifies a new rule.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* In React Native, we are more strict about it: **you must wrap all the text
|
2017-08-04 23:14:25 +00:00
|
|
|
* nodes inside of a `<Text>` component**. You cannot have a text node directly
|
|
|
|
* under a `<View>`.
|
|
|
|
*
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```javascript
|
|
|
|
* // BAD: will raise exception, can't have a text node as child of a <View>
|
|
|
|
* <View>
|
|
|
|
* Some text
|
|
|
|
* </View>
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* // GOOD
|
|
|
|
* <View>
|
|
|
|
* <Text>
|
|
|
|
* Some text
|
|
|
|
* </Text>
|
|
|
|
* </View>
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* You also lose the ability to set up a default font for an entire subtree.
|
|
|
|
* The recommended way to use consistent fonts and sizes across your
|
|
|
|
* application is to create a component `MyAppText` that includes them and use
|
|
|
|
* this component across your app. You can also use this component to make more
|
2017-08-04 23:14:25 +00:00
|
|
|
* specific components like `MyAppHeaderText` for other kinds of text.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```javascript
|
|
|
|
* <View>
|
|
|
|
* <MyAppText>Text styled with the default font for the entire application</MyAppText>
|
|
|
|
* <MyAppHeaderText>Text styled as a header</MyAppHeaderText>
|
|
|
|
* </View>
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* Assuming that `MyAppText` is a component that simply renders out its
|
|
|
|
* children into a `Text` component with styling, then `MyAppHeaderText` can be
|
2017-08-04 23:14:25 +00:00
|
|
|
* defined as follows:
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```javascript
|
|
|
|
* class MyAppHeaderText extends Component {
|
|
|
|
* render() {
|
|
|
|
* <MyAppText>
|
|
|
|
* <Text style={{fontSize: 20}}>
|
|
|
|
* {this.props.children}
|
|
|
|
* </Text>
|
|
|
|
* </MyAppText>
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* Composing `MyAppText` in this way ensures that we get the styles from a
|
|
|
|
* top-level component, but leaves us the ability to add / override them in
|
2017-08-04 23:14:25 +00:00
|
|
|
* specific use cases.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* React Native still has the concept of style inheritance, but limited to text
|
2017-08-04 23:14:25 +00:00
|
|
|
* subtrees. In this case, the second part will be both bold and red.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* ```javascript
|
|
|
|
* <Text style={{fontWeight: 'bold'}}>
|
|
|
|
* I am bold
|
|
|
|
* <Text style={{color: 'red'}}>
|
|
|
|
* and red
|
|
|
|
* </Text>
|
|
|
|
* </Text>
|
|
|
|
* ```
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2017-08-04 23:14:25 +00:00
|
|
|
* We believe that this more constrained way to style text will yield better
|
|
|
|
* apps:
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* - (Developer) React components are designed with strong isolation in mind:
|
|
|
|
* You should be able to drop a component anywhere in your application,
|
|
|
|
* trusting that as long as the props are the same, it will look and behave the
|
|
|
|
* same way. Text properties that could inherit from outside of the props would
|
2017-08-04 23:14:25 +00:00
|
|
|
* break this isolation.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
|
|
|
* - (Implementor) The implementation of React Native is also simplified. We do
|
|
|
|
* not need to have a `fontFamily` field on every single element, and we do not
|
|
|
|
* need to potentially traverse the tree up to the root every time we display a
|
|
|
|
* text node. The style inheritance is only encoded inside of the native Text
|
2017-08-04 23:14:25 +00:00
|
|
|
* component and doesn't leak to other components or the system itself.
|
2017-09-15 01:01:25 +00:00
|
|
|
*
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2017-07-07 21:24:25 +00:00
|
|
|
const Text = createReactClass({
|
|
|
|
displayName: 'Text',
|
2015-02-20 04:10:52 +00:00
|
|
|
propTypes: {
|
2016-06-10 11:26:45 +00:00
|
|
|
/**
|
2017-02-09 21:02:45 +00:00
|
|
|
* When `numberOfLines` is set, this prop defines how text will be truncated.
|
|
|
|
* `numberOfLines` must be set in conjunction with this prop.
|
|
|
|
*
|
2016-07-25 20:05:14 +00:00
|
|
|
* This can be one of the following values:
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
|
|
|
* - `head` - The line is displayed so that the end fits in the container and the missing text
|
|
|
|
* at the beginning of the line is indicated by an ellipsis glyph. e.g., "...wxyz"
|
|
|
|
* - `middle` - The line is displayed so that the beginning and end fit in the container and the
|
|
|
|
* missing text in the middle is indicated by an ellipsis glyph. "ab...yz"
|
|
|
|
* - `tail` - The line is displayed so that the beginning fits in the container and the
|
|
|
|
* missing text at the end of the line is indicated by an ellipsis glyph. e.g., "abcd..."
|
|
|
|
* - `clip` - Lines are not drawn past the edge of the text container.
|
|
|
|
*
|
|
|
|
* The default is `tail`.
|
|
|
|
*
|
|
|
|
* > `clip` is working only for iOS
|
2016-06-10 11:26:45 +00:00
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
ellipsizeMode: PropTypes.oneOf(['head', 'middle', 'tail', 'clip']),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-12-15 17:08:39 +00:00
|
|
|
* Used to truncate the text with an ellipsis after computing the text
|
2015-07-27 13:22:44 +00:00
|
|
|
* layout, including line wrapping, such that the total number of lines
|
|
|
|
* does not exceed this number.
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
2016-07-25 20:05:14 +00:00
|
|
|
* This prop is commonly used with `ellipsizeMode`.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
numberOfLines: PropTypes.number,
|
2016-12-16 09:19:16 +00:00
|
|
|
/**
|
|
|
|
* Set text break strategy on Android API Level 23+, possible values are `simple`, `highQuality`, `balanced`
|
|
|
|
* The default value is `highQuality`.
|
|
|
|
* @platform android
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
textBreakStrategy: PropTypes.oneOf(['simple', 'highQuality', 'balanced']),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-07-27 13:22:44 +00:00
|
|
|
* Invoked on mount and layout changes with
|
|
|
|
*
|
|
|
|
* `{nativeEvent: {layout: {x, y, width, height}}}`
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
onLayout: PropTypes.func,
|
2015-07-27 13:22:44 +00:00
|
|
|
/**
|
|
|
|
* This function is called on press.
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
2017-02-13 21:31:07 +00:00
|
|
|
* e.g., `onPress={() => console.log('1st')}`
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
onPress: PropTypes.func,
|
2016-04-24 01:13:41 +00:00
|
|
|
/**
|
|
|
|
* This function is called on long press.
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
2017-02-13 21:31:07 +00:00
|
|
|
* e.g., `onLongPress={this.increaseSize}>`
|
2016-04-24 01:13:41 +00:00
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
onLongPress: PropTypes.func,
|
2016-12-19 23:51:01 +00:00
|
|
|
/**
|
|
|
|
* When the scroll view is disabled, this defines how far your touch may
|
|
|
|
* move off of the button, before deactivating the button. Once deactivated,
|
|
|
|
* try moving it back and you'll see that the button is once again
|
|
|
|
* reactivated! Move it back and forth several times while the scroll view
|
|
|
|
* is disabled. Ensure you pass in a constant to reduce memory allocations.
|
|
|
|
*/
|
|
|
|
pressRetentionOffset: EdgeInsetsPropType,
|
2016-06-23 02:07:31 +00:00
|
|
|
/**
|
|
|
|
* Lets the user select text, to use the native copy and paste functionality.
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
selectable: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* The highlight color of the text.
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
selectionColor: ColorPropType,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2016-06-28 14:34:58 +00:00
|
|
|
* When `true`, no visual change is made when text is pressed down. By
|
2015-02-20 04:10:52 +00:00
|
|
|
* default, a gray oval highlights the text on press down.
|
2015-07-27 13:22:44 +00:00
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
suppressHighlighting: PropTypes.bool,
|
2015-02-20 04:10:52 +00:00
|
|
|
style: stylePropType,
|
2015-03-06 15:04:20 +00:00
|
|
|
/**
|
|
|
|
* Used to locate this view in end-to-end tests.
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
testID: PropTypes.string,
|
2017-04-07 18:47:35 +00:00
|
|
|
/**
|
|
|
|
* Used to locate this view from native code.
|
|
|
|
*/
|
|
|
|
nativeID: PropTypes.string,
|
2015-07-31 14:37:12 +00:00
|
|
|
/**
|
2017-01-30 20:48:58 +00:00
|
|
|
* Specifies whether fonts should scale to respect Text Size accessibility settings. The
|
2016-06-28 14:34:58 +00:00
|
|
|
* default is `true`.
|
2015-07-31 14:37:12 +00:00
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
allowFontScaling: PropTypes.bool,
|
2016-06-28 14:34:58 +00:00
|
|
|
/**
|
|
|
|
* When set to `true`, indicates that the view is an accessibility element. The default value
|
|
|
|
* for a `Text` element is `true`.
|
|
|
|
*
|
|
|
|
* See the
|
2017-01-31 20:07:15 +00:00
|
|
|
* [Accessibility guide](docs/accessibility.html#accessible-ios-android)
|
2016-06-28 14:34:58 +00:00
|
|
|
* for more information.
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
accessible: PropTypes.bool,
|
2016-08-10 18:14:36 +00:00
|
|
|
/**
|
|
|
|
* Specifies whether font should be scaled down automatically to fit given style constraints.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
adjustsFontSizeToFit: PropTypes.bool,
|
2016-08-10 18:14:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies smallest possible scale a font can reach when adjustsFontSizeToFit is enabled. (values 0.01-1.0).
|
|
|
|
* @platform ios
|
|
|
|
*/
|
2017-01-18 20:53:58 +00:00
|
|
|
minimumFontScale: PropTypes.number,
|
2017-04-07 04:21:45 +00:00
|
|
|
/**
|
|
|
|
* Specifies the disabled state of the text view for testing purposes
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
disabled: PropTypes.bool,
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
2015-12-08 07:08:26 +00:00
|
|
|
getDefaultProps(): Object {
|
2015-07-31 14:37:12 +00:00
|
|
|
return {
|
2015-12-08 07:08:26 +00:00
|
|
|
accessible: true,
|
2015-07-31 14:37:12 +00:00
|
|
|
allowFontScaling: true,
|
2016-07-25 20:05:14 +00:00
|
|
|
ellipsizeMode: 'tail',
|
2015-07-31 14:37:12 +00:00
|
|
|
};
|
|
|
|
},
|
2015-12-08 07:08:26 +00:00
|
|
|
getInitialState: function(): Object {
|
2016-09-01 00:26:51 +00:00
|
|
|
return mergeFast(Touchable.Mixin.touchableGetInitialState(), {
|
2015-02-20 04:10:52 +00:00
|
|
|
isHighlighted: false,
|
|
|
|
});
|
|
|
|
},
|
2015-12-08 07:08:26 +00:00
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
viewConfig: viewConfig,
|
|
|
|
getChildContext(): Object {
|
2015-05-29 20:18:39 +00:00
|
|
|
return {isInAParentText: true};
|
|
|
|
},
|
|
|
|
childContextTypes: {
|
2017-01-18 20:53:58 +00:00
|
|
|
isInAParentText: PropTypes.bool
|
2015-05-29 20:18:39 +00:00
|
|
|
},
|
2015-12-08 07:08:26 +00:00
|
|
|
contextTypes: {
|
2017-01-18 20:53:58 +00:00
|
|
|
isInAParentText: PropTypes.bool
|
2015-12-08 07:08:26 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Only assigned if touch is needed.
|
|
|
|
*/
|
|
|
|
_handlers: (null: ?Object),
|
2016-04-24 01:13:41 +00:00
|
|
|
_hasPressHandler(): boolean {
|
|
|
|
return !!this.props.onPress || !!this.props.onLongPress;
|
|
|
|
},
|
2015-12-08 07:08:26 +00:00
|
|
|
/**
|
|
|
|
* These are assigned lazily the first time the responder is set to make plain
|
|
|
|
* text nodes as cheap as possible.
|
|
|
|
*/
|
|
|
|
touchableHandleActivePressIn: (null: ?Function),
|
|
|
|
touchableHandleActivePressOut: (null: ?Function),
|
|
|
|
touchableHandlePress: (null: ?Function),
|
2016-04-24 01:13:41 +00:00
|
|
|
touchableHandleLongPress: (null: ?Function),
|
2015-12-08 07:08:26 +00:00
|
|
|
touchableGetPressRectOffset: (null: ?Function),
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> {
|
2015-12-08 07:08:26 +00:00
|
|
|
let newProps = this.props;
|
2016-04-24 01:13:41 +00:00
|
|
|
if (this.props.onStartShouldSetResponder || this._hasPressHandler()) {
|
2015-12-08 07:08:26 +00:00
|
|
|
if (!this._handlers) {
|
|
|
|
this._handlers = {
|
|
|
|
onStartShouldSetResponder: (): bool => {
|
|
|
|
const shouldSetFromProps = this.props.onStartShouldSetResponder &&
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.props.onStartShouldSetResponder();
|
2016-04-24 01:13:41 +00:00
|
|
|
const setResponder = shouldSetFromProps || this._hasPressHandler();
|
2015-12-08 07:08:26 +00:00
|
|
|
if (setResponder && !this.touchableHandleActivePressIn) {
|
|
|
|
// Attach and bind all the other handlers only the first time a touch
|
|
|
|
// actually happens.
|
2016-04-14 21:27:35 +00:00
|
|
|
for (const key in Touchable.Mixin) {
|
2015-12-08 07:08:26 +00:00
|
|
|
if (typeof Touchable.Mixin[key] === 'function') {
|
|
|
|
(this: any)[key] = Touchable.Mixin[key].bind(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.touchableHandleActivePressIn = () => {
|
2016-04-24 01:13:41 +00:00
|
|
|
if (this.props.suppressHighlighting || !this._hasPressHandler()) {
|
2015-12-08 07:08:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
isHighlighted: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.touchableHandleActivePressOut = () => {
|
2016-04-24 01:13:41 +00:00
|
|
|
if (this.props.suppressHighlighting || !this._hasPressHandler()) {
|
2015-12-08 07:08:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
isHighlighted: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
this.touchableHandlePress = (e: SyntheticEvent<>) => {
|
2016-08-30 12:35:45 +00:00
|
|
|
this.props.onPress && this.props.onPress(e);
|
2015-12-08 07:08:26 +00:00
|
|
|
};
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
this.touchableHandleLongPress = (e: SyntheticEvent<>) => {
|
2016-08-30 12:35:45 +00:00
|
|
|
this.props.onLongPress && this.props.onLongPress(e);
|
2016-04-24 01:13:41 +00:00
|
|
|
};
|
|
|
|
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableGetPressRectOffset = function(): RectOffset {
|
2016-12-19 23:51:01 +00:00
|
|
|
return this.props.pressRetentionOffset || PRESS_RECT_OFFSET;
|
2015-12-08 07:08:26 +00:00
|
|
|
};
|
|
|
|
}
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
return setResponder;
|
|
|
|
},
|
2017-08-18 01:36:54 +00:00
|
|
|
onResponderGrant: function(e: SyntheticEvent<>, dispatchID: string) {
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderGrant(e, dispatchID);
|
|
|
|
this.props.onResponderGrant &&
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.props.onResponderGrant.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
2017-08-18 01:36:54 +00:00
|
|
|
onResponderMove: function(e: SyntheticEvent<>) {
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderMove(e);
|
|
|
|
this.props.onResponderMove &&
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.props.onResponderMove.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
2017-08-18 01:36:54 +00:00
|
|
|
onResponderRelease: function(e: SyntheticEvent<>) {
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderRelease(e);
|
|
|
|
this.props.onResponderRelease &&
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.props.onResponderRelease.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
2017-08-18 01:36:54 +00:00
|
|
|
onResponderTerminate: function(e: SyntheticEvent<>) {
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderTerminate(e);
|
|
|
|
this.props.onResponderTerminate &&
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
this.props.onResponderTerminate.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
|
|
|
onResponderTerminationRequest: function(): bool {
|
2015-12-08 07:08:26 +00:00
|
|
|
// Allow touchable or props.onResponderTerminationRequest to deny
|
|
|
|
// the request
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-08 07:08:26 +00:00
|
|
|
var allowTermination = this.touchableHandleResponderTerminationRequest();
|
|
|
|
if (allowTermination && this.props.onResponderTerminationRequest) {
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-12-10 01:16:29 +00:00
|
|
|
allowTermination = this.props.onResponderTerminationRequest.apply(this, arguments);
|
2015-12-08 07:08:26 +00:00
|
|
|
}
|
|
|
|
return allowTermination;
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
2015-12-08 07:08:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
newProps = {
|
|
|
|
...this.props,
|
|
|
|
...this._handlers,
|
|
|
|
isHighlighted: this.state.isHighlighted,
|
|
|
|
};
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2017-01-18 20:53:58 +00:00
|
|
|
if (newProps.selectionColor != null) {
|
|
|
|
newProps = {
|
|
|
|
...newProps,
|
|
|
|
selectionColor: processColor(newProps.selectionColor)
|
|
|
|
};
|
|
|
|
}
|
2016-04-14 21:27:35 +00:00
|
|
|
if (Touchable.TOUCH_TARGET_DEBUG && newProps.onPress) {
|
|
|
|
newProps = {
|
|
|
|
...newProps,
|
|
|
|
style: [this.props.style, {color: 'magenta'}],
|
|
|
|
};
|
|
|
|
}
|
2015-12-08 07:08:26 +00:00
|
|
|
if (this.context.isInAParentText) {
|
|
|
|
return <RCTVirtualText {...newProps} />;
|
2015-05-29 20:18:39 +00:00
|
|
|
} else {
|
2015-12-08 07:08:26 +00:00
|
|
|
return <RCTText {...newProps} />;
|
2015-05-29 20:18:39 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-03-24 23:37:54 +00:00
|
|
|
type RectOffset = {
|
2016-08-09 13:32:41 +00:00
|
|
|
top: number,
|
|
|
|
left: number,
|
|
|
|
right: number,
|
|
|
|
bottom: number,
|
2015-03-24 23:37:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
var PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
|
|
|
|
|
2017-09-15 01:01:25 +00:00
|
|
|
var RCTText = createReactNativeComponentClass(
|
|
|
|
viewConfig.uiViewClassName,
|
|
|
|
() => viewConfig
|
|
|
|
);
|
2015-05-29 20:18:39 +00:00
|
|
|
var RCTVirtualText = RCTText;
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
2017-09-15 01:01:25 +00:00
|
|
|
RCTVirtualText = createReactNativeComponentClass('RCTVirtualText', () => ({
|
2016-09-01 00:26:51 +00:00
|
|
|
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, {
|
2015-05-29 20:18:39 +00:00
|
|
|
isHighlighted: true,
|
|
|
|
}),
|
|
|
|
uiViewClassName: 'RCTVirtualText',
|
2017-09-15 01:01:25 +00:00
|
|
|
}));
|
2015-05-29 20:18:39 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
module.exports = Text;
|