@ttoss/forms 0.35.2 → 0.35.3

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,442 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- import * as React from 'react';
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", {
5
- value,
6
- configurable: true
7
- });
8
-
9
- // src/FormErrorMessage.tsx
10
- import { ErrorMessage } from "@hookform/error-message";
11
- import { FormattedMessage } from "@ttoss/react-i18n";
12
- import { HelpText } from "@ttoss/ui";
13
- import { useFormContext } from "react-hook-form";
14
- var isMessageDescriptor = /* @__PURE__ */__name(possibleMessageDescriptor => {
15
- return possibleMessageDescriptor !== void 0 && possibleMessageDescriptor.defaultMessage !== void 0;
16
- }, "isMessageDescriptor");
17
- var FormErrorMessage = /* @__PURE__ */__name(({
18
- name,
19
- disabled
20
- }) => {
21
- const {
22
- formState: {
23
- errors
24
- }
25
- } = useFormContext();
26
- return /* @__PURE__ */React.createElement(ErrorMessage, {
27
- name,
28
- errors,
29
- render: /* @__PURE__ */__name(({
30
- message
31
- }) => {
32
- return /* @__PURE__ */React.createElement(HelpText, {
33
- negative: true,
34
- disabled
35
- }, (() => {
36
- if (typeof message === "string") {
37
- return message;
38
- }
39
- if (isMessageDescriptor(message)) {
40
- return /* @__PURE__ */React.createElement(FormattedMessage, message);
41
- }
42
- return JSON.stringify(message);
43
- })());
44
- }, "render")
45
- });
46
- }, "FormErrorMessage");
47
-
48
- // src/FormField.tsx
49
- import { Checkbox as Checkbox2, Flex as Flex2, Label as Label2, Switch } from "@ttoss/ui";
50
- import * as React3 from "react";
51
- import { useController as useController2, useFormContext as useFormContext2 } from "react-hook-form";
52
-
53
- // src/AuxiliaryCheckbox.tsx
54
- import { Checkbox, Flex, Label } from "@ttoss/ui";
55
- import * as React2 from "react";
56
- import { useController } from "react-hook-form";
57
- var AuxiliaryCheckbox = /* @__PURE__ */__name(({
58
- label,
59
- name,
60
- defaultValue = false,
61
- rules,
62
- disabled
63
- }) => {
64
- const uniqueId = React2.useId();
65
- const id = `auxiliary-checkbox-${name}-${uniqueId}`;
66
- const {
67
- field
68
- } = useController({
69
- name,
70
- defaultValue,
71
- rules
72
- });
73
- const isDisabled = disabled ?? field.disabled;
74
- const {
75
- value,
76
- ...fieldWithoutValue
77
- } = field;
78
- return /* @__PURE__ */React2.createElement(Label, {
79
- "aria-disabled": isDisabled,
80
- htmlFor: id,
81
- sx: {
82
- flexDirection: "row",
83
- alignItems: "center",
84
- cursor: isDisabled ? "not-allowed" : "pointer"
85
- }
86
- }, /* @__PURE__ */React2.createElement(Flex, {
87
- sx: {
88
- position: "relative"
89
- }
90
- }, /* @__PURE__ */React2.createElement(Checkbox, {
91
- id,
92
- ...fieldWithoutValue,
93
- checked: value,
94
- disabled: isDisabled
95
- })), label);
96
- }, "AuxiliaryCheckbox");
97
-
98
- // src/FormField.tsx
99
- var FormField = /* @__PURE__ */__name(({
100
- label,
101
- id: idProp,
102
- name,
103
- defaultValue,
104
- disabled: propsDisabled,
105
- labelTooltip,
106
- sx,
107
- css,
108
- render,
109
- warning,
110
- rules,
111
- auxiliaryCheckbox
112
- }) => {
113
- const controllerReturn = useController2({
114
- name,
115
- defaultValue,
116
- rules
117
- });
118
- const {
119
- formState: {
120
- errors
121
- }
122
- } = useFormContext2();
123
- const disabled = propsDisabled ?? controllerReturn.field.disabled;
124
- const hasError = !!errors[name];
125
- const hasAuxiliaryError = auxiliaryCheckbox ? !!errors[auxiliaryCheckbox.name] : false;
126
- const uniqueId = React3.useId();
127
- const id = idProp || `form-field-${name}-${uniqueId}`;
128
- const isCheckboxOrSwitch = /* @__PURE__ */__name(element => {
129
- return [Checkbox2, Switch].some(component => {
130
- return element.type === component;
131
- });
132
- }, "isCheckboxOrSwitch");
133
- const controllerReturnWithDisabled = React3.useMemo(() => {
134
- return {
135
- ...controllerReturn,
136
- field: {
137
- ...controllerReturn.field,
138
- disabled
139
- }
140
- };
141
- }, [controllerReturn, disabled]);
142
- const memoizedRender = React3.useMemo(() => {
143
- return React3.Children.map(render(controllerReturnWithDisabled), child => {
144
- if (! /* @__PURE__ */React3.isValidElement(child)) {
145
- return null;
146
- }
147
- const mergeProps = {
148
- id,
149
- ...(warning && {
150
- trailingIcon: "warning-alt"
151
- })
152
- };
153
- if (label && isCheckboxOrSwitch(child)) {
154
- return /* @__PURE__ */React3.createElement(Label2, {
155
- "aria-disabled": disabled,
156
- htmlFor: id,
157
- tooltip: labelTooltip,
158
- sx: {
159
- flexDirection: "row",
160
- alignItems: "center",
161
- cursor: disabled ? "not-allowed" : "pointer"
162
- }
163
- }, /* @__PURE__ */React3.createElement(Flex2, {
164
- sx: {
165
- position: "relative"
166
- }
167
- }, /* @__PURE__ */React3.cloneElement(child, mergeProps)), label);
168
- }
169
- return /* @__PURE__ */React3.createElement(Flex2, {
170
- sx: {
171
- width: "full",
172
- flexDirection: "column",
173
- gap: "2"
174
- }
175
- }, label && /* @__PURE__ */React3.createElement(Label2, {
176
- "aria-disabled": disabled,
177
- htmlFor: id,
178
- tooltip: labelTooltip
179
- }, label), /* @__PURE__ */React3.cloneElement(child, mergeProps));
180
- });
181
- }, [render, controllerReturnWithDisabled, label, disabled, id, labelTooltip, warning]);
182
- const errorNameToDisplay = hasError ? name : hasAuxiliaryError && auxiliaryCheckbox ? auxiliaryCheckbox.name : name;
183
- return /* @__PURE__ */React3.createElement(Flex2, {
184
- sx: {
185
- flexDirection: "column",
186
- width: "100%",
187
- gap: "1",
188
- ...sx
189
- },
190
- css
191
- }, memoizedRender, auxiliaryCheckbox && /* @__PURE__ */React3.createElement(AuxiliaryCheckbox, {
192
- ...auxiliaryCheckbox,
193
- disabled
194
- }), /* @__PURE__ */React3.createElement(FormErrorMessage, {
195
- name: errorNameToDisplay
196
- }), warning && !hasError && /* @__PURE__ */React3.createElement(Flex2, {
197
- className: "warning",
198
- sx: {
199
- color: "feedback.text.caution.default",
200
- fontSize: "sm",
201
- gap: "2",
202
- paddingBottom: "1",
203
- alignItems: "center"
204
- }
205
- }, warning));
206
- }, "FormField");
207
-
208
- // src/FormFieldPatternFormat.tsx
209
- import { Input } from "@ttoss/ui";
210
- import { PatternFormat } from "react-number-format";
211
- var FormFieldPatternFormat = /* @__PURE__ */__name(({
212
- disabled,
213
- ...props
214
- }) => {
215
- const {
216
- label,
217
- name,
218
- labelTooltip,
219
- warning,
220
- sx,
221
- css,
222
- rules,
223
- id,
224
- defaultValue,
225
- leadingIcon,
226
- trailingIcon,
227
- auxiliaryCheckbox,
228
- onBlur,
229
- onValueChange,
230
- ...patternFormatProps
231
- } = props;
232
- return /* @__PURE__ */React.createElement(FormField, {
233
- id,
234
- label,
235
- name,
236
- labelTooltip,
237
- warning,
238
- sx,
239
- css,
240
- defaultValue,
241
- rules,
242
- disabled,
243
- auxiliaryCheckbox,
244
- render: /* @__PURE__ */__name(({
245
- field,
246
- fieldState
247
- }) => {
248
- return /* @__PURE__ */React.createElement(PatternFormat, {
249
- ...patternFormatProps,
250
- name: field.name,
251
- value: field.value,
252
- onBlur: /* @__PURE__ */__name(e => {
253
- field.onBlur();
254
- onBlur?.(e);
255
- }, "onBlur"),
256
- onValueChange: /* @__PURE__ */__name((values, sourceInfo) => {
257
- field.onChange(values.value);
258
- onValueChange?.(values, sourceInfo);
259
- }, "onValueChange"),
260
- customInput: Input,
261
- leadingIcon,
262
- trailingIcon,
263
- disabled: disabled ?? field.disabled,
264
- "aria-invalid": fieldState.error ? "true" : void 0
265
- });
266
- }, "render")
267
- });
268
- }, "FormFieldPatternFormat");
269
-
270
- // src/Brazil/FormFieldCNPJ.tsx
271
- import { Input as Input2 } from "@ttoss/ui";
272
- import { PatternFormat as PatternFormat2 } from "react-number-format";
273
- var isCnpjValid = /* @__PURE__ */__name(cnpj => {
274
- if (cnpj?.length != 14) {
275
- return false;
276
- }
277
- if (cnpj == "00000000000000" || cnpj == "11111111111111" || cnpj == "22222222222222" || cnpj == "33333333333333" || cnpj == "44444444444444" || cnpj == "55555555555555" || cnpj == "66666666666666" || cnpj == "77777777777777" || cnpj == "88888888888888" || cnpj == "99999999999999") {
278
- return false;
279
- }
280
- let size = cnpj.length - 2;
281
- let numbers = cnpj.substring(0, size);
282
- const digits = cnpj.substring(size);
283
- let soma = 0;
284
- let pos = size - 7;
285
- for (let i = size; i >= 1; i--) {
286
- soma += numbers.charAt(size - i) * pos--;
287
- if (pos < 2) {
288
- pos = 9;
289
- }
290
- }
291
- let result = soma % 11 < 2 ? 0 : 11 - soma % 11;
292
- if (result != digits.charAt(0)) {
293
- return false;
294
- }
295
- size = size + 1;
296
- numbers = cnpj.substring(0, size);
297
- soma = 0;
298
- pos = size - 7;
299
- for (let i = size; i >= 1; i--) {
300
- soma += numbers.charAt(size - i) * pos--;
301
- if (pos < 2) {
302
- pos = 9;
303
- }
304
- }
305
- result = soma % 11 < 2 ? 0 : 11 - soma % 11;
306
- if (result != digits.charAt(1)) {
307
- return false;
308
- }
309
- return true;
310
- }, "isCnpjValid");
311
- var FormFieldCNPJ = /* @__PURE__ */__name(({
312
- disabled,
313
- ...props
314
- }) => {
315
- const {
316
- label,
317
- name,
318
- labelTooltip,
319
- warning,
320
- sx,
321
- css,
322
- rules,
323
- id,
324
- defaultValue,
325
- placeholder = "12.345.678/0000-00",
326
- auxiliaryCheckbox,
327
- ...patternFormatProps
328
- } = props;
329
- return /* @__PURE__ */React.createElement(FormField, {
330
- id,
331
- label,
332
- name,
333
- labelTooltip,
334
- warning,
335
- sx,
336
- css,
337
- defaultValue,
338
- rules,
339
- disabled,
340
- auxiliaryCheckbox,
341
- render: /* @__PURE__ */__name(({
342
- field
343
- }) => {
344
- return /* @__PURE__ */React.createElement(PatternFormat2, {
345
- ...patternFormatProps,
346
- name: field.name,
347
- value: field.value,
348
- onBlur: field.onBlur,
349
- onValueChange: /* @__PURE__ */__name(values => {
350
- field.onChange(values.value);
351
- }, "onValueChange"),
352
- format: "##.###.###/####-##",
353
- customInput: Input2,
354
- placeholder,
355
- disabled: disabled ?? field.disabled
356
- });
357
- }, "render")
358
- });
359
- }, "FormFieldCNPJ");
360
-
361
- // src/Brazil/FormFieldCPF.tsx
362
- import { Input as Input3 } from "@ttoss/ui";
363
- import { PatternFormat as PatternFormat3 } from "react-number-format";
364
- var isCpfValid = /* @__PURE__ */__name(cpf => {
365
- if (cpf?.length != 11) {
366
- return false;
367
- }
368
- if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") {
369
- return false;
370
- }
371
- let soma = 0;
372
- for (let i = 0; i < 9; i++) {
373
- soma += parseInt(cpf.charAt(i)) * (10 - i);
374
- }
375
- let resto = soma * 10 % 11;
376
- if (resto === 10 || resto === 11) {
377
- resto = 0;
378
- }
379
- if (resto != parseInt(cpf.charAt(9))) {
380
- return false;
381
- }
382
- soma = 0;
383
- for (let i = 0; i < 10; i++) {
384
- soma += parseInt(cpf.charAt(i)) * (11 - i);
385
- }
386
- resto = soma * 10 % 11;
387
- if (resto === 10 || resto === 11) {
388
- resto = 0;
389
- }
390
- if (resto != parseInt(cpf.charAt(10))) {
391
- return false;
392
- }
393
- return true;
394
- }, "isCpfValid");
395
- var FormFieldCPF = /* @__PURE__ */__name(({
396
- disabled,
397
- ...props
398
- }) => {
399
- const {
400
- label,
401
- name,
402
- labelTooltip,
403
- warning,
404
- sx,
405
- css,
406
- rules,
407
- id,
408
- defaultValue,
409
- placeholder = "123.456.789-00",
410
- ...patternFormatProps
411
- } = props;
412
- return /* @__PURE__ */React.createElement(FormField, {
413
- id,
414
- label,
415
- name,
416
- labelTooltip,
417
- warning,
418
- sx,
419
- css,
420
- defaultValue,
421
- rules,
422
- disabled,
423
- render: /* @__PURE__ */__name(({
424
- field
425
- }) => {
426
- return /* @__PURE__ */React.createElement(PatternFormat3, {
427
- ...patternFormatProps,
428
- name: field.name,
429
- value: field.value,
430
- onBlur: field.onBlur,
431
- onValueChange: /* @__PURE__ */__name(values => {
432
- field.onChange(values.value);
433
- }, "onValueChange"),
434
- format: "###.###.###-##",
435
- customInput: Input3,
436
- placeholder,
437
- disabled: disabled ?? field.disabled
438
- });
439
- }, "render")
440
- });
441
- }, "FormFieldCPF");
442
- export { __name, FormErrorMessage, FormField, FormFieldPatternFormat, isCnpjValid, FormFieldCNPJ, isCpfValid, FormFieldCPF };