-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(focus): improve input focus control
Closes #5536
- Loading branch information
1 parent
4ba9eb0
commit e27452b
Showing
7 changed files
with
105 additions
and
61 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 |
---|---|---|
|
@@ -340,19 +340,17 @@ export class InputBase { | |
*/ | ||
initFocus() { | ||
// begin the process of setting focus to the inner input element | ||
let scrollView = this._scrollView; | ||
var scrollView = this._scrollView; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
adamdbradley
Author
Contributor
|
||
|
||
if (scrollView) { | ||
// this input is inside of a scroll view | ||
|
||
// find out if text input should be manually scrolled into view | ||
let ele = this._elementRef.nativeElement; | ||
let itemEle = closest(ele, 'ion-item'); | ||
if (itemEle) { | ||
ele = itemEle; | ||
} | ||
|
||
let scrollData = InputBase.getScrollData(ele.offsetTop, ele.offsetHeight, scrollView.getContentDimensions(), this._keyboardHeight, this._platform.height()); | ||
// get container of this input, probably an ion-item a few nodes up | ||
var ele = this._elementRef.nativeElement; | ||
ele = closest(ele, 'ion-item,[ion-item]') || ele; | ||
|
||
var scrollData = InputBase.getScrollData(ele.offsetTop, ele.offsetHeight, scrollView.getContentDimensions(), this._keyboardHeight, this._platform.height()); | ||
if (scrollData.scrollAmount > -3 && scrollData.scrollAmount < 3) { | ||
// the text input is in a safe position that doesn't | ||
// require it to be scrolled into view, just set focus now | ||
|
@@ -368,21 +366,22 @@ export class InputBase { | |
|
||
// manually scroll the text input to the top | ||
// do not allow any clicks while it's scrolling | ||
let scrollDuration = getScrollAssistDuration(scrollData.scrollAmount); | ||
var scrollDuration = getScrollAssistDuration(scrollData.scrollAmount); | ||
this._app.setEnabled(false, scrollDuration); | ||
this._nav && this._nav.setTransitioning(true, scrollDuration); | ||
|
||
// temporarily move the focus to the focus holder so the browser | ||
// doesn't freak out while it's trying to get the input in place | ||
// at this point the native text input still does not have focus | ||
this._native.relocate(true, scrollData.inputSafeY); | ||
this._native.beginFocus(true, scrollData.inputSafeY); | ||
|
||
// scroll the input into place | ||
scrollView.scrollTo(0, scrollData.scrollTo, scrollDuration).then(() => { | ||
// the scroll view is in the correct position now | ||
// give the native text input focus | ||
this._native.relocate(false, 0); | ||
this._native.beginFocus(false, 0); | ||
|
||
// ensure this is the focused input | ||
this.setFocus(); | ||
|
||
// all good, allow clicks again | ||
|
@@ -417,6 +416,7 @@ export class InputBase { | |
this._form.setAsFocused(this); | ||
|
||
// set focus on the actual input element | ||
console.debug(`input-base, setFocus ${this._native.element().value}`); | ||
this._native.setFocus(); | ||
|
||
// ensure the body hasn't scrolled down | ||
|
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
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
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
@adamdbradley can I ask why are you switching back to
var
fromlet
? I've seen before you were replacing var's with let's and now it is opposite.Just trying to learn from "big" guys.