@ozura/elements 1.3.1-next.73 → 1.3.1-next.75

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.
@@ -1064,6 +1064,8 @@ class OzVault {
1064
1064
  this.bankTokenizeResolvers = new Map();
1065
1065
  // Track completion state per element for auto-advance (only fire on transition)
1066
1066
  this.completionState = new Map();
1067
+ // Latest per-field state for getFieldStates(), keyed by frameId.
1068
+ this.fieldStates = new Map();
1067
1069
  this.tokenizerFrame = null;
1068
1070
  this.tokenizerWindow = null;
1069
1071
  this.tokenizerReady = false;
@@ -1261,13 +1263,41 @@ class OzVault {
1261
1263
  * });
1262
1264
  */
1263
1265
  get isComplete() {
1264
- if (this.elements.size === 0)
1266
+ return this.allComplete([...this.elementsByType.values()]);
1267
+ }
1268
+ /**
1269
+ * Like {@link isComplete}, but for bank-account elements created via
1270
+ * {@link createBankElement}. Card and bank fields are tracked separately so a
1271
+ * card-only checkout is never gated on bank fields (and vice versa), matching
1272
+ * the `createToken()` / `createBankToken()` split. A vault with both card and
1273
+ * bank elements exposes each completion state independently.
1274
+ */
1275
+ get isBankComplete() {
1276
+ return this.allComplete([...this.bankElementsByType.values()]);
1277
+ }
1278
+ /** True iff the set is non-empty and every element has reported complete-and-valid. */
1279
+ allComplete(els) {
1280
+ if (els.length === 0)
1265
1281
  return false;
1266
- for (const frameId of this.elements.keys()) {
1267
- if (this.completionState.get(frameId) !== true)
1268
- return false;
1282
+ return els.every(el => this.completionState.get(el.frameId) === true);
1283
+ }
1284
+ /**
1285
+ * Snapshot of every created field's latest state, keyed by element type.
1286
+ * Card and bank fields are combined (their type keys never collide). Useful for
1287
+ * gating UI, rendering per-field errors, or showing the card brand without
1288
+ * wiring an onChange listener per element. Returns shallow copies so callers
1289
+ * cannot mutate the vault's internal state.
1290
+ */
1291
+ getFieldStates() {
1292
+ var _a, _b;
1293
+ const snapshot = {};
1294
+ for (const [type, el] of this.elementsByType) {
1295
+ snapshot[type] = Object.assign({}, ((_a = this.fieldStates.get(el.frameId)) !== null && _a !== void 0 ? _a : { empty: true, complete: false, valid: false }));
1296
+ }
1297
+ for (const [type, el] of this.bankElementsByType) {
1298
+ snapshot[type] = Object.assign({}, ((_b = this.fieldStates.get(el.frameId)) !== null && _b !== void 0 ? _b : { empty: true, complete: false, valid: false }));
1269
1299
  }
1270
- return true;
1300
+ return snapshot;
1271
1301
  }
1272
1302
  /**
1273
1303
  * `true` while a `createToken()` or `createBankToken()` call is in progress
@@ -1321,6 +1351,7 @@ class OzVault {
1321
1351
  if (existing) {
1322
1352
  this.elements.delete(existing.frameId);
1323
1353
  this.completionState.delete(existing.frameId);
1354
+ this.fieldStates.delete(existing.frameId);
1324
1355
  existing.destroy();
1325
1356
  }
1326
1357
  const el = new OzElement(type, options, this.vaultId, this.frameBaseUrl, this.fonts, this.resolvedAppearance, () => {
@@ -1328,9 +1359,11 @@ class OzVault {
1328
1359
  // don't grow unboundedly in SPA scenarios with repeated mount/unmount cycles.
1329
1360
  this.elements.delete(el.frameId);
1330
1361
  this.completionState.delete(el.frameId);
1362
+ this.fieldStates.delete(el.frameId);
1331
1363
  }, this._debug);
1332
1364
  this.elements.set(el.frameId, el);
1333
1365
  typeMap.set(type, el);
1366
+ this.fieldStates.set(el.frameId, { empty: true, complete: false, valid: false });
1334
1367
  return el;
1335
1368
  }
1336
1369
  /**
@@ -1630,6 +1663,10 @@ class OzVault {
1630
1663
  for (const frameId of this.completionState.keys()) {
1631
1664
  this.completionState.set(frameId, false);
1632
1665
  }
1666
+ // Mirror for field states: every created field returns to its default.
1667
+ for (const frameId of this.fieldStates.keys()) {
1668
+ this.fieldStates.set(frameId, { empty: true, complete: false, valid: false });
1669
+ }
1633
1670
  // NOTE: _tokenizeSuccessCount is intentionally NOT reset.
1634
1671
  // It reflects real server-side wax key budget consumption. Zeroing it
1635
1672
  // would desync the proactive refresh logic from the vault's state and
@@ -1677,6 +1714,7 @@ class OzVault {
1677
1714
  this.elementsByType.clear();
1678
1715
  this.bankElementsByType.clear();
1679
1716
  this.completionState.clear();
1717
+ this.fieldStates.clear();
1680
1718
  this._tokenizeSuccessCount = 0;
1681
1719
  (_a = this.tokenizerFrame) === null || _a === void 0 ? void 0 : _a.remove();
1682
1720
  this.tokenizerFrame = null;
@@ -1881,6 +1919,8 @@ class OzVault {
1881
1919
  const valid = msg.valid;
1882
1920
  const wasComplete = (_a = this.completionState.get(el.frameId)) !== null && _a !== void 0 ? _a : false;
1883
1921
  this.completionState.set(el.frameId, complete && valid);
1922
+ this.fieldStates.set(el.frameId, Object.assign(Object.assign({ empty: msg.empty, complete,
1923
+ valid }, (typeof msg.error === 'string' ? { error: msg.error } : {})), (typeof msg.cardBrand === 'string' ? { cardBrand: msg.cardBrand } : {})));
1884
1924
  // Require valid too — avoids advancing at 13 digits for unknown-brand cards
1885
1925
  // where isComplete() fires before the user has finished typing.
1886
1926
  const justCompleted = complete && valid && !wasComplete;
@@ -2459,13 +2499,39 @@ function useOzElements() {
2459
2499
  notifyChange();
2460
2500
  }
2461
2501
  };
2462
- const reset = () => { var _a; (_a = vault.value) === null || _a === void 0 ? void 0 : _a.reset(); };
2502
+ const reset = () => {
2503
+ var _a;
2504
+ (_a = vault.value) === null || _a === void 0 ? void 0 : _a.reset();
2505
+ // reset() clears completion state and cancels any in-flight tokenization;
2506
+ // bump changeTick so the derived computeds below recompute.
2507
+ notifyChange();
2508
+ };
2463
2509
  // `void changeTick.value` registers a reactive dependency so these recompute when
2464
2510
  // notifyChange() fires (field change / tokenize start-stop). vault.isComplete and
2465
2511
  // vault.isTokenizing read non-reactive internal Maps/flags that Vue cannot track.
2466
2512
  const isComplete = computed(() => { var _a, _b; void changeTick.value; return (_b = (_a = vault.value) === null || _a === void 0 ? void 0 : _a.isComplete) !== null && _b !== void 0 ? _b : false; });
2513
+ const isBankComplete = computed(() => { var _a, _b; void changeTick.value; return (_b = (_a = vault.value) === null || _a === void 0 ? void 0 : _a.isBankComplete) !== null && _b !== void 0 ? _b : false; });
2467
2514
  const isTokenizing = computed(() => { var _a, _b; void changeTick.value; return (_b = (_a = vault.value) === null || _a === void 0 ? void 0 : _a.isTokenizing) !== null && _b !== void 0 ? _b : false; });
2468
- return { createToken, createBankToken, ready, initError, tokenizeCount, reset, isComplete, isTokenizing };
2515
+ return { createToken, createBankToken, ready, initError, tokenizeCount, reset, isComplete, isBankComplete, isTokenizing };
2516
+ }
2517
+ /**
2518
+ * Reactive snapshot of every created field's state, keyed by element type
2519
+ * (e.g. `cardNumber`, `cvv`, `accountNumber`). Each entry is
2520
+ * `{ empty, complete, valid, error?, cardBrand? }`. Recomputes whenever any
2521
+ * field fires a change event.
2522
+ *
2523
+ * @throws if called outside an `<OzElements>` provider tree.
2524
+ * @example
2525
+ * const fields = useFieldStates();
2526
+ * // fields.value.cvv?.error
2527
+ */
2528
+ function useFieldStates() {
2529
+ const ctx = inject(OZ_KEY);
2530
+ if (!ctx) {
2531
+ throw new Error('[OzVault] useFieldStates() must be called inside an <OzElements> provider');
2532
+ }
2533
+ const { vault, changeTick } = ctx;
2534
+ return computed(() => { var _a, _b; void changeTick.value; return (_b = (_a = vault.value) === null || _a === void 0 ? void 0 : _a.getFieldStates()) !== null && _b !== void 0 ? _b : {}; });
2469
2535
  }
2470
2536
  function createFieldComponent(displayName, mountElement) {
2471
2537
  return defineComponent({
@@ -2527,5 +2593,5 @@ const OzBankAccountNumber = createFieldComponent('OzBankAccountNumber', (v, opts
2527
2593
  /** Bank routing number field. Emits `change` (ElementChangeEvent), `focus`, `blur`. */
2528
2594
  const OzBankRoutingNumber = createFieldComponent('OzBankRoutingNumber', (v, opts) => v.createBankElement('routingNumber', opts));
2529
2595
 
2530
- export { OzBankAccountNumber, OzBankRoutingNumber, OzCardNumber, OzCvv, OzElements, OzExpiry, useOzElements };
2596
+ export { OzBankAccountNumber, OzBankRoutingNumber, OzCardNumber, OzCvv, OzElements, OzExpiry, useFieldStates, useOzElements };
2531
2597
  //# sourceMappingURL=index.esm.js.map