@ttoss/forms 0.41.2 → 0.42.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.
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { FieldValues, FieldPath } from 'react-hook-form';
3
- import { a as FormFieldPatternFormatProps, F as FormFieldProps } from '../FormFieldPatternFormat-kaO0pl1R.js';
3
+ import { a as FormFieldPatternFormatProps, F as FormFieldProps, d as FormFieldPhoneProps$1 } from '../FormFieldPhone-BKeOS0i6.js';
4
4
  import { PatternFormatProps, NumberFormatBaseProps } from 'react-number-format';
5
5
  import '@ttoss/ui';
6
6
  import 'react';
@@ -19,7 +19,18 @@ declare const FormFieldCPF: <TFieldValues extends FieldValues = FieldValues, TNa
19
19
  type FormFieldCPFOrCNPJProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = FormFieldProps<TFieldValues, TName> & Omit<NumberFormatBaseProps, 'name' | 'format'>;
20
20
  declare const FormFieldCPFOrCNPJ: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, ...props }: FormFieldCPFOrCNPJProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
21
21
 
22
- type FormFieldPhoneProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = FormFieldProps<TFieldValues, TName> & Omit<PatternFormatProps, 'name' | 'format'>;
23
- declare const FormFieldPhone: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, ...props }: FormFieldPhoneProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
22
+ type FormFieldPhoneProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<FormFieldPhoneProps$1<TFieldValues, TName>, 'countryCode' | 'format'>;
23
+ /**
24
+ * Brazilian phone number form field.
25
+ *
26
+ * Wraps the generic `FormFieldPhone` with the Brazil country code (`+55`)
27
+ * and the appropriate local number format pre-configured.
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * <FormFieldPhone name="phone" label="Phone" />
32
+ * ```
33
+ */
34
+ declare const FormFieldPhone: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ placeholder, countryCodeOptions, ...props }: FormFieldPhoneProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
24
35
 
25
36
  export { FormFieldCEP, FormFieldCNPJ, FormFieldCPF, FormFieldCPFOrCNPJ, FormFieldPhone, isCnpjValid, isCpfValid };
