forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#23258 from kosmydel/@swm/edit-mode-focus
- Loading branch information
Showing
5 changed files
with
67 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {InteractionManager} from 'react-native'; | ||
|
||
/** | ||
* Creates a function that can be used to focus a text input | ||
* @param {Boolean} disableDelay whether to force focus without a delay (on web and desktop) | ||
* @returns {Function} a focusWithDelay function | ||
*/ | ||
function focusWithDelay(disableDelay = false) { | ||
/** | ||
* Create a function that focuses a text input. | ||
* @param {Object} textInput the text input to focus | ||
* @returns {Function} a function that focuses the text input with a configurable delay | ||
*/ | ||
return (textInput) => | ||
/** | ||
* Focus the text input | ||
* @param {Boolean} [shouldDelay=false] Impose delay before focusing the text input | ||
*/ | ||
(shouldDelay = false) => { | ||
// There could be other animations running while we trigger manual focus. | ||
// This prevents focus from making those animations janky. | ||
InteractionManager.runAfterInteractions(() => { | ||
if (!textInput) { | ||
return; | ||
} | ||
|
||
if (disableDelay || !shouldDelay) { | ||
textInput.focus(); | ||
} else { | ||
// Keyboard is not opened after Emoji Picker is closed | ||
// SetTimeout is used as a workaround | ||
// https://github.com/react-native-modal/react-native-modal/issues/114 | ||
// We carefully choose a delay. 100ms is found enough for keyboard to open. | ||
setTimeout(() => textInput.focus(), 100); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
export default focusWithDelay; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import focusWithDelay from './focusWithDelay'; | ||
|
||
/** | ||
* We pass true to disable the delay on the web because it doesn't require | ||
* using the workaround (explained in the focusWithDelay.js file). | ||
*/ | ||
export default focusWithDelay(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import focusWithDelay from './focusWithDelay'; | ||
|
||
/** | ||
* We enable the delay on native to display the keyboard correctly | ||
*/ | ||
export default focusWithDelay(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters