@vtex/country-forms 1.0.1 → 1.2.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/CHANGELOG.md +45 -1
- package/README.md +2 -1
- package/dist/{BRA-PMVV6KOM.js → BRA-CWGYPPBD.js} +43 -27
- package/dist/BRA-CWGYPPBD.js.map +1 -0
- package/dist/{BRA-PCJDRSEF.mjs → BRA-JUHEOV7J.mjs} +3 -3
- package/dist/{BRA-PCJDRSEF.mjs.map → BRA-JUHEOV7J.mjs.map} +1 -1
- package/dist/{BRA-ZBZXE5II.mjs → BRA-R2DBJTZ2.mjs} +43 -27
- package/dist/BRA-R2DBJTZ2.mjs.map +1 -0
- package/dist/{BRA-WGCSNRCO.js → BRA-VIVRV7X7.js} +3 -3
- package/dist/{BRA-WGCSNRCO.js.map → BRA-VIVRV7X7.js.map} +1 -1
- package/dist/CAN-RDQPFPZU.mjs +127 -0
- package/dist/CAN-RDQPFPZU.mjs.map +1 -0
- package/dist/CAN-ZJQJFUW4.js +129 -0
- package/dist/CAN-ZJQJFUW4.js.map +1 -0
- package/dist/MEX-6CJCALY3.js +125 -0
- package/dist/MEX-6CJCALY3.js.map +1 -0
- package/dist/MEX-ZQL7ZS3G.mjs +123 -0
- package/dist/MEX-ZQL7ZS3G.mjs.map +1 -0
- package/dist/USA-2QW25XAI.mjs +128 -0
- package/dist/USA-2QW25XAI.mjs.map +1 -0
- package/dist/USA-MWP4SDGW.js +130 -0
- package/dist/USA-MWP4SDGW.js.map +1 -0
- package/dist/{bra-PZBWBUGB.js → bra-DQO5W7HB.js} +23 -31
- package/dist/bra-DQO5W7HB.js.map +1 -0
- package/dist/{bra-ZDFUVAXT.mjs → bra-MJUXI46I.mjs} +16 -27
- package/dist/bra-MJUXI46I.mjs.map +1 -0
- package/dist/can-3MOZIOGB.js +65 -0
- package/dist/can-3MOZIOGB.js.map +1 -0
- package/dist/can-DKTB2M6I.mjs +41 -0
- package/dist/can-DKTB2M6I.mjs.map +1 -0
- package/dist/can-LLWX4LC2.js +48 -0
- package/dist/can-LLWX4LC2.js.map +1 -0
- package/dist/can-OONXPDKH.mjs +59 -0
- package/dist/can-OONXPDKH.mjs.map +1 -0
- package/dist/chunk-B2XPLIJU.mjs +19 -0
- package/dist/chunk-B2XPLIJU.mjs.map +1 -0
- package/dist/{chunk-HKDTDE53.js → chunk-CFX5EF4R.js} +2 -2
- package/dist/{chunk-HKDTDE53.js.map → chunk-CFX5EF4R.js.map} +1 -1
- package/dist/{chunk-IVJED4TG.mjs → chunk-KWESVZKV.mjs} +2 -2
- package/dist/{chunk-IVJED4TG.mjs.map → chunk-KWESVZKV.mjs.map} +1 -1
- package/dist/chunk-ZQQPNXLP.js +21 -0
- package/dist/chunk-ZQQPNXLP.js.map +1 -0
- package/dist/common-E4IN3G46.mjs +4 -0
- package/dist/common-E4IN3G46.mjs.map +1 -0
- package/dist/common-LFDTUEMM.js +13 -0
- package/dist/common-LFDTUEMM.js.map +1 -0
- package/dist/{en-US-67K5REMQ.mjs → en-US-KHII5INO.mjs} +29 -14
- package/dist/en-US-KHII5INO.mjs.map +1 -0
- package/dist/{en-US-DXYSM6Q2.js → en-US-RCNO3BZC.js} +29 -14
- package/dist/en-US-RCNO3BZC.js.map +1 -0
- package/dist/{es-MX-UL5OL7HT.js → es-MX-IGOEW2P4.js} +28 -13
- package/dist/es-MX-IGOEW2P4.js.map +1 -0
- package/dist/{es-MX-2R6G2ZLS.mjs → es-MX-R24GRBU5.mjs} +28 -13
- package/dist/es-MX-R24GRBU5.mjs.map +1 -0
- package/dist/{fr-FR-GGR2M6TJ.mjs → fr-FR-N4WJAN23.mjs} +29 -14
- package/dist/fr-FR-N4WJAN23.mjs.map +1 -0
- package/dist/{fr-FR-LWA6B3MW.js → fr-FR-WZBI6O4O.js} +29 -14
- package/dist/fr-FR-WZBI6O4O.js.map +1 -0
- package/dist/index.d.mts +227 -11
- package/dist/index.d.ts +227 -11
- package/dist/index.js +127 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -44
- package/dist/index.mjs.map +1 -1
- package/dist/mex-AMEX6IHY.mjs +108 -0
- package/dist/mex-AMEX6IHY.mjs.map +1 -0
- package/dist/mex-DYWVMWB6.js +38 -0
- package/dist/mex-DYWVMWB6.js.map +1 -0
- package/dist/mex-VX3Y5I6I.mjs +33 -0
- package/dist/mex-VX3Y5I6I.mjs.map +1 -0
- package/dist/mex-Y56WV2L7.js +114 -0
- package/dist/mex-Y56WV2L7.js.map +1 -0
- package/dist/{pt-BR-KH6RW3HM.mjs → pt-BR-PIBP3JHL.mjs} +31 -13
- package/dist/pt-BR-PIBP3JHL.mjs.map +1 -0
- package/dist/{pt-BR-WVTTIEAH.js → pt-BR-ZDM55ITO.js} +31 -13
- package/dist/pt-BR-ZDM55ITO.js.map +1 -0
- package/dist/usa-AC7YPVS3.mjs +52 -0
- package/dist/usa-AC7YPVS3.mjs.map +1 -0
- package/dist/usa-B6NCCTF6.js +61 -0
- package/dist/usa-B6NCCTF6.js.map +1 -0
- package/dist/usa-LOOP7MRM.mjs +55 -0
- package/dist/usa-LOOP7MRM.mjs.map +1 -0
- package/dist/usa-OQ44AFEI.js +61 -0
- package/dist/usa-OQ44AFEI.js.map +1 -0
- package/package.json +1 -1
- package/dist/BRA-PMVV6KOM.js.map +0 -1
- package/dist/BRA-ZBZXE5II.mjs.map +0 -1
- package/dist/bra-PZBWBUGB.js.map +0 -1
- package/dist/bra-ZDFUVAXT.mjs.map +0 -1
- package/dist/can-7TMYDEJF.mjs +0 -17
- package/dist/can-7TMYDEJF.mjs.map +0 -1
- package/dist/can-B5ME2ZFN.js +0 -20
- package/dist/can-B5ME2ZFN.js.map +0 -1
- package/dist/can-BUKTOPLB.js +0 -21
- package/dist/can-BUKTOPLB.js.map +0 -1
- package/dist/can-SHRLPUU5.mjs +0 -19
- package/dist/can-SHRLPUU5.mjs.map +0 -1
- package/dist/en-US-67K5REMQ.mjs.map +0 -1
- package/dist/en-US-DXYSM6Q2.js.map +0 -1
- package/dist/es-MX-2R6G2ZLS.mjs.map +0 -1
- package/dist/es-MX-UL5OL7HT.js.map +0 -1
- package/dist/fr-FR-GGR2M6TJ.mjs.map +0 -1
- package/dist/fr-FR-LWA6B3MW.js.map +0 -1
- package/dist/mex-I3LUVYVC.mjs +0 -19
- package/dist/mex-I3LUVYVC.mjs.map +0 -1
- package/dist/mex-QT4CJZDD.js +0 -21
- package/dist/mex-QT4CJZDD.js.map +0 -1
- package/dist/pt-BR-KH6RW3HM.mjs.map +0 -1
- package/dist/pt-BR-WVTTIEAH.js.map +0 -1
- package/dist/usa-6PGU5UHS.mjs +0 -18
- package/dist/usa-6PGU5UHS.mjs.map +0 -1
- package/dist/usa-I7DK2CTL.mjs +0 -18
- package/dist/usa-I7DK2CTL.mjs.map +0 -1
- package/dist/usa-LFLDXKPA.js +0 -20
- package/dist/usa-LFLDXKPA.js.map +0 -1
- package/dist/usa-LXT6WW64.js +0 -21
- package/dist/usa-LXT6WW64.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -188,6 +188,93 @@ interface FieldDefinition {
|
|
|
188
188
|
* }
|
|
189
189
|
*/
|
|
190
190
|
conditionalOptions?: Record<string, SelectOption[] | string>;
|
|
191
|
+
/**
|
|
192
|
+
* Map from a controller field's value to a validator name for this field.
|
|
193
|
+
* Used when a single field validates differently depending on another field's
|
|
194
|
+
* selection — e.g. a `document` field validated as a CPF for individuals and as
|
|
195
|
+
* a CNPJ for businesses.
|
|
196
|
+
*
|
|
197
|
+
* Requires {@link FieldDefinition.dependsOn} to identify the controller field.
|
|
198
|
+
* When a matching key exists for the current controller value, it overrides
|
|
199
|
+
* {@link ValidationRules.customValidator}. Resolve the effective validator with
|
|
200
|
+
* `resolveFieldValidator(field, values)`.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* dependsOn: "personType"
|
|
204
|
+
* conditionalValidator: { individual: "validateCPF", business: "validateCNPJ" }
|
|
205
|
+
*
|
|
206
|
+
* @remarks
|
|
207
|
+
* Declaring this property also opts the field into automatic reset: `setValue`
|
|
208
|
+
* clears the field's value to `''` whenever the controller ({@link FieldDefinition.dependsOn})
|
|
209
|
+
* changes, because the validation domain — and therefore any previously entered
|
|
210
|
+
* value — is no longer guaranteed to be valid (e.g. a CPF is not a valid CNPJ).
|
|
211
|
+
* If a future field needs a conditional validator *without* this reset, add a
|
|
212
|
+
* dedicated opt-out flag rather than relying on the absence of this behavior.
|
|
213
|
+
*/
|
|
214
|
+
conditionalValidator?: Record<string, string>;
|
|
215
|
+
/**
|
|
216
|
+
* Map from a controller field's value to a format function name for this field.
|
|
217
|
+
* Same semantics as {@link FieldDefinition.conditionalValidator} but for the mask
|
|
218
|
+
* applied on display. Overrides {@link FieldDefinition.formatRef} when a key
|
|
219
|
+
* matches. Resolve the effective ref with `resolveFieldFormatRef(field, values)`.
|
|
220
|
+
*
|
|
221
|
+
* A missing key falls back to {@link FieldDefinition.formatRef} (or identity when
|
|
222
|
+
* neither is set), so a controller value with no mask is valid.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* dependsOn: "personType"
|
|
226
|
+
* conditionalFormatRef: { individual: "formatCPF", business: "formatCNPJ" }
|
|
227
|
+
*/
|
|
228
|
+
conditionalFormatRef?: Record<string, string>;
|
|
229
|
+
/**
|
|
230
|
+
* Map from a controller field's value to an unformat function name for this field.
|
|
231
|
+
* Symmetric to {@link FieldDefinition.conditionalFormatRef}. Overrides
|
|
232
|
+
* {@link FieldDefinition.unformatRef} when a key matches. Resolve the effective ref
|
|
233
|
+
* with `resolveFieldUnformatRef(field, values)`.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* dependsOn: "personType"
|
|
237
|
+
* conditionalUnformatRef: { individual: "unformatCPF", business: "unformatCNPJ" }
|
|
238
|
+
*/
|
|
239
|
+
conditionalUnformatRef?: Record<string, string>;
|
|
240
|
+
/**
|
|
241
|
+
* Map from a controller field's value to an {@link InputMode} for this field.
|
|
242
|
+
* Same semantics as {@link FieldDefinition.conditionalValidator} but for the
|
|
243
|
+
* mobile virtual-keyboard hint. Overrides {@link FieldDefinition.inputMode} when
|
|
244
|
+
* a key matches. Resolve the effective value with `resolveFieldInputMode(field, values)`.
|
|
245
|
+
*
|
|
246
|
+
* Useful when a single field expects different input per controller value — e.g.
|
|
247
|
+
* a numeric keyboard for a numeric CPF (individual) but a text keyboard for an
|
|
248
|
+
* alphanumeric CNPJ (business).
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* dependsOn: "personType"
|
|
252
|
+
* conditionalInputMode: { individual: "numeric", business: "text" }
|
|
253
|
+
*/
|
|
254
|
+
conditionalInputMode?: Record<string, InputMode>;
|
|
255
|
+
/**
|
|
256
|
+
* Name of the sibling field that drives {@link FieldDefinition.derivedValue}.
|
|
257
|
+
*
|
|
258
|
+
* A field with `derivedFrom` is **data-only**: its value is computed automatically
|
|
259
|
+
* from the controller and it is never rendered (excluded from `getVisibleFields`)
|
|
260
|
+
* nor validated. It exists so the form state matches the API payload shape — e.g.
|
|
261
|
+
* a `documentType` field derived from `personType` so submissions carry both
|
|
262
|
+
* `document` and `documentType`.
|
|
263
|
+
*
|
|
264
|
+
* @example "personType"
|
|
265
|
+
*/
|
|
266
|
+
derivedFrom?: string;
|
|
267
|
+
/**
|
|
268
|
+
* Map from the {@link FieldDefinition.derivedFrom} controller value to this
|
|
269
|
+
* field's automatically-maintained value. `createFormState` seeds it and
|
|
270
|
+
* `setValue` recomputes it whenever the controller changes. A controller value
|
|
271
|
+
* with no entry resolves to `""`.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* derivedFrom: "personType"
|
|
275
|
+
* derivedValue: { individual: "cpf", business: "cnpj" }
|
|
276
|
+
*/
|
|
277
|
+
derivedValue?: Record<string, string>;
|
|
191
278
|
/**
|
|
192
279
|
* Declarative visibility condition.
|
|
193
280
|
* When present, the field is shown and validated only while the condition is `true`.
|
|
@@ -214,8 +301,9 @@ interface FieldDefinition {
|
|
|
214
301
|
conditionalPlaceholder?: Record<string, string>;
|
|
215
302
|
/**
|
|
216
303
|
* Name of the sibling field that drives {@link FieldDefinition.conditionalLabel},
|
|
217
|
-
* {@link FieldDefinition.conditionalPlaceholder},
|
|
218
|
-
* {@link FieldDefinition.
|
|
304
|
+
* {@link FieldDefinition.conditionalPlaceholder}, {@link FieldDefinition.conditionalOptions},
|
|
305
|
+
* {@link FieldDefinition.conditionalValidator}, {@link FieldDefinition.conditionalFormatRef},
|
|
306
|
+
* {@link FieldDefinition.conditionalUnformatRef}, and {@link FieldDefinition.conditionalInputMode}.
|
|
219
307
|
* Required when any of those properties is set.
|
|
220
308
|
* @example "personType", "state"
|
|
221
309
|
*/
|
|
@@ -375,12 +463,31 @@ type I18nBundle = Record<string, string>;
|
|
|
375
463
|
* resolveLabel('country-forms.address.postalCode.label', bundle, overrides)
|
|
376
464
|
*/
|
|
377
465
|
type I18nOverrides = Record<string, string>;
|
|
466
|
+
/**
|
|
467
|
+
* Interpolation parameters for `resolveLabel`.
|
|
468
|
+
*
|
|
469
|
+
* Each entry replaces every `{key}` placeholder in the resolved message with
|
|
470
|
+
* the provided value. Numeric values are coerced via `String(value)`. Missing
|
|
471
|
+
* placeholders (i.e. a `{key}` appears in the message but no entry exists in
|
|
472
|
+
* `params`) are intentionally left untouched so the gap is visible at runtime.
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* resolveLabel(
|
|
476
|
+
* 'country-forms.validation.maxLength',
|
|
477
|
+
* bundle,
|
|
478
|
+
* undefined,
|
|
479
|
+
* { fieldName: 'CEP', maxLength: 9 },
|
|
480
|
+
* )
|
|
481
|
+
* // bundle: "{fieldName} deve ter até {maxLength} caracteres"
|
|
482
|
+
* // → "CEP deve ter até 9 caracteres"
|
|
483
|
+
*/
|
|
484
|
+
type I18nParams = Record<string, string | number>;
|
|
378
485
|
|
|
379
486
|
/**
|
|
380
487
|
* A synchronous field validator function.
|
|
381
488
|
*
|
|
382
489
|
* - Returns `true` when the value is valid.
|
|
383
|
-
* - Returns an **i18n key** string (e.g. `"country-forms.validation.
|
|
490
|
+
* - Returns an **i18n key** string (e.g. `"country-forms.validation.invalid"`) when invalid.
|
|
384
491
|
* The engine passes this key to the error map; consumers resolve it via `resolveLabel`.
|
|
385
492
|
*
|
|
386
493
|
* Validators must be **pure** and **synchronous**.
|
|
@@ -448,9 +555,15 @@ declare function createFormState(schema: FormSchema, defaults?: Partial<FormValu
|
|
|
448
555
|
* The updated field is automatically marked as `touched: true`.
|
|
449
556
|
*
|
|
450
557
|
* When the changed field is referenced by another field's `dependsOn` and that
|
|
451
|
-
* field has `conditionalOptions`, the dependent field's
|
|
452
|
-
* reset to `''`. This prevents stale values when the
|
|
453
|
-
*
|
|
558
|
+
* field has `conditionalOptions` or `conditionalValidator`, the dependent field's
|
|
559
|
+
* value is automatically reset to `''`. This prevents stale values when the
|
|
560
|
+
* controller field changes — e.g. selecting a different state clears the previously
|
|
561
|
+
* selected city, or switching `personType` clears the `document` whose validator
|
|
562
|
+
* (CPF vs CNPJ) just changed.
|
|
563
|
+
*
|
|
564
|
+
* Derived, data-only fields ({@link FieldDefinition.derivedFrom}) referencing the
|
|
565
|
+
* changed field are recomputed from their `derivedValue` map (e.g. `documentType`
|
|
566
|
+
* follows `personType`).
|
|
454
567
|
*
|
|
455
568
|
* Existing errors are preserved so the consumer can decide when to re-validate.
|
|
456
569
|
* To clear all errors after a change, spread: `{ ...setValue(state, schema, f, v), errors: {} }`.
|
|
@@ -668,6 +781,83 @@ declare function unformatValue(ref: string | undefined, value: string): string;
|
|
|
668
781
|
*/
|
|
669
782
|
declare function resolveFieldOptions(field: FieldDefinition, values: FormValues): SelectOption[] | null;
|
|
670
783
|
|
|
784
|
+
/**
|
|
785
|
+
* @module engine/resolveField
|
|
786
|
+
*
|
|
787
|
+
* Resolvers for fields whose label, placeholder, validator, or mask change based
|
|
788
|
+
* on a sibling controller field (declared via `dependsOn`).
|
|
789
|
+
*
|
|
790
|
+
* These mirror {@link resolveFieldOptions}: each resolver looks up the controller
|
|
791
|
+
* value in the field's conditional map and falls back to the field's static
|
|
792
|
+
* property when no entry matches. They let a single field — e.g. a `document`
|
|
793
|
+
* field — behave as a CPF for individuals and a CNPJ for businesses without the
|
|
794
|
+
* consumer hard-coding the mapping.
|
|
795
|
+
*/
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Resolves the effective i18n label **key** for a field given the current values.
|
|
799
|
+
*
|
|
800
|
+
* Resolution order:
|
|
801
|
+
* 1) `conditionalLabel` entry matching `values[dependsOn]`
|
|
802
|
+
* 2) `field.label` fallback
|
|
803
|
+
*
|
|
804
|
+
* @returns The i18n key to pass to `resolveLabel`.
|
|
805
|
+
*/
|
|
806
|
+
declare function resolveFieldLabel(field: FieldDefinition, values: FormValues): string;
|
|
807
|
+
/**
|
|
808
|
+
* Resolves the effective i18n placeholder **key** for a field, or `undefined`
|
|
809
|
+
* when the field has neither a conditional nor a static placeholder.
|
|
810
|
+
*
|
|
811
|
+
* Resolution order:
|
|
812
|
+
* 1) `conditionalPlaceholder` entry matching `values[dependsOn]`
|
|
813
|
+
* 2) `field.placeholder` fallback
|
|
814
|
+
*/
|
|
815
|
+
declare function resolveFieldPlaceholder(field: FieldDefinition, values: FormValues): string | undefined;
|
|
816
|
+
/**
|
|
817
|
+
* Resolves the effective validator name for a field given the current values.
|
|
818
|
+
*
|
|
819
|
+
* Resolution order:
|
|
820
|
+
* 1) `conditionalValidator` entry matching `values[dependsOn]`
|
|
821
|
+
* 2) `field.validation.customValidator` fallback
|
|
822
|
+
*
|
|
823
|
+
* @returns The validator name to resolve from the rules registry, or `undefined`
|
|
824
|
+
* when the field has no custom validator for the current controller value.
|
|
825
|
+
*/
|
|
826
|
+
declare function resolveFieldValidator(field: FieldDefinition, values: FormValues): string | undefined;
|
|
827
|
+
/**
|
|
828
|
+
* Resolves the effective format function name for a field given the current values.
|
|
829
|
+
*
|
|
830
|
+
* Resolution order:
|
|
831
|
+
* 1) `conditionalFormatRef` entry matching `values[dependsOn]`
|
|
832
|
+
* 2) `field.formatRef` fallback
|
|
833
|
+
*
|
|
834
|
+
* Pass the result to `formatValue(ref, raw)`; `undefined` yields identity.
|
|
835
|
+
*/
|
|
836
|
+
declare function resolveFieldFormatRef(field: FieldDefinition, values: FormValues): string | undefined;
|
|
837
|
+
/**
|
|
838
|
+
* Resolves the effective unformat function name for a field given the current values.
|
|
839
|
+
*
|
|
840
|
+
* Resolution order:
|
|
841
|
+
* 1) `conditionalUnformatRef` entry matching `values[dependsOn]`
|
|
842
|
+
* 2) `field.unformatRef` fallback
|
|
843
|
+
*
|
|
844
|
+
* Pass the result to `unformatValue(ref, display)`; `undefined` yields identity.
|
|
845
|
+
*/
|
|
846
|
+
declare function resolveFieldUnformatRef(field: FieldDefinition, values: FormValues): string | undefined;
|
|
847
|
+
/**
|
|
848
|
+
* Resolves the effective `inputMode` (mobile keyboard hint) for a field given
|
|
849
|
+
* the current values.
|
|
850
|
+
*
|
|
851
|
+
* Resolution order:
|
|
852
|
+
* 1) `conditionalInputMode` entry matching `values[dependsOn]`
|
|
853
|
+
* 2) `field.inputMode` fallback
|
|
854
|
+
*
|
|
855
|
+
* Lets a single field hint a different virtual keyboard per controller value —
|
|
856
|
+
* e.g. a `document` field that is numeric for an individual (CPF) and text for a
|
|
857
|
+
* business (alphanumeric CNPJ).
|
|
858
|
+
*/
|
|
859
|
+
declare function resolveFieldInputMode(field: FieldDefinition, values: FormValues): InputMode | undefined;
|
|
860
|
+
|
|
671
861
|
/**
|
|
672
862
|
* Dynamic import map for address schemas.
|
|
673
863
|
* Each country is loaded on demand via `import()` for code-splitting.
|
|
@@ -705,7 +895,7 @@ declare const SUPPORTED_ADDRESS_COUNTRIES: readonly string[];
|
|
|
705
895
|
*
|
|
706
896
|
* @example
|
|
707
897
|
* import { SUPPORTED_PERSONAL_DATA_COUNTRIES } from '@vtex/country-forms'
|
|
708
|
-
* // currently supported countries: → ['BRA']
|
|
898
|
+
* // currently supported countries: → ['BRA', 'CAN', 'USA']
|
|
709
899
|
*/
|
|
710
900
|
declare const SUPPORTED_PERSONAL_DATA_COUNTRIES: readonly string[];
|
|
711
901
|
|
|
@@ -779,7 +969,10 @@ declare function loadLocale(locale: string): Promise<I18nBundle>;
|
|
|
779
969
|
/**
|
|
780
970
|
* @module i18n/resolveLabel
|
|
781
971
|
*
|
|
782
|
-
* Resolves an i18n key to its display string using a three-level cascade
|
|
972
|
+
* Resolves an i18n key to its display string using a three-level cascade and,
|
|
973
|
+
* optionally, interpolates `{name}` placeholders found in the resolved string.
|
|
974
|
+
*
|
|
975
|
+
* ## Cascade
|
|
783
976
|
*
|
|
784
977
|
* 1. **Consumer override** — `overrides[key]` wins if present.
|
|
785
978
|
* 2. **Bundle value** — `bundle[key]` from `loadLocale()`.
|
|
@@ -789,10 +982,22 @@ declare function loadLocale(locale: string): Promise<I18nBundle>;
|
|
|
789
982
|
* This design lets consumers customize individual labels without forking the
|
|
790
983
|
* library or replacing the entire bundle.
|
|
791
984
|
*
|
|
985
|
+
* ## Interpolation
|
|
986
|
+
*
|
|
987
|
+
* When `params` is provided, every `{name}` placeholder in the resolved string
|
|
988
|
+
* is replaced with `String(params[name])`. Replacement is **global** — the same
|
|
989
|
+
* placeholder may appear more than once in a message and is replaced everywhere
|
|
990
|
+
* it occurs. Placeholders without a matching `params` entry are left untouched
|
|
991
|
+
* so unfulfilled substitutions remain visible to developers.
|
|
992
|
+
*
|
|
993
|
+
* Interpolation is applied after the cascade, so it works equally for override
|
|
994
|
+
* strings, bundle strings, and the raw-key fallback.
|
|
995
|
+
*
|
|
792
996
|
* @param key - i18n key (e.g. `"country-forms.address.postalCode.label"`)
|
|
793
997
|
* @param bundle - Locale bundle loaded via `loadLocale()`
|
|
794
998
|
* @param overrides - Optional consumer-supplied overrides
|
|
795
|
-
* @
|
|
999
|
+
* @param params - Optional interpolation parameters for `{name}` placeholders
|
|
1000
|
+
* @returns Resolved (and optionally interpolated) display string
|
|
796
1001
|
*
|
|
797
1002
|
* @example
|
|
798
1003
|
* const bundle = await loadLocale('pt-BR')
|
|
@@ -807,9 +1012,20 @@ declare function loadLocale(locale: string): Promise<I18nBundle>;
|
|
|
807
1012
|
* @example
|
|
808
1013
|
* resolveLabel('country-forms.unknown.key', bundle)
|
|
809
1014
|
* // → "country-forms.unknown.key" (raw key fallback)
|
|
1015
|
+
*
|
|
1016
|
+
* @example
|
|
1017
|
+
* // bundle: { 'country-forms.validation.maxLength':
|
|
1018
|
+
* // '{fieldName} deve ter até {maxLength} caracteres' }
|
|
1019
|
+
* resolveLabel(
|
|
1020
|
+
* 'country-forms.validation.maxLength',
|
|
1021
|
+
* bundle,
|
|
1022
|
+
* undefined,
|
|
1023
|
+
* { fieldName: 'CEP', maxLength: 9 },
|
|
1024
|
+
* )
|
|
1025
|
+
* // → "CEP deve ter até 9 caracteres"
|
|
810
1026
|
*/
|
|
811
1027
|
|
|
812
|
-
declare function resolveLabel(key: string, bundle: I18nBundle, overrides?: I18nOverrides): string;
|
|
1028
|
+
declare function resolveLabel(key: string, bundle: I18nBundle, overrides?: I18nOverrides, params?: I18nParams): string;
|
|
813
1029
|
|
|
814
1030
|
/**
|
|
815
1031
|
* BCP 47 locale tags supported by the library's built-in translation bundles.
|
|
@@ -830,4 +1046,4 @@ declare const SUPPORTED_LOCALES: readonly ["en-US", "es-MX", "fr-FR", "pt-BR"];
|
|
|
830
1046
|
/** Union of BCP 47 tags with dedicated built-in bundles. */
|
|
831
1047
|
type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
|
|
832
1048
|
|
|
833
|
-
export { type ConditionalOperator, type ConditionalVisibility, type FieldDefinition, type FieldType, type FormErrors, type FormSchema, type FormState, type FormType, type FormValues, type FormatFn, type I18nBundle, type I18nOverrides, type InputMode, SUPPORTED_ADDRESS_COUNTRIES, SUPPORTED_LOCALES, SUPPORTED_PERSONAL_DATA_COUNTRIES, type SelectOption, type SupportedLocale, type UnformatFn, type ValidationErrors, type ValidationRules, type ValidatorFn, createFormState, formatValue, getVisibleFields, loadLocale, loadSchema, resolveFieldOptions, resolveLabel, setValue, unformatValue, validate };
|
|
1049
|
+
export { type ConditionalOperator, type ConditionalVisibility, type FieldDefinition, type FieldType, type FormErrors, type FormSchema, type FormState, type FormType, type FormValues, type FormatFn, type I18nBundle, type I18nOverrides, type I18nParams, type InputMode, SUPPORTED_ADDRESS_COUNTRIES, SUPPORTED_LOCALES, SUPPORTED_PERSONAL_DATA_COUNTRIES, type SelectOption, type SupportedLocale, type UnformatFn, type ValidationErrors, type ValidationRules, type ValidatorFn, createFormState, formatValue, getVisibleFields, loadLocale, loadSchema, resolveFieldFormatRef, resolveFieldInputMode, resolveFieldLabel, resolveFieldOptions, resolveFieldPlaceholder, resolveFieldUnformatRef, resolveFieldValidator, resolveLabel, setValue, unformatValue, validate };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var chunkYB6VNXLI_js = require('./chunk-YB6VNXLI.js');
|
|
4
|
-
var chunkHKDTDE53_js = require('./chunk-HKDTDE53.js');
|
|
5
4
|
var chunkBQPGLWMB_js = require('./chunk-BQPGLWMB.js');
|
|
5
|
+
var chunkCFX5EF4R_js = require('./chunk-CFX5EF4R.js');
|
|
6
6
|
|
|
7
7
|
// src/engine/createFormState.ts
|
|
8
8
|
function createFormState(schema, defaults) {
|
|
@@ -12,6 +12,12 @@ function createFormState(schema, defaults) {
|
|
|
12
12
|
values[field.name] = defaults?.[field.name] ?? "";
|
|
13
13
|
touched[field.name] = false;
|
|
14
14
|
}
|
|
15
|
+
for (const field of schema.fields) {
|
|
16
|
+
if (field.derivedFrom !== void 0) {
|
|
17
|
+
const controllerValue = values[field.derivedFrom] ?? "";
|
|
18
|
+
values[field.name] = field.derivedValue?.[controllerValue] ?? "";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
15
21
|
return { values, errors: {}, touched };
|
|
16
22
|
}
|
|
17
23
|
|
|
@@ -23,9 +29,12 @@ function setValue(state, schema, fieldName, value) {
|
|
|
23
29
|
touched: { ...state.touched, [fieldName]: true }
|
|
24
30
|
};
|
|
25
31
|
for (const field of schema.fields) {
|
|
26
|
-
if (field.dependsOn === fieldName && field.conditionalOptions !== void 0) {
|
|
32
|
+
if (field.dependsOn === fieldName && (field.conditionalOptions !== void 0 || field.conditionalValidator !== void 0)) {
|
|
27
33
|
next.values[field.name] = "";
|
|
28
34
|
}
|
|
35
|
+
if (field.derivedFrom === fieldName) {
|
|
36
|
+
next.values[field.name] = field.derivedValue?.[value] ?? "";
|
|
37
|
+
}
|
|
29
38
|
}
|
|
30
39
|
return next;
|
|
31
40
|
}
|
|
@@ -35,6 +44,9 @@ function getVisibleFields(schema, values) {
|
|
|
35
44
|
return schema.fields.filter((field) => isFieldVisible(field, values));
|
|
36
45
|
}
|
|
37
46
|
function isFieldVisible(field, values) {
|
|
47
|
+
if (field.derivedFrom !== void 0) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
38
50
|
if (field.conditionalVisibility === void 0) {
|
|
39
51
|
return true;
|
|
40
52
|
}
|
|
@@ -58,6 +70,33 @@ function evaluateCondition(condition, values) {
|
|
|
58
70
|
}
|
|
59
71
|
}
|
|
60
72
|
|
|
73
|
+
// src/engine/resolveField.ts
|
|
74
|
+
function resolveConditional(field, map, values) {
|
|
75
|
+
if (field.dependsOn === void 0 || map === void 0) {
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
const controllerValue = values[field.dependsOn] ?? "";
|
|
79
|
+
return Object.prototype.hasOwnProperty.call(map, controllerValue) ? map[controllerValue] : void 0;
|
|
80
|
+
}
|
|
81
|
+
function resolveFieldLabel(field, values) {
|
|
82
|
+
return resolveConditional(field, field.conditionalLabel, values) ?? field.label;
|
|
83
|
+
}
|
|
84
|
+
function resolveFieldPlaceholder(field, values) {
|
|
85
|
+
return resolveConditional(field, field.conditionalPlaceholder, values) ?? field.placeholder;
|
|
86
|
+
}
|
|
87
|
+
function resolveFieldValidator(field, values) {
|
|
88
|
+
return resolveConditional(field, field.conditionalValidator, values) ?? field.validation?.customValidator;
|
|
89
|
+
}
|
|
90
|
+
function resolveFieldFormatRef(field, values) {
|
|
91
|
+
return resolveConditional(field, field.conditionalFormatRef, values) ?? field.formatRef;
|
|
92
|
+
}
|
|
93
|
+
function resolveFieldUnformatRef(field, values) {
|
|
94
|
+
return resolveConditional(field, field.conditionalUnformatRef, values) ?? field.unformatRef;
|
|
95
|
+
}
|
|
96
|
+
function resolveFieldInputMode(field, values) {
|
|
97
|
+
return resolveConditional(field, field.conditionalInputMode, values) ?? field.inputMode;
|
|
98
|
+
}
|
|
99
|
+
|
|
61
100
|
// src/engine/validate.ts
|
|
62
101
|
var VALIDATION_REQUIRED_KEY = "country-forms.validation.required";
|
|
63
102
|
var VALIDATION_INVALID_KEY = "country-forms.validation.invalid";
|
|
@@ -65,14 +104,14 @@ function validate(state, schema) {
|
|
|
65
104
|
const errors = {};
|
|
66
105
|
for (const field of getVisibleFields(schema, state.values)) {
|
|
67
106
|
const value = state.values[field.name] ?? "";
|
|
68
|
-
const error = validateField(field, value);
|
|
107
|
+
const error = validateField(field, value, state.values);
|
|
69
108
|
if (error !== null) {
|
|
70
109
|
errors[field.name] = error;
|
|
71
110
|
}
|
|
72
111
|
}
|
|
73
112
|
return errors;
|
|
74
113
|
}
|
|
75
|
-
function validateField(field, value) {
|
|
114
|
+
function validateField(field, value, values) {
|
|
76
115
|
const trimmed = value.trim();
|
|
77
116
|
if (field.required && trimmed === "") {
|
|
78
117
|
return VALIDATION_REQUIRED_KEY;
|
|
@@ -81,23 +120,23 @@ function validateField(field, value) {
|
|
|
81
120
|
return null;
|
|
82
121
|
}
|
|
83
122
|
const rules = field.validation;
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return rules.pattern.message;
|
|
123
|
+
if (rules) {
|
|
124
|
+
if (rules.minLength !== void 0 && value.length < rules.minLength.value) {
|
|
125
|
+
return rules.minLength.message;
|
|
126
|
+
}
|
|
127
|
+
if (rules.maxLength !== void 0 && value.length > rules.maxLength.value) {
|
|
128
|
+
return rules.maxLength.message;
|
|
129
|
+
}
|
|
130
|
+
if (rules.pattern !== void 0) {
|
|
131
|
+
const regex = new RegExp(rules.pattern.value);
|
|
132
|
+
if (!regex.test(value)) {
|
|
133
|
+
return rules.pattern.message;
|
|
134
|
+
}
|
|
97
135
|
}
|
|
98
136
|
}
|
|
99
|
-
|
|
100
|
-
|
|
137
|
+
const customValidator = resolveFieldValidator(field, values);
|
|
138
|
+
if (customValidator !== void 0) {
|
|
139
|
+
const fn = chunkCFX5EF4R_js.resolveValidator(customValidator);
|
|
101
140
|
const result = fn(value);
|
|
102
141
|
if (result === true) {
|
|
103
142
|
return null;
|
|
@@ -225,8 +264,8 @@ async function loadAddressSchema(iso3) {
|
|
|
225
264
|
switch (iso3) {
|
|
226
265
|
case "BRA": {
|
|
227
266
|
const [{ default: schema }] = await Promise.all([
|
|
228
|
-
import('./BRA-
|
|
229
|
-
import('./bra-
|
|
267
|
+
import('./BRA-VIVRV7X7.js'),
|
|
268
|
+
import('./bra-DQO5W7HB.js'),
|
|
230
269
|
import('./bra-KEBYAD75.js'),
|
|
231
270
|
import('./bra-states-SQMU4WTB.js')
|
|
232
271
|
]);
|
|
@@ -235,8 +274,8 @@ async function loadAddressSchema(iso3) {
|
|
|
235
274
|
case "CAN": {
|
|
236
275
|
const [{ default: schema }] = await Promise.all([
|
|
237
276
|
import('./CAN-NGS6AUBG.js'),
|
|
238
|
-
import('./can-
|
|
239
|
-
import('./can-
|
|
277
|
+
import('./can-3MOZIOGB.js'),
|
|
278
|
+
import('./can-LLWX4LC2.js'),
|
|
240
279
|
import('./can-provinces-6NOHMW37.js')
|
|
241
280
|
]);
|
|
242
281
|
return schema;
|
|
@@ -244,7 +283,7 @@ async function loadAddressSchema(iso3) {
|
|
|
244
283
|
case "MEX": {
|
|
245
284
|
const [{ default: schema }] = await Promise.all([
|
|
246
285
|
import('./MEX-3PMZHDQP.js'),
|
|
247
|
-
import('./mex-
|
|
286
|
+
import('./mex-Y56WV2L7.js'),
|
|
248
287
|
import('./mex-states-GTCRN4NS.js')
|
|
249
288
|
]);
|
|
250
289
|
return schema;
|
|
@@ -252,8 +291,8 @@ async function loadAddressSchema(iso3) {
|
|
|
252
291
|
case "USA": {
|
|
253
292
|
const [{ default: schema }] = await Promise.all([
|
|
254
293
|
import('./USA-UBWW5NQB.js'),
|
|
255
|
-
import('./usa-
|
|
256
|
-
import('./usa-
|
|
294
|
+
import('./usa-B6NCCTF6.js'),
|
|
295
|
+
import('./usa-OQ44AFEI.js'),
|
|
257
296
|
import('./usa-states-EBFT4LUQ.js')
|
|
258
297
|
]);
|
|
259
298
|
return schema;
|
|
@@ -264,17 +303,44 @@ async function loadAddressSchema(iso3) {
|
|
|
264
303
|
}
|
|
265
304
|
|
|
266
305
|
// src/schemas/personal-data/loader.ts
|
|
267
|
-
var SUPPORTED_PERSONAL_DATA_COUNTRIES = ["BRA"];
|
|
306
|
+
var SUPPORTED_PERSONAL_DATA_COUNTRIES = ["BRA", "CAN", "MEX", "USA"];
|
|
268
307
|
async function loadPersonalDataSchema(iso3) {
|
|
269
308
|
switch (iso3) {
|
|
270
309
|
case "BRA": {
|
|
271
310
|
const [{ default: schema }] = await Promise.all([
|
|
272
|
-
import('./BRA-
|
|
273
|
-
import('./bra-
|
|
311
|
+
import('./BRA-CWGYPPBD.js'),
|
|
312
|
+
import('./bra-DQO5W7HB.js'),
|
|
274
313
|
import('./bra-KEBYAD75.js')
|
|
275
314
|
]);
|
|
276
315
|
return schema;
|
|
277
316
|
}
|
|
317
|
+
case "CAN": {
|
|
318
|
+
const [{ default: schema }] = await Promise.all([
|
|
319
|
+
import('./CAN-ZJQJFUW4.js'),
|
|
320
|
+
import('./common-LFDTUEMM.js'),
|
|
321
|
+
import('./can-3MOZIOGB.js'),
|
|
322
|
+
import('./can-LLWX4LC2.js')
|
|
323
|
+
]);
|
|
324
|
+
return schema;
|
|
325
|
+
}
|
|
326
|
+
case "MEX": {
|
|
327
|
+
const [{ default: schema }] = await Promise.all([
|
|
328
|
+
import('./MEX-6CJCALY3.js'),
|
|
329
|
+
import('./common-LFDTUEMM.js'),
|
|
330
|
+
import('./mex-Y56WV2L7.js'),
|
|
331
|
+
import('./mex-DYWVMWB6.js')
|
|
332
|
+
]);
|
|
333
|
+
return schema;
|
|
334
|
+
}
|
|
335
|
+
case "USA": {
|
|
336
|
+
const [{ default: schema }] = await Promise.all([
|
|
337
|
+
import('./USA-MWP4SDGW.js'),
|
|
338
|
+
import('./common-LFDTUEMM.js'),
|
|
339
|
+
import('./usa-B6NCCTF6.js'),
|
|
340
|
+
import('./usa-OQ44AFEI.js')
|
|
341
|
+
]);
|
|
342
|
+
return schema;
|
|
343
|
+
}
|
|
278
344
|
default:
|
|
279
345
|
return createFallbackSchema(iso3, "personal-data");
|
|
280
346
|
}
|
|
@@ -294,38 +360,50 @@ async function loadSchema(iso3, formType) {
|
|
|
294
360
|
async function loadLocale(locale) {
|
|
295
361
|
switch (locale) {
|
|
296
362
|
case "en-US":
|
|
297
|
-
return import('./en-US-
|
|
363
|
+
return import('./en-US-RCNO3BZC.js').then((m) => m.default);
|
|
298
364
|
case "es-MX":
|
|
299
|
-
return import('./es-MX-
|
|
365
|
+
return import('./es-MX-IGOEW2P4.js').then((m) => m.default);
|
|
300
366
|
case "fr-FR":
|
|
301
|
-
return import('./fr-FR-
|
|
367
|
+
return import('./fr-FR-WZBI6O4O.js').then((m) => m.default);
|
|
302
368
|
case "pt-BR":
|
|
303
|
-
return import('./pt-BR-
|
|
369
|
+
return import('./pt-BR-ZDM55ITO.js').then((m) => m.default);
|
|
304
370
|
}
|
|
305
371
|
const lang = locale.split("-")[0]?.toLowerCase();
|
|
306
372
|
switch (lang) {
|
|
307
373
|
case "en":
|
|
308
|
-
return import('./en-US-
|
|
374
|
+
return import('./en-US-RCNO3BZC.js').then((m) => m.default);
|
|
309
375
|
case "es":
|
|
310
|
-
return import('./es-MX-
|
|
376
|
+
return import('./es-MX-IGOEW2P4.js').then((m) => m.default);
|
|
311
377
|
case "fr":
|
|
312
|
-
return import('./fr-FR-
|
|
378
|
+
return import('./fr-FR-WZBI6O4O.js').then((m) => m.default);
|
|
313
379
|
case "pt":
|
|
314
|
-
return import('./pt-BR-
|
|
380
|
+
return import('./pt-BR-ZDM55ITO.js').then((m) => m.default);
|
|
315
381
|
default:
|
|
316
|
-
return import('./en-US-
|
|
382
|
+
return import('./en-US-RCNO3BZC.js').then((m) => m.default);
|
|
317
383
|
}
|
|
318
384
|
}
|
|
319
385
|
|
|
320
386
|
// src/i18n/resolveLabel.ts
|
|
321
|
-
|
|
387
|
+
var PLACEHOLDER_REGEX = /\{(\w+)\}/g;
|
|
388
|
+
function resolveLabel(key, bundle, overrides, params) {
|
|
389
|
+
let resolved = key;
|
|
322
390
|
if (overrides !== void 0) {
|
|
323
391
|
const override = overrides[key];
|
|
324
|
-
if (override !== void 0)
|
|
392
|
+
if (override !== void 0) {
|
|
393
|
+
resolved = override;
|
|
394
|
+
} else {
|
|
395
|
+
const value = bundle[key];
|
|
396
|
+
if (value !== void 0) resolved = value;
|
|
397
|
+
}
|
|
398
|
+
} else {
|
|
399
|
+
const value = bundle[key];
|
|
400
|
+
if (value !== void 0) resolved = value;
|
|
325
401
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
402
|
+
if (params === void 0) return resolved;
|
|
403
|
+
return resolved.replace(PLACEHOLDER_REGEX, (match, name) => {
|
|
404
|
+
const replacement = params[name];
|
|
405
|
+
return replacement === void 0 ? match : String(replacement);
|
|
406
|
+
});
|
|
329
407
|
}
|
|
330
408
|
|
|
331
409
|
// src/i18n/types.ts
|
|
@@ -339,7 +417,13 @@ exports.formatValue = formatValue;
|
|
|
339
417
|
exports.getVisibleFields = getVisibleFields;
|
|
340
418
|
exports.loadLocale = loadLocale;
|
|
341
419
|
exports.loadSchema = loadSchema;
|
|
420
|
+
exports.resolveFieldFormatRef = resolveFieldFormatRef;
|
|
421
|
+
exports.resolveFieldInputMode = resolveFieldInputMode;
|
|
422
|
+
exports.resolveFieldLabel = resolveFieldLabel;
|
|
342
423
|
exports.resolveFieldOptions = resolveFieldOptions;
|
|
424
|
+
exports.resolveFieldPlaceholder = resolveFieldPlaceholder;
|
|
425
|
+
exports.resolveFieldUnformatRef = resolveFieldUnformatRef;
|
|
426
|
+
exports.resolveFieldValidator = resolveFieldValidator;
|
|
343
427
|
exports.resolveLabel = resolveLabel;
|
|
344
428
|
exports.setValue = setValue;
|
|
345
429
|
exports.unformatValue = unformatValue;
|