diff --git a/src/components/auth/BaseAuthBody.jsx b/src/components/auth/BaseAuthBody.jsx index e0253f9..699739a 100644 --- a/src/components/auth/BaseAuthBody.jsx +++ b/src/components/auth/BaseAuthBody.jsx @@ -42,34 +42,11 @@ export default class BaseAuthBody extends Component { }; } - /** - * Fixes some issues with scroll, when input beeing focused - * - * When an element is focused, by default browsers will scroll its parents to display - * focused item to user. This behavior may cause unexpected visual effects, when - * you animating apearing of an input (e.g. transform) and auto focusing it. In - * that case the browser will scroll the parent container so that input will be - * visible. - * This method will fix that issue by finding parent with overflow: hidden and - * reseting its scrollLeft value to 0. - * - * Usage: - * - * - * @param {Object} event - */ - fixAutoFocus = (event) => { - let el = event.target; + autoFocus() { + const fieldId = this.autoFocusField; - while (el.parentNode) { - el = el.parentNode; - - if (getComputedStyle(el).overflow === 'hidden') { - el.scrollLeft = 0; - break; - } - } - }; + fieldId && this.form[fieldId] && this.form[fieldId].focus(); + } serialize() { return Object.keys(this.form).reduce((acc, key) => { diff --git a/src/components/auth/PanelTransition.jsx b/src/components/auth/PanelTransition.jsx index 6a750cf..629907f 100644 --- a/src/components/auth/PanelTransition.jsx +++ b/src/components/auth/PanelTransition.jsx @@ -134,6 +134,8 @@ class PanelTransition extends Component { height: forceHeight ? common.style.switchContextHeightSpring : 'auto' }; + this.tryToAutoFocus(panels.length); + const bodyHeight = { position: 'relative', height: `${canAnimateHeight ? common.style.heightSpring : formHeight}px` @@ -253,6 +255,26 @@ class PanelTransition extends Component { authFlow.goBack(); }; + /** + * Tries to auto focus form fields after transition end + * + * @param {number} length number of panels transitioned + */ + tryToAutoFocus(length) { + if (!this.body) { + return; + } + + if (length === 1) { + if (!this.wasAutoFocused) { + this.body.autoFocus(); + } + this.wasAutoFocused = true; + } else if (this.wasAutoFocused) { + this.wasAutoFocused = false; + } + } + getHeader({key, style, data}) { const {Title, hasBackButton} = data; const {transformSpring} = style; diff --git a/src/components/auth/activation/Activation.jsx b/src/components/auth/activation/Activation.jsx index 97b544d..c2217be 100644 --- a/src/components/auth/activation/Activation.jsx +++ b/src/components/auth/activation/Activation.jsx @@ -14,6 +14,8 @@ class Body extends BaseAuthBody { static displayName = 'ActivationBody'; static panelId = 'activation'; + autoFocusField = 'key'; + render() { return (