@ttoss/forms 0.22.1 → 0.22.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.
@@ -0,0 +1,638 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+
3
+ // src/i18n.ts
4
+ import { defineMessage } from "@ttoss/react-i18n";
5
+ import { setLocale } from "yup";
6
+ setLocale({
7
+ mixed: {
8
+ required: defineMessage({
9
+ id: "MfWGyg",
10
+ defaultMessage: [{
11
+ "type": 0,
12
+ "value": "Field is required"
13
+ }]
14
+ }),
15
+ notType: ({
16
+ type
17
+ }) => {
18
+ return {
19
+ ...defineMessage({
20
+ id: "ZhaPt0",
21
+ defaultMessage: [{
22
+ "type": 0,
23
+ "value": "Invalid Value for Field of type "
24
+ }, {
25
+ "type": 1,
26
+ "value": "type"
27
+ }]
28
+ }),
29
+ values: {
30
+ type
31
+ }
32
+ };
33
+ }
34
+ },
35
+ string: {
36
+ min: ({
37
+ min
38
+ }) => {
39
+ return {
40
+ ...defineMessage({
41
+ id: "D1C6fR",
42
+ defaultMessage: [{
43
+ "type": 0,
44
+ "value": "Field must be at least "
45
+ }, {
46
+ "type": 1,
47
+ "value": "min"
48
+ }, {
49
+ "type": 0,
50
+ "value": " characters"
51
+ }]
52
+ }),
53
+ values: {
54
+ min
55
+ }
56
+ };
57
+ }
58
+ }
59
+ });
60
+
61
+ // src/index.ts
62
+ import { yupResolver } from "@hookform/resolvers/yup";
63
+ export * from "react-hook-form";
64
+ import { useForm } from "react-hook-form";
65
+ import * as yup from "yup";
66
+
67
+ // src/Form.tsx
68
+ import { Box } from "@ttoss/ui";
69
+ import { FormProvider } from "react-hook-form";
70
+ import { jsx } from "react/jsx-runtime";
71
+ var Form = ({
72
+ children,
73
+ onSubmit,
74
+ sx,
75
+ ...formMethods
76
+ }) => {
77
+ return /* @__PURE__ */jsx(FormProvider, {
78
+ ...formMethods,
79
+ children: /* @__PURE__ */jsx(Box, {
80
+ as: "form",
81
+ variant: "forms.form",
82
+ onSubmit: formMethods.handleSubmit(data => {
83
+ return onSubmit?.(data);
84
+ }),
85
+ sx,
86
+ children
87
+ })
88
+ });
89
+ };
90
+
91
+ // src/FormField.tsx
92
+ import * as React from "react";
93
+
94
+ // src/ErrorMessage.tsx
95
+ import { useFormContext } from "react-hook-form";
96
+ import { FormattedMessage } from "@ttoss/react-i18n";
97
+ import { HelpText } from "@ttoss/ui";
98
+ import { ErrorMessage as HookformErrorMessage } from "@hookform/error-message";
99
+ import { jsx as jsx2 } from "react/jsx-runtime";
100
+ var isMessageDescriptor = possibleMessageDescriptor => {
101
+ return possibleMessageDescriptor !== void 0 && possibleMessageDescriptor.defaultMessage !== void 0;
102
+ };
103
+ var ErrorMessage = ({
104
+ name,
105
+ disabled
106
+ }) => {
107
+ const {
108
+ formState: {
109
+ errors
110
+ }
111
+ } = useFormContext();
112
+ return /* @__PURE__ */jsx2(HookformErrorMessage, {
113
+ name,
114
+ errors,
115
+ render: ({
116
+ message
117
+ }) => {
118
+ return /* @__PURE__ */jsx2(HelpText, {
119
+ negative: true,
120
+ disabled,
121
+ children: (() => {
122
+ if (typeof message === "string") {
123
+ return message;
124
+ }
125
+ if (isMessageDescriptor(message)) {
126
+ return /* @__PURE__ */jsx2(FormattedMessage, {
127
+ ...message
128
+ });
129
+ }
130
+ return JSON.stringify(message);
131
+ })()
132
+ });
133
+ }
134
+ });
135
+ };
136
+
137
+ // src/FormField.tsx
138
+ import { useController } from "react-hook-form";
139
+ import { Flex, Label } from "@ttoss/ui";
140
+ import { Fragment, jsx as jsx3, jsxs } from "react/jsx-runtime";
141
+ var FormField = ({
142
+ label,
143
+ id: idProp,
144
+ name,
145
+ defaultValue,
146
+ disabled,
147
+ tooltip,
148
+ onTooltipClick,
149
+ sx,
150
+ css,
151
+ render
152
+ }) => {
153
+ const controllerReturn = useController({
154
+ name,
155
+ defaultValue
156
+ });
157
+ const id = idProp || `form-field-${name}`;
158
+ const memoizedRender = React.useMemo(() => {
159
+ return React.Children.map(render(controllerReturn), child => {
160
+ return /* @__PURE__ */jsxs(Fragment, {
161
+ children: [label && /* @__PURE__ */jsx3(Label, {
162
+ "aria-disabled": disabled,
163
+ htmlFor: id,
164
+ tooltip,
165
+ onTooltipClick,
166
+ children: label
167
+ }), /*#__PURE__*/React.createElement(child.type, {
168
+ id,
169
+ ...child.props
170
+ })]
171
+ });
172
+ });
173
+ }, [controllerReturn, disabled, onTooltipClick, tooltip, id, label, render]);
174
+ return /* @__PURE__ */jsxs(Flex, {
175
+ sx: {
176
+ flexDirection: "column",
177
+ width: "100%",
178
+ gap: "md",
179
+ ...sx
180
+ },
181
+ css,
182
+ children: [memoizedRender, /* @__PURE__ */jsx3(ErrorMessage, {
183
+ name
184
+ })]
185
+ });
186
+ };
187
+
188
+ // src/FormFieldCheckbox.tsx
189
+ import { Checkbox, Flex as Flex2, Label as Label2 } from "@ttoss/ui";
190
+ import { useController as useController2 } from "react-hook-form";
191
+ import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
192
+ var FormFieldCheckbox = ({
193
+ label,
194
+ name,
195
+ sx,
196
+ ...checkboxProps
197
+ }) => {
198
+ const {
199
+ field: {
200
+ onChange,
201
+ onBlur,
202
+ value,
203
+ ref
204
+ },
205
+ formState: {
206
+ errors
207
+ }
208
+ } = useController2({
209
+ name,
210
+ defaultValue: false
211
+ });
212
+ const id = `form-field-checkbox-${name}`;
213
+ const error = !!errors[name]?.message;
214
+ return /* @__PURE__ */jsxs2(Flex2, {
215
+ sx: {
216
+ flexDirection: "column",
217
+ width: "100%",
218
+ ...sx
219
+ },
220
+ children: [/* @__PURE__ */jsx4(Flex2, {
221
+ sx: {
222
+ alignItems: "center"
223
+ },
224
+ children: /* @__PURE__ */jsxs2(Label2, {
225
+ "aria-disabled": checkboxProps.disabled,
226
+ htmlFor: id,
227
+ children: [/* @__PURE__ */jsx4(Checkbox, {
228
+ id,
229
+ ref,
230
+ checked: value,
231
+ onChange,
232
+ onBlur,
233
+ name,
234
+ "aria-invalid": error ? "true" : "false",
235
+ ...checkboxProps
236
+ }), label]
237
+ })
238
+ }), /* @__PURE__ */jsx4(ErrorMessage, {
239
+ name
240
+ })]
241
+ });
242
+ };
243
+
244
+ // src/FormFieldPatternFormat.tsx
245
+ import { Input } from "@ttoss/ui";
246
+ import { PatternFormat } from "react-number-format";
247
+ import { jsx as jsx5 } from "react/jsx-runtime";
248
+ var FormFieldPatternFormat = ({
249
+ label,
250
+ name,
251
+ ...patternFormatProps
252
+ }) => {
253
+ return /* @__PURE__ */jsx5(FormField, {
254
+ name,
255
+ label,
256
+ render: ({
257
+ field,
258
+ fieldState
259
+ }) => {
260
+ return /* @__PURE__ */jsx5(PatternFormat, {
261
+ name: field.name,
262
+ value: field.value,
263
+ onBlur: field.onBlur,
264
+ onValueChange: values => {
265
+ field.onChange(values.value);
266
+ },
267
+ customInput: Input,
268
+ "aria-invalid": Boolean(fieldState.error).valueOf(),
269
+ ...patternFormatProps
270
+ });
271
+ }
272
+ });
273
+ };
274
+
275
+ // src/FormFieldCreditCardNumber.tsx
276
+ import { jsx as jsx6 } from "react/jsx-runtime";
277
+ var FormFieldCreditCardNumber = ({
278
+ label,
279
+ name,
280
+ ...formFieldPatternFormatProps
281
+ }) => {
282
+ return /* @__PURE__ */jsx6(FormFieldPatternFormat, {
283
+ name,
284
+ label,
285
+ format: "#### #### #### ####",
286
+ placeholder: "1234 1234 1234 1234",
287
+ ...formFieldPatternFormatProps
288
+ });
289
+ };
290
+
291
+ // src/FormFieldNumericFormat.tsx
292
+ import { Input as Input2 } from "@ttoss/ui";
293
+ import { NumericFormat } from "react-number-format";
294
+ import { jsx as jsx7 } from "react/jsx-runtime";
295
+ var FormFieldNumericFormat = ({
296
+ label,
297
+ name,
298
+ ...numericFormatProps
299
+ }) => {
300
+ return /* @__PURE__ */jsx7(FormField, {
301
+ label,
302
+ name,
303
+ render: ({
304
+ field
305
+ }) => {
306
+ return /* @__PURE__ */jsx7(NumericFormat, {
307
+ name: field.name,
308
+ value: field.value,
309
+ onBlur: field.onBlur,
310
+ onValueChange: values => {
311
+ field.onChange(values.floatValue);
312
+ },
313
+ customInput: Input2,
314
+ ...numericFormatProps
315
+ });
316
+ }
317
+ });
318
+ };
319
+
320
+ // src/FormFieldCurrencyInput.tsx
321
+ import { jsx as jsx8 } from "react/jsx-runtime";
322
+ var FormFieldCurrencyInput = ({
323
+ label,
324
+ name,
325
+ prefix,
326
+ decimalSeparator,
327
+ thousandSeparator,
328
+ ...formFieldNumericFormatProps
329
+ }) => {
330
+ return /* @__PURE__ */jsx8(FormFieldNumericFormat, {
331
+ name,
332
+ label,
333
+ fixedDecimalScale: true,
334
+ decimalScale: 2,
335
+ prefix,
336
+ decimalSeparator,
337
+ thousandSeparator,
338
+ placeholder: `${prefix} 0${decimalSeparator}00`,
339
+ allowNegative: false,
340
+ ...formFieldNumericFormatProps
341
+ });
342
+ };
343
+
344
+ // src/FormFieldInput.tsx
345
+ import { Input as Input3 } from "@ttoss/ui";
346
+ import { jsx as jsx9 } from "react/jsx-runtime";
347
+ var FormFieldInput = ({
348
+ label,
349
+ name,
350
+ tooltip,
351
+ onTooltipClick,
352
+ sx,
353
+ defaultValue = "",
354
+ ...inputProps
355
+ }) => {
356
+ return /* @__PURE__ */jsx9(FormField, {
357
+ name,
358
+ label,
359
+ disabled: inputProps.disabled,
360
+ tooltip,
361
+ onTooltipClick,
362
+ sx,
363
+ defaultValue,
364
+ render: ({
365
+ field,
366
+ fieldState
367
+ }) => {
368
+ return /* @__PURE__ */jsx9(Input3, {
369
+ ...inputProps,
370
+ ...field,
371
+ "aria-invalid": fieldState.error ? "true" : void 0
372
+ });
373
+ }
374
+ });
375
+ };
376
+
377
+ // src/FormFieldPassword.tsx
378
+ import { InputPassword } from "@ttoss/ui";
379
+ import { jsx as jsx10 } from "react/jsx-runtime";
380
+ var FormFieldPassword = ({
381
+ label,
382
+ name,
383
+ tooltip,
384
+ onTooltipClick,
385
+ sx,
386
+ defaultValue = "",
387
+ ...inputProps
388
+ }) => {
389
+ return /* @__PURE__ */jsx10(FormField, {
390
+ name,
391
+ label,
392
+ disabled: inputProps.disabled,
393
+ tooltip,
394
+ onTooltipClick,
395
+ sx,
396
+ defaultValue,
397
+ render: ({
398
+ field,
399
+ fieldState
400
+ }) => {
401
+ return /* @__PURE__ */jsx10(InputPassword, {
402
+ ...inputProps,
403
+ ...field,
404
+ "aria-invalid": fieldState.error ? "true" : void 0
405
+ });
406
+ }
407
+ });
408
+ };
409
+
410
+ // src/FormFieldRadio.tsx
411
+ import { Box as Box2, Flex as Flex3, Label as Label3, Radio } from "@ttoss/ui";
412
+ import { useController as useController3 } from "react-hook-form";
413
+ import { jsx as jsx11, jsxs as jsxs3 } from "react/jsx-runtime";
414
+ var FormFieldRadio = ({
415
+ label,
416
+ name,
417
+ options,
418
+ sx,
419
+ ...radioProps
420
+ }) => {
421
+ const {
422
+ field: {
423
+ onChange,
424
+ onBlur,
425
+ value,
426
+ ref
427
+ }
428
+ } = useController3({
429
+ name,
430
+ defaultValue: ""
431
+ });
432
+ return /* @__PURE__ */jsxs3(Flex3, {
433
+ sx: {
434
+ flexDirection: "column",
435
+ width: "100%",
436
+ ...sx
437
+ },
438
+ children: [label && /* @__PURE__ */jsx11(Label3, {
439
+ sx: {
440
+ marginBottom: "md"
441
+ },
442
+ children: label
443
+ }), /* @__PURE__ */jsx11(Box2, {
444
+ children: options.map(option => {
445
+ const id = `form-field-radio-${name}-${option.value}`;
446
+ return /* @__PURE__ */jsxs3(Label3, {
447
+ htmlFor: id,
448
+ children: [/* @__PURE__ */jsx11(Radio, {
449
+ ref,
450
+ onChange,
451
+ onBlur,
452
+ value: option.value,
453
+ checked: value === option.value,
454
+ name,
455
+ id,
456
+ ...radioProps
457
+ }), option.label]
458
+ }, id);
459
+ })
460
+ }), /* @__PURE__ */jsx11(ErrorMessage, {
461
+ name
462
+ })]
463
+ });
464
+ };
465
+
466
+ // src/FormFieldSelect.tsx
467
+ import { Select } from "@ttoss/ui";
468
+ import { jsx as jsx12 } from "react/jsx-runtime";
469
+ var FormFieldSelect = ({
470
+ label,
471
+ name,
472
+ id,
473
+ defaultValue,
474
+ sx,
475
+ css,
476
+ disabled,
477
+ tooltip,
478
+ onTooltipClick,
479
+ ...selectProps
480
+ }) => {
481
+ return /* @__PURE__ */jsx12(FormField, {
482
+ name,
483
+ label,
484
+ id,
485
+ defaultValue,
486
+ disabled,
487
+ tooltip,
488
+ onTooltipClick,
489
+ sx,
490
+ css,
491
+ render: ({
492
+ field,
493
+ fieldState
494
+ }) => {
495
+ return /* @__PURE__ */jsx12(Select, {
496
+ ...selectProps,
497
+ ...field,
498
+ isDisabled: disabled,
499
+ "aria-invalid": fieldState.error ? "true" : void 0
500
+ });
501
+ }
502
+ });
503
+ };
504
+
505
+ // src/FormFieldTextarea.tsx
506
+ import { Textarea } from "@ttoss/ui";
507
+ import { jsx as jsx13 } from "react/jsx-runtime";
508
+ var FormFieldTextarea = ({
509
+ label,
510
+ name,
511
+ sx,
512
+ ...textareaProps
513
+ }) => {
514
+ const id = `form-field-textarea-${name}`;
515
+ return /* @__PURE__ */jsx13(FormField, {
516
+ label,
517
+ name,
518
+ id,
519
+ sx,
520
+ render: ({
521
+ field,
522
+ fieldState
523
+ }) => {
524
+ return /* @__PURE__ */jsx13(Textarea, {
525
+ ...field,
526
+ ...textareaProps,
527
+ "aria-invalid": fieldState.error ? "true" : void 0
528
+ });
529
+ }
530
+ });
531
+ };
532
+
533
+ // src/FormGroup.tsx
534
+ import * as React2 from "react";
535
+ import { Box as Box3, Flex as Flex4, Text } from "@ttoss/ui";
536
+ import { jsx as jsx14, jsxs as jsxs4 } from "react/jsx-runtime";
537
+ var FormGroupLevelsManagerContext = /*#__PURE__*/React2.createContext({
538
+ levelsLength: 1,
539
+ registerChild: () => {
540
+ return null;
541
+ }
542
+ });
543
+ var FormGroupLevelsManager = ({
544
+ children
545
+ }) => {
546
+ const [levelsLength, setLevelsLength] = React2.useState(0);
547
+ const registerChild = React2.useCallback(level => {
548
+ if (level + 1 > levelsLength) {
549
+ setLevelsLength(level + 1);
550
+ }
551
+ }, [levelsLength]);
552
+ return /* @__PURE__ */jsx14(FormGroupLevelsManagerContext.Provider, {
553
+ value: {
554
+ levelsLength,
555
+ registerChild
556
+ },
557
+ children
558
+ });
559
+ };
560
+ var FormGroupContext = /*#__PURE__*/React2.createContext({});
561
+ var useFormGroup = () => {
562
+ const {
563
+ parentLevel
564
+ } = React2.useContext(FormGroupContext);
565
+ const {
566
+ levelsLength
567
+ } = React2.useContext(FormGroupLevelsManagerContext);
568
+ return {
569
+ level: parentLevel,
570
+ levelsLength
571
+ };
572
+ };
573
+ var FormGroupWrapper = ({
574
+ title,
575
+ direction,
576
+ children,
577
+ ...boxProps
578
+ }) => {
579
+ const {
580
+ level
581
+ } = useFormGroup();
582
+ const {
583
+ registerChild
584
+ } = React2.useContext(FormGroupLevelsManagerContext);
585
+ React2.useEffect(() => {
586
+ if (typeof level === "number") {
587
+ registerChild(level);
588
+ }
589
+ }, [level, registerChild]);
590
+ const childrenContainerSx = {
591
+ flexDirection: direction || "column",
592
+ gap: "md",
593
+ width: "100%"
594
+ };
595
+ return /* @__PURE__ */jsxs4(Box3, {
596
+ "aria-level": level,
597
+ ...boxProps,
598
+ sx: {
599
+ marginTop: level === 0 ? "none" : "lg",
600
+ marginBottom: "lg",
601
+ ...boxProps.sx
602
+ },
603
+ children: [title && /* @__PURE__ */jsx14(Box3, {
604
+ sx: {
605
+ marginBottom: "md"
606
+ },
607
+ children: /* @__PURE__ */jsx14(Text, {
608
+ sx: {
609
+ fontSize: "2xl",
610
+ fontWeight: "bold"
611
+ },
612
+ children: title
613
+ })
614
+ }), /* @__PURE__ */jsx14(Flex4, {
615
+ sx: childrenContainerSx,
616
+ children
617
+ })]
618
+ });
619
+ };
620
+ var FormGroup = props => {
621
+ const {
622
+ level
623
+ } = useFormGroup();
624
+ const currentLevel = level === void 0 ? 0 : level + 1;
625
+ return /* @__PURE__ */jsx14(FormGroupContext.Provider, {
626
+ value: {
627
+ parentLevel: currentLevel
628
+ },
629
+ children: currentLevel === 0 ? /* @__PURE__ */jsx14(FormGroupLevelsManager, {
630
+ children: /* @__PURE__ */jsx14(FormGroupWrapper, {
631
+ ...props
632
+ })
633
+ }) : /* @__PURE__ */jsx14(FormGroupWrapper, {
634
+ ...props
635
+ })
636
+ });
637
+ };
638
+ export { Form, FormField, FormFieldCheckbox, FormFieldPatternFormat, FormFieldCreditCardNumber, FormFieldNumericFormat, FormFieldCurrencyInput, FormFieldInput, FormFieldPassword, FormFieldRadio, FormFieldSelect, FormFieldTextarea, useFormGroup, FormGroup, yupResolver, useForm, yup };