2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-24 23:37:54 +00:00
|
|
|
* @flow
|
2018-01-15 03:32:24 +00:00
|
|
|
* @format
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2018-05-09 07:47:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-08 07:08:26 +00:00
|
|
|
const React = require('React');
|
2018-01-18 20:01:38 +00:00
|
|
|
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
2018-05-09 07:47:46 +00:00
|
|
|
const TextAncestor = require('TextAncestor');
|
2018-03-11 02:29:05 +00:00
|
|
|
const TextPropTypes = require('TextPropTypes');
|
2015-12-08 07:08:26 +00:00
|
|
|
const Touchable = require('Touchable');
|
2018-01-31 04:17:44 +00:00
|
|
|
const UIManager = require('UIManager');
|
2015-12-08 07:08:26 +00:00
|
|
|
|
2018-01-18 20:01:38 +00:00
|
|
|
const createReactNativeComponentClass = require('createReactNativeComponentClass');
|
2018-05-09 07:47:55 +00:00
|
|
|
const nullthrows = require('fbjs/lib/nullthrows');
|
2017-04-07 18:47:35 +00:00
|
|
|
const processColor = require('processColor');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-03-13 00:39:54 +00:00
|
|
|
import type {PressEvent} from 'CoreEventTypes';
|
2018-05-09 07:47:57 +00:00
|
|
|
import type {NativeComponent} from 'ReactNative';
|
2018-05-09 07:47:54 +00:00
|
|
|
import type {PressRetentionOffset, TextProps} from 'TextProps';
|
2018-03-13 00:39:54 +00:00
|
|
|
|
2018-05-09 07:47:55 +00:00
|
|
|
type ResponseHandlers = $ReadOnly<{|
|
|
|
|
onStartShouldSetResponder: () => boolean,
|
|
|
|
onResponderGrant: (event: SyntheticEvent<>, dispatchID: string) => void,
|
|
|
|
onResponderMove: (event: SyntheticEvent<>) => void,
|
|
|
|
onResponderRelease: (event: SyntheticEvent<>) => void,
|
|
|
|
onResponderTerminate: (event: SyntheticEvent<>) => void,
|
|
|
|
onResponderTerminationRequest: () => boolean,
|
|
|
|
|}>;
|
|
|
|
|
2018-05-09 07:47:57 +00:00
|
|
|
type Props = $ReadOnly<{
|
|
|
|
...TextProps,
|
2018-05-15 00:36:36 +00:00
|
|
|
forwardedRef: ?React.Ref<'RCTText' | 'RCTVirtualText'>,
|
2018-05-09 07:47:57 +00:00
|
|
|
}>;
|
2018-05-09 07:47:55 +00:00
|
|
|
|
2018-05-09 07:47:54 +00:00
|
|
|
type State = {|
|
|
|
|
touchable: {|
|
|
|
|
touchState: ?string,
|
|
|
|
responderID: ?number,
|
|
|
|
|},
|
2018-03-13 00:39:54 +00:00
|
|
|
isHighlighted: boolean,
|
2018-05-09 07:47:55 +00:00
|
|
|
createResponderHandlers: () => ResponseHandlers,
|
|
|
|
responseHandlers: ?ResponseHandlers,
|
2018-05-09 07:47:54 +00:00
|
|
|
|};
|
2018-03-13 00:39:54 +00:00
|
|
|
|
|
|
|
const PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
|
|
|
|
|
2018-01-18 20:01:38 +00:00
|
|
|
const viewConfig = {
|
2018-05-09 07:47:54 +00:00
|
|
|
validAttributes: {
|
|
|
|
...ReactNativeViewAttributes.UIView,
|
2018-01-18 20:01:38 +00:00
|
|
|
isHighlighted: true,
|
|
|
|
numberOfLines: true,
|
|
|
|
ellipsizeMode: true,
|
|
|
|
allowFontScaling: true,
|
|
|
|
disabled: true,
|
|
|
|
selectable: true,
|
|
|
|
selectionColor: true,
|
|
|
|
adjustsFontSizeToFit: true,
|
|
|
|
minimumFontScale: true,
|
|
|
|
textBreakStrategy: true,
|
2018-05-09 07:47:54 +00:00
|
|
|
},
|
2018-01-18 20:01:38 +00:00
|
|
|
uiViewClassName: 'RCTText',
|
|
|
|
};
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2016-06-28 14:34:58 +00:00
|
|
|
* A React component for displaying text.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/text.html
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2018-05-09 07:47:57 +00:00
|
|
|
class TouchableText extends React.Component<Props, State> {
|
2018-03-13 00:39:54 +00:00
|
|
|
static defaultProps = {
|
|
|
|
accessible: true,
|
|
|
|
allowFontScaling: true,
|
|
|
|
ellipsizeMode: 'tail',
|
|
|
|
};
|
|
|
|
|
2018-05-09 07:47:55 +00:00
|
|
|
touchableGetPressRectOffset: ?() => PressRetentionOffset;
|
|
|
|
touchableHandleActivePressIn: ?() => void;
|
|
|
|
touchableHandleActivePressOut: ?() => void;
|
|
|
|
touchableHandleLongPress: ?(event: PressEvent) => void;
|
|
|
|
touchableHandlePress: ?(event: PressEvent) => void;
|
|
|
|
touchableHandleResponderGrant: ?(
|
|
|
|
event: SyntheticEvent<>,
|
|
|
|
dispatchID: string,
|
|
|
|
) => void;
|
|
|
|
touchableHandleResponderMove: ?(event: SyntheticEvent<>) => void;
|
|
|
|
touchableHandleResponderRelease: ?(event: SyntheticEvent<>) => void;
|
|
|
|
touchableHandleResponderTerminate: ?(event: SyntheticEvent<>) => void;
|
|
|
|
touchableHandleResponderTerminationRequest: ?() => boolean;
|
|
|
|
|
2018-05-09 07:47:54 +00:00
|
|
|
state = {
|
|
|
|
...Touchable.Mixin.touchableGetInitialState(),
|
2018-03-13 00:39:54 +00:00
|
|
|
isHighlighted: false,
|
2018-05-09 07:47:55 +00:00
|
|
|
createResponderHandlers: this._createResponseHandlers.bind(this),
|
|
|
|
responseHandlers: null,
|
2018-05-09 07:47:54 +00:00
|
|
|
};
|
2018-03-13 00:39:54 +00:00
|
|
|
|
2018-05-09 07:47:55 +00:00
|
|
|
static getDerivedStateFromProps(nextProps: Props, prevState: State): ?State {
|
|
|
|
return prevState.responseHandlers == null && isTouchable(nextProps)
|
|
|
|
? {
|
|
|
|
...prevState,
|
|
|
|
responseHandlers: prevState.createResponderHandlers(),
|
|
|
|
}
|
|
|
|
: null;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-05-09 07:47:55 +00:00
|
|
|
static viewConfig = viewConfig;
|
2018-03-13 00:39:54 +00:00
|
|
|
|
2018-05-09 07:47:55 +00:00
|
|
|
render(): React.Node {
|
|
|
|
let props = this.props;
|
|
|
|
if (isTouchable(props)) {
|
|
|
|
props = {
|
|
|
|
...props,
|
|
|
|
...this.state.responseHandlers,
|
2015-12-08 07:08:26 +00:00
|
|
|
isHighlighted: this.state.isHighlighted,
|
|
|
|
};
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2018-05-09 07:47:55 +00:00
|
|
|
if (props.selectionColor != null) {
|
|
|
|
props = {
|
|
|
|
...props,
|
|
|
|
selectionColor: processColor(props.selectionColor),
|
2017-01-18 20:53:58 +00:00
|
|
|
};
|
|
|
|
}
|
2018-05-09 07:47:55 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
if (Touchable.TOUCH_TARGET_DEBUG && props.onPress != null) {
|
|
|
|
props = {
|
|
|
|
...props,
|
|
|
|
style: [props.style, {color: 'magenta'}],
|
|
|
|
};
|
|
|
|
}
|
2016-04-14 21:27:35 +00:00
|
|
|
}
|
2018-05-09 07:47:46 +00:00
|
|
|
return (
|
|
|
|
<TextAncestor.Consumer>
|
|
|
|
{hasTextAncestor =>
|
|
|
|
hasTextAncestor ? (
|
2018-05-09 07:47:57 +00:00
|
|
|
<RCTVirtualText {...props} ref={props.forwardedRef} />
|
2018-05-09 07:47:46 +00:00
|
|
|
) : (
|
|
|
|
<TextAncestor.Provider value={true}>
|
2018-05-09 07:47:57 +00:00
|
|
|
<RCTText {...props} ref={props.forwardedRef} />
|
2018-05-09 07:47:46 +00:00
|
|
|
</TextAncestor.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</TextAncestor.Consumer>
|
|
|
|
);
|
2018-03-13 00:39:54 +00:00
|
|
|
}
|
2018-05-09 07:47:55 +00:00
|
|
|
|
|
|
|
_createResponseHandlers(): ResponseHandlers {
|
|
|
|
return {
|
|
|
|
onStartShouldSetResponder: (): boolean => {
|
|
|
|
const {onStartShouldSetResponder} = this.props;
|
|
|
|
const shouldSetResponder =
|
|
|
|
(onStartShouldSetResponder == null
|
|
|
|
? false
|
|
|
|
: onStartShouldSetResponder()) || isTouchable(this.props);
|
|
|
|
|
|
|
|
if (shouldSetResponder) {
|
|
|
|
this._attachTouchHandlers();
|
|
|
|
}
|
|
|
|
return shouldSetResponder;
|
|
|
|
},
|
|
|
|
onResponderGrant: (event: SyntheticEvent<>, dispatchID: string): void => {
|
|
|
|
nullthrows(this.touchableHandleResponderGrant)(event, dispatchID);
|
|
|
|
if (this.props.onResponderGrant != null) {
|
2018-05-21 19:00:01 +00:00
|
|
|
this.props.onResponderGrant.call(this, event, dispatchID);
|
2018-05-09 07:47:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onResponderMove: (event: SyntheticEvent<>): void => {
|
|
|
|
nullthrows(this.touchableHandleResponderMove)(event);
|
|
|
|
if (this.props.onResponderMove != null) {
|
2018-05-21 19:00:01 +00:00
|
|
|
this.props.onResponderMove.call(this, event);
|
2018-05-09 07:47:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onResponderRelease: (event: SyntheticEvent<>): void => {
|
|
|
|
nullthrows(this.touchableHandleResponderRelease)(event);
|
|
|
|
if (this.props.onResponderRelease != null) {
|
2018-05-21 19:00:01 +00:00
|
|
|
this.props.onResponderRelease.call(this, event);
|
2018-05-09 07:47:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onResponderTerminate: (event: SyntheticEvent<>): void => {
|
|
|
|
nullthrows(this.touchableHandleResponderTerminate)(event);
|
|
|
|
if (this.props.onResponderTerminate != null) {
|
2018-05-21 19:00:01 +00:00
|
|
|
this.props.onResponderTerminate.call(this, event);
|
2018-05-09 07:47:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onResponderTerminationRequest: (): boolean => {
|
|
|
|
const {onResponderTerminationRequest} = this.props;
|
|
|
|
if (!nullthrows(this.touchableHandleResponderTerminationRequest)()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (onResponderTerminationRequest == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return onResponderTerminationRequest();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lazily attaches Touchable.Mixin handlers.
|
|
|
|
*/
|
|
|
|
_attachTouchHandlers(): void {
|
|
|
|
if (this.touchableGetPressRectOffset != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const key in Touchable.Mixin) {
|
|
|
|
if (typeof Touchable.Mixin[key] === 'function') {
|
|
|
|
(this: any)[key] = Touchable.Mixin[key].bind(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.touchableHandleActivePressIn = (): void => {
|
|
|
|
if (!this.props.suppressHighlighting && isTouchable(this.props)) {
|
|
|
|
this.setState({isHighlighted: true});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.touchableHandleActivePressOut = (): void => {
|
|
|
|
if (!this.props.suppressHighlighting && isTouchable(this.props)) {
|
|
|
|
this.setState({isHighlighted: false});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.touchableHandlePress = (event: PressEvent): void => {
|
|
|
|
if (this.props.onPress != null) {
|
|
|
|
this.props.onPress(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.touchableHandleLongPress = (event: PressEvent): void => {
|
|
|
|
if (this.props.onLongPress != null) {
|
|
|
|
this.props.onLongPress(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.touchableGetPressRectOffset = (): PressRetentionOffset =>
|
|
|
|
this.props.pressRetentionOffset == null
|
|
|
|
? PRESS_RECT_OFFSET
|
|
|
|
: this.props.pressRetentionOffset;
|
|
|
|
}
|
2018-03-13 00:39:54 +00:00
|
|
|
}
|
2018-01-18 20:01:38 +00:00
|
|
|
|
2018-05-09 07:47:55 +00:00
|
|
|
const isTouchable = (props: Props): boolean =>
|
|
|
|
props.onPress != null ||
|
|
|
|
props.onLongPress != null ||
|
|
|
|
props.onStartShouldSetResponder != null;
|
|
|
|
|
|
|
|
const RCTText = createReactNativeComponentClass(
|
2018-01-18 20:01:38 +00:00
|
|
|
viewConfig.uiViewClassName,
|
|
|
|
() => viewConfig,
|
|
|
|
);
|
2018-05-09 07:47:55 +00:00
|
|
|
|
|
|
|
const RCTVirtualText =
|
|
|
|
UIManager.RCTVirtualText == null
|
|
|
|
? RCTText
|
|
|
|
: createReactNativeComponentClass('RCTVirtualText', () => ({
|
|
|
|
validAttributes: {
|
|
|
|
...ReactNativeViewAttributes.UIView,
|
|
|
|
isHighlighted: true,
|
|
|
|
},
|
|
|
|
uiViewClassName: 'RCTVirtualText',
|
|
|
|
}));
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-05-09 07:47:57 +00:00
|
|
|
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
|
|
|
|
const Text = React.forwardRef((props, ref) => (
|
|
|
|
<TouchableText {...props} forwardedRef={ref} />
|
|
|
|
));
|
|
|
|
Text.displayName = 'Text';
|
|
|
|
|
|
|
|
// TODO: Deprecate this.
|
|
|
|
Text.propTypes = TextPropTypes;
|
|
|
|
|
2018-05-13 07:48:49 +00:00
|
|
|
module.exports = ((Text: any): Class<NativeComponent<TextProps>>);
|