@saas-ui/forms 2.0.0-next.3 → 2.0.0-next.6
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +43 -0
- package/README.md +53 -6
- package/dist/ajv/index.d.ts +358 -11
- package/dist/ajv/index.js +7 -9
- package/dist/ajv/index.js.map +1 -1
- package/dist/ajv/index.mjs +7 -10
- package/dist/ajv/index.mjs.map +1 -1
- package/dist/index.d.ts +448 -247
- package/dist/index.js +707 -682
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +691 -666
- package/dist/index.mjs.map +1 -1
- package/dist/yup/index.d.ts +580 -21
- package/dist/yup/index.js +6 -10
- package/dist/yup/index.js.map +1 -1
- package/dist/yup/index.mjs +4 -8
- package/dist/yup/index.mjs.map +1 -1
- package/dist/zod/index.d.ts +580 -11
- package/dist/zod/index.js +5 -0
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/index.mjs +5 -1
- package/dist/zod/index.mjs.map +1 -1
- package/package.json +19 -10
- package/src/array-field.tsx +82 -45
- package/src/auto-form.tsx +7 -3
- package/src/base-field.tsx +54 -0
- package/src/create-field.tsx +144 -0
- package/src/create-form.tsx +54 -0
- package/src/default-fields.tsx +163 -0
- package/src/display-field.tsx +9 -11
- package/src/display-if.tsx +20 -13
- package/src/field-resolver.ts +10 -8
- package/src/field.tsx +18 -445
- package/src/fields-context.tsx +23 -0
- package/src/fields.tsx +34 -21
- package/src/form-context.tsx +84 -0
- package/src/form.tsx +69 -52
- package/src/index.ts +44 -4
- package/src/input-right-button/input-right-button.stories.tsx +1 -1
- package/src/input-right-button/input-right-button.tsx +0 -2
- package/src/layout.tsx +16 -11
- package/src/number-input/number-input.tsx +9 -5
- package/src/object-field.tsx +13 -8
- package/src/password-input/password-input.stories.tsx +23 -2
- package/src/password-input/password-input.tsx +6 -6
- package/src/pin-input/pin-input.tsx +1 -5
- package/src/radio/radio-input.stories.tsx +1 -1
- package/src/radio/radio-input.tsx +12 -10
- package/src/select/native-select.tsx +1 -4
- package/src/select/select-context.tsx +130 -0
- package/src/select/select.stories.tsx +116 -85
- package/src/select/select.test.tsx +1 -1
- package/src/select/select.tsx +160 -146
- package/src/step-form.tsx +29 -11
- package/src/submit-button.tsx +5 -1
- package/src/types.ts +144 -0
- package/src/use-array-field.tsx +9 -3
- package/src/utils.ts +23 -1
- package/src/watch-field.tsx +2 -6
- /package/src/radio/{radio.test.tsx → radio-input.test.tsx} +0 -0
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,42 @@
|
|
1
1
|
# @saas-ui/forms
|
2
2
|
|
3
|
+
## 2.0.0-next.6
|
4
|
+
|
5
|
+
### Major Changes
|
6
|
+
|
7
|
+
- 532011d6: Restructured the Select component to make it atomic, the new composition is Select, SelectButton, SelectList and SelectOption.
|
8
|
+
- 39e778d8: Form will now render AutoField by default when no children are passed.
|
9
|
+
|
10
|
+
### Minor Changes
|
11
|
+
|
12
|
+
- 532011d6: Select can now supports theming using the SuiSelect theme config.
|
13
|
+
- 6dd737ce: Select field now renders invalid state.
|
14
|
+
- c85541cb: AutoForm field props can now be overridden using the fields prop on Form
|
15
|
+
|
16
|
+
## 2.0.0-next.5
|
17
|
+
|
18
|
+
### Minor Changes
|
19
|
+
|
20
|
+
- a8ea24da: PasswordInput now accepts leftAddon property.
|
21
|
+
|
22
|
+
### Patch Changes
|
23
|
+
|
24
|
+
- Updated dependencies [61b27fa6]
|
25
|
+
- @saas-ui/core@2.0.0-next.5
|
26
|
+
|
27
|
+
## 2.0.0-next.4
|
28
|
+
|
29
|
+
### Patch Changes
|
30
|
+
|
31
|
+
- Updated dependencies [83f54180]
|
32
|
+
- Updated dependencies [ba61612f]
|
33
|
+
- Updated dependencies [83f54180]
|
34
|
+
- Updated dependencies [83f54180]
|
35
|
+
- Updated dependencies [83f54180]
|
36
|
+
- Updated dependencies [83f54180]
|
37
|
+
- Updated dependencies [27a68bca]
|
38
|
+
- @saas-ui/core@2.0.0-next.4
|
39
|
+
|
3
40
|
## 2.0.0-next.3
|
4
41
|
|
5
42
|
### Patch Changes
|
@@ -39,6 +76,12 @@
|
|
39
76
|
- Updated dependencies [3a15e8c8]
|
40
77
|
- @saas-ui/core@2.0.0-next.0
|
41
78
|
|
79
|
+
## 1.5.3
|
80
|
+
|
81
|
+
### Patch Changes
|
82
|
+
|
83
|
+
- 382e095: InputRightButton no longer exported from forms package to prevent conflicts.
|
84
|
+
|
42
85
|
## 1.5.2
|
43
86
|
|
44
87
|
### Patch Changes
|
package/README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# Forms Manager
|
2
2
|
|
3
|
-
Chakra UI
|
3
|
+
Powerfull forms library for Chakra UI.
|
4
|
+
Create typesafe React forms with speed.
|
4
5
|
|
5
|
-
|
6
|
+
Supports Zod, Yup and AJV for validation and form generation.
|
6
7
|
|
7
|
-
|
8
|
+
- [Docs](https://www.saas-ui.dev/docs/forms/form)
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
@@ -16,9 +17,55 @@ $ yarn add @saas-ui/forms
|
|
16
17
|
$ npm i @saas-ui/forms --save
|
17
18
|
```
|
18
19
|
|
19
|
-
##
|
20
|
+
## Usage with Zod
|
20
21
|
|
21
|
-
|
22
|
+
### AutoForm
|
23
|
+
|
24
|
+
Generate forms from schema.
|
25
|
+
|
26
|
+
```tsx
|
27
|
+
import { createZodForm } from '@saas-ui/forms/zod'
|
28
|
+
|
29
|
+
const { AutoForm } = createZodForm()
|
30
|
+
|
31
|
+
const schema = z.object({
|
32
|
+
name: z.string(),
|
33
|
+
})
|
34
|
+
|
35
|
+
function App() {
|
36
|
+
const onSubmit = (data: z.infer<typeof schema>) => {}
|
37
|
+
return <AutoForm schema={schema} onSubmit={onSubmit} />
|
38
|
+
}
|
39
|
+
```
|
40
|
+
|
41
|
+
### Form
|
42
|
+
|
43
|
+
Create custom typesafe forms.
|
44
|
+
|
45
|
+
```tsx
|
46
|
+
import { FormLayout, createZodForm } from '@saas-ui/forms/zod'
|
47
|
+
|
48
|
+
const { Form, SubmitButton } = createZodForm()
|
49
|
+
|
50
|
+
const schema = z.object({
|
51
|
+
name: z.string()
|
52
|
+
})
|
53
|
+
|
54
|
+
function App() {
|
55
|
+
const onSubmit = (data: z.infer<typeof schema>) => {}
|
56
|
+
|
57
|
+
return (
|
58
|
+
<Form schema={schema} onSubmit={onSubmit}>
|
59
|
+
{({ Field }) => (
|
60
|
+
<FormLayout>
|
61
|
+
<Field name="name" type="text" />
|
62
|
+
<SubmitButton>Save</SubmitButton>
|
63
|
+
</FormLayotu>
|
64
|
+
)}
|
65
|
+
</Form>
|
66
|
+
)
|
67
|
+
}
|
68
|
+
```
|
22
69
|
|
23
70
|
## Source
|
24
71
|
|
package/dist/ajv/index.d.ts
CHANGED
@@ -1,21 +1,368 @@
|
|
1
|
+
import * as React$1 from 'react';
|
2
|
+
import React$1__default from 'react';
|
3
|
+
import * as _chakra_ui_system_dist_system_types from '@chakra-ui/system/dist/system.types';
|
4
|
+
import { HTMLChakraProps, ButtonProps, TextareaProps, SwitchProps, CheckboxProps, FormControlProps, ResponsiveValue, InputProps, RadioGroupProps, MenuProps, SelectProps as SelectProps$1, UsePinInputProps, SystemProps, NumberInputProps as NumberInputProps$1, StackDirection, MenuItemOptionProps, RadioProps } from '@chakra-ui/react';
|
5
|
+
import * as react_hook_form from 'react-hook-form';
|
6
|
+
import { FieldValues, UseFormProps, FieldPath, UseFormReturn, WatchObserver, SubmitHandler, SubmitErrorHandler, RegisterOptions, UseFieldArrayReturn } from 'react-hook-form';
|
7
|
+
import { MaybeRenderProp } from '@chakra-ui/react-utils';
|
8
|
+
import * as ajv_dist_jtd from 'ajv/dist/jtd';
|
9
|
+
import { JTDDataType } from 'ajv/dist/jtd';
|
10
|
+
export { JTDDataType, JTDSchemaType } from 'ajv/dist/jtd';
|
11
|
+
import * as _saas_ui_forms from '@saas-ui/forms';
|
12
|
+
import { FieldResolver as FieldResolver$1, CreateFormProps, WithFields as WithFields$1, FormProps as FormProps$1 } from '@saas-ui/forms';
|
13
|
+
import { JSONSchemaType } from 'ajv';
|
1
14
|
import { ajvResolver } from '@hookform/resolvers/ajv';
|
2
15
|
export { ajvResolver } from '@hookform/resolvers/ajv';
|
3
|
-
import { FieldResolver, CreateFormProps } from '@saas-ui/forms';
|
4
|
-
import { JSONSchemaType } from 'ajv';
|
5
|
-
export { JTDDataType, JTDSchemaType } from 'ajv/dist/jtd';
|
6
16
|
|
7
|
-
interface
|
8
|
-
|
9
|
-
|
10
|
-
|
17
|
+
interface SubmitButtonProps extends ButtonProps {
|
18
|
+
/**
|
19
|
+
* Disable the submit button if the form is untouched.
|
20
|
+
*
|
21
|
+
* Change the default behavior by updating
|
22
|
+
* `SubmitButton.defaultProps.disableIfUntouched`
|
23
|
+
*/
|
24
|
+
disableIfUntouched?: boolean;
|
25
|
+
/**
|
26
|
+
* Disable the submit button if the form is invalid.
|
27
|
+
*
|
28
|
+
* Change the default behavior by updating
|
29
|
+
* `SubmitButton.defaultProps.disableIfInvalid`
|
30
|
+
*/
|
31
|
+
disableIfInvalid?: boolean;
|
32
|
+
}
|
33
|
+
|
34
|
+
interface NumberInputOptions {
|
35
|
+
/**
|
36
|
+
* Hide the stepper.
|
37
|
+
*/
|
38
|
+
hideStepper?: boolean;
|
39
|
+
/**
|
40
|
+
* Render a custom increment icon.
|
41
|
+
*/
|
42
|
+
incrementIcon?: React$1.ReactNode;
|
43
|
+
/**
|
44
|
+
* Render a custom decrement icon.
|
45
|
+
*/
|
46
|
+
decrementIcon?: React$1.ReactNode;
|
47
|
+
}
|
48
|
+
interface NumberInputProps extends NumberInputProps$1, NumberInputOptions {
|
49
|
+
}
|
50
|
+
|
51
|
+
interface PasswordOptions {
|
52
|
+
viewIcon?: React$1__default.ReactNode;
|
53
|
+
viewOffIcon?: React$1__default.ReactNode;
|
54
|
+
leftAddon?: React$1__default.ReactNode;
|
55
|
+
}
|
56
|
+
interface PasswordInputProps extends InputProps, PasswordOptions {
|
57
|
+
}
|
58
|
+
|
59
|
+
interface RadioOption extends Omit<RadioProps, 'value' | 'label'>, FieldOption {
|
60
|
+
}
|
61
|
+
type RadioOptions = FieldOptions<RadioOption>;
|
62
|
+
interface RadioInputOptions {
|
63
|
+
options: RadioOptions;
|
64
|
+
spacing?: SystemProps['margin'];
|
65
|
+
direction?: StackDirection;
|
66
|
+
}
|
67
|
+
interface RadioInputProps extends Omit<RadioGroupProps, 'children'>, RadioInputOptions {
|
68
|
+
}
|
69
|
+
|
70
|
+
interface SelectOptions {
|
71
|
+
/**
|
72
|
+
* The name of the input field in a native form.
|
73
|
+
*/
|
74
|
+
name: string;
|
75
|
+
/**
|
76
|
+
* The value of the select field.
|
77
|
+
*/
|
78
|
+
value?: string | string[];
|
79
|
+
/**
|
80
|
+
* The initial value of the select field.
|
81
|
+
*/
|
82
|
+
defaultValue?: string | string[];
|
83
|
+
/**
|
84
|
+
* The callback invoked when the value of the select field changes.
|
85
|
+
* @param value The value of the select field.
|
86
|
+
*/
|
87
|
+
onChange?: (value: string | string[]) => void;
|
88
|
+
/**
|
89
|
+
* The placeholder text when there's no value.
|
90
|
+
*/
|
91
|
+
placeholder?: string;
|
92
|
+
/**
|
93
|
+
* If `true`, the select will be disabled.
|
94
|
+
*/
|
95
|
+
isDisabled?: boolean;
|
96
|
+
/**
|
97
|
+
* An array of options
|
98
|
+
* If you leave this empty the children prop will be rendered.
|
99
|
+
*/
|
100
|
+
options?: FieldOptions<SelectOption>;
|
101
|
+
/**
|
102
|
+
* Enable multiple select.
|
103
|
+
*/
|
104
|
+
multiple?: boolean;
|
105
|
+
/**
|
106
|
+
* The function used to render the value of the select field.
|
107
|
+
* @param value The value of the select field.
|
108
|
+
* @returns The rendered value.
|
109
|
+
*/
|
110
|
+
renderValue?: (value: string | string[]) => React$1__default.ReactNode;
|
111
|
+
}
|
112
|
+
|
113
|
+
interface SelectProps extends Omit<MenuProps, 'children'>, SelectOptions {
|
114
|
+
}
|
115
|
+
interface SelectOption extends Omit<MenuItemOptionProps, 'value'>, FieldOption {
|
116
|
+
}
|
117
|
+
/**
|
118
|
+
* An option in a select list
|
119
|
+
*
|
120
|
+
* @see https://saas-ui.dev/docs/components/forms/select
|
121
|
+
*/
|
122
|
+
declare const SelectOption: _chakra_ui_system_dist_system_types.ComponentWithAs<"button", MenuItemOptionProps>;
|
123
|
+
|
124
|
+
interface Option$1 {
|
125
|
+
value: string;
|
126
|
+
label?: string;
|
127
|
+
}
|
128
|
+
interface NativeSelectOptions {
|
129
|
+
options?: Option$1[];
|
130
|
+
}
|
131
|
+
interface NativeSelectProps extends SelectProps$1, NativeSelectOptions {
|
11
132
|
}
|
12
|
-
|
133
|
+
|
134
|
+
interface InputFieldProps extends InputProps {
|
135
|
+
type?: string;
|
136
|
+
leftAddon?: React$1.ReactNode;
|
137
|
+
rightAddon?: React$1.ReactNode;
|
138
|
+
}
|
139
|
+
interface NumberInputFieldProps extends NumberInputProps {
|
140
|
+
type: 'number';
|
141
|
+
}
|
142
|
+
interface PinFieldProps extends Omit<UsePinInputProps, 'type'> {
|
143
|
+
pinLength?: number;
|
144
|
+
pinType?: 'alphanumeric' | 'number';
|
145
|
+
spacing?: SystemProps['margin'];
|
146
|
+
}
|
147
|
+
declare const defaultFieldTypes: {
|
148
|
+
text: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
149
|
+
email: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
150
|
+
url: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
151
|
+
phone: React$1.FC<InputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
152
|
+
number: React$1.FC<NumberInputFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
153
|
+
password: React$1.FC<PasswordInputProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
154
|
+
textarea: React$1.FC<TextareaProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
155
|
+
switch: React$1.FC<SwitchProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
156
|
+
select: React$1.FC<SelectProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
157
|
+
checkbox: React$1.FC<CheckboxProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
158
|
+
radio: React$1.FC<RadioInputProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
159
|
+
pin: React$1.FC<PinFieldProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
160
|
+
'native-select': React$1.FC<NativeSelectProps & BaseFieldProps<react_hook_form.FieldValues, string>>;
|
161
|
+
};
|
162
|
+
type DefaultFields = typeof defaultFieldTypes;
|
163
|
+
|
164
|
+
type FieldOption = {
|
165
|
+
label?: string;
|
166
|
+
value: string;
|
167
|
+
};
|
168
|
+
type FieldOptions<TOption extends FieldOption = FieldOption> = Array<string> | Array<TOption>;
|
169
|
+
type ValueOf<T> = T[keyof T];
|
170
|
+
type ShallowMerge<A, B> = Omit<A, keyof B> & B;
|
171
|
+
type Split<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
|
172
|
+
type MapPath<T extends string[]> = T extends [infer U, ...infer R] ? U extends string ? `${U extends `${number}` ? '$' : U}${R[0] extends string ? '.' : ''}${R extends string[] ? MapPath<R> : ''}` : '' : '';
|
173
|
+
type TransformPath<T extends string> = MapPath<Split<T, '.'>>;
|
174
|
+
type ArrayFieldPath<Name extends string> = Name extends string ? TransformPath<Name> : never;
|
175
|
+
interface BaseFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends Omit<FormControlProps, 'label' | 'type'> {
|
176
|
+
/**
|
177
|
+
* The field name
|
178
|
+
*/
|
179
|
+
name: TName | ArrayFieldPath<TName>;
|
180
|
+
/**
|
181
|
+
* The field label
|
182
|
+
*/
|
183
|
+
label?: string;
|
184
|
+
/**
|
185
|
+
* Hide the field label
|
186
|
+
*/
|
187
|
+
hideLabel?: boolean;
|
188
|
+
/**
|
189
|
+
* Field help text
|
190
|
+
*/
|
191
|
+
help?: string;
|
192
|
+
/**
|
193
|
+
* React hook form rules
|
194
|
+
*/
|
195
|
+
rules?: Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
|
196
|
+
/**
|
197
|
+
* Build-in types:
|
198
|
+
* text, number, password, textarea, select, native-select, checkbox, radio, switch, pin
|
199
|
+
*
|
200
|
+
* Will default to a text field if there is no matching type.
|
201
|
+
*/
|
202
|
+
type?: string;
|
203
|
+
/**
|
204
|
+
* The input placeholder
|
205
|
+
*/
|
206
|
+
placeholder?: string;
|
207
|
+
}
|
208
|
+
type FieldPathWithArray<TFieldValues extends FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = TName | ArrayFieldPath<TName>;
|
209
|
+
type MergeFieldProps<FieldDefs, TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = ValueOf<{
|
210
|
+
[K in keyof FieldDefs]: FieldDefs[K] extends React.FC<infer Props> ? {
|
211
|
+
type?: K;
|
212
|
+
} & ShallowMerge<Props, BaseFieldProps<TFieldValues, TName>> : never;
|
213
|
+
}>;
|
214
|
+
type FieldProps<TFieldValues extends FieldValues = FieldValues> = MergeFieldProps<DefaultFields, TFieldValues>;
|
215
|
+
type FormChildren<FieldDefs, TFieldValues extends FieldValues = FieldValues, TContext extends object = object> = MaybeRenderProp<FormRenderContext<TFieldValues, TContext, MergeFieldProps<FieldDefs extends never ? DefaultFields : ShallowMerge<DefaultFields, FieldDefs>, TFieldValues>>>;
|
216
|
+
type DefaultFieldOverrides = {
|
217
|
+
submit?: SubmitButtonProps;
|
218
|
+
[key: string]: any;
|
219
|
+
};
|
220
|
+
type FieldOverrides<FieldDefs, TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = {
|
221
|
+
[K in FieldPathWithArray<TFieldValues, TName>]?: Omit<MergeFieldProps<FieldDefs extends never ? DefaultFields : ShallowMerge<DefaultFields, FieldDefs>, TFieldValues>, 'name'>;
|
222
|
+
};
|
223
|
+
type WithFields<TFormProps extends FormProps<any, any, any, any>, FieldDefs> = TFormProps extends FormProps<infer TFieldValues, infer TContext> ? Omit<TFormProps, 'children' | 'fields'> & {
|
224
|
+
children?: FormChildren<FieldDefs, TFieldValues, TContext>;
|
225
|
+
fields?: FieldOverrides<FieldDefs, TFieldValues> & {
|
226
|
+
submit?: SubmitButtonProps;
|
227
|
+
};
|
228
|
+
} : never;
|
229
|
+
|
230
|
+
type FieldResolver = {
|
231
|
+
getFields(): BaseFieldProps[];
|
232
|
+
getNestedFields(name: string): BaseFieldProps[];
|
233
|
+
};
|
234
|
+
|
235
|
+
interface DisplayIfProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> {
|
236
|
+
children: React$1.ReactElement;
|
237
|
+
name: TName;
|
238
|
+
defaultValue?: unknown;
|
239
|
+
isDisabled?: boolean;
|
240
|
+
isExact?: boolean;
|
241
|
+
condition?: (value: unknown, context: UseFormReturn<TFieldValues>) => boolean;
|
242
|
+
}
|
243
|
+
|
244
|
+
interface UseArrayFieldReturn extends UseFieldArrayReturn {
|
245
|
+
/**
|
246
|
+
* The array field name
|
247
|
+
*/
|
248
|
+
name: string;
|
249
|
+
/**
|
250
|
+
* The default value for new items
|
251
|
+
*/
|
252
|
+
defaultValue: Record<string, any>;
|
253
|
+
/**
|
254
|
+
* Min amount of items
|
255
|
+
*/
|
256
|
+
min?: number;
|
257
|
+
/**
|
258
|
+
* Max amount of items
|
259
|
+
*/
|
260
|
+
max?: number;
|
261
|
+
}
|
262
|
+
interface ArrayFieldOptions<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> {
|
263
|
+
/**
|
264
|
+
* The field name
|
265
|
+
*/
|
266
|
+
name: TName;
|
267
|
+
/**
|
268
|
+
* Default value for new values in the array
|
269
|
+
*/
|
270
|
+
defaultValue?: Record<string, any>;
|
271
|
+
/**
|
272
|
+
* Default key name for rows, change this if your data uses a different 'id' field
|
273
|
+
* @default "id"
|
274
|
+
*/
|
275
|
+
keyName?: string;
|
276
|
+
min?: number;
|
277
|
+
max?: number;
|
278
|
+
}
|
279
|
+
interface ArrayFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends ArrayFieldOptions<TFieldValues, TName>, Omit<BaseFieldProps<TFieldValues, TName>, 'name' | 'defaultValue' | 'children'> {
|
280
|
+
children: MaybeRenderProp<ArrayField[]>;
|
281
|
+
}
|
282
|
+
interface ArrayField {
|
283
|
+
id: string;
|
284
|
+
[key: string]: unknown;
|
285
|
+
}
|
286
|
+
/**
|
287
|
+
* The wrapper component that composes the default ArrayField functionality.
|
288
|
+
*
|
289
|
+
* @see Docs https://saas-ui.dev/docs/components/forms/array-field
|
290
|
+
*/
|
291
|
+
declare const ArrayField: ((props: ArrayFieldProps & {
|
292
|
+
ref?: React$1.ForwardedRef<UseArrayFieldReturn>;
|
293
|
+
}) => React$1.ReactElement) & {
|
294
|
+
displayName: string;
|
295
|
+
};
|
296
|
+
|
297
|
+
interface ObjectFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends BaseFieldProps {
|
298
|
+
name: TName;
|
299
|
+
children: React$1.ReactNode;
|
300
|
+
columns?: ResponsiveValue<number>;
|
301
|
+
spacing?: ResponsiveValue<string | number>;
|
302
|
+
}
|
303
|
+
|
304
|
+
interface FormRenderContext<TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TFieldTypes = FieldProps<TFieldValues>> extends UseFormReturn<TFieldValues, TContext> {
|
305
|
+
Field: React$1.FC<TFieldTypes>;
|
306
|
+
DisplayIf: React$1.FC<DisplayIfProps<TFieldValues>>;
|
307
|
+
ArrayField: React$1.FC<ArrayFieldProps<TFieldValues>>;
|
308
|
+
ObjectField: React$1.FC<ObjectFieldProps<TFieldValues>>;
|
309
|
+
}
|
310
|
+
interface FormOptions<TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TSchema = any, TFieldTypes = FieldProps<TFieldValues>> {
|
311
|
+
/**
|
312
|
+
* The form schema.
|
313
|
+
*/
|
314
|
+
schema?: TSchema;
|
315
|
+
/**
|
316
|
+
* Triggers when any of the field change.
|
317
|
+
*/
|
318
|
+
onChange?: WatchObserver<TFieldValues>;
|
319
|
+
/**
|
320
|
+
* The submit handler.
|
321
|
+
*/
|
322
|
+
onSubmit: SubmitHandler<TFieldValues>;
|
323
|
+
/**
|
324
|
+
* Triggers when there are validation errors.
|
325
|
+
*/
|
326
|
+
onError?: SubmitErrorHandler<TFieldValues>;
|
327
|
+
/**
|
328
|
+
* The Hook Form state ref.
|
329
|
+
*/
|
330
|
+
formRef?: React$1.RefObject<UseFormReturn<TFieldValues, TContext>>;
|
331
|
+
/**
|
332
|
+
* The form children, can be a render prop or a ReactNode.
|
333
|
+
*/
|
334
|
+
children?: MaybeRenderProp<FormRenderContext<TFieldValues, TContext, TFieldTypes>>;
|
335
|
+
/**
|
336
|
+
* The field resolver, used to resolve the fields from schemas.
|
337
|
+
*/
|
338
|
+
fieldResolver?: FieldResolver;
|
339
|
+
/**
|
340
|
+
* Field overrides
|
341
|
+
*/
|
342
|
+
fields?: DefaultFieldOverrides;
|
343
|
+
}
|
344
|
+
interface FormProps<TFieldValues extends FieldValues = FieldValues, TContext extends object = object, TSchema = any, TFieldTypes = FieldProps<TFieldValues>> extends UseFormProps<TFieldValues, TContext>, Omit<HTMLChakraProps<'form'>, 'children' | 'onChange' | 'onSubmit' | 'onError'>, FormOptions<TFieldValues, TContext, TSchema, TFieldTypes> {
|
345
|
+
}
|
346
|
+
|
347
|
+
declare const ajvFieldResolver: (schema: JSONSchemaType<unknown>) => FieldResolver$1;
|
13
348
|
|
14
349
|
type ResolverArgs = Parameters<typeof ajvResolver>;
|
15
|
-
interface CreateAjvFormProps extends CreateFormProps {
|
350
|
+
interface CreateAjvFormProps<FieldDefs> extends CreateFormProps<FieldDefs> {
|
16
351
|
schemaOptions?: ResolverArgs[1];
|
17
352
|
resolverOptions?: ResolverArgs[2];
|
18
353
|
}
|
19
|
-
|
354
|
+
/**
|
355
|
+
* Create a Form component with AJV validation that accepts JSON Type Definition schema
|
356
|
+
*
|
357
|
+
* @see Docs https://saas-ui.dev/docs/components/forms/form
|
358
|
+
* @see https://ajv.js.org/json-type-definition.html
|
359
|
+
*/
|
360
|
+
declare function createAjvForm<FieldDefs>(options?: CreateAjvFormProps<FieldDefs>): <TSchema extends Record<string, any>, TContext extends object = object, TJSONSchema = JTDDataType<TSchema>>(props: WithFields$1<FormProps$1<TJSONSchema, TContext, TSchema, _saas_ui_forms.FieldProps<TJSONSchema>>, FieldDefs> & {
|
361
|
+
ref?: React$1.ForwardedRef<HTMLFormElement> | undefined;
|
362
|
+
}) => React.ReactElement;
|
363
|
+
|
364
|
+
declare const Form: <TSchema extends Record<string, any>, TContext extends object = object, TJSONSchema = ajv_dist_jtd.JTDDataType<TSchema>>(props: WithFields<FormProps<TJSONSchema, TContext, TSchema, FieldProps<TJSONSchema>>, unknown> & {
|
365
|
+
ref?: React$1.ForwardedRef<HTMLFormElement> | undefined;
|
366
|
+
}) => React$1.ReactElement<any, string | React$1.JSXElementConstructor<any>>;
|
20
367
|
|
21
|
-
export { CreateAjvFormProps,
|
368
|
+
export { CreateAjvFormProps, Form, ajvFieldResolver, createAjvForm };
|
package/dist/ajv/index.js
CHANGED
@@ -4,28 +4,26 @@ var ajv = require('@hookform/resolvers/ajv');
|
|
4
4
|
var forms = require('@saas-ui/forms');
|
5
5
|
|
6
6
|
// ajv/src/ajv-resolver.ts
|
7
|
-
var
|
7
|
+
var ajvFieldResolver = (schema) => {
|
8
8
|
return forms.objectFieldResolver(schema.properties);
|
9
9
|
};
|
10
|
-
var jsonSchemaForm = (schema) => {
|
11
|
-
return {
|
12
|
-
schema,
|
13
|
-
fieldResolver: jsonSchemaFieldResolver(schema),
|
14
|
-
resolver: ajv.ajvResolver(schema)
|
15
|
-
};
|
16
|
-
};
|
17
10
|
function createAjvForm(options) {
|
18
11
|
return forms.createForm({
|
19
12
|
resolver: (schema) => ajv.ajvResolver(schema, options == null ? void 0 : options.schemaOptions, options == null ? void 0 : options.resolverOptions),
|
13
|
+
fieldResolver: ajvFieldResolver,
|
20
14
|
...options
|
21
15
|
});
|
22
16
|
}
|
23
17
|
|
18
|
+
// ajv/src/index.ts
|
19
|
+
var Form = createAjvForm();
|
20
|
+
|
24
21
|
Object.defineProperty(exports, 'ajvResolver', {
|
25
22
|
enumerable: true,
|
26
23
|
get: function () { return ajv.ajvResolver; }
|
27
24
|
});
|
25
|
+
exports.Form = Form;
|
26
|
+
exports.ajvFieldResolver = ajvFieldResolver;
|
28
27
|
exports.createAjvForm = createAjvForm;
|
29
|
-
exports.jsonSchemaForm = jsonSchemaForm;
|
30
28
|
//# sourceMappingURL=out.js.map
|
31
29
|
//# sourceMappingURL=index.js.map
|
package/dist/ajv/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../ajv/src/ajv-resolver.ts","../../ajv/src/create-ajv-form.ts"],"names":[],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,2BAA0C;
|
1
|
+
{"version":3,"sources":["../../ajv/src/ajv-resolver.ts","../../ajv/src/create-ajv-form.ts","../../ajv/src/index.ts"],"names":[],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,2BAA0C;AAM5C,IAAM,mBAAmB,CAAC,WAAoC;AACnE,SAAO,oBAAoB,OAAO,UAAU;AAC9C;;;ACTA;AAAA,EACE;AAAA,OAIK;AAiBA,SAAS,cACd,SACA;AACA,SAAO,WAAgB;AAAA,IACrB,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAUH;;;ACjCO,IAAM,OAAO,cAAc","sourcesContent":["import { ajvResolver } from '@hookform/resolvers/ajv'\nimport { objectFieldResolver, FieldResolver } from '@saas-ui/forms'\n\nimport { JSONSchemaType } from 'ajv'\n\nexport { ajvResolver }\n\nexport const ajvFieldResolver = (schema: JSONSchemaType<unknown>) => {\n return objectFieldResolver(schema.properties)\n}\n","import {\n createForm,\n CreateFormProps,\n FormProps,\n WithFields,\n} from '@saas-ui/forms'\nimport { ajvFieldResolver, ajvResolver } from './ajv-resolver'\nimport { JTDDataType } from 'ajv/dist/jtd'\ntype ResolverArgs = Parameters<typeof ajvResolver>\n\nexport interface CreateAjvFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\n/**\n * Create a Form component with AJV validation that accepts JSON Type Definition schema\n *\n * @see Docs https://saas-ui.dev/docs/components/forms/form\n * @see https://ajv.js.org/json-type-definition.html\n */\nexport function createAjvForm<FieldDefs>(\n options?: CreateAjvFormProps<FieldDefs>\n) {\n return createForm<any>({\n resolver: (schema) =>\n ajvResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: ajvFieldResolver,\n ...options,\n }) as <\n TSchema extends Record<string, any>,\n TContext extends object = object,\n TJSONSchema = JTDDataType<TSchema>\n >(\n /** @ts-expect-error @todo properly fix these types */\n props: WithFields<FormProps<TJSONSchema, TContext, TSchema>, FieldDefs> & {\n ref?: React.ForwardedRef<HTMLFormElement>\n }\n ) => React.ReactElement\n}\n","export * from './ajv-resolver'\nexport { createAjvForm } from './create-ajv-form'\nexport type { CreateAjvFormProps } from './create-ajv-form'\nexport type { JTDDataType, JTDSchemaType } from 'ajv/dist/jtd'\n\nimport { createAjvForm } from './create-ajv-form'\n\nexport const Form = createAjvForm()\n"]}
|
package/dist/ajv/index.mjs
CHANGED
@@ -1,25 +1,22 @@
|
|
1
1
|
import { ajvResolver } from '@hookform/resolvers/ajv';
|
2
2
|
export { ajvResolver } from '@hookform/resolvers/ajv';
|
3
|
-
import {
|
3
|
+
import { objectFieldResolver, createForm } from '@saas-ui/forms';
|
4
4
|
|
5
5
|
// ajv/src/ajv-resolver.ts
|
6
|
-
var
|
6
|
+
var ajvFieldResolver = (schema) => {
|
7
7
|
return objectFieldResolver(schema.properties);
|
8
8
|
};
|
9
|
-
var jsonSchemaForm = (schema) => {
|
10
|
-
return {
|
11
|
-
schema,
|
12
|
-
fieldResolver: jsonSchemaFieldResolver(schema),
|
13
|
-
resolver: ajvResolver(schema)
|
14
|
-
};
|
15
|
-
};
|
16
9
|
function createAjvForm(options) {
|
17
10
|
return createForm({
|
18
11
|
resolver: (schema) => ajvResolver(schema, options == null ? void 0 : options.schemaOptions, options == null ? void 0 : options.resolverOptions),
|
12
|
+
fieldResolver: ajvFieldResolver,
|
19
13
|
...options
|
20
14
|
});
|
21
15
|
}
|
22
16
|
|
23
|
-
|
17
|
+
// ajv/src/index.ts
|
18
|
+
var Form = createAjvForm();
|
19
|
+
|
20
|
+
export { Form, ajvFieldResolver, createAjvForm };
|
24
21
|
//# sourceMappingURL=out.js.map
|
25
22
|
//# sourceMappingURL=index.mjs.map
|
package/dist/ajv/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../ajv/src/ajv-resolver.ts","../../ajv/src/create-ajv-form.ts"],"names":[],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,2BAA0C;
|
1
|
+
{"version":3,"sources":["../../ajv/src/ajv-resolver.ts","../../ajv/src/create-ajv-form.ts","../../ajv/src/index.ts"],"names":[],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,2BAA0C;AAM5C,IAAM,mBAAmB,CAAC,WAAoC;AACnE,SAAO,oBAAoB,OAAO,UAAU;AAC9C;;;ACTA;AAAA,EACE;AAAA,OAIK;AAiBA,SAAS,cACd,SACA;AACA,SAAO,WAAgB;AAAA,IACrB,UAAU,CAAC,WACT,YAAY,QAAQ,mCAAS,eAAe,mCAAS,eAAe;AAAA,IACtE,eAAe;AAAA,IACf,GAAG;AAAA,EACL,CAAC;AAUH;;;ACjCO,IAAM,OAAO,cAAc","sourcesContent":["import { ajvResolver } from '@hookform/resolvers/ajv'\nimport { objectFieldResolver, FieldResolver } from '@saas-ui/forms'\n\nimport { JSONSchemaType } from 'ajv'\n\nexport { ajvResolver }\n\nexport const ajvFieldResolver = (schema: JSONSchemaType<unknown>) => {\n return objectFieldResolver(schema.properties)\n}\n","import {\n createForm,\n CreateFormProps,\n FormProps,\n WithFields,\n} from '@saas-ui/forms'\nimport { ajvFieldResolver, ajvResolver } from './ajv-resolver'\nimport { JTDDataType } from 'ajv/dist/jtd'\ntype ResolverArgs = Parameters<typeof ajvResolver>\n\nexport interface CreateAjvFormProps<FieldDefs>\n extends CreateFormProps<FieldDefs> {\n schemaOptions?: ResolverArgs[1]\n resolverOptions?: ResolverArgs[2]\n}\n\n/**\n * Create a Form component with AJV validation that accepts JSON Type Definition schema\n *\n * @see Docs https://saas-ui.dev/docs/components/forms/form\n * @see https://ajv.js.org/json-type-definition.html\n */\nexport function createAjvForm<FieldDefs>(\n options?: CreateAjvFormProps<FieldDefs>\n) {\n return createForm<any>({\n resolver: (schema) =>\n ajvResolver(schema, options?.schemaOptions, options?.resolverOptions),\n fieldResolver: ajvFieldResolver,\n ...options,\n }) as <\n TSchema extends Record<string, any>,\n TContext extends object = object,\n TJSONSchema = JTDDataType<TSchema>\n >(\n /** @ts-expect-error @todo properly fix these types */\n props: WithFields<FormProps<TJSONSchema, TContext, TSchema>, FieldDefs> & {\n ref?: React.ForwardedRef<HTMLFormElement>\n }\n ) => React.ReactElement\n}\n","export * from './ajv-resolver'\nexport { createAjvForm } from './create-ajv-form'\nexport type { CreateAjvFormProps } from './create-ajv-form'\nexport type { JTDDataType, JTDSchemaType } from 'ajv/dist/jtd'\n\nimport { createAjvForm } from './create-ajv-form'\n\nexport const Form = createAjvForm()\n"]}
|