|
1 | 1 | import { UCC_CART_CONTEXT } from "../contexts/ucc.context.ts";
|
2 | 2 | import { CartConfig, CartItem } from "../types.ts";
|
3 | 3 | import { UccModalElement } from "./ucc-modal.element.ts";
|
4 |
| -import { debounce, delegate } from "../utils.ts"; |
| 4 | +import {debounce, delegate, getUniqueSelector} from "../utils.ts"; |
5 | 5 | import { UccCartRepository } from "../repositories/cart.respository.ts";
|
6 | 6 | import { UccEvent } from "../events/ucc.event.ts";
|
7 | 7 |
|
@@ -65,6 +65,9 @@ export class UccCartModalElement extends UccModalElement
|
65 | 65 |
|
66 | 66 | const cart = this._context.cart.get();
|
67 | 67 | if (cart && cart.items.length > 0) {
|
| 68 | + const activeEl = this._host.ownerDocument.activeElement as HTMLElement; |
| 69 | + const activeElSelector = activeEl ? getUniqueSelector(activeEl) : undefined; |
| 70 | + const activeElCaretPos = activeEl instanceof HTMLInputElement ? activeEl.selectionStart : undefined; |
68 | 71 | this._host.querySelector('.ucc-cart-items')!.innerHTML = cart.items.map((item) => {
|
69 | 72 | const propsToDisplay = Object.keys(item.properties ?? {}).filter((x) => (config.properties ?? []).map(y => y.toLowerCase()).includes(x.toLowerCase()));
|
70 | 73 | return `
|
@@ -136,6 +139,15 @@ export class UccCartModalElement extends UccModalElement
|
136 | 139 | this._host.querySelector<HTMLElement>('.ucc-cart-total--taxes .ucc-cart-total-value')!.textContent = cart.subtotal.tax;
|
137 | 140 | this._host.querySelector<HTMLElement>('.ucc-cart-total--total .ucc-cart-total-value')!.textContent = cart.subtotal.withTax;
|
138 | 141 | this._host.querySelector<HTMLElement>('.ucc-cart-checkout')!.classList.remove('ucc-cart-checkout--disabled');
|
| 142 | + if (activeElSelector) { |
| 143 | + const activeEl = this._host.querySelector<HTMLElement>(activeElSelector); |
| 144 | + if (activeEl) { |
| 145 | + activeEl.focus(); |
| 146 | + if (activeEl instanceof HTMLInputElement && activeElCaretPos) { |
| 147 | + activeEl.setSelectionRange(activeElCaretPos!, activeElCaretPos!); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
139 | 151 | } else {
|
140 | 152 | this._host.querySelector<HTMLElement>('.ucc-cart-items')!.innerHTML = `
|
141 | 153 | <div class="ucc-cart-empty">
|
|
0 commit comments