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