[fix] TextInputState focus management

This commit is contained in:
Nicolas Gallagher
2016-10-28 22:12:20 -07:00
parent 5a03cb25cb
commit b46acd4f50
2 changed files with 11 additions and 8 deletions
+5 -2
View File
@@ -24,6 +24,9 @@ const TextInputState = {
* If no text field is focused it returns null
*/
currentlyFocusedField(): ?Object {
if (document.activeElement !== this._currentlyFocusedNode) {
this._currentlyFocusedNode = null;
}
return this._currentlyFocusedNode;
},
@@ -33,7 +36,7 @@ const TextInputState = {
* noop if the text field was already focused
*/
focusTextInput(textFieldNode: ?Object) {
if (this._currentlyFocusedNode !== textFieldNode && textFieldNode !== null) {
if (document.activeElement !== textFieldNode && textFieldNode !== null) {
this._currentlyFocusedNode = textFieldNode;
UIManager.focus(textFieldNode);
}
@@ -45,7 +48,7 @@ const TextInputState = {
* noop if it wasn't focused
*/
blurTextInput(textFieldNode: ?Object) {
if (this._currentlyFocusedNode === textFieldNode && textFieldNode !== null) {
if (document.activeElement === textFieldNode && textFieldNode !== null) {
this._currentlyFocusedNode = null;
UIManager.blur(textFieldNode);
}
+6 -6
View File
@@ -1,8 +1,8 @@
import applyNativeMethods from '../../modules/applyNativeMethods';
import createDOMElement from '../../modules/createDOMElement';
import findNodeHandle from '../../modules/findNodeHandle';
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import ReactDOM from 'react-dom';
import StyleSheet from '../../apis/StyleSheet';
import Text from '../Text';
import TextareaAutosize from 'react-textarea-autosize';
@@ -74,7 +74,7 @@ class TextInput extends Component {
}
blur() {
TextInputState.blurTextInput(ReactDOM.findDOMNode(this._inputRef));
TextInputState.blurTextInput(findNodeHandle(this._inputRef));
}
clear() {
@@ -82,11 +82,11 @@ class TextInput extends Component {
}
focus() {
TextInputState.focusTextInput(ReactDOM.findDOMNode(this._inputRef));
TextInputState.focusTextInput(findNodeHandle(this._inputRef));
}
isFocused() {
return TextInputState.currentlyFocusedField() === ReactDOM.findDOMNode(this._inputRef);
return TextInputState.currentlyFocusedField() === findNodeHandle(this._inputRef);
}
setNativeProps(props) {
@@ -232,7 +232,7 @@ class TextInput extends Component {
_handleFocus = (e) => {
const { clearTextOnFocus, onFocus, selectTextOnFocus } = this.props;
const { text } = e.nativeEvent;
const node = ReactDOM.findDOMNode(this._inputRef);
const node = findNodeHandle(this._inputRef);
if (onFocus) { onFocus(e); }
if (clearTextOnFocus) { this.clear(); }
if (selectTextOnFocus) { node && node.select(); }
@@ -242,7 +242,7 @@ class TextInput extends Component {
_handleKeyPress = (e) => {
const { blurOnSubmit, multiline, onKeyPress, onSubmitEditing } = this.props;
const blurOnSubmitDefault = !multiline;
const shouldBlurOnSubmit = blurOnSubmit == null ? blurOnSubmitDefault : blurOnSubmit
const shouldBlurOnSubmit = blurOnSubmit == null ? blurOnSubmitDefault : blurOnSubmit;
if (onKeyPress) { onKeyPress(e); }
if (!e.isDefaultPrevented() && e.which === 13) {
if (onSubmitEditing) { onSubmitEditing(e); }