@saas-ui/forms 0.5.0 → 0.6.0-next.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +51 -0
- package/dist/array-field.d.ts +3 -3
- package/dist/array-field.d.ts.map +1 -1
- package/dist/auto-form.d.ts +2 -1
- package/dist/auto-form.d.ts.map +1 -1
- package/dist/field-resolver.d.ts +8 -0
- package/dist/field-resolver.d.ts.map +1 -0
- package/dist/field.d.ts +3 -2
- package/dist/field.d.ts.map +1 -1
- package/dist/fields.d.ts +2 -0
- package/dist/fields.d.ts.map +1 -1
- package/dist/form.d.ts +3 -3
- package/dist/form.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +1 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/step-form.d.ts +5 -1
- package/dist/step-form.d.ts.map +1 -1
- package/dist/use-array-field.d.ts +1 -1
- package/dist/use-step-form.d.ts +3 -0
- package/dist/use-step-form.d.ts.map +1 -1
- package/package.json +21 -13
- package/src/array-field.tsx +56 -45
- package/src/auto-form.tsx +6 -5
- package/src/field-resolver.ts +39 -0
- package/src/field.tsx +3 -2
- package/src/fields.tsx +45 -30
- package/src/form.tsx +6 -13
- package/src/index.ts +1 -1
- package/src/step-form.tsx +8 -3
- package/src/use-array-field.tsx +1 -1
- package/src/use-step-form.tsx +6 -3
- package/yup/package.json +17 -0
- package/dist/resolvers/yup.d.ts +0 -12
- package/dist/resolvers/yup.d.ts.map +0 -1
- package/src/resolvers/yup.ts +0 -79
package/src/resolvers/yup.ts
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
import { SchemaOf, AnySchema, reach } from 'yup'
|
2
|
-
export { yupResolver } from '@hookform/resolvers/yup'
|
3
|
-
|
4
|
-
import { FieldProps } from '../field'
|
5
|
-
|
6
|
-
// @TODO get proper typings for the schema fields
|
7
|
-
|
8
|
-
const getType = (field: any) => {
|
9
|
-
if (field.spec.meta?.type) {
|
10
|
-
return field.spec.meta.type
|
11
|
-
}
|
12
|
-
|
13
|
-
switch (field.type) {
|
14
|
-
case 'array':
|
15
|
-
return 'array'
|
16
|
-
case 'object':
|
17
|
-
return 'object'
|
18
|
-
case 'number':
|
19
|
-
return 'number'
|
20
|
-
case 'date':
|
21
|
-
return 'date'
|
22
|
-
case 'string':
|
23
|
-
default:
|
24
|
-
return 'text'
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
type Options = {
|
29
|
-
min?: number
|
30
|
-
max?: number
|
31
|
-
}
|
32
|
-
|
33
|
-
const getArrayOption = (field: any, name: string) => {
|
34
|
-
for (const test of field.tests) {
|
35
|
-
if (test.OPTIONS?.params[name]) return test.OPTIONS.params[name]
|
36
|
-
}
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* A helper function to render forms automatically based on a Yup schema
|
41
|
-
*
|
42
|
-
* @param schema The Yup schema
|
43
|
-
* @returns {FieldProps[]}
|
44
|
-
*/
|
45
|
-
export const getFieldsFromSchema = (
|
46
|
-
schema: SchemaOf<AnySchema>
|
47
|
-
): FieldProps[] => {
|
48
|
-
const fields = []
|
49
|
-
|
50
|
-
let schemaFields: Record<string, any> = {}
|
51
|
-
if (schema.type === 'array') {
|
52
|
-
/* @ts-ignore this is actually valid */
|
53
|
-
schemaFields = schema.innerType.fields
|
54
|
-
} else {
|
55
|
-
schemaFields = schema.fields
|
56
|
-
}
|
57
|
-
|
58
|
-
for (const name in schemaFields) {
|
59
|
-
const field = schemaFields[name]
|
60
|
-
|
61
|
-
const options: Options = {}
|
62
|
-
if (field.type === 'array') {
|
63
|
-
options.min = getArrayOption(field, 'min')
|
64
|
-
options.max = getArrayOption(field, 'max')
|
65
|
-
}
|
66
|
-
|
67
|
-
fields.push({
|
68
|
-
name,
|
69
|
-
label: field.spec.label || name,
|
70
|
-
type: getType(field),
|
71
|
-
...options,
|
72
|
-
})
|
73
|
-
}
|
74
|
-
return fields
|
75
|
-
}
|
76
|
-
|
77
|
-
export const getNestedSchema = (schema: SchemaOf<AnySchema>, path: string) => {
|
78
|
-
return reach(schema, path)
|
79
|
-
}
|