Skip to content

Commit 6e04318

Browse files
authored
Merge pull request #926 from tblivet/refactor/product-quantity-selectors
Refactor quantity inputs following recent CORE changes
2 parents 608d874 + 63d7e70 commit 6e04318

File tree

5 files changed

+6
-83
lines changed

5 files changed

+6
-83
lines changed

src/js/components/useQuantityInput.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -322,50 +322,17 @@ const sendUpdateCartRequest = async (updateUrl: string, quantity: number) => {
322322
return response;
323323
};
324324

325-
export const populateMinQuantityInput = (selector = quantityInputMap.default) => {
326-
const qtyInputNodeList = document.querySelectorAll<HTMLElement>(selector);
327-
328-
const products = window.prestashop?.cart?.products;
329-
330-
if (!Array.isArray(products) || products.length === 0) {
331-
return;
332-
}
333-
334-
qtyInputNodeList.forEach((qtyInputWrapper: HTMLElement) => {
335-
const form = qtyInputWrapper.closest('form');
336-
const idProductInput = form?.querySelector<HTMLInputElement>(quantityInputMap.idProductInput);
337-
const qtyInput = qtyInputWrapper.querySelector<HTMLInputElement>('input');
338-
339-
if (!idProductInput || !qtyInput) return;
340-
341-
const idProduct = parseInt(idProductInput.value, 10);
342-
const minAttr = qtyInput.getAttribute('min');
343-
344-
if (!minAttr) return;
345-
346-
const min = parseInt(minAttr, 10);
347-
const productInCart = products.find((p: { id: number }) => p.id === idProduct);
348-
349-
const minimalQuantity = productInCart && productInCart.quantity_wanted >= min ? 1 : min;
350-
351-
qtyInput.setAttribute('min', minimalQuantity.toString());
352-
qtyInput.setAttribute('value', minimalQuantity.toString());
353-
});
354-
};
355-
356325
document.addEventListener('DOMContentLoaded', () => {
357326
const {prestashop, Theme: {events}} = window;
358327

359-
populateMinQuantityInput();
360-
361328
// Delegated keydown: store focus only on Enter key press
362329
document.addEventListener('keydown', (e: KeyboardEvent) => {
363330
const target = e.target as HTMLElement | null;
364331

365332
if (!target) return;
366333

367334
// If Enter key pressed and element is inside productQuantity wrapper, store focus
368-
if (e.key === ENTER_KEY && target.closest(cartSelectorMap.productQuantity)) {
335+
if ((e.key === ENTER_KEY || e.key === ' ' || e.code === 'Space') && target.closest(cartSelectorMap.productQuantity)) {
369336
// Set state.lastUpdateAction to track the last update action
370337
state.set('lastUpdateAction', availableLastUpdateAction.UPDATE_PRODUCT_QUANTITY);
371338
a11y.storeFocus();
@@ -381,7 +348,7 @@ document.addEventListener('DOMContentLoaded', () => {
381348
// nearest button inside the product quantity wrapper
382349
const btn = target.closest(`${cartSelectorMap.productQuantity} button`) as HTMLElement | null;
383350

384-
if (btn && (e.key === ENTER_KEY || e.key === ' ')) {
351+
if (btn && (e.key === ENTER_KEY || e.key === ' ' || e.code === 'Space')) {
385352
// Set state.lastUpdateAction to track the last update action
386353
state.set('lastUpdateAction', availableLastUpdateAction.UPDATE_PRODUCT_QUANTITY);
387354
a11y.storeFocus();
@@ -390,16 +357,10 @@ document.addEventListener('DOMContentLoaded', () => {
390357

391358
prestashop.on(events.updatedCart, () => {
392359
useQuantityInput(cartSelectorMap.productQuantity);
393-
populateMinQuantityInput();
394-
});
395-
396-
prestashop.on(events.updateProduct, () => {
397-
populateMinQuantityInput();
398360
});
399361

400362
prestashop.on(events.quickviewOpened, () => {
401363
useQuantityInput(quantityInputMap.modal);
402-
populateMinQuantityInput(quantityInputMap.modal);
403364
});
404365
});
405366

src/js/modules/facetedsearch/update.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file that was distributed with this source code.
44
*/
55

6-
import useQuantityInput, {populateMinQuantityInput} from '@js/components/useQuantityInput';
6+
import useQuantityInput from '@js/components/useQuantityInput';
77

88
export const parseSearchUrl = (event: Event): string => {
99
const target = (event.target as HTMLElement).closest<HTMLElement>('[data-search-url]');
@@ -123,7 +123,6 @@ export default function initFacetedSearch(): void {
123123
prestashop.on(events.updateProductList, (data: Record<string, unknown>) => {
124124
updateProductListDOM(data);
125125
useQuantityInput();
126-
populateMinQuantityInput();
127126
});
128127

129128
// Listen for color label space key press

src/js/modules/ps_searchbar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const initSearchbar = () => {
5757

5858
// Add keyboard support for clear button
5959
searchClear?.addEventListener('keydown', (e: KeyboardEvent) => {
60-
if (e.key === 'Enter' || e.key === ' ') {
60+
if (e.key === 'Enter' || e.key === ' ' || e.code === 'Space') {
6161
e.preventDefault();
6262
clearSearch();
6363
}

src/js/product.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ export default () => {
3131
prestashop.on(events.updatedProduct, initProductSlide);
3232
prestashop.on(events.quickviewOpened, initProductSlide);
3333

34-
prestashop.on(events.updateCart, (event: UpdateCartEvent) => {
35-
const quantityInput = document.querySelector(
36-
SelectorsMap.qtyInput.quantityWanted,
37-
) as HTMLInputElement;
38-
39-
if (quantityInput) {
40-
const minQuantity = getMinValue(quantityInput);
41-
const quantityInCart = Number(event.resp.quantity) || 0;
42-
43-
if (quantityInCart >= minQuantity) {
44-
quantityInput.setAttribute('min', '1');
45-
quantityInput.setAttribute('value', '1');
46-
}
47-
}
48-
});
49-
5034
function detectQuantityChange() {
5135
const quantityInput = document.querySelector(
5236
SelectorsMap.qtyInput.quantityWanted,
@@ -78,28 +62,11 @@ export default () => {
7862

7963
const debouncedTriggerEmit = debounce(triggerEmit, 500);
8064

81-
quantityInput.addEventListener('input', (event: Event) => {
82-
const input = event.target as HTMLInputElement;
83-
const minQuantity = getMinValue(input);
84-
const sanitizedValue = sanitizeInputToNumber(input.value);
85-
const clampedValue = clampToMin(sanitizedValue, minQuantity);
86-
87-
if (input.value !== clampedValue.toString()) {
88-
input.value = clampedValue.toString();
89-
}
90-
65+
quantityInput.addEventListener('input', () => {
9166
debouncedTriggerEmit();
9267
});
9368

9469
quantityInput.addEventListener('blur', () => {
95-
const minQuantity = getMinValue(quantityInput);
96-
const sanitizedValue = sanitizeInputToNumber(quantityInput.value);
97-
const clampedValue = clampToMin(sanitizedValue, minQuantity);
98-
99-
if (quantityInput.value !== clampedValue.toString()) {
100-
quantityInput.value = clampedValue.toString();
101-
}
102-
10370
triggerEmit();
10471
});
10572

@@ -111,10 +78,6 @@ export default () => {
11178

11279
const getMinValue = (input: HTMLInputElement): number => Number(input.getAttribute('min')) || 1;
11380

114-
const sanitizeInputToNumber = (value: string): number => Number(value.replace(/[^\d]/g, '')) || 0;
115-
116-
const clampToMin = (value: number, min: number): number => Math.max(value, min);
117-
11881
// Call the function to start listening for quantity changes
11982
detectQuantityChange();
12083
};

templates/catalog/_partials/miniatures/product.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
{include file='components/qty-input.tpl'
8585
attributes=[
8686
"id" => "quantity_wanted_{$product.id_product}",
87-
"value" => "{$product.quantity_required}",
87+
"value" => "{$product.quantity_wanted}",
8888
"min" => "{$product.quantity_required}"
8989
]
9090
}

0 commit comments

Comments
 (0)