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 TextInput
|
2015-03-25 19:55:10 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var DocumentSelectionState = require('DocumentSelectionState');
|
|
|
|
var EventEmitter = require('EventEmitter');
|
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2015-03-27 18:44:43 +00:00
|
|
|
var Platform = require('Platform');
|
2015-02-20 04:10:52 +00:00
|
|
|
var PropTypes = require('ReactPropTypes');
|
2015-11-18 13:32:46 +00:00
|
|
|
var RCTUIManager = require('NativeModules').UIManager;
|
2015-02-20 04:10:52 +00:00
|
|
|
var React = require('React');
|
|
|
|
var ReactChildren = require('ReactChildren');
|
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var Text = require('Text');
|
|
|
|
var TextInputState = require('TextInputState');
|
2015-03-24 18:35:59 +00:00
|
|
|
var TimerMixin = require('react-timer-mixin');
|
2015-02-20 04:10:52 +00:00
|
|
|
var TouchableWithoutFeedback = require('TouchableWithoutFeedback');
|
2015-11-18 13:32:46 +00:00
|
|
|
var View = require('View');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-05-08 16:45:43 +00:00
|
|
|
var createReactNativeComponentClass = require('createReactNativeComponentClass');
|
2015-02-20 04:10:52 +00:00
|
|
|
var emptyFunction = require('emptyFunction');
|
|
|
|
var invariant = require('invariant');
|
2015-06-09 23:02:16 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
var onlyMultiline = {
|
2015-07-21 19:37:24 +00:00
|
|
|
onTextInput: true, // not supported in Open Source yet
|
2015-02-20 04:10:52 +00:00
|
|
|
children: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
var notMultiline = {
|
|
|
|
onSubmitEditing: true,
|
|
|
|
};
|
|
|
|
|
2015-09-24 00:03:06 +00:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
var AndroidTextInput = requireNativeComponent('AndroidTextInput', null);
|
|
|
|
} else if (Platform.OS === 'ios') {
|
|
|
|
var RCTTextView = requireNativeComponent('RCTTextView', null);
|
|
|
|
var RCTTextField = requireNativeComponent('RCTTextField', null);
|
|
|
|
}
|
2015-06-09 23:02:16 +00:00
|
|
|
|
2015-11-05 05:01:49 +00:00
|
|
|
type DefaultProps = {
|
|
|
|
blurOnSubmit: boolean;
|
|
|
|
};
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type Event = Object;
|
|
|
|
|
2015-03-09 16:28:51 +00:00
|
|
|
/**
|
|
|
|
* A foundational component for inputting text into the app via a
|
2015-06-16 17:05:27 +00:00
|
|
|
* keyboard. Props provide configurability for several features, such as
|
|
|
|
* auto-correction, auto-capitalization, placeholder text, and different keyboard
|
2015-03-09 16:28:51 +00:00
|
|
|
* types, such as a numeric keypad.
|
|
|
|
*
|
|
|
|
* The simplest use case is to plop down a `TextInput` and subscribe to the
|
2015-07-27 13:20:37 +00:00
|
|
|
* `onChangeText` events to read the user input. There are also other events,
|
|
|
|
* such as `onSubmitEditing` and `onFocus` that can be subscribed to. A simple
|
2015-03-09 16:28:51 +00:00
|
|
|
* example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <TextInput
|
|
|
|
* style={{height: 40, borderColor: 'gray', borderWidth: 1}}
|
2015-07-21 19:37:24 +00:00
|
|
|
* onChangeText={(text) => this.setState({text})}
|
|
|
|
* value={this.state.text}
|
2015-03-09 16:28:51 +00:00
|
|
|
* />
|
|
|
|
* ```
|
|
|
|
*
|
2015-10-01 01:56:00 +00:00
|
|
|
* Note that some props are only available with `multiline={true/false}`:
|
|
|
|
* ```
|
2015-07-21 19:37:24 +00:00
|
|
|
* var onlyMultiline = {
|
|
|
|
* onSelectionChange: true, // not supported in Open Source yet
|
|
|
|
* onTextInput: true, // not supported in Open Source yet
|
|
|
|
* children: true,
|
|
|
|
* };
|
2015-03-09 16:28:51 +00:00
|
|
|
*
|
2015-07-21 19:37:24 +00:00
|
|
|
* var notMultiline = {
|
|
|
|
* onSubmitEditing: true,
|
|
|
|
* };
|
2015-10-01 01:56:00 +00:00
|
|
|
* ```
|
2015-03-09 16:28:51 +00:00
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
var TextInput = React.createClass({
|
2015-11-05 11:31:11 +00:00
|
|
|
statics: {
|
|
|
|
/* TODO(brentvatne) docs are needed for this */
|
|
|
|
State: TextInputState,
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
propTypes: {
|
2015-11-18 13:32:46 +00:00
|
|
|
...View.propTypes,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Can tell TextInput to automatically capitalize certain characters.
|
|
|
|
*
|
|
|
|
* - characters: all characters,
|
|
|
|
* - words: first letter of each word
|
|
|
|
* - sentences: first letter of each sentence (default)
|
|
|
|
* - none: don't auto capitalize anything
|
|
|
|
*/
|
2015-03-04 22:04:52 +00:00
|
|
|
autoCapitalize: PropTypes.oneOf([
|
|
|
|
'none',
|
|
|
|
'sentences',
|
|
|
|
'words',
|
|
|
|
'characters',
|
|
|
|
]),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* If false, disables auto-correct. The default value is true.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
autoCorrect: PropTypes.bool,
|
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* If true, focuses the input on componentDidMount.
|
|
|
|
* The default value is false.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
autoFocus: PropTypes.bool,
|
2015-06-22 14:06:11 +00:00
|
|
|
/**
|
|
|
|
* Set the position of the cursor from where editing will begin.
|
2015-10-16 10:47:03 +00:00
|
|
|
* @platform android
|
2015-06-22 14:06:11 +00:00
|
|
|
*/
|
|
|
|
textAlign: PropTypes.oneOf([
|
|
|
|
'start',
|
|
|
|
'center',
|
|
|
|
'end',
|
|
|
|
]),
|
2015-07-27 13:20:37 +00:00
|
|
|
/**
|
|
|
|
* Aligns text vertically within the TextInput.
|
|
|
|
* @platform android
|
|
|
|
*/
|
2015-06-22 14:06:11 +00:00
|
|
|
textAlignVertical: PropTypes.oneOf([
|
|
|
|
'top',
|
|
|
|
'center',
|
|
|
|
'bottom',
|
|
|
|
]),
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* If false, text is not editable. The default value is true.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
editable: PropTypes.bool,
|
|
|
|
/**
|
2015-03-04 22:04:52 +00:00
|
|
|
* Determines which keyboard to open, e.g.`numeric`.
|
2015-07-27 13:20:37 +00:00
|
|
|
*
|
|
|
|
* The following values work across platforms:
|
|
|
|
* - default
|
|
|
|
* - numeric
|
|
|
|
* - email-address
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2015-03-04 22:04:52 +00:00
|
|
|
keyboardType: PropTypes.oneOf([
|
2015-06-05 15:46:17 +00:00
|
|
|
// Cross-platform
|
2015-06-09 23:02:16 +00:00
|
|
|
'default',
|
2015-06-05 15:46:17 +00:00
|
|
|
'numeric',
|
|
|
|
'email-address',
|
|
|
|
// iOS-only
|
2015-03-31 01:25:30 +00:00
|
|
|
'ascii-capable',
|
|
|
|
'numbers-and-punctuation',
|
|
|
|
'url',
|
|
|
|
'number-pad',
|
|
|
|
'phone-pad',
|
|
|
|
'name-phone-pad',
|
|
|
|
'decimal-pad',
|
|
|
|
'twitter',
|
|
|
|
'web-search',
|
2015-03-04 22:04:52 +00:00
|
|
|
]),
|
2015-11-11 13:31:18 +00:00
|
|
|
/**
|
|
|
|
* Determines the color of the keyboard.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
keyboardAppearance: PropTypes.oneOf([
|
|
|
|
'default',
|
|
|
|
'light',
|
|
|
|
'dark',
|
|
|
|
]),
|
2015-03-31 01:25:30 +00:00
|
|
|
/**
|
|
|
|
* Determines how the return key should look.
|
2015-07-27 13:20:37 +00:00
|
|
|
* @platform ios
|
2015-03-31 01:25:30 +00:00
|
|
|
*/
|
|
|
|
returnKeyType: PropTypes.oneOf([
|
|
|
|
'default',
|
|
|
|
'go',
|
|
|
|
'google',
|
|
|
|
'join',
|
|
|
|
'next',
|
|
|
|
'route',
|
|
|
|
'search',
|
|
|
|
'send',
|
|
|
|
'yahoo',
|
|
|
|
'done',
|
|
|
|
'emergency-call',
|
|
|
|
]),
|
2015-07-21 19:37:24 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* Limits the maximum number of characters that can be entered. Use this
|
2015-07-21 19:37:24 +00:00
|
|
|
* instead of implementing the logic in JS to avoid flicker.
|
|
|
|
*/
|
|
|
|
maxLength: PropTypes.number,
|
2015-09-08 17:31:37 +00:00
|
|
|
/**
|
|
|
|
* Sets the number of lines for a TextInput. Use it with multiline set to
|
|
|
|
* true to be able to fill the lines.
|
|
|
|
* @platform android
|
|
|
|
*/
|
|
|
|
numberOfLines: PropTypes.number,
|
2015-03-31 01:25:30 +00:00
|
|
|
/**
|
|
|
|
* If true, the keyboard disables the return key when there is no text and
|
2015-07-27 13:20:37 +00:00
|
|
|
* automatically enables it when there is text. The default value is false.
|
|
|
|
* @platform ios
|
2015-03-31 01:25:30 +00:00
|
|
|
*/
|
|
|
|
enablesReturnKeyAutomatically: PropTypes.bool,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* If true, the text input can be multiple lines.
|
|
|
|
* The default value is false.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
multiline: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Callback that is called when the text input is blurred
|
|
|
|
*/
|
|
|
|
onBlur: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* Callback that is called when the text input is focused
|
|
|
|
*/
|
|
|
|
onFocus: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* Callback that is called when the text input's text changes.
|
|
|
|
*/
|
2015-03-27 18:44:43 +00:00
|
|
|
onChange: PropTypes.func,
|
2015-06-15 19:17:11 +00:00
|
|
|
/**
|
|
|
|
* Callback that is called when the text input's text changes.
|
|
|
|
* Changed text is passed as an argument to the callback handler.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
onChangeText: PropTypes.func,
|
2015-04-10 00:16:14 +00:00
|
|
|
/**
|
|
|
|
* Callback that is called when text input ends.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
onEndEditing: PropTypes.func,
|
2015-04-10 00:16:14 +00:00
|
|
|
/**
|
|
|
|
* Callback that is called when the text input's submit button is pressed.
|
2015-11-11 01:32:06 +00:00
|
|
|
* Invalid if multiline={true} is specified.
|
2015-04-10 00:16:14 +00:00
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
onSubmitEditing: PropTypes.func,
|
2015-11-02 17:13:41 +00:00
|
|
|
/**
|
|
|
|
* Callback that is called when a key is pressed.
|
|
|
|
* Pressed key value is passed as an argument to the callback handler.
|
|
|
|
* Fires before onChange callbacks.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
onKeyPress: PropTypes.func,
|
2015-05-15 18:41:15 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* Invoked on mount and layout changes with `{x, y, width, height}`.
|
2015-05-15 18:41:15 +00:00
|
|
|
*/
|
|
|
|
onLayout: PropTypes.func,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* The string that will be rendered before text input has been entered
|
|
|
|
*/
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* The text color of the placeholder string
|
|
|
|
*/
|
|
|
|
placeholderTextColor: PropTypes.string,
|
2015-07-27 13:20:37 +00:00
|
|
|
/**
|
|
|
|
* If true, the text input obscures the text entered so that sensitive text
|
|
|
|
* like passwords stay secure. The default value is false.
|
|
|
|
*/
|
|
|
|
secureTextEntry: PropTypes.bool,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* See DocumentSelectionState.js, some state that is responsible for
|
|
|
|
* maintaining selection information for a document
|
2015-07-27 13:20:37 +00:00
|
|
|
* @platform ios
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
selectionState: PropTypes.instanceOf(DocumentSelectionState),
|
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* The value to show for the text input. TextInput is a controlled
|
2015-07-21 19:37:24 +00:00
|
|
|
* component, which means the native value will be forced to match this
|
2015-07-27 13:20:37 +00:00
|
|
|
* value prop if provided. For most uses this works great, but in some
|
2015-07-21 19:37:24 +00:00
|
|
|
* cases this may cause flickering - one common cause is preventing edits
|
2015-07-27 13:20:37 +00:00
|
|
|
* by keeping value the same. In addition to simply setting the same value,
|
2015-07-21 19:37:24 +00:00
|
|
|
* either set `editable={false}`, or set/update `maxLength` to prevent
|
|
|
|
* unwanted edits without flicker.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
value: PropTypes.string,
|
2015-07-22 21:03:32 +00:00
|
|
|
/**
|
|
|
|
* Provides an initial value that will change when the user starts typing.
|
|
|
|
* Useful for simple use-cases where you don't want to deal with listening
|
|
|
|
* to events and updating the value prop to keep the controlled state in sync.
|
|
|
|
*/
|
|
|
|
defaultValue: PropTypes.string,
|
2015-02-26 23:24:19 +00:00
|
|
|
/**
|
|
|
|
* When the clear button should appear on the right side of the text view
|
2015-07-27 13:20:37 +00:00
|
|
|
* @platform ios
|
2015-02-26 23:24:19 +00:00
|
|
|
*/
|
2015-03-04 22:04:52 +00:00
|
|
|
clearButtonMode: PropTypes.oneOf([
|
|
|
|
'never',
|
|
|
|
'while-editing',
|
|
|
|
'unless-editing',
|
|
|
|
'always',
|
|
|
|
]),
|
2015-04-15 00:51:28 +00:00
|
|
|
/**
|
|
|
|
* If true, clears the text field automatically when editing begins
|
2015-07-27 13:20:37 +00:00
|
|
|
* @platform ios
|
2015-04-15 00:51:28 +00:00
|
|
|
*/
|
|
|
|
clearTextOnFocus: PropTypes.bool,
|
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* If true, all text will automatically be selected on focus
|
|
|
|
* @platform ios
|
2015-04-15 00:51:28 +00:00
|
|
|
*/
|
|
|
|
selectTextOnFocus: PropTypes.bool,
|
2015-11-05 05:01:49 +00:00
|
|
|
/**
|
|
|
|
* If true, the text field will blur when submitted.
|
|
|
|
* The default value is true.
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
blurOnSubmit: PropTypes.bool,
|
2015-04-15 00:51:28 +00:00
|
|
|
/**
|
|
|
|
* Styles
|
|
|
|
*/
|
2015-03-09 16:29:09 +00:00
|
|
|
style: Text.propTypes.style,
|
2015-03-27 18:44:43 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* Used to locate this view in end-to-end tests
|
2015-03-27 18:44:43 +00:00
|
|
|
*/
|
|
|
|
testID: PropTypes.string,
|
2015-06-17 22:14:42 +00:00
|
|
|
/**
|
2015-07-27 13:20:37 +00:00
|
|
|
* The color of the textInput underline.
|
|
|
|
* @platform android
|
2015-06-17 22:14:42 +00:00
|
|
|
*/
|
|
|
|
underlineColorAndroid: PropTypes.string,
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-11-05 05:01:49 +00:00
|
|
|
getDefaultProps: function(): DefaultProps {
|
|
|
|
return {
|
|
|
|
blurOnSubmit: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* `NativeMethodsMixin` will look for this when invoking `setNativeProps`. We
|
|
|
|
* make `this` look like an actual native component class.
|
|
|
|
*/
|
2015-03-11 21:45:49 +00:00
|
|
|
mixins: [NativeMethodsMixin, TimerMixin],
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-06-09 23:02:16 +00:00
|
|
|
viewConfig: ((Platform.OS === 'ios' ? RCTTextField.viewConfig :
|
2015-09-17 15:36:08 +00:00
|
|
|
(Platform.OS === 'android' ? AndroidTextInput.viewConfig : {})) : Object),
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
isFocused: function(): boolean {
|
2015-02-20 04:10:52 +00:00
|
|
|
return TextInputState.currentlyFocusedField() ===
|
2015-05-13 01:55:13 +00:00
|
|
|
React.findNodeHandle(this.refs.input);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2015-07-21 19:37:24 +00:00
|
|
|
mostRecentEventCount: 0,
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
contextTypes: {
|
|
|
|
onFocusRequested: React.PropTypes.func,
|
|
|
|
focusEmitter: React.PropTypes.instanceOf(EventEmitter),
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_focusSubscription: (undefined: ?Function),
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
if (!this.context.focusEmitter) {
|
|
|
|
if (this.props.autoFocus) {
|
|
|
|
this.requestAnimationFrame(this.focus);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-03-11 21:45:49 +00:00
|
|
|
this._focusSubscription = this.context.focusEmitter.addListener(
|
|
|
|
'focus',
|
|
|
|
(el) => {
|
|
|
|
if (this === el) {
|
|
|
|
this.requestAnimationFrame(this.focus);
|
|
|
|
} else if (this.isFocused()) {
|
|
|
|
this.blur();
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-03-11 21:45:49 +00:00
|
|
|
);
|
2015-02-20 04:10:52 +00:00
|
|
|
if (this.props.autoFocus) {
|
|
|
|
this.context.onFocusRequested(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-11 21:45:49 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._focusSubscription && this._focusSubscription.remove();
|
2015-04-24 16:12:04 +00:00
|
|
|
if (this.isFocused()) {
|
|
|
|
this.blur();
|
|
|
|
}
|
2015-03-11 21:45:49 +00:00
|
|
|
},
|
|
|
|
|
2015-05-29 22:24:00 +00:00
|
|
|
getChildContext: function(): Object {
|
2015-05-29 20:16:57 +00:00
|
|
|
return {isInAParentText: true};
|
|
|
|
},
|
|
|
|
|
|
|
|
childContextTypes: {
|
|
|
|
isInAParentText: React.PropTypes.bool
|
|
|
|
},
|
|
|
|
|
2015-07-24 16:34:18 +00:00
|
|
|
clear: function() {
|
|
|
|
this.setNativeProps({text: ''});
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
render: function() {
|
2015-03-27 18:44:43 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
2015-05-29 12:18:42 +00:00
|
|
|
return this._renderIOS();
|
2015-03-27 18:44:43 +00:00
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
return this._renderAndroid();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-22 21:03:32 +00:00
|
|
|
_getText: function(): ?string {
|
|
|
|
return typeof this.props.value === 'string' ?
|
|
|
|
this.props.value :
|
|
|
|
this.props.defaultValue;
|
|
|
|
},
|
|
|
|
|
2015-05-29 12:18:42 +00:00
|
|
|
_renderIOS: function() {
|
2015-02-20 04:10:52 +00:00
|
|
|
var textContainer;
|
|
|
|
|
2015-11-14 17:41:59 +00:00
|
|
|
var onSelectionChange;
|
|
|
|
if (this.props.selectionState || this.props.onSelectionChange) {
|
2015-11-16 22:01:53 +00:00
|
|
|
onSelectionChange = (event: Event) => {
|
2015-11-14 17:41:59 +00:00
|
|
|
if (this.props.selectionState) {
|
|
|
|
var selection = event.nativeEvent.selection;
|
|
|
|
this.props.selectionState.update(selection.start, selection.end);
|
|
|
|
}
|
|
|
|
this.props.onSelectionChange && this.props.onSelectionChange(event);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:37:24 +00:00
|
|
|
var props = Object.assign({}, this.props);
|
2015-06-05 15:46:17 +00:00
|
|
|
props.style = [styles.input, this.props.style];
|
|
|
|
if (!props.multiline) {
|
2015-02-20 04:10:52 +00:00
|
|
|
for (var propKey in onlyMultiline) {
|
2015-06-05 15:46:17 +00:00
|
|
|
if (props[propKey]) {
|
2015-02-20 04:10:52 +00:00
|
|
|
throw new Error(
|
|
|
|
'TextInput prop `' + propKey + '` is only supported with multiline.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
textContainer =
|
2015-03-17 10:08:46 +00:00
|
|
|
<RCTTextField
|
2015-02-20 04:10:52 +00:00
|
|
|
ref="input"
|
2015-06-05 15:46:17 +00:00
|
|
|
{...props}
|
2015-02-20 04:10:52 +00:00
|
|
|
onFocus={this._onFocus}
|
|
|
|
onBlur={this._onBlur}
|
|
|
|
onChange={this._onChange}
|
2015-11-14 17:41:59 +00:00
|
|
|
onSelectionChange={onSelectionChange}
|
|
|
|
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue}
|
2015-07-22 21:03:32 +00:00
|
|
|
text={this._getText()}
|
2015-07-21 19:37:24 +00:00
|
|
|
mostRecentEventCount={this.state.mostRecentEventCount}
|
2015-02-20 04:10:52 +00:00
|
|
|
/>;
|
|
|
|
} else {
|
|
|
|
for (var propKey in notMultiline) {
|
2015-06-05 15:46:17 +00:00
|
|
|
if (props[propKey]) {
|
2015-02-20 04:10:52 +00:00
|
|
|
throw new Error(
|
|
|
|
'TextInput prop `' + propKey + '` cannot be used with multiline.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:46:17 +00:00
|
|
|
var children = props.children;
|
2015-02-20 04:10:52 +00:00
|
|
|
var childCount = 0;
|
|
|
|
ReactChildren.forEach(children, () => ++childCount);
|
|
|
|
invariant(
|
2015-06-05 15:46:17 +00:00
|
|
|
!(props.value && childCount),
|
2015-02-20 04:10:52 +00:00
|
|
|
'Cannot specify both value and children.'
|
|
|
|
);
|
|
|
|
if (childCount > 1) {
|
|
|
|
children = <Text>{children}</Text>;
|
|
|
|
}
|
2015-06-05 15:46:17 +00:00
|
|
|
if (props.inputView) {
|
|
|
|
children = [children, props.inputView];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
textContainer =
|
2015-03-17 10:08:46 +00:00
|
|
|
<RCTTextView
|
2015-02-20 04:10:52 +00:00
|
|
|
ref="input"
|
2015-06-05 15:46:17 +00:00
|
|
|
{...props}
|
2015-02-20 04:10:52 +00:00
|
|
|
children={children}
|
2015-07-21 19:37:24 +00:00
|
|
|
mostRecentEventCount={this.state.mostRecentEventCount}
|
2015-02-20 04:10:52 +00:00
|
|
|
onFocus={this._onFocus}
|
|
|
|
onBlur={this._onBlur}
|
|
|
|
onChange={this._onChange}
|
2015-11-14 17:41:59 +00:00
|
|
|
onSelectionChange={onSelectionChange}
|
2015-02-20 04:10:52 +00:00
|
|
|
onTextInput={this._onTextInput}
|
|
|
|
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue}
|
2015-07-22 21:03:32 +00:00
|
|
|
text={this._getText()}
|
2015-02-20 04:10:52 +00:00
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this._onPress}
|
2015-05-29 12:18:42 +00:00
|
|
|
rejectResponderTermination={true}
|
2015-06-05 15:46:17 +00:00
|
|
|
testID={props.testID}>
|
2015-02-20 04:10:52 +00:00
|
|
|
{textContainer}
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-03-27 18:44:43 +00:00
|
|
|
_renderAndroid: function() {
|
2015-06-05 15:46:17 +00:00
|
|
|
var autoCapitalize = RCTUIManager.UIText.AutocapitalizationType[this.props.autoCapitalize];
|
2015-06-22 14:06:11 +00:00
|
|
|
var textAlign =
|
|
|
|
RCTUIManager.AndroidTextInput.Constants.TextAlign[this.props.textAlign];
|
|
|
|
var textAlignVertical =
|
|
|
|
RCTUIManager.AndroidTextInput.Constants.TextAlignVertical[this.props.textAlignVertical];
|
2015-05-29 20:16:57 +00:00
|
|
|
var children = this.props.children;
|
|
|
|
var childCount = 0;
|
|
|
|
ReactChildren.forEach(children, () => ++childCount);
|
|
|
|
invariant(
|
|
|
|
!(this.props.value && childCount),
|
|
|
|
'Cannot specify both value and children.'
|
|
|
|
);
|
|
|
|
if (childCount > 1) {
|
|
|
|
children = <Text>{children}</Text>;
|
|
|
|
}
|
2015-04-08 13:54:32 +00:00
|
|
|
var textContainer =
|
2015-03-27 18:44:43 +00:00
|
|
|
<AndroidTextInput
|
|
|
|
ref="input"
|
|
|
|
style={[this.props.style]}
|
|
|
|
autoCapitalize={autoCapitalize}
|
|
|
|
autoCorrect={this.props.autoCorrect}
|
2015-06-22 14:06:11 +00:00
|
|
|
textAlign={textAlign}
|
|
|
|
textAlignVertical={textAlignVertical}
|
2015-03-27 18:44:43 +00:00
|
|
|
keyboardType={this.props.keyboardType}
|
2015-08-06 12:02:36 +00:00
|
|
|
mostRecentEventCount={this.state.mostRecentEventCount}
|
2015-03-27 18:44:43 +00:00
|
|
|
multiline={this.props.multiline}
|
2015-09-08 17:31:37 +00:00
|
|
|
numberOfLines={this.props.numberOfLines}
|
2015-11-06 21:25:05 +00:00
|
|
|
maxLength={this.props.maxLength}
|
2015-03-27 18:44:43 +00:00
|
|
|
onFocus={this._onFocus}
|
|
|
|
onBlur={this._onBlur}
|
|
|
|
onChange={this._onChange}
|
2015-06-08 10:45:44 +00:00
|
|
|
onTextInput={this._onTextInput}
|
2015-04-08 15:26:14 +00:00
|
|
|
onEndEditing={this.props.onEndEditing}
|
|
|
|
onSubmitEditing={this.props.onSubmitEditing}
|
2015-05-15 18:41:15 +00:00
|
|
|
onLayout={this.props.onLayout}
|
2015-04-10 00:16:14 +00:00
|
|
|
password={this.props.password || this.props.secureTextEntry}
|
2015-03-27 18:44:43 +00:00
|
|
|
placeholder={this.props.placeholder}
|
2015-06-25 12:11:35 +00:00
|
|
|
placeholderTextColor={this.props.placeholderTextColor}
|
2015-07-22 21:03:32 +00:00
|
|
|
text={this._getText()}
|
2015-06-17 22:14:42 +00:00
|
|
|
underlineColorAndroid={this.props.underlineColorAndroid}
|
2015-05-29 20:16:57 +00:00
|
|
|
children={children}
|
2015-08-06 18:02:19 +00:00
|
|
|
editable={this.props.editable}
|
2015-04-08 13:54:32 +00:00
|
|
|
/>;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this._onPress}
|
|
|
|
testID={this.props.testID}>
|
|
|
|
{textContainer}
|
|
|
|
</TouchableWithoutFeedback>
|
2015-03-27 18:44:43 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onFocus: function(event: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
if (this.props.onFocus) {
|
|
|
|
this.props.onFocus(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onPress: function(event: Event) {
|
2015-07-15 15:23:48 +00:00
|
|
|
if (this.props.editable || this.props.editable === undefined) {
|
|
|
|
this.focus();
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onChange: function(event: Event) {
|
2015-08-06 12:02:36 +00:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
// Android expects the event count to be updated as soon as possible.
|
|
|
|
this.refs.input.setNativeProps({
|
|
|
|
mostRecentEventCount: event.nativeEvent.eventCount,
|
|
|
|
});
|
|
|
|
}
|
2015-07-21 19:37:24 +00:00
|
|
|
var text = event.nativeEvent.text;
|
|
|
|
var eventCount = event.nativeEvent.eventCount;
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onChange && this.props.onChange(event);
|
2015-07-21 19:37:24 +00:00
|
|
|
this.props.onChangeText && this.props.onChangeText(text);
|
|
|
|
this.setState({mostRecentEventCount: eventCount}, () => {
|
2015-11-04 12:04:37 +00:00
|
|
|
// NOTE: this doesn't seem to be needed on iOS - keeping for now in case it's required on Android
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
// This is a controlled component, so make sure to force the native value
|
|
|
|
// to match. Most usage shouldn't need this, but if it does this will be
|
|
|
|
// more correct but might flicker a bit and/or cause the cursor to jump.
|
|
|
|
if (text !== this.props.value && typeof this.props.value === 'string') {
|
|
|
|
this.refs.input.setNativeProps({
|
|
|
|
text: this.props.value,
|
|
|
|
});
|
|
|
|
}
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onBlur: function(event: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.blur();
|
|
|
|
if (this.props.onBlur) {
|
|
|
|
this.props.onBlur(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onTextInput: function(event: Event) {
|
2015-02-20 04:10:52 +00:00
|
|
|
this.props.onTextInput && this.props.onTextInput(event);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
input: {
|
|
|
|
alignSelf: 'stretch',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TextInput;
|