From 4308de6aab93630c2c24a2d1d2ffd2db4e1d4b6b Mon Sep 17 00:00:00 2001 From: Mathieu Duckerts-Antoine Date: Wed, 9 Feb 2022 14:35:04 +0000 Subject: [PATCH] [REF] web: useAutofocus simplified API We bring two changes to the API of useAutofocus: - it takes no parameter, the element focused is the one that is the target of a t-ref="autofocus". - it returns the element reference instead of a function to call if one wants to force the focus on a next patch. Indeed, having access to the element reference makes it easy to focus that element but also allow to remove an eventual useRef that would target the same element. This in turn permits the use of t-ref="autofocus" alone. closes odoo/odoo#84306 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/core/pager/pager.js | 11 +++++----- addons/web/static/src/core/pager/pager.xml | 2 +- addons/web/static/src/core/utils/hooks.js | 20 ++++++++----------- .../src/legacy/js/components/datepicker.js | 5 +---- .../js/control_panel/custom_favorite_item.js | 9 +++------ .../src/legacy/js/control_panel/search_bar.js | 11 +++++----- addons/web/static/src/legacy/xml/base.xml | 4 ++-- .../favorite_menu/custom_favorite_item.js | 5 ++--- .../favorite_menu/custom_favorite_item.xml | 2 +- .../src/search/search_bar/search_bar.js | 10 ++++------ .../src/search/search_bar/search_bar.xml | 2 +- .../static/tests/core/utils/hooks_tests.js | 16 ++++++--------- 12 files changed, 40 insertions(+), 57 deletions(-) diff --git a/addons/web/static/src/core/pager/pager.js b/addons/web/static/src/core/pager/pager.js index d562af39d64..108c347332b 100644 --- a/addons/web/static/src/core/pager/pager.js +++ b/addons/web/static/src/core/pager/pager.js @@ -2,7 +2,7 @@ import { useAutofocus } from "../utils/hooks"; -const { Component, useExternalListener, useRef, useState } = owl; +const { Component, useExternalListener, useState } = owl; /** * Pager @@ -23,11 +23,8 @@ export class Pager extends Component { isEditing: false, isDisabled: false, }); - - this.inputRef = useRef("input"); + this.inputRef = useAutofocus(); useExternalListener(document, "mousedown", this.onClickAway, { capture: true }); - - this.forceFocus = useAutofocus("input"); } /** @@ -146,7 +143,9 @@ export class Pager extends Component { } onValueClick() { if (this.props.isEditable && !this.state.isEditing && !this.state.isDisabled) { - this.forceFocus(); + if (this.inputRef.el) { + this.inputRef.el.focus(); + } this.state.isEditing = true; } } diff --git a/addons/web/static/src/core/pager/pager.xml b/addons/web/static/src/core/pager/pager.xml index 9745a3d04bd..a11dea6423b 100644 --- a/addons/web/static/src/core/pager/pager.xml +++ b/addons/web/static/src/core/pager/pager.xml @@ -5,7 +5,7 @@