[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) <caburj@users.noreply.github.com>
This commit is contained in:
Joseph Caburnay
2020-10-12 15:02:12 +00:00
parent a942e7561f
commit bb565bcd0e
2 changed files with 18 additions and 1 deletions
@@ -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 = '';
}
@@ -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';