@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.
@@ -1065,6 +1065,8 @@ class OzVault {
1065
1065
  this.bankTokenizeResolvers = new Map();
1066
1066
  // Track completion state per element for auto-advance (only fire on transition)
1067
1067
  this.completionState = new Map();
1068
+ // Latest per-field state for getFieldStates(), keyed by frameId.
1069
+ this.fieldStates = new Map();
1068
1070
  this.tokenizerFrame = null;
1069
1071
  this.tokenizerWindow = null;
1070
1072
  this.tokenizerReady = false;
@@ -1262,13 +1264,41 @@ class OzVault {
1262
1264
  * });
1263
1265
  */
1264
1266
  get isComplete() {
1265
- if (this.elements.size === 0)
1267
+ return this.allComplete([...this.elementsByType.values()]);
1268
+ }
1269
+ /**
1270
+ * Like {@link isComplete}, but for bank-account elements created via
1271
+ * {@link createBankElement}. Card and bank fields are tracked separately so a
1272
+ * card-only checkout is never gated on bank fields (and vice versa), matching
1273
+ * the `createToken()` / `createBankToken()` split. A vault with both card and
1274
+ * bank elements exposes each completion state independently.
1275
+ */
1276
+ get isBankComplete() {
1277
+ return this.allComplete([...this.bankElementsByType.values()]);
1278
+ }
1279
+ /** True iff the set is non-empty and every element has reported complete-and-valid. */
1280
+ allComplete(els) {
1281
+ if (els.length === 0)
1266
1282
  return false;
1267
- for (const frameId of this.elements.keys()) {
1268
- if (this.completionState.get(frameId) !== true)
1269
- return false;
1283
+ return els.every(el => this.completionState.get(el.frameId) === true);
1284
+ }
1285
+ /**
1286
+ * Snapshot of every created field's latest state, keyed by element type.
1287
+ * Card and bank fields are combined (their type keys never collide). Useful for
1288
+ * gating UI, rendering per-field errors, or showing the card brand without
1289
+ * wiring an onChange listener per element. Returns shallow copies so callers
1290
+ * cannot mutate the vault's internal state.
1291
+ */
1292
+ getFieldStates() {
1293
+ var _a, _b;
1294
+ const snapshot = {};
1295
+ for (const [type, el] of this.elementsByType) {
1296
+ snapshot[type] = Object.assign({}, ((_a = this.fieldStates.get(el.frameId)) !== null && _a !== void 0 ? _a : { empty: true, complete: false, valid: false }));
1270
1297
  }
1271
- return true;
1298
+ for (const [type, el] of this.bankElementsByType) {
1299
+ snapshot[type] = Object.assign({}, ((_b = this.fieldStates.get(el.frameId)) !== null && _b !== void 0 ? _b : { empty: true, complete: false, valid: false }));
1300
+ }
1301
+ return snapshot;
1272
1302
  }
1273
1303
  /**
1274
1304
  * `true` while a `createToken()` or `createBankToken()` call is in progress
@@ -1322,6 +1352,7 @@ class OzVault {
1322
1352
  if (existing) {
1323
1353
  this.elements.delete(existing.frameId);
1324
1354
  this.completionState.delete(existing.frameId);
1355
+ this.fieldStates.delete(existing.frameId);
1325
1356
  existing.destroy();
1326
1357
  }
1327
1358
  const el = new OzElement(type, options, this.vaultId, this.frameBaseUrl, this.fonts, this.resolvedAppearance, () => {
@@ -1329,9 +1360,11 @@ class OzVault {
1329
1360
  // don't grow unboundedly in SPA scenarios with repeated mount/unmount cycles.
1330
1361
  this.elements.delete(el.frameId);
1331
1362
  this.completionState.delete(el.frameId);
1363
+ this.fieldStates.delete(el.frameId);
1332
1364
  }, this._debug);
1333
1365
  this.elements.set(el.frameId, el);
1334
1366
  typeMap.set(type, el);
1367
+ this.fieldStates.set(el.frameId, { empty: true, complete: false, valid: false });
1335
1368
  return el;
1336
1369
  }
1337
1370
  /**
@@ -1631,6 +1664,10 @@ class OzVault {
1631
1664
  for (const frameId of this.completionState.keys()) {
1632
1665
  this.completionState.set(frameId, false);
1633
1666
  }
1667
+ // Mirror for field states: every created field returns to its default.
1668
+ for (const frameId of this.fieldStates.keys()) {
1669
+ this.fieldStates.set(frameId, { empty: true, complete: false, valid: false });
1670
+ }
1634
1671
  // NOTE: _tokenizeSuccessCount is intentionally NOT reset.
1635
1672
  // It reflects real server-side wax key budget consumption. Zeroing it
1636
1673
  // would desync the proactive refresh logic from the vault's state and
@@ -1678,6 +1715,7 @@ class OzVault {
1678
1715
  this.elementsByType.clear();
1679
1716
  this.bankElementsByType.clear();
1680
1717
  this.completionState.clear();
1718
+ this.fieldStates.clear();
1681
1719
  this._tokenizeSuccessCount = 0;
1682
1720
  (_a = this.tokenizerFrame) === null || _a === void 0 ? void 0 : _a.remove();
1683
1721
  this.tokenizerFrame = null;
@@ -1882,6 +1920,8 @@ class OzVault {
1882
1920
  const valid = msg.valid;
1883
1921
  const wasComplete = (_a = this.completionState.get(el.frameId)) !== null && _a !== void 0 ? _a : false;
1884
1922
  this.completionState.set(el.frameId, complete && valid);
1923
+ this.fieldStates.set(el.frameId, Object.assign(Object.assign({ empty: msg.empty, complete,
1924
+ valid }, (typeof msg.error === 'string' ? { error: msg.error } : {})), (typeof msg.cardBrand === 'string' ? { cardBrand: msg.cardBrand } : {})));
1885
1925
  // Require valid too — avoids advancing at 13 digits for unknown-brand cards
1886
1926
  // where isComplete() fires before the user has finished typing.
1887
1927
  const justCompleted = complete && valid && !wasComplete;
@@ -2422,7 +2462,7 @@ function OzElements({ sessionUrl, getSessionKey, fetchWaxKey, pubKey, frameBaseU
2422
2462
  * an `<OzElements>` provider tree.
2423
2463
  */
2424
2464
  function useOzElements() {
2425
- var _a, _b;
2465
+ var _a, _b, _c;
2426
2466
  const { vault, initError, mountedCount, readyCount, notifyTokenize, notifyChange, tokenizeCount } = useContext(OzContext);
2427
2467
  const createToken = useCallback(async (options) => {
2428
2468
  if (!vault) {
@@ -2460,11 +2500,30 @@ function useOzElements() {
2460
2500
  }, [vault, notifyTokenize, notifyChange]);
2461
2501
  const reset = useCallback(() => {
2462
2502
  vault === null || vault === void 0 ? void 0 : vault.reset();
2463
- }, [vault]);
2503
+ // reset() clears completion state and cancels any in-flight tokenization, so
2504
+ // re-render to refresh the derived isComplete / isTokenizing getters below.
2505
+ notifyChange();
2506
+ }, [vault, notifyChange]);
2464
2507
  const ready = vault !== null && vault.isReady && mountedCount > 0 && readyCount >= mountedCount;
2465
2508
  const isComplete = (_a = vault === null || vault === void 0 ? void 0 : vault.isComplete) !== null && _a !== void 0 ? _a : false;
2466
- const isTokenizing = (_b = vault === null || vault === void 0 ? void 0 : vault.isTokenizing) !== null && _b !== void 0 ? _b : false;
2467
- return { createToken, createBankToken, reset, ready, initError, tokenizeCount, isComplete, isTokenizing };
2509
+ const isBankComplete = (_b = vault === null || vault === void 0 ? void 0 : vault.isBankComplete) !== null && _b !== void 0 ? _b : false;
2510
+ const isTokenizing = (_c = vault === null || vault === void 0 ? void 0 : vault.isTokenizing) !== null && _c !== void 0 ? _c : false;
2511
+ return { createToken, createBankToken, reset, ready, initError, tokenizeCount, isComplete, isBankComplete, isTokenizing };
2512
+ }
2513
+ /**
2514
+ * Reactive snapshot of every created field's state, keyed by element type
2515
+ * (e.g. `cardNumber`, `cvv`, `accountNumber`). Each entry is
2516
+ * `{ empty, complete, valid, error?, cardBrand? }`. Recomputes whenever any
2517
+ * field fires a change event. Must be called inside an `<OzElements>` provider;
2518
+ * returns `{}` otherwise.
2519
+ *
2520
+ * @example
2521
+ * const states = useFieldStates();
2522
+ * if (states.cvv?.error) showCvvError(states.cvv.error);
2523
+ */
2524
+ function useFieldStates() {
2525
+ const { vault, changeTick } = useContext(OzContext);
2526
+ return useMemo(() => { var _a; return (_a = vault === null || vault === void 0 ? void 0 : vault.getFieldStates()) !== null && _a !== void 0 ? _a : {}; }, [vault, changeTick]);
2468
2527
  }
2469
2528
  const SKELETON_STYLE = {
2470
2529
  height: 46,
@@ -2763,5 +2822,5 @@ function OzBankCard({ style, styles, classNames, labels, labelStyle, labelClassN
2763
2822
  return (jsxs("div", { className: className, style: { width: '100%', display: 'flex', flexDirection: 'column', gap: gapStr }, children: [jsxs("div", { children: [renderLabel(labels === null || labels === void 0 ? void 0 : labels.accountNumber), jsx(OzBankAccountNumber, { style: mergeStyles(style, styles === null || styles === void 0 ? void 0 : styles.accountNumber), className: classNames === null || classNames === void 0 ? void 0 : classNames.accountNumber, placeholder: (_a = placeholders === null || placeholders === void 0 ? void 0 : placeholders.accountNumber) !== null && _a !== void 0 ? _a : 'Account number', disabled: disabled, onChange: (e) => { fieldState.current.accountNumber = e; emitChange(); }, onFocus: () => { var _a; return (_a = onFocusRef.current) === null || _a === void 0 ? void 0 : _a.call(onFocusRef, 'accountNumber'); }, onBlur: () => { var _a; return (_a = onBlurRef.current) === null || _a === void 0 ? void 0 : _a.call(onBlurRef, 'accountNumber'); }, onReady: readyHandlers['accountNumber'] })] }), jsxs("div", { children: [renderLabel(labels === null || labels === void 0 ? void 0 : labels.routingNumber), jsx(OzBankRoutingNumber, { style: mergeStyles(style, styles === null || styles === void 0 ? void 0 : styles.routingNumber), className: classNames === null || classNames === void 0 ? void 0 : classNames.routingNumber, placeholder: (_b = placeholders === null || placeholders === void 0 ? void 0 : placeholders.routingNumber) !== null && _b !== void 0 ? _b : 'Routing number', disabled: disabled, onChange: (e) => { fieldState.current.routingNumber = e; emitChange(); }, onFocus: () => { var _a; return (_a = onFocusRef.current) === null || _a === void 0 ? void 0 : _a.call(onFocusRef, 'routingNumber'); }, onBlur: () => { var _a; return (_a = onBlurRef.current) === null || _a === void 0 ? void 0 : _a.call(onBlurRef, 'routingNumber'); }, onReady: readyHandlers['routingNumber'] })] }), errorNode] }));
2764
2823
  }
2765
2824
 
2766
- export { OzBankAccountNumber, OzBankCard, OzBankRoutingNumber, OzCard, OzCardNumber, OzCvv, OzElements, OzExpiry, createSessionFetcher as createFetchWaxKey, createSessionFetcher, useOzElements };
2825
+ export { OzBankAccountNumber, OzBankCard, OzBankRoutingNumber, OzCard, OzCardNumber, OzCvv, OzElements, OzExpiry, createSessionFetcher as createFetchWaxKey, createSessionFetcher, useFieldStates, useOzElements };
2767
2826
  //# sourceMappingURL=index.esm.js.map