@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.
@@ -1067,6 +1067,8 @@ class OzVault {
1067
1067
  this.bankTokenizeResolvers = new Map();
1068
1068
  // Track completion state per element for auto-advance (only fire on transition)
1069
1069
  this.completionState = new Map();
1070
+ // Latest per-field state for getFieldStates(), keyed by frameId.
1071
+ this.fieldStates = new Map();
1070
1072
  this.tokenizerFrame = null;
1071
1073
  this.tokenizerWindow = null;
1072
1074
  this.tokenizerReady = false;
@@ -1264,13 +1266,41 @@ class OzVault {
1264
1266
  * });
1265
1267
  */
1266
1268
  get isComplete() {
1267
- if (this.elements.size === 0)
1269
+ return this.allComplete([...this.elementsByType.values()]);
1270
+ }
1271
+ /**
1272
+ * Like {@link isComplete}, but for bank-account elements created via
1273
+ * {@link createBankElement}. Card and bank fields are tracked separately so a
1274
+ * card-only checkout is never gated on bank fields (and vice versa), matching
1275
+ * the `createToken()` / `createBankToken()` split. A vault with both card and
1276
+ * bank elements exposes each completion state independently.
1277
+ */
1278
+ get isBankComplete() {
1279
+ return this.allComplete([...this.bankElementsByType.values()]);
1280
+ }
1281
+ /** True iff the set is non-empty and every element has reported complete-and-valid. */
1282
+ allComplete(els) {
1283
+ if (els.length === 0)
1268
1284
  return false;
1269
- for (const frameId of this.elements.keys()) {
1270
- if (this.completionState.get(frameId) !== true)
1271
- return false;
1285
+ return els.every(el => this.completionState.get(el.frameId) === true);
1286
+ }
1287
+ /**
1288
+ * Snapshot of every created field's latest state, keyed by element type.
1289
+ * Card and bank fields are combined (their type keys never collide). Useful for
1290
+ * gating UI, rendering per-field errors, or showing the card brand without
1291
+ * wiring an onChange listener per element. Returns shallow copies so callers
1292
+ * cannot mutate the vault's internal state.
1293
+ */
1294
+ getFieldStates() {
1295
+ var _a, _b;
1296
+ const snapshot = {};
1297
+ for (const [type, el] of this.elementsByType) {
1298
+ snapshot[type] = Object.assign({}, ((_a = this.fieldStates.get(el.frameId)) !== null && _a !== void 0 ? _a : { empty: true, complete: false, valid: false }));
1272
1299
  }
1273
- return true;
1300
+ for (const [type, el] of this.bankElementsByType) {
1301
+ snapshot[type] = Object.assign({}, ((_b = this.fieldStates.get(el.frameId)) !== null && _b !== void 0 ? _b : { empty: true, complete: false, valid: false }));
1302
+ }
1303
+ return snapshot;
1274
1304
  }
1275
1305
  /**
1276
1306
  * `true` while a `createToken()` or `createBankToken()` call is in progress
@@ -1324,6 +1354,7 @@ class OzVault {
1324
1354
  if (existing) {
1325
1355
  this.elements.delete(existing.frameId);
1326
1356
  this.completionState.delete(existing.frameId);
1357
+ this.fieldStates.delete(existing.frameId);
1327
1358
  existing.destroy();
1328
1359
  }
1329
1360
  const el = new OzElement(type, options, this.vaultId, this.frameBaseUrl, this.fonts, this.resolvedAppearance, () => {
@@ -1331,9 +1362,11 @@ class OzVault {
1331
1362
  // don't grow unboundedly in SPA scenarios with repeated mount/unmount cycles.
1332
1363
  this.elements.delete(el.frameId);
1333
1364
  this.completionState.delete(el.frameId);
1365
+ this.fieldStates.delete(el.frameId);
1334
1366
  }, this._debug);
1335
1367
  this.elements.set(el.frameId, el);
1336
1368
  typeMap.set(type, el);
1369
+ this.fieldStates.set(el.frameId, { empty: true, complete: false, valid: false });
1337
1370
  return el;
1338
1371
  }
1339
1372
  /**
@@ -1633,6 +1666,10 @@ class OzVault {
1633
1666
  for (const frameId of this.completionState.keys()) {
1634
1667
  this.completionState.set(frameId, false);
1635
1668
  }
1669
+ // Mirror for field states: every created field returns to its default.
1670
+ for (const frameId of this.fieldStates.keys()) {
1671
+ this.fieldStates.set(frameId, { empty: true, complete: false, valid: false });
1672
+ }
1636
1673
  // NOTE: _tokenizeSuccessCount is intentionally NOT reset.
1637
1674
  // It reflects real server-side wax key budget consumption. Zeroing it
1638
1675
  // would desync the proactive refresh logic from the vault's state and
@@ -1680,6 +1717,7 @@ class OzVault {
1680
1717
  this.elementsByType.clear();
1681
1718
  this.bankElementsByType.clear();
1682
1719
  this.completionState.clear();
1720
+ this.fieldStates.clear();
1683
1721
  this._tokenizeSuccessCount = 0;
1684
1722
  (_a = this.tokenizerFrame) === null || _a === void 0 ? void 0 : _a.remove();
1685
1723
  this.tokenizerFrame = null;
@@ -1884,6 +1922,8 @@ class OzVault {
1884
1922
  const valid = msg.valid;
1885
1923
  const wasComplete = (_a = this.completionState.get(el.frameId)) !== null && _a !== void 0 ? _a : false;
1886
1924
  this.completionState.set(el.frameId, complete && valid);
1925
+ this.fieldStates.set(el.frameId, Object.assign(Object.assign({ empty: msg.empty, complete,
1926
+ valid }, (typeof msg.error === 'string' ? { error: msg.error } : {})), (typeof msg.cardBrand === 'string' ? { cardBrand: msg.cardBrand } : {})));
1887
1927
  // Require valid too — avoids advancing at 13 digits for unknown-brand cards
1888
1928
  // where isComplete() fires before the user has finished typing.
1889
1929
  const justCompleted = complete && valid && !wasComplete;
@@ -2424,7 +2464,7 @@ function OzElements({ sessionUrl, getSessionKey, fetchWaxKey, pubKey, frameBaseU
2424
2464
  * an `<OzElements>` provider tree.
2425
2465
  */
2426
2466
  function useOzElements() {
2427
- var _a, _b;
2467
+ var _a, _b, _c;
2428
2468
  const { vault, initError, mountedCount, readyCount, notifyTokenize, notifyChange, tokenizeCount } = react.useContext(OzContext);
2429
2469
  const createToken = react.useCallback(async (options) => {
2430
2470
  if (!vault) {
@@ -2462,11 +2502,30 @@ function useOzElements() {
2462
2502
  }, [vault, notifyTokenize, notifyChange]);
2463
2503
  const reset = react.useCallback(() => {
2464
2504
  vault === null || vault === void 0 ? void 0 : vault.reset();
2465
- }, [vault]);
2505
+ // reset() clears completion state and cancels any in-flight tokenization, so
2506
+ // re-render to refresh the derived isComplete / isTokenizing getters below.
2507
+ notifyChange();
2508
+ }, [vault, notifyChange]);
2466
2509
  const ready = vault !== null && vault.isReady && mountedCount > 0 && readyCount >= mountedCount;
2467
2510
  const isComplete = (_a = vault === null || vault === void 0 ? void 0 : vault.isComplete) !== null && _a !== void 0 ? _a : false;
2468
- const isTokenizing = (_b = vault === null || vault === void 0 ? void 0 : vault.isTokenizing) !== null && _b !== void 0 ? _b : false;
2469
- return { createToken, createBankToken, reset, ready, initError, tokenizeCount, isComplete, isTokenizing };
2511
+ const isBankComplete = (_b = vault === null || vault === void 0 ? void 0 : vault.isBankComplete) !== null && _b !== void 0 ? _b : false;
2512
+ const isTokenizing = (_c = vault === null || vault === void 0 ? void 0 : vault.isTokenizing) !== null && _c !== void 0 ? _c : false;
2513
+ return { createToken, createBankToken, reset, ready, initError, tokenizeCount, isComplete, isBankComplete, isTokenizing };
2514
+ }
2515
+ /**
2516
+ * Reactive snapshot of every created field's state, keyed by element type
2517
+ * (e.g. `cardNumber`, `cvv`, `accountNumber`). Each entry is
2518
+ * `{ empty, complete, valid, error?, cardBrand? }`. Recomputes whenever any
2519
+ * field fires a change event. Must be called inside an `<OzElements>` provider;
2520
+ * returns `{}` otherwise.
2521
+ *
2522
+ * @example
2523
+ * const states = useFieldStates();
2524
+ * if (states.cvv?.error) showCvvError(states.cvv.error);
2525
+ */
2526
+ function useFieldStates() {
2527
+ const { vault, changeTick } = react.useContext(OzContext);
2528
+ return react.useMemo(() => { var _a; return (_a = vault === null || vault === void 0 ? void 0 : vault.getFieldStates()) !== null && _a !== void 0 ? _a : {}; }, [vault, changeTick]);
2470
2529
  }
2471
2530
  const SKELETON_STYLE = {
2472
2531
  height: 46,
@@ -2775,5 +2834,6 @@ exports.OzElements = OzElements;
2775
2834
  exports.OzExpiry = OzExpiry;
2776
2835
  exports.createFetchWaxKey = createSessionFetcher;
2777
2836
  exports.createSessionFetcher = createSessionFetcher;
2837
+ exports.useFieldStates = useFieldStates;
2778
2838
  exports.useOzElements = useOzElements;
2779
2839
  //# sourceMappingURL=index.cjs.js.map