@@ -0,0 +1,217 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { TooltipProps, SxProp, InputProps } from '@ttoss/ui';
3
+ import { FieldValues, FieldPath, RegisterOptions, FieldPathValue, UseControllerReturn } from 'react-hook-form';
4
+ import { PatternFormatProps } from 'react-number-format';
5
+ import * as React from 'react';
6
+
7
+ type AuxiliaryCheckboxRules<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
8
+ /**
9
+ * Props for the AuxiliaryCheckbox component.
10
+ */
11
+ type AuxiliaryCheckboxProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
12
+ /**
13
+ * The label to display next to the checkbox.
14
+ */
15
+ label: React.ReactNode;
16
+ /**
17
+ * The name of the checkbox field in the form.
18
+ */
19
+ name: TName;
20
+ /**
21
+ * The default value of the checkbox.
22
+ * @default false
23
+ */
24
+ defaultValue?: boolean;
25
+ /**
26
+ * Validation rules for the checkbox field.
27
+ */
28
+ rules?: AuxiliaryCheckboxRules<TFieldValues, TName>;
29
+ /**
30
+ * Whether the checkbox is disabled.
31
+ */
32
+ disabled?: boolean;
33
+ };
34
+
35
+ type Rules<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
36
+ type FormFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
37
+ label?: React.ReactNode;
38
+ id?: string;
39
+ name: TName;
40
+ defaultValue?: FieldPathValue<TFieldValues, TName>;
41
+ disabled?: boolean;
42
+ labelTooltip?: TooltipProps;
43
+ warning?: string | React.ReactNode;
44
+ rules?: Rules<TFieldValues, TName>;
45
+ /**
46
+ * Optional auxiliary checkbox to render between the field and error message.
47
+ * Useful for input confirmation or conditional display of other fields.
48
+ */
49
+ auxiliaryCheckbox?: AuxiliaryCheckboxProps<TFieldValues, FieldPath<TFieldValues>>;
50
+ } & SxProp;
51
+ type FormFieldCompleteProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
52
+ render: (props: UseControllerReturn<TFieldValues, TName>) => React.ReactElement;
53
+ } & FormFieldProps<TFieldValues, TName>;
54
+ declare const FormField: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ label, id: idProp, name, defaultValue, disabled: propsDisabled, labelTooltip, sx, css, render, warning, rules, auxiliaryCheckbox, }: FormFieldCompleteProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
55
+
56
+ type FormFieldPatternFormatProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = FormFieldProps<TFieldValues, TName> & Omit<PatternFormatProps, 'name'> & Pick<InputProps, 'leadingIcon' | 'trailingIcon'>;
57
+ declare const FormFieldPatternFormat: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, ...props }: FormFieldPatternFormatProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
58
+
59
+ /**
60
+ * A curated list of the most common country calling codes paired with their
61
+ * typical local phone number format patterns (using `#` as digit placeholders).
62
+ *
63
+ * Import this constant and pass it to the `countryCodeOptions` prop of
64
+ * `FormFieldPhone` to give users a ready-made country-code picker that also
65
+ * automatically updates the number format when they switch countries.
66
+ *
67
+ * @example
68
+ * ```tsx
69
+ * import { FormFieldPhone } from '@ttoss/forms';
70
+ *
71
+ * // COMMON_PHONE_COUNTRY_CODES is the default — no need to pass it explicitly.
72
+ * // Index 0 is the "Manual" entry; index 1 is US (+1).
73
+ * const [countryCode, setCountryCode] = React.useState(
74
+ * COMMON_PHONE_COUNTRY_CODES[1].value // '+1'
75
+ * );
76
+ *
77
+ * <FormFieldPhone
78
+ * name="phone"
79
+ * label="Phone"
80
+ * countryCode={countryCode}
81
+ * onCountryCodeChange={setCountryCode}
82
+ * />
83
+ * ```
84
+ */
85
+ type CountryCodeOption = {
86
+ /** Label displayed in the dropdown (e.g. 'US +1'). */
87
+ label: string;
88
+ /** The calling-code value (e.g. '+1'). */
89
+ value: string;
90
+ /**
91
+ * Optional phone number format for the local part specific to this country
92
+ * (e.g. '(###) ###-####'). When the user selects this option the format
93
+ * is used automatically, overriding the `format` prop of `FormFieldPhone`.
94
+ */
95
+ format?: string | ((value: string) => string);
96
+ };
97
+ /**
98
+ * Sentinel value used as `countryCode` to indicate that the user wants to
99
+ * type the entire phone number manually (no pattern mask is applied).
100
+ * A `+` prefix is displayed before the input.
101
+ */
102
+ declare const MANUAL_PHONE_COUNTRY_CODE = "manual";
103
+ /**
104
+ * Common country calling codes sorted by numeric dial-code order.
105
+ * The "Manual" option is listed first, allowing users to type the full
106
+ * international number freely without any mask.
107
+ */
108
+ declare const COMMON_PHONE_COUNTRY_CODES: CountryCodeOption[];
109
+
110
+ type FormFieldPhoneProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = FormFieldProps<TFieldValues, TName> & Omit<PatternFormatProps, 'name' | 'format'> & {
111
+ /**
112
+ * The country calling code to display as a literal prefix in the input.
113
+ * For example, '+55' for Brazil or '+1' for the United States.
114
+ * Defaults to the first entry in `countryCodeOptions` when not provided.
115
+ */
116
+ countryCode?: string;
117
+ /**
118
+ * The pattern format for the local part of the phone number.
119
+ * Accepts either a static string (e.g., '(##) #####-####') or a function
120
+ * that receives the current raw value and returns the format string,
121
+ * which is useful for dynamic formats (e.g., different lengths).
122
+ *
123
+ * When the selected entry in countryCodeOptions includes its own
124
+ * format, that value takes precedence over this prop.
125
+ *
126
+ * Defaults to '(###) ###-####' when neither this prop nor the selected
127
+ * country option supplies a format.
128
+ *
129
+ * Ignored when `countryCode` is `MANUAL_PHONE_COUNTRY_CODE`.
130
+ */
131
+ format?: string | ((value: string) => string);
132
+ /**
133
+ * List of country calling code options to display in the dropdown.
134
+ * Defaults to `COMMON_PHONE_COUNTRY_CODES` (15 common countries + Manual).
135
+ * Pass an empty array to hide the dropdown and show a plain phone input.
136
+ */
137
+ countryCodeOptions?: CountryCodeOption[];
138
+ /**
139
+ * Called with the newly selected country code value when the user changes
140
+ * the country code via the dropdown. Only relevant when
141
+ * countryCodeOptions is non-empty.
142
+ */
143
+ onCountryCodeChange?: (countryCode: string) => void;
144
+ };
145
+ /**
146
+ * Generic phone number form field that supports an optional country code prefix.
147
+ *
148
+ * By default, a country-code dropdown is rendered using `COMMON_PHONE_COUNTRY_CODES`
149
+ * (15 common countries + a Manual entry; Manual is the first entry at index 0).
150
+ * Pass `countryCodeOptions={[]}` to disable the dropdown and show a plain phone input.
151
+ *
152
+ * The format prop defines the pattern for the local phone number (using #
153
+ * as digit placeholders). When a countryCode is provided it is prepended to
154
+ * the format and rendered as a read-only literal inside the input.
155
+ *
156
+ * When the user selects the "Manual" option (`MANUAL_PHONE_COUNTRY_CODE`),
157
+ * the pattern mask is disabled and a plain text input is shown so the user
158
+ * can type the full international number freely.
159
+ *
160
+ * When a country code is provided, the stored value is the country code
161
+ * concatenated directly with the raw local digits (e.g., `"+15555555555"`
162
+ * for country code `"+1"` and local digits `"5555555555"`). When no country
163
+ * code is set, only the raw local digits are stored.
164
+ *
165
+ * Changing the country code via the dropdown automatically resets the phone
166
+ * number field to an empty string, so a new number can be entered in the
167
+ * correct format for the selected country.
168
+ *
169
+ * @example
170
+ * ```tsx
171
+ * // Default: dropdown with COMMON_PHONE_COUNTRY_CODES, US (+1) pre-selected
172
+ * <FormFieldPhone name="phone" label="Phone" countryCode="+1" />
173
+ * ```
174
+ *
175
+ * @example
176
+ * ```tsx
177
+ * // Controlled country code — submitted value includes the prefix
178
+ * // e.g. { phone: '+15555555555' }
179
+ * const [countryCode, setCountryCode] = React.useState('+1');
180
+ * <FormFieldPhone
181
+ * name="phone"
182
+ * label="Phone"
183
+ * countryCode={countryCode}
184
+ * onCountryCodeChange={setCountryCode}
185
+ * />
186
+ * ```
187
+ *
188
+ * @example
189
+ * ```tsx
190
+ * // No dropdown — plain phone input; value includes the prefix
191
+ * // e.g. { phone: '+15555555555' }
192
+ * <FormFieldPhone
193
+ * name="phone"
194
+ * label="Phone"
195
+ * countryCode="+1"
196
+ * format="(###) ###-####"
197
+ * countryCodeOptions={[]}
198
+ * />
199
+ * ```
200
+ *
201
+ * @example
202
+ * ```tsx
203
+ * // Dynamic format (e.g. Brazilian numbers with 8 or 9 local digits)
204
+ * <FormFieldPhone
205
+ * name="phone"
206
+ * label="Phone"
207
+ * countryCode="+55"
208
+ * format={(value) =>
209
+ * value.length > 10 ? '(##) #####-####' : '(##) ####-#####'
210
+ * }
211
+ * countryCodeOptions={[]}
212
+ * />
213
+ * ```
214
+ */
215
+ declare const FormFieldPhone: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, countryCode: countryCodeProp, format, countryCodeOptions, onCountryCodeChange, ...props }: FormFieldPhoneProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
216
+
217
+ export { type CountryCodeOption as C, type FormFieldProps as F, MANUAL_PHONE_COUNTRY_CODE as M, type FormFieldPatternFormatProps as a, FormField as b, FormFieldPatternFormat as c, type FormFieldPhoneProps as d, FormFieldPhone as e, COMMON_PHONE_COUNTRY_CODES as f };
@@ -1,6 +1,6 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
  import * as React from 'react';
