@ttoss/forms 0.15.10 → 0.15.12
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/README.md +80 -0
- package/dist/esm/index.js +25 -2
- package/dist/index.d.ts +6 -1
- package/dist/index.js +25 -2
- package/package.json +3 -3
- package/src/FormFieldSelect.tsx +36 -3
package/README.md
CHANGED
|
@@ -37,4 +37,84 @@ export const FormComponent = () => {
|
|
|
37
37
|
};
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
## FormFieldSelect support for Default Value
|
|
41
|
+
|
|
42
|
+
FormFieldSelect has support for default values, by assigning the first option defined or the value passed to it in the parameter `defaultValue`.
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
const RADIO_OPTIONS = [
|
|
46
|
+
{ value: 'Ferrari', label: 'Ferrari' },
|
|
47
|
+
{ value: 'Mercedes', label: 'Mercedes' },
|
|
48
|
+
{ value: 'BMW', label: 'BMW' },
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
// RenderForm gonna use "Ferrari" as defaultValue
|
|
52
|
+
const RenderForm = () => {
|
|
53
|
+
const formMethods = useForm();
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<Form {...formMethods} onSubmit={onSubmit}>
|
|
57
|
+
<FormFieldSelect name="car" label="Cars" options={RADIO_OPTIONS} />
|
|
58
|
+
<Button type="submit">Submit</Button>
|
|
59
|
+
</Form>
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// RenderForm gonna use "Mercedes" as defaultValue
|
|
64
|
+
const RenderForm = () => {
|
|
65
|
+
const formMethods = useForm();
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Form {...formMethods} onSubmit={onSubmit}>
|
|
69
|
+
<FormFieldSelect
|
|
70
|
+
name="car"
|
|
71
|
+
label="Cars"
|
|
72
|
+
options={RADIO_OPTIONS}
|
|
73
|
+
defaultValue="Mercedes"
|
|
74
|
+
/>
|
|
75
|
+
<Button type="submit">Submit</Button>
|
|
76
|
+
</Form>
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
When a placeholder is set, if in the options don't have an empty value, automatically an empty value gonna be injected.
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
const RenderForm = () => {
|
|
85
|
+
const formMethods = useForm();
|
|
86
|
+
|
|
87
|
+
// automatically injects an empty value on the "RADIO_OPTIONS"
|
|
88
|
+
return (
|
|
89
|
+
<Form {...formMethods} onSubmit={onSubmit}>
|
|
90
|
+
<FormFieldSelect
|
|
91
|
+
name="car"
|
|
92
|
+
label="Cars"
|
|
93
|
+
options={RADIO_OPTIONS}
|
|
94
|
+
placeholder="Select a car"
|
|
95
|
+
/>
|
|
96
|
+
<Button type="submit">Submit</Button>
|
|
97
|
+
</Form>
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Note that a placeholder cannot be set when an defaultValue is set and vice-versa**
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
// type error!!
|
|
106
|
+
return (
|
|
107
|
+
<Form {...formMethods} onSubmit={onSubmit}>
|
|
108
|
+
<FormFieldSelect
|
|
109
|
+
name="car"
|
|
110
|
+
label="Cars"
|
|
111
|
+
options={RADIO_OPTIONS}
|
|
112
|
+
placeholder="Select a car"
|
|
113
|
+
defaultValue="Ferrari"
|
|
114
|
+
/>
|
|
115
|
+
<Button type="submit">Submit</Button>
|
|
116
|
+
</Form>
|
|
117
|
+
);
|
|
118
|
+
```
|
|
119
|
+
|
|
40
120
|
> NOTE: You can also use yup and all of API from react-hook-form importing `import { yup, useForm } from @ttoss/forms`
|
package/dist/esm/index.js
CHANGED
|
@@ -264,6 +264,21 @@ var FormFieldRadio = ({
|
|
|
264
264
|
import { useController as useController5 } from "react-hook-form";
|
|
265
265
|
import { Flex as Flex5, Label as Label5, Select } from "@ttoss/ui";
|
|
266
266
|
import { jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
267
|
+
var checkDefaultValue = (options, defaultValue, placeholder) => {
|
|
268
|
+
const hasEmptyValue = options.some(opt => {
|
|
269
|
+
return opt.value === "" || opt.value === 0;
|
|
270
|
+
});
|
|
271
|
+
if (placeholder && hasEmptyValue) return "";
|
|
272
|
+
if (placeholder && !hasEmptyValue) {
|
|
273
|
+
options.push({
|
|
274
|
+
label: "",
|
|
275
|
+
value: ""
|
|
276
|
+
});
|
|
277
|
+
return "";
|
|
278
|
+
}
|
|
279
|
+
if (!placeholder && defaultValue) return defaultValue;
|
|
280
|
+
return options[0].value;
|
|
281
|
+
};
|
|
267
282
|
var FormFieldSelect = ({
|
|
268
283
|
label,
|
|
269
284
|
name,
|
|
@@ -271,6 +286,11 @@ var FormFieldSelect = ({
|
|
|
271
286
|
sx,
|
|
272
287
|
...selectProps
|
|
273
288
|
}) => {
|
|
289
|
+
const {
|
|
290
|
+
defaultValue,
|
|
291
|
+
placeholder
|
|
292
|
+
} = selectProps;
|
|
293
|
+
const checkedDefaultValue = checkDefaultValue(options, defaultValue, placeholder);
|
|
274
294
|
const {
|
|
275
295
|
field: {
|
|
276
296
|
onChange,
|
|
@@ -280,7 +300,7 @@ var FormFieldSelect = ({
|
|
|
280
300
|
}
|
|
281
301
|
} = useController5({
|
|
282
302
|
name,
|
|
283
|
-
defaultValue:
|
|
303
|
+
defaultValue: checkedDefaultValue
|
|
284
304
|
});
|
|
285
305
|
const id = `form-field-select-${name}`;
|
|
286
306
|
return /* @__PURE__ */jsxs5(Flex5, {
|
|
@@ -299,7 +319,10 @@ var FormFieldSelect = ({
|
|
|
299
319
|
onBlur,
|
|
300
320
|
value,
|
|
301
321
|
id,
|
|
302
|
-
...
|
|
322
|
+
...{
|
|
323
|
+
...selectProps,
|
|
324
|
+
defaultValue: void 0
|
|
325
|
+
},
|
|
303
326
|
children: options.map(option => {
|
|
304
327
|
return /* @__PURE__ */jsx7("option", {
|
|
305
328
|
value: option.value,
|
package/dist/index.d.ts
CHANGED
|
@@ -48,11 +48,16 @@ type FormRadioOption = {
|
|
|
48
48
|
value: string | number;
|
|
49
49
|
label: string;
|
|
50
50
|
};
|
|
51
|
+
type SelectSwitchProps = (SelectProps & {
|
|
52
|
+
placeholder?: never;
|
|
53
|
+
}) | (SelectProps & {
|
|
54
|
+
defaultValue?: never;
|
|
55
|
+
});
|
|
51
56
|
declare const FormFieldSelect: <TFieldValues extends FieldValues = FieldValues>({ label, name, options, sx, ...selectProps }: {
|
|
52
57
|
label?: string | undefined;
|
|
53
58
|
name: FieldPath<TFieldValues>;
|
|
54
59
|
options: FormRadioOption[];
|
|
55
|
-
} &
|
|
60
|
+
} & SelectSwitchProps) => JSX.Element;
|
|
56
61
|
|
|
57
62
|
declare const FormFieldTextarea: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ label, name, sx, ...textareaProps }: {
|
|
58
63
|
label?: string | undefined;
|
package/dist/index.js
CHANGED
|
@@ -315,6 +315,21 @@ var FormFieldRadio = ({
|
|
|
315
315
|
var import_react_hook_form7 = require("react-hook-form");
|
|
316
316
|
var import_ui7 = require("@ttoss/ui");
|
|
317
317
|
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
318
|
+
var checkDefaultValue = (options, defaultValue, placeholder) => {
|
|
319
|
+
const hasEmptyValue = options.some(opt => {
|
|
320
|
+
return opt.value === "" || opt.value === 0;
|
|
321
|
+
});
|
|
322
|
+
if (placeholder && hasEmptyValue) return "";
|
|
323
|
+
if (placeholder && !hasEmptyValue) {
|
|
324
|
+
options.push({
|
|
325
|
+
label: "",
|
|
326
|
+
value: ""
|
|
327
|
+
});
|
|
328
|
+
return "";
|
|
329
|
+
}
|
|
330
|
+
if (!placeholder && defaultValue) return defaultValue;
|
|
331
|
+
return options[0].value;
|
|
332
|
+
};
|
|
318
333
|
var FormFieldSelect = ({
|
|
319
334
|
label,
|
|
320
335
|
name,
|
|
@@ -322,6 +337,11 @@ var FormFieldSelect = ({
|
|
|
322
337
|
sx,
|
|
323
338
|
...selectProps
|
|
324
339
|
}) => {
|
|
340
|
+
const {
|
|
341
|
+
defaultValue,
|
|
342
|
+
placeholder
|
|
343
|
+
} = selectProps;
|
|
344
|
+
const checkedDefaultValue = checkDefaultValue(options, defaultValue, placeholder);
|
|
325
345
|
const {
|
|
326
346
|
field: {
|
|
327
347
|
onChange,
|
|
@@ -331,7 +351,7 @@ var FormFieldSelect = ({
|
|
|
331
351
|
}
|
|
332
352
|
} = (0, import_react_hook_form7.useController)({
|
|
333
353
|
name,
|
|
334
|
-
defaultValue:
|
|
354
|
+
defaultValue: checkedDefaultValue
|
|
335
355
|
});
|
|
336
356
|
const id = `form-field-select-${name}`;
|
|
337
357
|
return /* @__PURE__ */(0, import_jsx_runtime7.jsxs)(import_ui7.Flex, {
|
|
@@ -350,7 +370,10 @@ var FormFieldSelect = ({
|
|
|
350
370
|
onBlur,
|
|
351
371
|
value,
|
|
352
372
|
id,
|
|
353
|
-
...
|
|
373
|
+
...{
|
|
374
|
+
...selectProps,
|
|
375
|
+
defaultValue: void 0
|
|
376
|
+
},
|
|
354
377
|
children: options.map(option => {
|
|
355
378
|
return /* @__PURE__ */(0, import_jsx_runtime7.jsx)("option", {
|
|
356
379
|
value: option.value,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/forms",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.12",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@ttoss/config": "^1.29.5",
|
|
33
33
|
"@ttoss/test-utils": "^1.21.5",
|
|
34
|
-
"@ttoss/ui": "^1.
|
|
34
|
+
"@ttoss/ui": "^1.34.0",
|
|
35
35
|
"@types/jest": "^29.5.0",
|
|
36
36
|
"@types/react": "^18.0.37",
|
|
37
37
|
"jest": "^29.5.0",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"publishConfig": {
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "85c81ad0d34207a3bdaa8cb907619f8e70837094"
|
|
48
48
|
}
|
package/src/FormFieldSelect.tsx
CHANGED
|
@@ -7,6 +7,31 @@ type FormRadioOption = {
|
|
|
7
7
|
label: string;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
type SelectSwitchProps =
|
|
11
|
+
| (SelectProps & { placeholder?: never })
|
|
12
|
+
| (SelectProps & { defaultValue?: never });
|
|
13
|
+
|
|
14
|
+
const checkDefaultValue = (
|
|
15
|
+
options: Array<FormRadioOption>,
|
|
16
|
+
defaultValue?: string | number | readonly string[],
|
|
17
|
+
placeholder?: string
|
|
18
|
+
) => {
|
|
19
|
+
const hasEmptyValue = options.some((opt) => {
|
|
20
|
+
return opt.value === '' || opt.value === 0;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
if (placeholder && hasEmptyValue) return '';
|
|
24
|
+
if (placeholder && !hasEmptyValue) {
|
|
25
|
+
options.push({
|
|
26
|
+
label: '',
|
|
27
|
+
value: '',
|
|
28
|
+
});
|
|
29
|
+
return '';
|
|
30
|
+
}
|
|
31
|
+
if (!placeholder && defaultValue) return defaultValue;
|
|
32
|
+
return options[0].value;
|
|
33
|
+
};
|
|
34
|
+
|
|
10
35
|
export const FormFieldSelect = <
|
|
11
36
|
TFieldValues extends FieldValues = FieldValues
|
|
12
37
|
>({
|
|
@@ -19,12 +44,20 @@ export const FormFieldSelect = <
|
|
|
19
44
|
label?: string;
|
|
20
45
|
name: FieldPath<TFieldValues>;
|
|
21
46
|
options: FormRadioOption[];
|
|
22
|
-
} &
|
|
47
|
+
} & SelectSwitchProps) => {
|
|
48
|
+
const { defaultValue, placeholder } = selectProps;
|
|
49
|
+
|
|
50
|
+
const checkedDefaultValue = checkDefaultValue(
|
|
51
|
+
options,
|
|
52
|
+
defaultValue,
|
|
53
|
+
placeholder
|
|
54
|
+
);
|
|
55
|
+
|
|
23
56
|
const {
|
|
24
57
|
field: { onChange, onBlur, value, ref },
|
|
25
58
|
} = useController<any>({
|
|
26
59
|
name,
|
|
27
|
-
defaultValue:
|
|
60
|
+
defaultValue: checkedDefaultValue,
|
|
28
61
|
});
|
|
29
62
|
|
|
30
63
|
const id = `form-field-select-${name}`;
|
|
@@ -39,7 +72,7 @@ export const FormFieldSelect = <
|
|
|
39
72
|
onBlur={onBlur}
|
|
40
73
|
value={value}
|
|
41
74
|
id={id}
|
|
42
|
-
{...selectProps}
|
|
75
|
+
{...{ ...selectProps, defaultValue: undefined }}
|
|
43
76
|
>
|
|
44
77
|
{options.map((option) => {
|
|
45
78
|
return (
|