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';
|
|
|
|
|
2016-07-05 13:34:00 +00:00
|
|
|
const NativeMethodsMixin = require('react/lib/NativeMethodsMixin');
|
2015-12-08 07:08:26 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
|
|
|
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
|
|
|
const StyleSheetPropType = require('StyleSheetPropType');
|
|
|
|
const TextStylePropTypes = require('TextStylePropTypes');
|
|
|
|
const Touchable = require('Touchable');
|
|
|
|
|
|
|
|
const createReactNativeComponentClass =
|
2016-07-05 13:34:00 +00:00
|
|
|
require('react/lib/createReactNativeComponentClass');
|
2016-09-01 00:26:51 +00:00
|
|
|
const mergeFast = require('mergeFast');
|
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,
|
2016-06-23 02:07:31 +00:00
|
|
|
selectable: true,
|
2016-08-10 18:14:36 +00:00
|
|
|
adjustsFontSizeToFit: true,
|
|
|
|
minimumFontScale: 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.
|
|
|
|
*
|
|
|
|
* In the following example, the nested title and body text will inherit the `fontFamily` from
|
|
|
|
*`styles.baseText`, but the title provides its own additional styles. The title and body will
|
|
|
|
* stack on top of each other on account of the literal newlines:
|
|
|
|
*
|
|
|
|
* ```ReactNativeWebPlayer
|
|
|
|
* import React, { Component } from 'react';
|
|
|
|
* import { AppRegistry, Text, StyleSheet } from 'react-native';
|
|
|
|
*
|
|
|
|
* class TextInANest extends Component {
|
|
|
|
* 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
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // App registration and rendering
|
|
|
|
* AppRegistry.registerComponent('TextInANest', () => TextInANest);
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
|
2015-12-08 07:08:26 +00:00
|
|
|
const Text = React.createClass({
|
2015-02-20 04:10:52 +00:00
|
|
|
propTypes: {
|
2016-06-10 11:26:45 +00:00
|
|
|
/**
|
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`.
|
|
|
|
*
|
|
|
|
* `numberOfLines` must be set in conjunction with this prop.
|
|
|
|
*
|
|
|
|
* > `clip` is working only for iOS
|
2016-06-10 11:26:45 +00:00
|
|
|
*/
|
2016-07-25 20:05:14 +00:00
|
|
|
ellipsizeMode: React.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
|
|
|
*/
|
|
|
|
numberOfLines: React.PropTypes.number,
|
|
|
|
/**
|
2015-07-27 13:22:44 +00:00
|
|
|
* Invoked on mount and layout changes with
|
|
|
|
*
|
|
|
|
* `{nativeEvent: {layout: {x, y, width, height}}}`
|
|
|
|
*/
|
|
|
|
onLayout: React.PropTypes.func,
|
|
|
|
/**
|
|
|
|
* This function is called on press.
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
|
|
|
* e.g., `onPress={() => console.log('1st')}``
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
onPress: React.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
|
|
|
*
|
|
|
|
* e.g., `onLongPress={this.increaseSize}>``
|
2016-04-24 01:13:41 +00:00
|
|
|
*/
|
|
|
|
onLongPress: React.PropTypes.func,
|
2016-06-23 02:07:31 +00:00
|
|
|
/**
|
|
|
|
* Lets the user select text, to use the native copy and paste functionality.
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
2016-06-23 02:07:31 +00:00
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
selectable: React.PropTypes.bool,
|
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.
|
2016-06-28 14:34:58 +00:00
|
|
|
*
|
2015-07-27 13:22:44 +00:00
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
suppressHighlighting: React.PropTypes.bool,
|
|
|
|
style: stylePropType,
|
2015-03-06 15:04:20 +00:00
|
|
|
/**
|
|
|
|
* Used to locate this view in end-to-end tests.
|
|
|
|
*/
|
|
|
|
testID: React.PropTypes.string,
|
2015-07-31 14:37:12 +00:00
|
|
|
/**
|
2016-06-28 14:34:58 +00:00
|
|
|
* Specifies whether fonts should scale to respect Text Size accessibility setting on iOS. The
|
|
|
|
* default is `true`.
|
|
|
|
*
|
2016-01-23 19:00:39 +00:00
|
|
|
* @platform ios
|
2015-07-31 14:37:12 +00:00
|
|
|
*/
|
|
|
|
allowFontScaling: React.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
|
|
|
|
* [Accessibility guide](/react-native/docs/accessibility.html#accessible-ios-android)
|
|
|
|
* for more information.
|
|
|
|
*/
|
2016-08-10 18:14:36 +00:00
|
|
|
accessible: React.PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Specifies whether font should be scaled down automatically to fit given style constraints.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
adjustsFontSizeToFit: React.PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies smallest possible scale a font can reach when adjustsFontSizeToFit is enabled. (values 0.01-1.0).
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
minimumFontScale: React.PropTypes.number,
|
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: {
|
|
|
|
isInAParentText: React.PropTypes.bool
|
|
|
|
},
|
2015-12-08 07:08:26 +00:00
|
|
|
contextTypes: {
|
|
|
|
isInAParentText: React.PropTypes.bool
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* 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 &&
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-30 12:35:45 +00:00
|
|
|
this.touchableHandlePress = (e: SyntheticEvent) => {
|
|
|
|
this.props.onPress && this.props.onPress(e);
|
2015-12-08 07:08:26 +00:00
|
|
|
};
|
|
|
|
|
2016-08-30 12:35:45 +00:00
|
|
|
this.touchableHandleLongPress = (e: SyntheticEvent) => {
|
|
|
|
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 {
|
|
|
|
return PRESS_RECT_OFFSET;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return setResponder;
|
|
|
|
},
|
2015-12-10 01:16:29 +00:00
|
|
|
onResponderGrant: function(e: SyntheticEvent, dispatchID: string) {
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderGrant(e, dispatchID);
|
|
|
|
this.props.onResponderGrant &&
|
|
|
|
this.props.onResponderGrant.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
|
|
|
onResponderMove: function(e: SyntheticEvent) {
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderMove(e);
|
|
|
|
this.props.onResponderMove &&
|
|
|
|
this.props.onResponderMove.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
|
|
|
onResponderRelease: function(e: SyntheticEvent) {
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderRelease(e);
|
|
|
|
this.props.onResponderRelease &&
|
|
|
|
this.props.onResponderRelease.apply(this, arguments);
|
2015-12-10 01:16:29 +00:00
|
|
|
}.bind(this),
|
|
|
|
onResponderTerminate: function(e: SyntheticEvent) {
|
2015-12-08 07:08:26 +00:00
|
|
|
this.touchableHandleResponderTerminate(e);
|
|
|
|
this.props.onResponderTerminate &&
|
|
|
|
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
|
|
|
|
var allowTermination = this.touchableHandleResponderTerminationRequest();
|
|
|
|
if (allowTermination && this.props.onResponderTerminationRequest) {
|
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
|
|
|
}
|
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};
|
|
|
|
|
2015-05-08 16:45:43 +00:00
|
|
|
var RCTText = createReactNativeComponentClass(viewConfig);
|
2015-05-29 20:18:39 +00:00
|
|
|
var RCTVirtualText = RCTText;
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
RCTVirtualText = createReactNativeComponentClass({
|
2016-09-01 00:26:51 +00:00
|
|
|
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, {
|
2015-05-29 20:18:39 +00:00
|
|
|
isHighlighted: true,
|
|
|
|
}),
|
|
|
|
uiViewClassName: 'RCTVirtualText',
|
|
|
|
});
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
module.exports = Text;
|