@asteby/storefront-react 0.17.0 → 0.18.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
@@ -236,6 +236,7 @@ var ES2 = {
236
236
  address2: "N\xFAmero interior o departamento",
237
237
  reference: "Referencias para la entrega",
238
238
  provincePlaceholder: "Elige tu estado",
239
+ neighborhoodPlaceholder: "Elige tu colonia",
239
240
  address1Hint: "Por ejemplo: Av. Insurgentes Sur 1234",
240
241
  referenceHint: "Entre qu\xE9 calles, color de la fachada\u2026",
241
242
  emailHint: "Te enviamos aqu\xED la confirmaci\xF3n y el seguimiento.",
@@ -255,6 +256,7 @@ var EN2 = {
255
256
  address2: "Apartment, suite",
256
257
  reference: "Delivery notes",
257
258
  provincePlaceholder: "Choose your state",
259
+ neighborhoodPlaceholder: "Choose your neighborhood",
258
260
  address1Hint: "For example: 1234 Main St",
259
261
  referenceHint: "Cross streets, building color\u2026",
260
262
  emailHint: "We send the confirmation and tracking here.",
@@ -311,6 +313,9 @@ function AddressFields(props) {
311
313
  const listId = react.useId();
312
314
  const [colonias, setColonias] = react.useState([]);
313
315
  const [filled, setFilled] = react.useState(false);
316
+ const [coloniaSelect, setColoniaSelect] = react.useState(false);
317
+ const coloniaSelectRef = react.useRef(false);
318
+ const coloniaCarry = react.useRef(null);
314
319
  const lastZip = react.useRef(null);
315
320
  const zipRef = react.useRef(null);
316
321
  const lookupRef = react.useRef(lookupPostalCode);
@@ -350,18 +355,18 @@ function AddressFields(props) {
350
355
  enqueue(() => {
351
356
  if (lastZip.current !== zip) return;
352
357
  const fill2 = (name, value, lock) => {
353
- const el = form?.elements.namedItem(name);
354
- if (!el || !value || "length" in el && !(el instanceof HTMLSelectElement)) return false;
355
- if (el.dataset.sfLocked === "1") return false;
356
- if (el.dataset.sfUserEdited === "1" || el.ownerDocument.activeElement === el && el.value) return false;
357
- if (el.value && el.dataset.sfAutofilled !== "1") return false;
358
- if (el instanceof HTMLSelectElement && ![...el.options].some((o) => o.value === value)) return false;
359
- el.value = value;
360
- el.dataset.sfAutofilled = "1";
358
+ const el2 = form?.elements.namedItem(name);
359
+ if (!el2 || !value || "length" in el2 && !(el2 instanceof HTMLSelectElement)) return false;
360
+ if (el2.dataset.sfLocked === "1") return false;
361
+ if (el2.dataset.sfUserEdited === "1" || el2.ownerDocument.activeElement === el2 && el2.value) return false;
362
+ if (el2.value && el2.dataset.sfAutofilled !== "1") return false;
363
+ if (el2 instanceof HTMLSelectElement && ![...el2.options].some((o) => o.value === value)) return false;
364
+ el2.value = value;
365
+ el2.dataset.sfAutofilled = "1";
361
366
  const event = new Event("input", { bubbles: true });
362
367
  Object.defineProperty(event, ADDRESS_FILL_EVENT_FLAG, { value: true });
363
- el.dispatchEvent(event);
364
- if (lock) lockField(el);
368
+ el2.dispatchEvent(event);
369
+ if (lock) lockField(el2);
365
370
  return true;
366
371
  };
367
372
  const a = fill2(n("province"), info.province, lockProvinceOnFill);
@@ -369,13 +374,42 @@ function AddressFields(props) {
369
374
  const only = info.neighborhoods?.length === 1 ? info.neighborhoods[0] : null;
370
375
  const c = showNeighborhood ? fill2(n("neighborhood"), only) : false;
371
376
  setColonias(info.neighborhoods ?? []);
372
- setFilled(a || b || c);
377
+ const list = info.neighborhoods ?? [];
378
+ const nameEl = form?.elements.namedItem(n("neighborhood"));
379
+ const el = nameEl && !("length" in nameEl && !(nameEl instanceof HTMLSelectElement)) ? nameEl : null;
380
+ const typed = el ? el.value : "";
381
+ const wantSelect = props.neighborhoodList === "select" && showNeighborhood !== false && list.length > 1 && (!typed || list.includes(typed));
382
+ let carried = false;
383
+ if (showNeighborhood && wantSelect !== coloniaSelectRef.current) {
384
+ const userEdited = el?.dataset.sfUserEdited === "1";
385
+ const value = only && !userEdited ? only : wantSelect ? list.includes(typed) ? typed : "" : typed;
386
+ coloniaCarry.current = { value, autofilled: !userEdited && (!!only || el?.dataset.sfAutofilled === "1"), userEdited };
387
+ carried = !!only && !userEdited;
388
+ }
389
+ coloniaSelectRef.current = wantSelect;
390
+ setColoniaSelect(wantSelect);
391
+ setFilled(a || b || c || carried);
373
392
  });
374
393
  },
375
394
  () => {
376
395
  }
377
396
  );
378
397
  };
398
+ react.useEffect(() => {
399
+ const carry = coloniaCarry.current;
400
+ coloniaCarry.current = null;
401
+ if (!carry || !carry.value) return;
402
+ const form = zipRef.current?.closest("form");
403
+ const el = form?.elements.namedItem(n("neighborhood"));
404
+ if (!el || "length" in el && !(el instanceof HTMLSelectElement)) return;
405
+ if (el instanceof HTMLSelectElement && ![...el.options].some((o) => o.value === carry.value)) return;
406
+ el.value = carry.value;
407
+ if (carry.userEdited) el.dataset.sfUserEdited = "1";
408
+ else if (carry.autofilled) el.dataset.sfAutofilled = "1";
409
+ const event = new Event("input", { bubbles: true });
410
+ Object.defineProperty(event, ADDRESS_FILL_EVENT_FLAG, { value: true });
411
+ el.dispatchEvent(event);
412
+ }, [coloniaSelect]);
379
413
  react.useEffect(() => {
380
414
  const zip = defaults?.zip;
381
415
  if (zip) runLookup(zip);
@@ -409,7 +443,24 @@ function AddressFields(props) {
409
443
  provinces.map((p) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: p.name, children: p.name }, p.code ?? p.name))
410
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") }),
411
445
  /* @__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") }),
412
- showNeighborhood ? /* @__PURE__ */ jsxRuntime.jsx(
446
+ showNeighborhood && coloniaSelect ? /* @__PURE__ */ jsxRuntime.jsxs(
447
+ CheckoutField,
448
+ {
449
+ ...common,
450
+ as: "select",
451
+ name: n("neighborhood"),
452
+ label: m.neighborhood,
453
+ autoComplete: "address-level3",
454
+ optional: showNeighborhood !== "required",
455
+ defaultValue: colonias.includes(defaults?.neighborhood ?? "") ? defaults?.neighborhood ?? "" : "",
456
+ error: errors[n("neighborhood")] ?? null,
457
+ classNames: field(cx, "half"),
458
+ children: [
459
+ /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", disabled: showNeighborhood === "required", children: m.neighborhoodPlaceholder }),
460
+ colonias.map((c) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: c, children: c }, c))
461
+ ]
462
+ }
463
+ ) : showNeighborhood ? /* @__PURE__ */ jsxRuntime.jsx(
413
464
  CheckoutField,
414
465
  {
415
466
  ...common,
@@ -420,10 +471,10 @@ function AddressFields(props) {
420
471
  defaultValue: defaults?.neighborhood ?? "",
421
472
  error: errors[n("neighborhood")] ?? null,
422
473
  classNames: field(cx, "half"),
423
- ...colonias.length ? { list: listId } : {}
474
+ ...colonias.length && !coloniaSelect ? { list: listId } : {}
424
475
  }
425
476
  ) : null,
426
- colonias.length ? /* @__PURE__ */ jsxRuntime.jsx("datalist", { id: listId, children: colonias.map((c) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: c }, c)) }) : null,
477
+ colonias.length && !coloniaSelect ? /* @__PURE__ */ jsxRuntime.jsx("datalist", { id: listId, children: colonias.map((c) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: c }, c)) }) : null,
427
478
  /* @__PURE__ */ jsxRuntime.jsx("p", { role: "status", "aria-live": "polite", "data-sf-zip-status": true, style: filled ? void 0 : { position: "absolute", width: 1, height: 1, overflow: "hidden", clip: "rect(0 0 0 0)", whiteSpace: "nowrap" }, className: filled ? join(cx.hint, cx.wide) : void 0, children: filled ? m.lookupFilled : "" }),
428
479
  /* @__PURE__ */ jsxRuntime.jsx(CheckoutField, { ...common, name: n("address1"), label: m.address1, autoComplete: "address-line1", defaultValue: defaults?.address1 ?? "", error: errors[n("address1")] ?? null, hint: m.address1Hint, classNames: field(cx, "wide") }),
429
480
  /* @__PURE__ */ jsxRuntime.jsx(CheckoutField, { ...common, name: n("address2"), label: m.address2, autoComplete: "address-line2", optional: true, defaultValue: defaults?.address2 ?? "", error: errors[n("address2")] ?? null, classNames: field(cx, "half") }),