mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-01-12 22:51:09 +08:00
[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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user