@asteby/storefront-react 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/checkout.cjs CHANGED
@@ -241,7 +241,9 @@ var ES2 = {
241
241
  referenceHint: "Entre qu\xE9 calles, color de la fachada\u2026",
242
242
  emailHint: "Te enviamos aqu\xED la confirmaci\xF3n y el seguimiento.",
243
243
  phoneHint: "Solo para la paqueter\xEDa, por si necesita contactarte.",
244
- lookupFilled: "Completamos estado y ciudad con tu c\xF3digo postal."
244
+ lookupFilled: "Completamos estado y ciudad con tu c\xF3digo postal.",
245
+ provinceLocked: "Definido por tu c\xF3digo postal",
246
+ changePostalCode: "Cambiar c\xF3digo postal"
245
247
  };
246
248
  var EN2 = {
247
249
  email: "Email",
@@ -261,7 +263,9 @@ var EN2 = {
261
263
  referenceHint: "Cross streets, building color\u2026",
262
264
  emailHint: "We send the confirmation and tracking here.",
263
265
  phoneHint: "Only for the carrier, in case they need to reach you.",
264
- lookupFilled: "We filled in state and city from your postal code."
266
+ lookupFilled: "We filled in state and city from your postal code.",
267
+ provinceLocked: "Set by your postal code",
268
+ changePostalCode: "Change postal code"
265
269
  };
266
270
  function addressFieldsMessages(locale, overrides) {
267
271
  const base = (locale ?? "es-MX").toLowerCase().startsWith("en") ? EN2 : ES2;
@@ -287,8 +291,10 @@ function lockField(el) {
287
291
  if (el instanceof HTMLSelectElement) {
288
292
  const name = el.getAttribute("name");
289
293
  if (name) {
294
+ el.dataset.sfName = name;
290
295
  const hidden = document.createElement("input");
291
296
  hidden.type = "hidden";
297
+ hidden.dataset.sfLockMirror = "1";
292
298
  hidden.name = name;
293
299
  hidden.value = el.value;
294
300
  el.insertAdjacentElement("afterend", hidden);
@@ -299,12 +305,27 @@ function lockField(el) {
299
305
  el.readOnly = true;
300
306
  }
301
307
  }
308
+ function unlockField(el) {
309
+ if (el.dataset.sfLocked !== "1") return;
310
+ delete el.dataset.sfLocked;
311
+ el.removeAttribute("aria-readonly");
312
+ el.removeAttribute("title");
313
+ if (el instanceof HTMLSelectElement) {
314
+ const mirror = el.nextElementSibling;
315
+ if (mirror instanceof HTMLInputElement && mirror.dataset.sfLockMirror === "1") mirror.remove();
316
+ if (el.dataset.sfName) el.setAttribute("name", el.dataset.sfName);
317
+ delete el.dataset.sfName;
318
+ el.disabled = false;
319
+ } else {
320
+ el.readOnly = false;
321
+ }
322
+ }
302
323
  var ADDRESS_FILL_EVENT_FLAG = "sfSynthetic";
303
324
  function isAddressFillEvent(event) {
304
325
  return event[ADDRESS_FILL_EVENT_FLAG] === true;
305
326
  }
306
327
  function AddressFields(props) {
307
- const { defaults, names = {}, errors = {}, countryCode, provinces, postalCode, lookupPostalCode, lockProvinceOnFill, locale, messages, fieldMessages: fm, classNames: cx = {}, children } = props;
328
+ const { defaults, names = {}, errors = {}, countryCode, provinces, postalCode, lookupPostalCode, lockProvinceOnFill = true, locale, messages, fieldMessages: fm, classNames: cx = {}, children } = props;
308
329
  const showNeighborhood = props.neighborhood ?? true;
309
330
  const showReference = props.reference ?? true;
310
331
  const m = addressFieldsMessages(locale, messages);
@@ -313,6 +334,7 @@ function AddressFields(props) {
313
334
  const listId = react.useId();
314
335
  const [colonias, setColonias] = react.useState([]);
315
336
  const [filled, setFilled] = react.useState(false);
337
+ const [provinceLocked, setProvinceLocked] = react.useState(false);
316
338
  const [coloniaSelect, setColoniaSelect] = react.useState(false);
317
339
  const coloniaSelectRef = react.useRef(false);
318
340
  const coloniaCarry = react.useRef(null);
@@ -343,6 +365,31 @@ function AddressFields(props) {
343
365
  form.removeEventListener("change", mark, true);
344
366
  };
345
367
  }, []);
368
+ const releaseLookup = () => {
369
+ const form = zipRef.current?.closest("form");
370
+ if (!form) return;
371
+ const locked = form.querySelector('[data-sf-locked="1"]');
372
+ const touched = [];
373
+ if (locked) {
374
+ unlockField(locked);
375
+ touched.push(locked);
376
+ }
377
+ for (const key of ["province", "city", "neighborhood"]) {
378
+ const el = form.elements.namedItem(n(key));
379
+ if (el && !("length" in el && !(el instanceof HTMLSelectElement)) && !touched.includes(el)) touched.push(el);
380
+ }
381
+ for (const el of touched) {
382
+ if (el.dataset.sfAutofilled !== "1") continue;
383
+ el.value = "";
384
+ delete el.dataset.sfAutofilled;
385
+ const event = new Event("input", { bubbles: true });
386
+ Object.defineProperty(event, ADDRESS_FILL_EVENT_FLAG, { value: true });
387
+ el.dispatchEvent(event);
388
+ }
389
+ lastZip.current = null;
390
+ setProvinceLocked(false);
391
+ setFilled(false);
392
+ };
346
393
  const runLookup = (zip) => {
347
394
  const lookup = lookupRef.current;
348
395
  if (!lookup || !zip || zip === lastZip.current) return;
@@ -366,7 +413,11 @@ function AddressFields(props) {
366
413
  const event = new Event("input", { bubbles: true });
367
414
  Object.defineProperty(event, ADDRESS_FILL_EVENT_FLAG, { value: true });
368
415
  el2.dispatchEvent(event);
369
- if (lock) lockField(el2);
416
+ if (lock) {
417
+ lockField(el2);
418
+ el2.title = m.provinceLocked;
419
+ setProvinceLocked(true);
420
+ }
370
421
  return true;
371
422
  };
372
423
  const a = fill2(n("province"), info.province, lockProvinceOnFill);
@@ -414,6 +465,22 @@ function AddressFields(props) {
414
465
  const zip = defaults?.zip;
415
466
  if (zip) runLookup(zip);
416
467
  }, []);
468
+ const hasLock = (root) => !!root?.closest("form")?.querySelector('[data-sf-locked="1"]');
469
+ const changeZip = () => {
470
+ releaseLookup();
471
+ const zipEl = zipRef.current?.querySelector("input");
472
+ zipEl?.focus();
473
+ zipEl?.select();
474
+ };
475
+ const provinceHint = provinceLocked ? /* @__PURE__ */ jsxRuntime.jsxs("span", { "data-sf-province-locked": "", title: m.provinceLocked, children: [
476
+ /* @__PURE__ */ jsxRuntime.jsxs("svg", { "aria-hidden": "true", width: "12", height: "12", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", style: { display: "inline-block", verticalAlign: "-1px", marginRight: 4 }, children: [
477
+ /* @__PURE__ */ jsxRuntime.jsx("rect", { x: "4", y: "11", width: "16", height: "10", rx: "2" }),
478
+ /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M8 11V7a4 4 0 0 1 8 0v4" })
479
+ ] }),
480
+ m.provinceLocked,
481
+ " \xB7 ",
482
+ /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", "data-sf-change-zip": "", onClick: changeZip, style: { background: "none", border: 0, padding: 0, font: "inherit", color: "inherit", textDecoration: "underline", cursor: "pointer" }, children: m.changePostalCode })
483
+ ] }) : void 0;
417
484
  const numericZip = postalCode?.inputMode === "numeric";
418
485
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
419
486
  /* @__PURE__ */ jsxRuntime.jsx("div", { ref: zipRef, className: join(cx.half), "data-sf-zip": true, children: /* @__PURE__ */ jsxRuntime.jsx(
@@ -433,15 +500,16 @@ function AddressFields(props) {
433
500
  classNames: { ...cx, root: cx.root },
434
501
  onInput: (e) => {
435
502
  const v = e.currentTarget.value.trim();
503
+ if (v !== lastZip.current && hasLock(zipRef.current)) releaseLookup();
436
504
  if (postalCode?.maxLength && v.length === postalCode.maxLength) runLookup(v);
437
505
  },
438
506
  onBlur: (e) => runLookup(e.currentTarget.value.trim())
439
507
  }
440
508
  ) }),
441
- provinces && provinces.length ? /* @__PURE__ */ jsxRuntime.jsxs(CheckoutField, { ...common, as: "select", name: n("province"), label: m.province, autoComplete: "address-level1", defaultValue: defaults?.province ?? "", error: errors[n("province")] ?? null, classNames: field(cx, "half"), children: [
509
+ provinces && provinces.length ? /* @__PURE__ */ jsxRuntime.jsxs(CheckoutField, { ...common, as: "select", name: n("province"), label: m.province, autoComplete: "address-level1", defaultValue: defaults?.province ?? "", error: errors[n("province")] ?? null, classNames: field(cx, "half"), hint: provinceHint, children: [
442
510
  /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", disabled: true, children: m.provincePlaceholder }),
443
511
  provinces.map((p) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: p.name, children: p.name }, p.code ?? p.name))
444
- ] }) : /* @__PURE__ */ jsxRuntime.jsx(CheckoutField, { ...common, name: n("province"), label: m.province, autoComplete: "address-level1", defaultValue: defaults?.province ?? "", error: errors[n("province")] ?? null, classNames: field(cx, "half") }),
512
+ ] }) : /* @__PURE__ */ jsxRuntime.jsx(CheckoutField, { ...common, name: n("province"), label: m.province, autoComplete: "address-level1", defaultValue: defaults?.province ?? "", error: errors[n("province")] ?? null, classNames: field(cx, "half"), hint: provinceHint }),
445
513
  /* @__PURE__ */ jsxRuntime.jsx(CheckoutField, { ...common, name: n("city"), label: m.city, autoComplete: "address-level2", defaultValue: defaults?.city ?? "", error: errors[n("city")] ?? null, classNames: field(cx, "half") }),
446
514
  showNeighborhood && coloniaSelect ? /* @__PURE__ */ jsxRuntime.jsxs(
447
515
  CheckoutField,