From bb565bcd0e60c81ccbb2458be4845294c2aaffc3 Mon Sep 17 00:00:00 2001 From: Joseph Caburnay Date: Mon, 12 Oct 2020 07:59:21 +0000 Subject: [PATCH] [FIX] point_of_sale: add product on single search result In this commit, we restore the following behavior from v13: when search resulted to a single product and 'Enter' key is pressed, the product is added to the order and the search field is cleared. This works for both manual keyboard input and barcode input. closes odoo/odoo#59770 X-original-commit: 225e3898f25ddac061be99c43501c99d4bd3226b Signed-off-by: Joseph Caburnay (jcb) --- .../src/js/Screens/ProductScreen/ProductsWidget.js | 13 +++++++++++++ .../ProductScreen/ProductsWidgetControlPanel.js | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js index 8475697926d..17481058a40 100644 --- a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js +++ b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidget.js @@ -15,6 +15,7 @@ odoo.define('point_of_sale.ProductsWidget', function(require) { super(...arguments); useListener('switch-category', this._switchCategory); useListener('update-search', this._updateSearch); + useListener('try-add-product', this._tryAddProduct); useListener('clear-search', this._clearSearch); this.state = useState({ searchWord: '' }); } @@ -63,6 +64,18 @@ odoo.define('point_of_sale.ProductsWidget', function(require) { _updateSearch(event) { this.state.searchWord = event.detail; } + _tryAddProduct(event) { + const searchResults = this.productsToDisplay; + // If the search result contains one item, add the product and clear the search. + if (searchResults.length === 1) { + const { searchWordInput } = event.detail; + this.trigger('click-product', searchResults[0]); + // the value of the input element is not linked to the searchWord state, + // so we clear both the state and the element's value. + searchWordInput.el.value = ''; + this._clearSearch(); + } + } _clearSearch() { this.state.searchWord = ''; } diff --git a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js index ec1897f1fb5..fc2df5b09c3 100644 --- a/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js +++ b/addons/point_of_sale/static/src/js/Screens/ProductScreen/ProductsWidgetControlPanel.js @@ -9,7 +9,6 @@ odoo.define('point_of_sale.ProductsWidgetControlPanel', function(require) { class ProductsWidgetControlPanel extends PosComponent { constructor() { super(...arguments); - this.searchTimeout = null; this.searchWordInput = useRef('search-word-input'); this.updateSearch = debounce(this.updateSearch, 100); } @@ -19,6 +18,11 @@ odoo.define('point_of_sale.ProductsWidgetControlPanel', function(require) { } updateSearch(event) { this.trigger('update-search', event.target.value); + if (event.key === 'Enter') { + // We are passing the searchWordInput ref so that when necessary, + // it can be modified by the parent. + this.trigger('try-add-product', { searchWordInput: this.searchWordInput }); + } } } ProductsWidgetControlPanel.template = 'ProductsWidgetControlPanel';