@saas-ui/forms 1.0.0-rc.1 → 1.0.0-rc.10
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +107 -0
- package/README.md +29 -0
- package/ajv/package.json +18 -0
- package/dist/ajv/ajv-resolver.d.ts +11 -0
- package/dist/ajv/ajv-resolver.d.ts.map +1 -0
- package/dist/ajv/index.d.ts +2 -0
- package/dist/ajv/index.d.ts.map +1 -0
- package/dist/ajv/index.js +97 -0
- package/dist/ajv/index.js.map +1 -0
- package/dist/ajv/index.modern.mjs +97 -0
- package/dist/ajv/index.modern.mjs.map +1 -0
- package/dist/array-field.d.ts +14 -3
- package/dist/array-field.d.ts.map +1 -1
- package/dist/auto-form.d.ts +13 -1
- package/dist/auto-form.d.ts.map +1 -1
- package/dist/field.d.ts +82 -27
- package/dist/field.d.ts.map +1 -1
- package/dist/form.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.modern.mjs +1 -1
- package/dist/index.modern.mjs.map +1 -1
- package/dist/layout.d.ts +0 -1
- package/dist/layout.d.ts.map +1 -1
- package/dist/step-form.d.ts +4 -4
- package/dist/step-form.d.ts.map +1 -1
- package/dist/use-step-form.d.ts +7 -3
- package/dist/use-step-form.d.ts.map +1 -1
- package/dist/zod/index.js +1 -1
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/index.modern.mjs +1 -1
- package/dist/zod/index.modern.mjs.map +1 -1
- package/package.json +32 -17
- package/src/array-field.tsx +21 -22
- package/src/auto-form.tsx +22 -3
- package/src/field-resolver.ts +2 -2
- package/src/field.tsx +186 -71
- package/src/form.tsx +0 -1
- package/src/object-field.tsx +3 -3
- package/src/step-form.tsx +23 -17
- package/src/submit-button.tsx +1 -1
- package/src/use-step-form.tsx +27 -12
- package/yup/package.json +2 -2
- package/zod/package.json +4 -4
package/src/use-step-form.tsx
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import * as React from 'react'
|
2
|
-
import { FieldValues, SubmitHandler } from 'react-hook-form'
|
3
|
-
import { createContext } from '@chakra-ui/react-utils'
|
2
|
+
import { FieldValues, SubmitHandler, UnpackNestedValue } from 'react-hook-form'
|
3
|
+
import { createContext, MaybeRenderProp } from '@chakra-ui/react-utils'
|
4
4
|
import {
|
5
5
|
useStepper,
|
6
6
|
useStep,
|
@@ -14,8 +14,16 @@ export interface StepState {
|
|
14
14
|
resolver?: any
|
15
15
|
isActive?: boolean
|
16
16
|
isCompleted?: boolean
|
17
|
+
onSubmit?: FormStepSubmitHandler
|
17
18
|
}
|
18
19
|
|
20
|
+
export type FormStepSubmitHandler<
|
21
|
+
TFieldValues extends FieldValues = FieldValues
|
22
|
+
> = (
|
23
|
+
data: UnpackNestedValue<TFieldValues>,
|
24
|
+
stepper: UseStepperReturn
|
25
|
+
) => Promise<void>
|
26
|
+
|
19
27
|
export interface StepFormContext extends UseStepperReturn {
|
20
28
|
updateStep(state: StepState): void
|
21
29
|
steps: Record<string, StepState>
|
@@ -32,11 +40,9 @@ import { FormProps } from './form'
|
|
32
40
|
|
33
41
|
export interface UseStepFormProps<
|
34
42
|
TFieldValues extends FieldValues = FieldValues
|
35
|
-
> extends UseStepperProps,
|
43
|
+
> extends Omit<UseStepperProps, 'onChange'>,
|
36
44
|
Omit<FormProps<TFieldValues>, 'children'> {
|
37
|
-
children:
|
38
|
-
| React.ReactNode
|
39
|
-
| ((stepper: UseStepFormReturn<TFieldValues>) => React.ReactElement)
|
45
|
+
children: MaybeRenderProp<UseStepFormReturn<TFieldValues>>
|
40
46
|
}
|
41
47
|
|
42
48
|
export interface UseStepFormReturn<
|
@@ -54,7 +60,8 @@ export interface UseStepFormReturn<
|
|
54
60
|
export function useStepForm<TFieldValues extends FieldValues = FieldValues>(
|
55
61
|
props: UseStepFormProps<TFieldValues>
|
56
62
|
): UseStepFormReturn<TFieldValues> {
|
57
|
-
const
|
63
|
+
const { onChange, ...rest } = props
|
64
|
+
const stepper = useStepper(rest)
|
58
65
|
|
59
66
|
const { activeStep, isLastStep, nextStep } = stepper
|
60
67
|
|
@@ -62,11 +69,12 @@ export function useStepForm<TFieldValues extends FieldValues = FieldValues>(
|
|
62
69
|
|
63
70
|
const onSubmitStep: SubmitHandler<TFieldValues> = React.useCallback(
|
64
71
|
async (data) => {
|
72
|
+
const step = steps[activeStep]
|
73
|
+
|
65
74
|
if (isLastStep) {
|
66
75
|
return props
|
67
76
|
.onSubmit?.(data)
|
68
77
|
.then(() => {
|
69
|
-
const step = steps[activeStep]
|
70
78
|
updateStep({
|
71
79
|
...step,
|
72
80
|
isCompleted: true,
|
@@ -75,9 +83,15 @@ export function useStepForm<TFieldValues extends FieldValues = FieldValues>(
|
|
75
83
|
.then(nextStep) // Show completed step
|
76
84
|
}
|
77
85
|
|
78
|
-
|
86
|
+
try {
|
87
|
+
await step.onSubmit?.(data, stepper)
|
88
|
+
|
89
|
+
nextStep()
|
90
|
+
} catch (e) {
|
91
|
+
// Step submission failed.
|
92
|
+
}
|
79
93
|
},
|
80
|
-
[activeStep, isLastStep]
|
94
|
+
[steps, activeStep, isLastStep]
|
81
95
|
)
|
82
96
|
|
83
97
|
const getFormProps = React.useCallback(() => {
|
@@ -113,16 +127,17 @@ export interface UseFormStepProps {
|
|
113
127
|
name: string
|
114
128
|
schema?: any
|
115
129
|
resolver?: any
|
130
|
+
onSubmit?: FormStepSubmitHandler
|
116
131
|
}
|
117
132
|
|
118
133
|
export function useFormStep(props: UseFormStepProps): StepState {
|
119
|
-
const { name, schema, resolver } = props
|
134
|
+
const { name, schema, resolver, onSubmit } = props
|
120
135
|
const step = useStep({ name })
|
121
136
|
|
122
137
|
const { steps, updateStep } = useStepFormContext()
|
123
138
|
|
124
139
|
React.useEffect(() => {
|
125
|
-
updateStep({ name, schema, resolver })
|
140
|
+
updateStep({ name, schema, resolver, onSubmit })
|
126
141
|
}, [name, schema])
|
127
142
|
|
128
143
|
return {
|
package/yup/package.json
CHANGED
@@ -10,8 +10,8 @@
|
|
10
10
|
"author": "Eelco Wiersma <eelco@appulse.nl>",
|
11
11
|
"license": "MIT",
|
12
12
|
"peerDependencies": {
|
13
|
-
"@hookform/resolvers": "^2.
|
14
|
-
"react-hook-form": "^7.
|
13
|
+
"@hookform/resolvers": "^2.9.0",
|
14
|
+
"react-hook-form": "^7.31.3",
|
15
15
|
"yup": "^0.32.11"
|
16
16
|
}
|
17
17
|
}
|
package/zod/package.json
CHANGED
@@ -10,9 +10,9 @@
|
|
10
10
|
"author": "Eelco Wiersma <eelco@appulse.nl>",
|
11
11
|
"license": "MIT",
|
12
12
|
"peerDependencies": {
|
13
|
-
"@chakra-ui/utils": "^2.0.
|
14
|
-
"@hookform/resolvers": "^2.
|
15
|
-
"react-hook-form": "^7.
|
16
|
-
"zod": "^3.
|
13
|
+
"@chakra-ui/utils": "^2.0.2",
|
14
|
+
"@hookform/resolvers": "^2.9.0",
|
15
|
+
"react-hook-form": "^7.31.3",
|
16
|
+
"zod": "^3.17.3"
|
17
17
|
}
|
18
18
|
}
|