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';