@ultraviolet/form 4.0.0-beta.8 → 4.0.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.
- package/README.md +191 -3
- package/dist/components/CheckboxGroupField/index.d.ts +1 -3
- package/dist/components/{DateField → DateInputField}/index.cjs +2 -2
- package/dist/components/DateInputField/index.d.ts +7 -0
- package/dist/components/{DateField → DateInputField}/index.js +2 -2
- package/dist/components/KeyValueField/index.cjs +1 -1
- package/dist/components/KeyValueField/index.d.ts +6 -6
- package/dist/components/KeyValueField/index.js +1 -1
- package/dist/components/NumberInputField/index.cjs +24 -24
- package/dist/components/NumberInputField/index.d.ts +3 -9
- package/dist/components/NumberInputField/index.js +24 -24
- package/dist/components/RadioField/index.d.ts +1 -4
- package/dist/components/RadioGroupField/index.d.ts +17 -7
- package/dist/components/SelectInputField/index.cjs +19 -71
- package/dist/components/SelectInputField/index.d.ts +3 -81
- package/dist/components/SelectInputField/index.js +20 -72
- package/dist/components/SelectableCardField/index.d.ts +1 -1
- package/dist/components/SelectableCardGroupField/index.d.ts +1 -1
- package/dist/components/SelectableCardOptionGroupField/index.d.ts +3 -3
- package/dist/components/SliderField/index.d.ts +1 -1
- package/dist/components/SwitchButtonField/index.cjs +2 -3
- package/dist/components/SwitchButtonField/index.d.ts +11 -2
- package/dist/components/SwitchButtonField/index.js +2 -3
- package/dist/components/TagInputField/index.d.ts +1 -1
- package/dist/components/TextInputField/index.cjs +19 -73
- package/dist/components/TextInputField/index.d.ts +5 -11
- package/dist/components/TextInputField/index.js +19 -73
- package/dist/components/{TimeInputFieldV2 → TimeInputField}/index.cjs +3 -3
- package/dist/components/TimeInputField/index.d.ts +11 -0
- package/dist/components/{TimeInputFieldV2 → TimeInputField}/index.js +4 -4
- package/dist/components/index.d.ts +3 -7
- package/dist/index.cjs +38 -46
- package/dist/index.js +5 -13
- package/package.json +10 -10
- package/dist/components/DateField/index.d.ts +0 -7
- package/dist/components/NumberInputFieldV2/index.cjs +0 -59
- package/dist/components/NumberInputFieldV2/index.d.ts +0 -7
- package/dist/components/NumberInputFieldV2/index.js +0 -59
- package/dist/components/SelectInputFieldV2/index.cjs +0 -50
- package/dist/components/SelectInputFieldV2/index.d.ts +0 -7
- package/dist/components/SelectInputFieldV2/index.js +0 -50
- package/dist/components/TextInputFieldV2/index.cjs +0 -62
- package/dist/components/TextInputFieldV2/index.d.ts +0 -12
- package/dist/components/TextInputFieldV2/index.js +0 -62
- package/dist/components/TimeField/index.cjs +0 -68
- package/dist/components/TimeField/index.d.ts +0 -7
- package/dist/components/TimeField/index.js +0 -68
- package/dist/components/TimeInputFieldV2/index.d.ts +0 -11
package/README.md
CHANGED
|
@@ -21,18 +21,206 @@ import { Form, TextInputField } from '@ultraviolet/form'
|
|
|
21
21
|
import { theme } from '@ultraviolet/ui'
|
|
22
22
|
import { useForm } from '@ultraviolet/form'
|
|
23
23
|
|
|
24
|
+
// Here are the input types of your form
|
|
25
|
+
type FormValues = {
|
|
26
|
+
firstName: string
|
|
27
|
+
lastName: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// We define the initial values of the form
|
|
31
|
+
const INITIAL_VALUES: FormValues = {
|
|
32
|
+
firstName: 'Marc',
|
|
33
|
+
lastName: 'Scout',
|
|
34
|
+
} as const
|
|
35
|
+
|
|
36
|
+
export default function App() {
|
|
37
|
+
const methods = useForm<FormValues>({
|
|
38
|
+
defaultValues: INITIAL_VALUES,
|
|
39
|
+
mode: 'onChange',
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const formErrors = {
|
|
43
|
+
required: () => 'This field is required',
|
|
44
|
+
// Add more error messages as needed for min, max, etc.
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const onSubmit = async ({
|
|
48
|
+
firstName,
|
|
49
|
+
lastName,
|
|
50
|
+
}: FormValues) => {
|
|
51
|
+
// Add your form submission logic here
|
|
52
|
+
console.log('Form submitted with values:', { firstName, lastName })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<ThemeProvider theme={theme}>
|
|
57
|
+
<Form methods={methods} errors={formErrors} onSubmit={onSubmit}>
|
|
58
|
+
<TextInputField name="firstName" />
|
|
59
|
+
<TextInputField name="lastName" />
|
|
60
|
+
</Form>
|
|
61
|
+
</ThemeProvider>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `useWatch` Hook
|
|
67
|
+
|
|
68
|
+
You can use the `useWatch` hook from `@ultraviolet/form` to watch specific fields in your form thus subscribing to their changes.
|
|
69
|
+
It can be useful for displaying real-time updates or triggering actions based on field values.
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
// FirstNameWatched is a component that needs to watch the firstName field
|
|
73
|
+
function FirstNameWatched({ control }: { control: Control<FormInputs> }) {
|
|
74
|
+
const firstName = useWatch({
|
|
75
|
+
control,
|
|
76
|
+
name: "firstName",
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
return <p>Watch: {firstName}</p>
|
|
80
|
+
}
|
|
81
|
+
|
|
24
82
|
export default function App() {
|
|
25
|
-
|
|
83
|
+
... // same setup as before
|
|
84
|
+
|
|
26
85
|
return (
|
|
27
86
|
<ThemeProvider theme={theme}>
|
|
28
|
-
<Form methods={methods}>
|
|
29
|
-
<TextInputField name="
|
|
87
|
+
<Form methods={methods} errors={formErrors} onSubmit={onSubmit}>
|
|
88
|
+
<TextInputField name="firstName" />
|
|
89
|
+
<TextInputField name="lastName" />
|
|
90
|
+
|
|
91
|
+
<FirstNameWatched control={control} />
|
|
30
92
|
</Form>
|
|
31
93
|
</ThemeProvider>
|
|
32
94
|
)
|
|
33
95
|
}
|
|
34
96
|
```
|
|
35
97
|
|
|
98
|
+
### Form Validation
|
|
99
|
+
|
|
100
|
+
You can validate each fields passing either `regex` or `validate` to any field that support it. Not all field supports `regex` for instance but all fields support `validate`.
|
|
101
|
+
In addition many field support `required`, `minLength`, `maxLength`, `min`, and `max` validation.
|
|
102
|
+
|
|
103
|
+
#### Native Validation
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
<TextInputField
|
|
107
|
+
name="firstName"
|
|
108
|
+
required
|
|
109
|
+
minLength={2}
|
|
110
|
+
maxLength={30}
|
|
111
|
+
/>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### With Validate
|
|
115
|
+
|
|
116
|
+
```tsx
|
|
117
|
+
const EXISTING_IPS = ['192.168.1.1']
|
|
118
|
+
|
|
119
|
+
<TextInputField
|
|
120
|
+
name="ip"
|
|
121
|
+
validate={{
|
|
122
|
+
ipAlreadyExists: (ip: string) =>
|
|
123
|
+
EXISTING_IPS.includes(ip) ? 'This ip is already in use' : undefined,
|
|
124
|
+
}}
|
|
125
|
+
/>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### With Regex
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
<TextInputField
|
|
132
|
+
name="email"
|
|
133
|
+
regex={[/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/]}
|
|
134
|
+
/>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
We all know regex can be tricky, so to help you with that we made [Scaleway Regex](https://github.com/scaleway/scaleway-lib/tree/main/packages/regex) library that contains a lot of useful regexes that you can use in your forms.
|
|
138
|
+
You can easily install it with:
|
|
139
|
+
|
|
140
|
+
```sh
|
|
141
|
+
pnpm add @scaleway/regex
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
You can then use it like this:
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
import { email } from '@scaleway/regex'
|
|
148
|
+
|
|
149
|
+
<TextInputField
|
|
150
|
+
name="email"
|
|
151
|
+
regex={[email]}
|
|
152
|
+
/>
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Check all the available regexes in the [Scaleway Regex file](https://github.com/scaleway/scaleway-lib/blob/main/packages/regex/src/index.ts)
|
|
156
|
+
|
|
157
|
+
### Resolvers | Zod
|
|
158
|
+
|
|
159
|
+
You can use [Zod](https://zod.dev/) for validation by integrating it with `@ultraviolet/form`. First you will need to install Zod and the Zod resolver for React Hook Form:
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
pnpm add zod @hookform/resolvers
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
Here's how you can do it:
|
|
167
|
+
|
|
168
|
+
```tsx
|
|
169
|
+
import { ThemeProvider } from '@emotion/react'
|
|
170
|
+
import { Form, TextInputField } from '@ultraviolet/form'
|
|
171
|
+
import { theme } from '@ultraviolet/ui'
|
|
172
|
+
import { useForm } from '@ultraviolet/form'
|
|
173
|
+
import { zodResolver } from "@hookform/resolvers/zod"
|
|
174
|
+
import * as z from "zod"
|
|
175
|
+
|
|
176
|
+
// Define your Zod schema for validation
|
|
177
|
+
const schema = z.object({
|
|
178
|
+
firstName: z.string(),
|
|
179
|
+
lastName: z.string(),
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
// Types are inferred from the Zod schema
|
|
183
|
+
type FormValues = z.infer<typeof schema>
|
|
184
|
+
|
|
185
|
+
// We define the initial values of the form
|
|
186
|
+
const INITIAL_VALUES: FormValues = {
|
|
187
|
+
firstName: 'Marc',
|
|
188
|
+
lastName: 'Scout',
|
|
189
|
+
} as const
|
|
190
|
+
|
|
191
|
+
export default function App() {
|
|
192
|
+
const methods = useForm<FormValues>({
|
|
193
|
+
defaultValues: INITIAL_VALUES,
|
|
194
|
+
resolver: zodResolver(schema),
|
|
195
|
+
mode: 'onChange',
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
const formErrors = {
|
|
199
|
+
required: () => 'This field is required',
|
|
200
|
+
// Add more error messages as needed for min, max, etc.
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const onSubmit = async ({
|
|
204
|
+
firstName,
|
|
205
|
+
lastName,
|
|
206
|
+
}: FormValues) => {
|
|
207
|
+
// Add your form submission logic here
|
|
208
|
+
console.log('Form submitted with values:', { firstName, lastName })
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<ThemeProvider theme={theme}>
|
|
213
|
+
<Form methods={methods} errors={formErrors} onSubmit={onSubmit}>
|
|
214
|
+
<TextInputField name="firstName" />
|
|
215
|
+
<TextInputField name="lastName" />
|
|
216
|
+
</Form>
|
|
217
|
+
</ThemeProvider>
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
If you need more examples with other resolvers we invite you to check [React Hook Form Resolvers Documentation](https://github.com/react-hook-form/resolvers#quickstart)
|
|
223
|
+
|
|
36
224
|
## Documentation
|
|
37
225
|
|
|
38
226
|
Checkout our [documentation website](https://storybook.ultraviolet.scaleway.com/).
|
|
@@ -5,10 +5,8 @@ import type { BaseFieldProps } from '../../types';
|
|
|
5
5
|
type CheckboxGroupFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = Omit<BaseFieldProps<TFieldValues, TFieldName>, 'label'> & Omit<ComponentProps<typeof CheckboxGroup>, 'value' | 'onChange'>;
|
|
6
6
|
export declare const CheckboxGroupField: {
|
|
7
7
|
<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ control, children, onChange, error: customError, name, required, shouldUnregister, validate, legend, ...props }: CheckboxGroupFieldProps<TFieldValues, TFieldName>): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
8
|
-
Checkbox: ({ onFocus, onBlur, disabled, error, name, value, children, helper, className, autoFocus, "data-testid": dataTestId, required, }: Omit<({
|
|
8
|
+
Checkbox: ({ onFocus, onBlur, disabled, error, name, value, children, helper, className, autoFocus, "data-testid": dataTestId, required, tooltip, }: Omit<({
|
|
9
9
|
error?: string | import("react").ReactNode;
|
|
10
|
-
size?: number;
|
|
11
|
-
progress?: boolean;
|
|
12
10
|
helper?: import("react").ReactNode;
|
|
13
11
|
disabled?: boolean;
|
|
14
12
|
checked?: boolean | "indeterminate";
|
|
@@ -9,7 +9,7 @@ const minDate = require("../../validators/minDate.cjs");
|
|
|
9
9
|
const index = require("../../providers/ErrorContext/index.cjs");
|
|
10
10
|
const parseDate = (value) => typeof value === "string" ? new Date(value) : value;
|
|
11
11
|
const isEmpty = (value) => !value;
|
|
12
|
-
const
|
|
12
|
+
const DateInputField = ({
|
|
13
13
|
required,
|
|
14
14
|
name,
|
|
15
15
|
control,
|
|
@@ -80,4 +80,4 @@ const DateField = ({
|
|
|
80
80
|
label
|
|
81
81
|
}, error), selectsRange, showMonthYearPicker, startDate: selectsRange && Array.isArray(field.value) ? field.value[0] : void 0, endDate: selectsRange && Array.isArray(field.value) ? field.value[1] : void 0 });
|
|
82
82
|
};
|
|
83
|
-
exports.
|
|
83
|
+
exports.DateInputField = DateInputField;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DateInput } from '@ultraviolet/ui';
|
|
2
|
+
import type { ComponentProps } from 'react';
|
|
3
|
+
import type { FieldPath, FieldValues } from 'react-hook-form';
|
|
4
|
+
import type { BaseFieldProps } from '../../types';
|
|
5
|
+
type DateInputFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TFieldName> & Omit<ComponentProps<typeof DateInput>, 'required' | 'name' | 'onChange' | 'value'>;
|
|
6
|
+
export declare const DateInputField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ required, name, control, label, format, minDate, maxDate, onChange, onBlur, validate, selectsRange, showMonthYearPicker, shouldUnregister, ...props }: DateInputFieldProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
|
|
7
|
+
export {};
|
|
@@ -7,7 +7,7 @@ import { minDateValidator } from "../../validators/minDate.js";
|
|
|
7
7
|
import { useErrors } from "../../providers/ErrorContext/index.js";
|
|
8
8
|
const parseDate = (value) => typeof value === "string" ? new Date(value) : value;
|
|
9
9
|
const isEmpty = (value) => !value;
|
|
10
|
-
const
|
|
10
|
+
const DateInputField = ({
|
|
11
11
|
required,
|
|
12
12
|
name,
|
|
13
13
|
control,
|
|
@@ -79,5 +79,5 @@ const DateField = ({
|
|
|
79
79
|
}, error), selectsRange, showMonthYearPicker, startDate: selectsRange && Array.isArray(field.value) ? field.value[0] : void 0, endDate: selectsRange && Array.isArray(field.value) ? field.value[1] : void 0 });
|
|
80
80
|
};
|
|
81
81
|
export {
|
|
82
|
-
|
|
82
|
+
DateInputField
|
|
83
83
|
};
|
|
@@ -5,7 +5,7 @@ const jsxRuntime = require("@emotion/react/jsx-runtime");
|
|
|
5
5
|
const icons = require("@ultraviolet/icons");
|
|
6
6
|
const ui = require("@ultraviolet/ui");
|
|
7
7
|
const reactHookForm = require("react-hook-form");
|
|
8
|
-
const index = require("../
|
|
8
|
+
const index = require("../TextInputField/index.cjs");
|
|
9
9
|
const KeyValueField = ({
|
|
10
10
|
name,
|
|
11
11
|
inputKey,
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { Button } from '@ultraviolet/ui';
|
|
2
2
|
import type { ComponentProps } from 'react';
|
|
3
3
|
import type { Control, FieldArrayPath, FieldValues } from 'react-hook-form';
|
|
4
|
-
import { TextInputField
|
|
4
|
+
import { TextInputField } from '../TextInputField';
|
|
5
5
|
type InputKeyProps = {
|
|
6
|
-
label: ComponentProps<typeof
|
|
7
|
-
required?: ComponentProps<typeof
|
|
8
|
-
regex?: ComponentProps<typeof
|
|
6
|
+
label: ComponentProps<typeof TextInputField>['label'];
|
|
7
|
+
required?: ComponentProps<typeof TextInputField>['required'];
|
|
8
|
+
regex?: ComponentProps<typeof TextInputField>['regex'];
|
|
9
9
|
};
|
|
10
10
|
type InputValueProps = {
|
|
11
|
-
type?: ComponentProps<typeof
|
|
12
|
-
placeholder?: ComponentProps<typeof
|
|
11
|
+
type?: ComponentProps<typeof TextInputField>['type'];
|
|
12
|
+
placeholder?: ComponentProps<typeof TextInputField>['placeholder'];
|
|
13
13
|
} & InputKeyProps;
|
|
14
14
|
type AddButtonProps = {
|
|
15
15
|
name: ComponentProps<typeof Button>['children'];
|
|
@@ -3,7 +3,7 @@ import { jsxs, jsx } from "@emotion/react/jsx-runtime";
|
|
|
3
3
|
import { DeleteIcon, PlusIcon } from "@ultraviolet/icons";
|
|
4
4
|
import { Stack, Row, Button } from "@ultraviolet/ui";
|
|
5
5
|
import { useFieldArray } from "react-hook-form";
|
|
6
|
-
import { TextInputField } from "../
|
|
6
|
+
import { TextInputField } from "../TextInputField/index.js";
|
|
7
7
|
const KeyValueField = ({
|
|
8
8
|
name,
|
|
9
9
|
inputKey,
|
|
@@ -4,26 +4,22 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
4
4
|
const jsxRuntime = require("@emotion/react/jsx-runtime");
|
|
5
5
|
const ui = require("@ultraviolet/ui");
|
|
6
6
|
const reactHookForm = require("react-hook-form");
|
|
7
|
+
const isInteger = require("../../validators/isInteger.cjs");
|
|
7
8
|
const index = require("../../providers/ErrorContext/index.cjs");
|
|
8
9
|
const NumberInputField = ({
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
control,
|
|
11
|
+
max = Number.MAX_SAFE_INTEGER,
|
|
12
|
+
min = 0,
|
|
12
13
|
name,
|
|
13
14
|
onChange,
|
|
14
15
|
onBlur,
|
|
15
|
-
onFocus,
|
|
16
|
-
onMaxCrossed,
|
|
17
|
-
onMinCrossed,
|
|
18
|
-
required,
|
|
19
|
-
size,
|
|
20
16
|
step,
|
|
21
|
-
text,
|
|
22
|
-
className,
|
|
23
17
|
label,
|
|
18
|
+
"aria-label": ariaLabel,
|
|
19
|
+
required,
|
|
24
20
|
shouldUnregister = false,
|
|
25
21
|
validate,
|
|
26
|
-
|
|
22
|
+
...props
|
|
27
23
|
}) => {
|
|
28
24
|
const {
|
|
29
25
|
getError
|
|
@@ -35,25 +31,29 @@ const NumberInputField = ({
|
|
|
35
31
|
}
|
|
36
32
|
} = reactHookForm.useController({
|
|
37
33
|
name,
|
|
38
|
-
shouldUnregister,
|
|
39
34
|
control,
|
|
35
|
+
shouldUnregister,
|
|
40
36
|
rules: {
|
|
41
|
-
max
|
|
42
|
-
min
|
|
37
|
+
max,
|
|
38
|
+
min,
|
|
43
39
|
required,
|
|
44
|
-
validate
|
|
40
|
+
validate: {
|
|
41
|
+
...validate,
|
|
42
|
+
isInteger: isInteger.isInteger(step)
|
|
43
|
+
}
|
|
45
44
|
}
|
|
46
45
|
});
|
|
47
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.NumberInput, { name: field.name, value: field.value,
|
|
46
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ui.NumberInput, { ...props, name: field.name, value: field.value, onBlur: (event) => {
|
|
48
47
|
field.onBlur();
|
|
49
48
|
onBlur?.(event);
|
|
50
|
-
}, onChange: (
|
|
51
|
-
field.onChange(
|
|
52
|
-
onChange?.(
|
|
53
|
-
},
|
|
54
|
-
label: label ??
|
|
55
|
-
max
|
|
56
|
-
min
|
|
57
|
-
|
|
49
|
+
}, onChange: (newValue) => {
|
|
50
|
+
field.onChange(newValue);
|
|
51
|
+
onChange?.(newValue);
|
|
52
|
+
}, max, min, step, label, error: getError({
|
|
53
|
+
label: label ?? ariaLabel ?? name,
|
|
54
|
+
max,
|
|
55
|
+
min,
|
|
56
|
+
isInteger: step
|
|
57
|
+
}, error), "aria-label": ariaLabel, required });
|
|
58
58
|
};
|
|
59
59
|
exports.NumberInputField = NumberInputField;
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { NumberInput } from '@ultraviolet/ui';
|
|
2
|
-
import type { ComponentProps
|
|
2
|
+
import type { ComponentProps } from 'react';
|
|
3
3
|
import type { FieldPath, FieldValues } from 'react-hook-form';
|
|
4
4
|
import type { BaseFieldProps } from '../../types';
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
onFocus?: FocusEventHandler<HTMLInputElement>;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated This component is deprecated, use `NumberInputFieldV2` instead.
|
|
11
|
-
*/
|
|
12
|
-
export declare const NumberInputField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ disabled, maxValue, minValue, name, onChange, onBlur, onFocus, onMaxCrossed, onMinCrossed, required, size, step, text, className, label, shouldUnregister, validate, control, }: NumberInputValueFieldProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
|
|
5
|
+
type NumberInputProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = BaseFieldProps<TFieldValues, TFieldName> & Omit<ComponentProps<typeof NumberInput>, 'onChange'>;
|
|
6
|
+
export declare const NumberInputField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ control, max, min, name, onChange, onBlur, step, label, "aria-label": ariaLabel, required, shouldUnregister, validate, ...props }: NumberInputProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
|
|
13
7
|
export {};
|
|
@@ -2,26 +2,22 @@
|
|
|
2
2
|
import { jsx } from "@emotion/react/jsx-runtime";
|
|
3
3
|
import { NumberInput } from "@ultraviolet/ui";
|
|
4
4
|
import { useController } from "react-hook-form";
|
|
5
|
+
import { isInteger } from "../../validators/isInteger.js";
|
|
5
6
|
import { useErrors } from "../../providers/ErrorContext/index.js";
|
|
6
7
|
const NumberInputField = ({
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
control,
|
|
9
|
+
max = Number.MAX_SAFE_INTEGER,
|
|
10
|
+
min = 0,
|
|
10
11
|
name,
|
|
11
12
|
onChange,
|
|
12
13
|
onBlur,
|
|
13
|
-
onFocus,
|
|
14
|
-
onMaxCrossed,
|
|
15
|
-
onMinCrossed,
|
|
16
|
-
required,
|
|
17
|
-
size,
|
|
18
14
|
step,
|
|
19
|
-
text,
|
|
20
|
-
className,
|
|
21
15
|
label,
|
|
16
|
+
"aria-label": ariaLabel,
|
|
17
|
+
required,
|
|
22
18
|
shouldUnregister = false,
|
|
23
19
|
validate,
|
|
24
|
-
|
|
20
|
+
...props
|
|
25
21
|
}) => {
|
|
26
22
|
const {
|
|
27
23
|
getError
|
|
@@ -33,26 +29,30 @@ const NumberInputField = ({
|
|
|
33
29
|
}
|
|
34
30
|
} = useController({
|
|
35
31
|
name,
|
|
36
|
-
shouldUnregister,
|
|
37
32
|
control,
|
|
33
|
+
shouldUnregister,
|
|
38
34
|
rules: {
|
|
39
|
-
max
|
|
40
|
-
min
|
|
35
|
+
max,
|
|
36
|
+
min,
|
|
41
37
|
required,
|
|
42
|
-
validate
|
|
38
|
+
validate: {
|
|
39
|
+
...validate,
|
|
40
|
+
isInteger: isInteger(step)
|
|
41
|
+
}
|
|
43
42
|
}
|
|
44
43
|
});
|
|
45
|
-
return /* @__PURE__ */ jsx(NumberInput, { name: field.name, value: field.value,
|
|
44
|
+
return /* @__PURE__ */ jsx(NumberInput, { ...props, name: field.name, value: field.value, onBlur: (event) => {
|
|
46
45
|
field.onBlur();
|
|
47
46
|
onBlur?.(event);
|
|
48
|
-
}, onChange: (
|
|
49
|
-
field.onChange(
|
|
50
|
-
onChange?.(
|
|
51
|
-
},
|
|
52
|
-
label: label ??
|
|
53
|
-
max
|
|
54
|
-
min
|
|
55
|
-
|
|
47
|
+
}, onChange: (newValue) => {
|
|
48
|
+
field.onChange(newValue);
|
|
49
|
+
onChange?.(newValue);
|
|
50
|
+
}, max, min, step, label, error: getError({
|
|
51
|
+
label: label ?? ariaLabel ?? name,
|
|
52
|
+
max,
|
|
53
|
+
min,
|
|
54
|
+
isInteger: step
|
|
55
|
+
}, error), "aria-label": ariaLabel, required });
|
|
56
56
|
};
|
|
57
57
|
export {
|
|
58
58
|
NumberInputField
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { Radio } from '@ultraviolet/ui';
|
|
2
|
-
import {
|
|
2
|
+
import type { ComponentProps } from 'react';
|
|
3
3
|
import type { FieldPath, FieldValues } from 'react-hook-form';
|
|
4
4
|
import type { BaseFieldProps } from '../../types';
|
|
5
5
|
type RadioFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = Omit<BaseFieldProps<TFieldValues, TFieldName>, 'label'> & Omit<ComponentProps<typeof Radio>, 'value' | 'onChange'>;
|
|
6
|
-
/**
|
|
7
|
-
* @deprecated This component is deprecated, use `RadioGroupField` instead.
|
|
8
|
-
*/
|
|
9
6
|
export declare const RadioField: <TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ control, disabled, name, onBlur, label, onChange, onFocus, required, value, shouldUnregister, validate, "aria-label": ariaLabel, ...props }: RadioFieldProps<TFieldValues, TFieldName>) => import("@emotion/react/jsx-runtime").JSX.Element;
|
|
10
7
|
export {};
|
|
@@ -5,15 +5,25 @@ import type { BaseFieldProps } from '../../types';
|
|
|
5
5
|
type RadioGroupFieldProps<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues>> = Omit<BaseFieldProps<TFieldValues, TFieldName>, 'label'> & Omit<ComponentProps<typeof RadioGroup>, 'value' | 'onChange' | 'legend'> & Partial<Pick<ComponentProps<typeof RadioGroup>, 'legend'>>;
|
|
6
6
|
export declare const RadioGroupField: {
|
|
7
7
|
<TFieldValues extends FieldValues, TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ control, name, onChange, required, children, error: customError, shouldUnregister, validate, legend, ...props }: RadioGroupFieldProps<TFieldValues, TFieldName>): JSX.Element;
|
|
8
|
-
Radio: ({ onFocus, onBlur, disabled, error,
|
|
9
|
-
error?: import("react").ReactNode;
|
|
8
|
+
Radio: ({ onFocus, onBlur, disabled, error, value, label, helper, className, autoFocus, onKeyDown, tooltip, "data-testid": dataTestId, }: {
|
|
10
9
|
value: string | number;
|
|
10
|
+
onBlur?: import("react").FocusEventHandler<HTMLInputElement> | undefined;
|
|
11
|
+
disabled?: boolean | undefined | undefined;
|
|
12
|
+
className?: string | undefined;
|
|
13
|
+
ref?: import("react").Ref<HTMLInputElement> | undefined;
|
|
14
|
+
label?: import("react").ReactNode;
|
|
15
|
+
autoFocus?: boolean | undefined | undefined;
|
|
16
|
+
id?: string | undefined | undefined;
|
|
17
|
+
onClick?: import("react").MouseEventHandler<HTMLInputElement> | undefined;
|
|
18
|
+
onFocus?: import("react").FocusEventHandler<HTMLInputElement> | undefined;
|
|
19
|
+
tabIndex?: number | undefined | undefined;
|
|
20
|
+
error?: import("react").ReactNode;
|
|
11
21
|
helper?: import("react").ReactNode;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
'data-testid'?: string | undefined;
|
|
23
|
+
tooltip?: string | undefined;
|
|
24
|
+
'aria-label'?: string | undefined;
|
|
25
|
+
key?: import("react").Key | null | undefined;
|
|
26
|
+
onKeyDown?: import("react").KeyboardEventHandler<HTMLInputElement> | undefined;
|
|
17
27
|
}) => import("@emotion/react/jsx-runtime").JSX.Element;
|
|
18
28
|
};
|
|
19
29
|
export {};
|
|
@@ -6,71 +6,19 @@ const ui = require("@ultraviolet/ui");
|
|
|
6
6
|
const react = require("react");
|
|
7
7
|
const reactHookForm = require("react-hook-form");
|
|
8
8
|
const index = require("../../providers/ErrorContext/index.cjs");
|
|
9
|
-
const identity = (x) => x;
|
|
10
9
|
const SelectInputField = ({
|
|
11
|
-
animation,
|
|
12
|
-
animationDuration,
|
|
13
|
-
animationOnChange,
|
|
14
|
-
children,
|
|
15
|
-
className,
|
|
16
|
-
disabled,
|
|
17
|
-
// error: errorProp,
|
|
18
|
-
format: formatProp = identity,
|
|
19
|
-
// formatOnBlur,
|
|
20
|
-
id,
|
|
21
|
-
inputId,
|
|
22
|
-
isClearable,
|
|
23
|
-
isLoading,
|
|
24
|
-
isSearchable,
|
|
25
10
|
label = "",
|
|
26
|
-
maxLength,
|
|
27
|
-
menuPortalTarget,
|
|
28
|
-
minLength,
|
|
29
|
-
multiple,
|
|
30
|
-
name,
|
|
31
11
|
onBlur,
|
|
32
|
-
onChange,
|
|
33
|
-
onFocus,
|
|
34
|
-
options: optionsProp,
|
|
35
|
-
parse: parseProp = identity,
|
|
36
|
-
placeholder,
|
|
37
|
-
readOnly,
|
|
38
12
|
required,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
customStyle,
|
|
13
|
+
name,
|
|
14
|
+
"aria-label": ariaLabel,
|
|
42
15
|
shouldUnregister = false,
|
|
43
|
-
|
|
44
|
-
validate
|
|
16
|
+
control,
|
|
17
|
+
validate,
|
|
18
|
+
onChange,
|
|
19
|
+
multiselect,
|
|
20
|
+
...props
|
|
45
21
|
}) => {
|
|
46
|
-
const options = react.useMemo(() => optionsProp || react.Children.toArray(children).flat().filter(Boolean).map(({
|
|
47
|
-
props: {
|
|
48
|
-
children: labelChild,
|
|
49
|
-
...option
|
|
50
|
-
}
|
|
51
|
-
}) => ({
|
|
52
|
-
...option,
|
|
53
|
-
label: labelChild
|
|
54
|
-
})), [optionsProp, children]);
|
|
55
|
-
const parse = react.useMemo(() => multiple ? parseProp : (option) => parseProp(option?.value ?? null, name), [multiple, parseProp, name]);
|
|
56
|
-
const format = react.useCallback((val) => {
|
|
57
|
-
if (multiple) return formatProp(val, name);
|
|
58
|
-
const find = (opts, valueToFind) => opts?.find((option) => option.value === valueToFind);
|
|
59
|
-
let selected = "";
|
|
60
|
-
if (val && options) {
|
|
61
|
-
selected = find(options, val);
|
|
62
|
-
if (!selected) {
|
|
63
|
-
selected = options.map((curr) => find(curr.options, val)).filter(identity);
|
|
64
|
-
if (Array.isArray(selected) && selected.length === 0) {
|
|
65
|
-
selected = "";
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return formatProp(selected, name);
|
|
70
|
-
}, [formatProp, multiple, name, options]);
|
|
71
|
-
const {
|
|
72
|
-
getError
|
|
73
|
-
} = index.useErrors();
|
|
74
22
|
const {
|
|
75
23
|
field,
|
|
76
24
|
fieldState: {
|
|
@@ -78,25 +26,25 @@ const SelectInputField = ({
|
|
|
78
26
|
}
|
|
79
27
|
} = reactHookForm.useController({
|
|
80
28
|
name,
|
|
29
|
+
control,
|
|
81
30
|
shouldUnregister,
|
|
82
31
|
rules: {
|
|
83
32
|
required,
|
|
84
|
-
minLength: minLength || required ? 1 : void 0,
|
|
85
|
-
maxLength,
|
|
86
33
|
validate
|
|
87
34
|
}
|
|
88
35
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
36
|
+
const {
|
|
37
|
+
getError
|
|
38
|
+
} = index.useErrors();
|
|
39
|
+
const handleChange = react.useCallback((value) => {
|
|
40
|
+
onChange?.(value);
|
|
41
|
+
field.onChange(value);
|
|
42
|
+
}, [onChange, field]);
|
|
43
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ui.SelectInput, { name: field.name, multiselect, required, onBlur: (event) => {
|
|
94
44
|
field.onBlur();
|
|
95
45
|
onBlur?.(event);
|
|
96
|
-
},
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}, onFocus, options, placeholder, readOnly, noTopLabel, required, value: format(field.value), emptyState, "data-testid": dataTestId, children });
|
|
46
|
+
}, value: field.value, error: getError({
|
|
47
|
+
label: label ?? ariaLabel ?? name
|
|
48
|
+
}, error), label, "aria-label": ariaLabel, onChange: handleChange, ...props });
|
|
100
49
|
};
|
|
101
|
-
SelectInputField.Option = ui.SelectInput.Option;
|
|
102
50
|
exports.SelectInputField = SelectInputField;
|