@saas-ui/forms 0.5.0 → 0.6.0-next.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.
@@ -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
- }