3
- import { FormField, FormFieldCNPJ, FormFieldCPF, FormFieldPatternFormat, __name, isCnpjValid, isCpfValid } from "../chunk-BL55OAIO.js";
3
+ import { FormField, FormFieldCNPJ, FormFieldCPF, FormFieldPatternFormat, FormFieldPhone, __name, isCnpjValid, isCpfValid } from "../chunk-ZORKZWD7.js";
4
4
 
5
5
  // src/Brazil/FormFieldCEP.tsx
6
6
  var FormFieldCEP = /* @__PURE__ */__name(({
@@ -73,56 +73,21 @@ var FormFieldCPFOrCNPJ = /* @__PURE__ */__name(({
73
73
  }, "FormFieldCPFOrCNPJ");
74
74
 
75
75
  // src/Brazil/FormFieldPhone.tsx
76
- import { Input as Input2 } from "@ttoss/ui";
77
- import { PatternFormat } from "react-number-format";
78
- var FormFieldPhone = /* @__PURE__ */__name(({
79
- disabled,
76
+ var BRAZIL_COUNTRY_CODE = "+55";
77
+ var getBrazilPhoneFormat = /* @__PURE__ */__name(value => {
78
+ return value.length > 10 ? "(##) #####-####" : "(##) ####-#####";
79
+ }, "getBrazilPhoneFormat");
80
+ var FormFieldPhone2 = /* @__PURE__ */__name(({
81
+ placeholder = "(11) 91234-1234",
82
+ countryCodeOptions = [],
80
83
  ...props
81
84
  }) => {
82
- const {
83
- label,
84
- name,
85
- labelTooltip,
86
- warning,
87
- sx,
88
- css,
89
- rules,
90
- id,
91
- defaultValue,
92
- placeholder = "(11) 91234-1234",
93
- auxiliaryCheckbox,
94
- ...patternFormatProps
95
- } = props;
96
- return /* @__PURE__ */React.createElement(FormField, {
97
- id,
98
- label,
99
- name,
100
- labelTooltip,
101
- warning,
102
- sx,
103
- css,
104
- defaultValue,
105
- rules,
106
- disabled,
107
- auxiliaryCheckbox,
108
- render: /* @__PURE__ */__name(({
109
- field
110
- }) => {
111
- const format = field.value?.length > 10 ? "(##) #####-####" : "(##) ####-#####";
112
- return /* @__PURE__ */React.createElement(PatternFormat, {
113
- ...patternFormatProps,
114
- name: field.name,
115
- value: field.value,
116
- onBlur: field.onBlur,
117
- onValueChange: /* @__PURE__ */__name(values => {
118
- field.onChange(values.value);
119
- }, "onValueChange"),
120
- format,
121
- customInput: Input2,
122
- placeholder,
123
- disabled: disabled ?? field.disabled
124
- });
125
- }, "render")
85
+ return /* @__PURE__ */React.createElement(FormFieldPhone, {
86
+ ...props,
87
+ countryCode: BRAZIL_COUNTRY_CODE,
88
+ format: getBrazilPhoneFormat,
89
+ placeholder,
90
+ countryCodeOptions
126
91
  });
127
92
  }, "FormFieldPhone");
128
- export { FormFieldCEP, FormFieldCNPJ, FormFieldCPF, FormFieldCPFOrCNPJ, FormFieldPhone, isCnpjValid, isCpfValid };
93
+ export { FormFieldCEP, FormFieldCNPJ, FormFieldCPF, FormFieldCPFOrCNPJ, FormFieldPhone2 as FormFieldPhone, isCnpjValid, isCpfValid };
@@ -1,7 +1,7 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
  import * as React from 'react';
3
- import { Form, useForm, yupResolver } from "../chunk-HN7QPZLJ.js";
4
- import { __name } from "../chunk-BL55OAIO.js";
3
+ import { Form, useForm, yupResolver } from "../chunk-YCW3GPTZ.js";
4
+ import { __name } from "../chunk-ZORKZWD7.js";
5
5
 
6
6
  // src/MultistepForm/MultistepForm.tsx
7
7
  import { Flex as Flex6 } from "@ttoss/ui";
@@ -1,6 +1,6 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
  import * as React from 'react';
3
- import { FormErrorMessage, FormField, FormFieldPatternFormat, __name, isCnpjValid, isCpfValid } from "./chunk-BL55OAIO.js";
3
+ import { FormErrorMessage, FormField, FormFieldPatternFormat, __name, isCnpjValid, isCpfValid } from "./chunk-ZORKZWD7.js";
4
4
 
5
5
  // src/Form.tsx
6
6
  import { Box } from "@ttoss/ui";
@@ -268,9 +268,223 @@ var FormFieldPatternFormat = /* @__PURE__ */__name(({
268
268
  });
269
269
  }, "FormFieldPatternFormat");
270
270
 
271
+ // src/phoneCountryCodes.ts
272
+ var MANUAL_PHONE_COUNTRY_CODE = "manual";
273
+ var COMMON_PHONE_COUNTRY_CODES = [{
274
+ label: "Manual",
275
+ value: MANUAL_PHONE_COUNTRY_CODE
276
+ }, {
277
+ label: "\u{1F1FA}\u{1F1F8} +1 (US/Canada)",
278
+ value: "+1",
279
+ format: "(###) ###-####"
280
+ }, {
281
+ label: "\u{1F1FF}\u{1F1E6} +27 (South Africa)",
282
+ value: "+27",
283
+ format: "## ### ####"
284
+ }, {
285
+ label: "\u{1F1EB}\u{1F1F7} +33 (France)",
286
+ value: "+33",
287
+ format: "# ## ## ## ##"
288
+ }, {
289
+ label: "\u{1F1EA}\u{1F1F8} +34 (Spain)",
290
+ value: "+34",
291
+ format: "### ### ###"
292
+ }, {
293
+ label: "\u{1F1EE}\u{1F1F9} +39 (Italy)",
294
+ value: "+39",
295
+ format: "### ### ####"
296
+ }, {
297
+ label: "\u{1F1EC}\u{1F1E7} +44 (UK)",
298
+ value: "+44",
299
+ format: "#### ### ####"
300
+ }, {
301
+ label: "\u{1F1E9}\u{1F1EA} +49 (Germany)",
302
+ value: "+49",
303
+ format: "### ########"
304
+ }, {
305
+ label: "\u{1F1F2}\u{1F1FD} +52 (Mexico)",
306
+ value: "+52",
307
+ format: "## ########"
308
+ }, {
309
+ label: "\u{1F1E6}\u{1F1F7} +54 (Argentina)",
310
+ value: "+54",
311
+ format: "## ########"
312
+ }, {
313
+ label: "\u{1F1E7}\u{1F1F7} +55 (Brazil)",
314
+ value: "+55",
315
+ format: "(##) #####-####"
316
+ }, {
317
+ label: "\u{1F1E6}\u{1F1FA} +61 (Australia)",
318
+ value: "+61",
319
+ format: "#### ### ###"
320
+ }, {
321
+ label: "\u{1F1EF}\u{1F1F5} +81 (Japan)",
322
+ value: "+81",
323
+ format: "##-####-####"
324
+ }, {
325
+ label: "\u{1F1E8}\u{1F1F3} +86 (China)",
326
+ value: "+86",
327
+ format: "###-####-####"
328
+ }, {
329
+ label: "\u{1F1EE}\u{1F1F3} +91 (India)",
330
+ value: "+91",
331
+ format: "#####-#####"
332
+ }, {
333
+ label: "\u{1F1F5}\u{1F1F9} +351 (Portugal)",
334
+ value: "+351",
335
+ format: "### ### ###"
336
+ }];
337
+
338
+ // src/FormFieldPhone.tsx
339
+ import { Box, Flex as Flex3, Input as Input2, Select } from "@ttoss/ui";
340
+ import * as React4 from "react";
341
+ import { NumericFormat, PatternFormat as PatternFormat2 } from "react-number-format";
342
+ var PhoneDropdownWrapper = /* @__PURE__ */__name(({
343
+ id,
344
+ countryCode,
345
+ countryCodeOptions,
346
+ disabled,
347
+ onCountryCodeChange,
348
+ onPhoneReset,
349
+ inputNode
350
+ }) => {
351
+ return /* @__PURE__ */React4.createElement(Flex3, {
352
+ sx: {
353
+ gap: "2",
354
+ alignItems: "stretch"
355
+ }
356
+ }, /* @__PURE__ */React4.createElement(Box, {
357
+ sx: {
358
+ minWidth: "180px"
359
+ }
360
+ }, /* @__PURE__ */React4.createElement(Select, {
361
+ options: countryCodeOptions,
362
+ value: countryCode,
363
+ disabled,
364
+ onChange: /* @__PURE__ */__name(value => {
365
+ if (value !== void 0) {
366
+ onPhoneReset?.();
367
+ onCountryCodeChange?.(String(value));
368
+ }
369
+ }, "onChange")
370
+ })), /* @__PURE__ */React4.createElement(Box, {
371
+ sx: {
372
+ flex: 1
373
+ }
374
+ }, /* @__PURE__ */React4.cloneElement(inputNode, {
375
+ id
376
+ })));
377
+ }, "PhoneDropdownWrapper");
378
+ var FormFieldPhone = /* @__PURE__ */__name(({
379
+ disabled,
380
+ countryCode: countryCodeProp,
381
+ format,
382
+ countryCodeOptions = COMMON_PHONE_COUNTRY_CODES,
383
+ onCountryCodeChange,
384
+ ...props
385
+ }) => {
386
+ const {
387
+ label,
388
+ name,
389
+ labelTooltip,
390
+ warning,
391
+ sx,
392
+ css,
393
+ rules,
394
+ id,
395
+ defaultValue,
396
+ auxiliaryCheckbox,
397
+ onBlur,
398
+ onValueChange,
399
+ placeholder,
400
+ ...patternFormatProps
401
+ } = props;
402
+ const countryCode = countryCodeProp ?? countryCodeOptions[0]?.value ?? void 0;
403
+ const isManual = countryCode === MANUAL_PHONE_COUNTRY_CODE;
404
+ const getFormat = /* @__PURE__ */__name(value => {
405
+ const selectedOption = countryCodeOptions.find(opt => {
406
+ return opt.value === countryCode;
407
+ });
408
+ const resolvedFormat = selectedOption?.format ?? format ?? "(###) ###-####";
409
+ const baseFormat = typeof resolvedFormat === "function" ? resolvedFormat(value) : resolvedFormat;
410
+ return countryCode && !isManual ? `${countryCode} ${baseFormat}` : baseFormat;
411
+ }, "getFormat");
412
+ const showDropdown = countryCodeOptions.length > 0;
413
+ return /* @__PURE__ */React4.createElement(FormField, {
414
+ id,
415
+ label,
416
+ name,
417
+ labelTooltip,
418
+ warning,
419
+ sx,
420
+ css,
421
+ defaultValue,
422
+ rules,
423
+ disabled,
424
+ auxiliaryCheckbox,
425
+ render: /* @__PURE__ */__name(({
426
+ field,
427
+ fieldState
428
+ }) => {
429
+ const localPhoneValue = !isManual && countryCode && field.value?.startsWith(countryCode) ? field.value.slice(countryCode.length) : field.value;
430
+ const inputNode = isManual ? /* @__PURE__ */React4.createElement(NumericFormat, {
431
+ name: field.name,
432
+ value: field.value ?? "",
433
+ placeholder,
434
+ prefix: "+",
435
+ allowLeadingZeros: true,
436
+ decimalScale: 0,
437
+ thousandSeparator: false,
438
+ onBlur: /* @__PURE__ */__name(e => {
439
+ field.onBlur();
440
+ onBlur?.(e);
441
+ }, "onBlur"),
442
+ onValueChange: /* @__PURE__ */__name((values, sourceInfo) => {
443
+ field.onChange(values.formattedValue);
444
+ onValueChange?.(values, sourceInfo);
445
+ }, "onValueChange"),
446
+ customInput: Input2,
447
+ disabled: disabled ?? field.disabled,
448
+ "aria-invalid": fieldState.error ? "true" : void 0
449
+ }) : /* @__PURE__ */React4.createElement(PatternFormat2, {
450
+ ...patternFormatProps,
451
+ name: field.name,
452
+ value: localPhoneValue,
453
+ placeholder,
454
+ onBlur: /* @__PURE__ */__name(e => {
455
+ field.onBlur();
456
+ onBlur?.(e);
457
+ }, "onBlur"),
458
+ onValueChange: /* @__PURE__ */__name((values, sourceInfo) => {
459
+ const fullValue = countryCode && values.value ? countryCode + values.value : values.value;
460
+ field.onChange(fullValue);
461
+ onValueChange?.(values, sourceInfo);
462
+ }, "onValueChange"),
463
+ format: getFormat(localPhoneValue ?? ""),
464
+ customInput: Input2,
465
+ disabled: disabled ?? field.disabled,
466
+ "aria-invalid": fieldState.error ? "true" : void 0
467
+ });
468
+ if (!showDropdown) {
469
+ return inputNode;
470
+ }
471
+ return /* @__PURE__ */React4.createElement(PhoneDropdownWrapper, {
472
+ countryCode,
473
+ countryCodeOptions,
474
+ disabled: disabled ?? field.disabled,
475
+ onCountryCodeChange,
476
+ onPhoneReset: /* @__PURE__ */__name(() => {
477
+ return field.onChange("");
478
+ }, "onPhoneReset"),
479
+ inputNode
480
+ });
481
+ }, "render")
482
+ });
483
+ }, "FormFieldPhone");
484
+
271
485
  // src/Brazil/FormFieldCNPJ.tsx
272
- import { Input as Input2 } from "@ttoss/ui";
273
- import { PatternFormat as PatternFormat2 } from "react-number-format";
486
+ import { Input as Input3 } from "@ttoss/ui";
487
+ import { PatternFormat as PatternFormat3 } from "react-number-format";
274
488
  var isCnpjValid = /* @__PURE__ */__name(cnpj => {
275
489
  if (cnpj?.length != 14) {
276
490
  return false;
@@ -342,7 +556,7 @@ var FormFieldCNPJ = /* @__PURE__ */__name(({
342
556
  render: /* @__PURE__ */__name(({
343
557
  field
344
558
  }) => {
345
- return /* @__PURE__ */React.createElement(PatternFormat2, {
559
+ return /* @__PURE__ */React.createElement(PatternFormat3, {
346
560
  ...patternFormatProps,
347
561
  name: field.name,
348
562
  value: field.value,
@@ -351,7 +565,7 @@ var FormFieldCNPJ = /* @__PURE__ */__name(({
351
565
  field.onChange(values.value);
352
566
  }, "onValueChange"),
353
567
  format: "##.###.###/####-##",
354
- customInput: Input2,
568
+ customInput: Input3,
355
569
  placeholder,
356
570
  disabled: disabled ?? field.disabled
357
571
  });
@@ -360,8 +574,8 @@ var FormFieldCNPJ = /* @__PURE__ */__name(({
360
574
  }, "FormFieldCNPJ");
361
575
 
362
576
  // src/Brazil/FormFieldCPF.tsx
363
- import { Input as Input3 } from "@ttoss/ui";
364
- import { PatternFormat as PatternFormat3 } from "react-number-format";
577
+ import { Input as Input4 } from "@ttoss/ui";
578
+ import { PatternFormat as PatternFormat4 } from "react-number-format";
365
579
  var isCpfValid = /* @__PURE__ */__name(cpf => {
366
580
  if (cpf?.length != 11) {
367
581
  return false;
@@ -424,7 +638,7 @@ var FormFieldCPF = /* @__PURE__ */__name(({
424
638
  render: /* @__PURE__ */__name(({
425
639
  field
426
640
  }) => {
427
- return /* @__PURE__ */React.createElement(PatternFormat3, {
641
+ return /* @__PURE__ */React.createElement(PatternFormat4, {
428
642
  ...patternFormatProps,
429
643
  name: field.name,
430
644
  value: field.value,
@@ -433,11 +647,11 @@ var FormFieldCPF = /* @__PURE__ */__name(({
433
647
  field.onChange(values.value);
434
648
  }, "onValueChange"),
435
649
  format: "###.###.###-##",
436
- customInput: Input3,
650
+ customInput: Input4,
437
651
  placeholder,
438
652
  disabled: disabled ?? field.disabled
439
653
  });
440
654
  }, "render")
441
655
  });
442
656
  }, "FormFieldCPF");
443
- export { __name, FormErrorMessage, FormField, FormFieldPatternFormat, isCnpjValid, FormFieldCNPJ, isCpfValid, FormFieldCPF };
657
+ export { __name, FormErrorMessage, FormField, FormFieldPatternFormat, MANUAL_PHONE_COUNTRY_CODE, COMMON_PHONE_COUNTRY_CODES, FormFieldPhone, isCnpjValid, FormFieldCNPJ, isCpfValid, FormFieldCPF };
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- import { Controller, Form, FormFieldCheckbox, FormFieldCreditCardNumber, FormFieldCurrencyInput, FormFieldDatePicker, FormFieldInput, FormFieldNumericFormat, FormFieldPassword, FormFieldRadio, FormFieldRadioCard, FormFieldRadioCardIcony, FormFieldSegmentedControl, FormFieldSelect, FormFieldSwitch, FormFieldTextarea, FormGroup, FormProvider, useController, useFieldArray, useForm, useFormContext, useFormGroup, useFormState, useWatch, yup, yupResolver, z, zodResolver } from "./chunk-HN7QPZLJ.js";
3
- import { FormErrorMessage, FormField, FormFieldPatternFormat } from "./chunk-BL55OAIO.js";
4
- export { Controller, Form, FormErrorMessage, FormField, FormFieldCheckbox, FormFieldCreditCardNumber, FormFieldCurrencyInput, FormFieldDatePicker, FormFieldInput, FormFieldNumericFormat, FormFieldPassword, FormFieldPatternFormat, FormFieldRadio, FormFieldRadioCard, FormFieldRadioCardIcony, FormFieldSegmentedControl, FormFieldSelect, FormFieldSwitch, FormFieldTextarea, FormGroup, FormProvider, useController, useFieldArray, useForm, useFormContext, useFormGroup, useFormState, useWatch, yup, yupResolver, z, zodResolver };
2
+ import { Controller, Form, FormFieldCheckbox, FormFieldCreditCardNumber, FormFieldCurrencyInput, FormFieldDatePicker, FormFieldInput, FormFieldNumericFormat, FormFieldPassword, FormFieldRadio, FormFieldRadioCard, FormFieldRadioCardIcony, FormFieldSegmentedControl, FormFieldSelect, FormFieldSwitch, FormFieldTextarea, FormGroup, FormProvider, useController, useFieldArray, useForm, useFormContext, useFormGroup, useFormState, useWatch, yup, yupResolver, z, zodResolver } from "./chunk-YCW3GPTZ.js";
3
+ import { COMMON_PHONE_COUNTRY_CODES, FormErrorMessage, FormField, FormFieldPatternFormat, FormFieldPhone, MANUAL_PHONE_COUNTRY_CODE } from "./chunk-ZORKZWD7.js";
4
+ export { COMMON_PHONE_COUNTRY_CODES, Controller, Form, FormErrorMessage, FormField, FormFieldCheckbox, FormFieldCreditCardNumber, FormFieldCurrencyInput, FormFieldDatePicker, FormFieldInput, FormFieldNumericFormat, FormFieldPassword, FormFieldPatternFormat, FormFieldPhone, FormFieldRadio, FormFieldRadioCard, FormFieldRadioCardIcony, FormFieldSegmentedControl, FormFieldSelect, FormFieldSwitch, FormFieldTextarea, FormGroup, FormProvider, MANUAL_PHONE_COUNTRY_CODE, useController, useFieldArray, useForm, useFormContext, useFormGroup, useFormState, useWatch, yup, yupResolver, z, zodResolver };
package/dist/index.d.ts CHANGED
@@ -4,8 +4,8 @@ import * as React from 'react';
4
4
  import { FieldValues, FormProviderProps, FieldName, FieldPath } from 'react-hook-form';
5
5
  export * from 'react-hook-form';
6
6
  export { Controller, FormProvider, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch } from 'react-hook-form';
7
- import { F as FormFieldProps, a as FormFieldPatternFormatProps } from './FormFieldPatternFormat-kaO0pl1R.js';
8
- export { b as FormField, c as FormFieldPatternFormat } from './FormFieldPatternFormat-kaO0pl1R.js';
7
+ import { F as FormFieldProps, a as FormFieldPatternFormatProps } from './FormFieldPhone-BKeOS0i6.js';
8
+ export { f as COMMON_PHONE_COUNTRY_CODES, C as CountryCodeOption, b as FormField, c as FormFieldPatternFormat, e as FormFieldPhone, d as FormFieldPhoneProps, M as MANUAL_PHONE_COUNTRY_CODE } from './FormFieldPhone-BKeOS0i6.js';
9
9
  import { NumericFormatProps } from 'react-number-format';
10
10
  import { DateRange } from '@ttoss/components/DatePicker';
11
11
  import './typings.d-BzNUo1mD.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/forms",
3
- "version": "0.41.2",
3
+ "version": "0.42.0",
4
4
  "license": "MIT",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -40,9 +40,9 @@
40
40
  },
41
41
  "peerDependencies": {
42
42
  "react": ">=16.8.0",
43
- "@ttoss/ui": "^6.7.0",
44
- "@ttoss/components": "^2.13.1",
45
- "@ttoss/react-i18n": "^2.1.0"
43
+ "@ttoss/components": "^2.13.2",
44
+ "@ttoss/react-i18n": "^2.1.0",
45
+ "@ttoss/ui": "^6.8.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/jest": "^30.0.0",
@@ -52,13 +52,13 @@
52
52
  "react-error-boundary": "^6.1.0",
53
53
  "tsup": "^8.5.1",
54
54
  "yup": "^1.7.1",
55
- "@ttoss/components": "^2.13.1",
55
+ "@ttoss/components": "^2.13.2",
56
56
  "@ttoss/i18n-cli": "^0.7.39",
57
+ "@ttoss/react-i18n": "^2.1.0",
57
58
  "@ttoss/react-icons": "^0.6.0",
58
- "@ttoss/config": "^1.36.0",
59
- "@ttoss/ui": "^6.7.0",
60
59
  "@ttoss/test-utils": "^4.1.0",
61
- "@ttoss/react-i18n": "^2.1.0"
60
+ "@ttoss/ui": "^6.8.0",
61
+ "@ttoss/config": "^1.36.0"
62
62
  },
63
63
  "publishConfig": {
64
64
  "access": "public",
@@ -1,59 +0,0 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { TooltipProps, SxProp, InputProps } from '@ttoss/ui';
3
- import { FieldValues, FieldPath, RegisterOptions, FieldPathValue, UseControllerReturn } from 'react-hook-form';
4
- import { PatternFormatProps } from 'react-number-format';
5
- import * as React from 'react';
6
-
7
- type AuxiliaryCheckboxRules<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
8
- /**
9
- * Props for the AuxiliaryCheckbox component.
10
- */
11
- type AuxiliaryCheckboxProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
12
- /**
13
- * The label to display next to the checkbox.
14
- */
15
- label: React.ReactNode;
16
- /**
17
- * The name of the checkbox field in the form.
18
- */
19
- name: TName;
20
- /**
21
- * The default value of the checkbox.
22
- * @default false
23
- */
24
- defaultValue?: boolean;
25
- /**
26
- * Validation rules for the checkbox field.
27
- */
28
- rules?: AuxiliaryCheckboxRules<TFieldValues, TName>;
29
- /**
30
- * Whether the checkbox is disabled.
31
- */
32
- disabled?: boolean;
33
- };
34
-
35
- type Rules<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
36
- type FormFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
37
- label?: React.ReactNode;
38
- id?: string;
39
- name: TName;
40
- defaultValue?: FieldPathValue<TFieldValues, TName>;
41
- disabled?: boolean;
42
- labelTooltip?: TooltipProps;
43
- warning?: string | React.ReactNode;
44
- rules?: Rules<TFieldValues, TName>;
45
- /**
46
- * Optional auxiliary checkbox to render between the field and error message.
47
- * Useful for input confirmation or conditional display of other fields.
48
- */
49
- auxiliaryCheckbox?: AuxiliaryCheckboxProps<TFieldValues, FieldPath<TFieldValues>>;
50
- } & SxProp;
51
- type FormFieldCompleteProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
52
- render: (props: UseControllerReturn<TFieldValues, TName>) => React.ReactElement;
53
- } & FormFieldProps<TFieldValues, TName>;
54
- declare const FormField: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ label, id: idProp, name, defaultValue, disabled: propsDisabled, labelTooltip, sx, css, render, warning, rules, auxiliaryCheckbox, }: FormFieldCompleteProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
55
-
56
- type FormFieldPatternFormatProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = FormFieldProps<TFieldValues, TName> & Omit<PatternFormatProps, 'name'> & Pick<InputProps, 'leadingIcon' | 'trailingIcon'>;
57
- declare const FormFieldPatternFormat: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, ...props }: FormFieldPatternFormatProps<TFieldValues, TName>) => react_jsx_runtime.JSX.Element;
58
-
59
- export { type FormFieldProps as F, type FormFieldPatternFormatProps as a, FormField as b, FormFieldPatternFormat as c };