From f33e83903b131720d5cc233013d2ce1afc0f22ca Mon Sep 17 00:00:00 2001 From: Wojciech Stanisz <42337257+wojtus7@users.noreply.github.com> Date: Thu, 24 Oct 2019 18:44:00 +0200 Subject: [PATCH] fix: add clearTimeout to keyboard manager (#275) --- src/views/KeyboardManager.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/views/KeyboardManager.tsx b/src/views/KeyboardManager.tsx index 4e8dcfe27..8ccec2ec1 100644 --- a/src/views/KeyboardManager.tsx +++ b/src/views/KeyboardManager.tsx @@ -14,8 +14,21 @@ export default class KeyboardManager extends React.Component { // When a gesture didn't change the tab, we can restore the focused input with this private previouslyFocusedTextInput: number | null = null; private startTimestamp: number = 0; + private keyboardTimeout: NodeJS.Timeout | undefined; + + clearKeyboardTimeout = () => { + if (this.keyboardTimeout !== undefined) { + clearTimeout(this.keyboardTimeout); + this.keyboardTimeout = undefined; + } + }; + + componentWillUnmount = () => { + this.clearKeyboardTimeout(); + }; private handlePageChangeStart = () => { + this.clearKeyboardTimeout(); const input = TextInput.State.currentlyFocusedField(); // When a page change begins, blur the currently focused input @@ -29,6 +42,7 @@ export default class KeyboardManager extends React.Component { }; private handlePageChangeConfirm = () => { + this.clearKeyboardTimeout(); Keyboard.dismiss(); // Cleanup the ID on successful page change @@ -36,6 +50,7 @@ export default class KeyboardManager extends React.Component { }; private handlePageChangeCancel = () => { + this.clearKeyboardTimeout(); // The page didn't change, we should restore the focus of text input const input = this.previouslyFocusedTextInput; @@ -48,7 +63,7 @@ export default class KeyboardManager extends React.Component { // That's why when the interaction is shorter than 100ms we add delay so it won't hide once again. // Subtracting timestamps makes us sure the delay is executed only when needed. if (Date.now() - this.startTimestamp < 100) { - setTimeout(() => { + this.keyboardTimeout = setTimeout(() => { TextInput.State.focusTextInput(input); this.previouslyFocusedTextInput = null; }, 100);