[fix] TextInput focus/blur management

1. Focusing/blurring a TextInput should update TextInputState.
2. Using the focus/blur instance methods should trigger related events.

Close #715
This commit is contained in:
Nicolas Gallagher
2017-11-15 15:15:22 -08:00
parent 214121480e
commit 117ce59f27
2 changed files with 11 additions and 11 deletions

View File

@@ -40,9 +40,11 @@ const TextInputState = {
* noop if the text field was already focused
*/
focusTextInput(textFieldNode: ?Object) {
if (document.activeElement !== textFieldNode && textFieldNode !== null) {
if (textFieldNode !== null) {
this._currentlyFocusedNode = textFieldNode;
UIManager.focus(textFieldNode);
if (document.activeElement !== textFieldNode) {
UIManager.focus(textFieldNode);
}
}
},
@@ -52,9 +54,11 @@ const TextInputState = {
* noop if it wasn't focused
*/
blurTextInput(textFieldNode: ?Object) {
if (document.activeElement === textFieldNode && textFieldNode !== null) {
if (textFieldNode !== null) {
this._currentlyFocusedNode = null;
UIManager.blur(textFieldNode);
if (document.activeElement === textFieldNode) {
UIManager.blur(textFieldNode);
}
}
}
};

View File

@@ -150,18 +150,12 @@ class TextInput extends Component {
static State = TextInputState;
blur() {
TextInputState.blurTextInput(this._node);
}
blur: Function;
clear() {
this._node.value = '';
}
focus() {
TextInputState.focusTextInput(this._node);
}
isFocused() {
return TextInputState.currentlyFocusedField() === this._node;
}
@@ -270,6 +264,7 @@ class TextInput extends Component {
_handleBlur = e => {
const { onBlur } = this.props;
TextInputState.blurTextInput(this._node);
if (onBlur) {
onBlur(e);
}
@@ -289,6 +284,7 @@ class TextInput extends Component {
_handleFocus = e => {
const { clearTextOnFocus, onFocus, selectTextOnFocus } = this.props;
const node = this._node;
TextInputState.focusTextInput(this._node);
if (onFocus) {
onFocus(e);
}