@saas-ui/forms 1.0.0-rc.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +118 -0
- package/README.md +29 -0
- package/dist/ajv/index.js +94 -94
- package/dist/ajv/index.js.map +1 -1
- package/dist/ajv/index.modern.mjs +94 -94
- package/dist/ajv/index.modern.mjs.map +1 -1
- package/dist/array-field.d.ts +14 -3
- package/dist/array-field.d.ts.map +1 -1
- package/dist/field.d.ts +81 -26
- 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/step-form.d.ts +4 -4
- package/dist/step-form.d.ts.map +1 -1
- package/dist/submit-button.d.ts +2 -1
- package/dist/submit-button.d.ts.map +1 -1
- package/dist/use-step-form.d.ts +6 -2
- package/dist/use-step-form.d.ts.map +1 -1
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/index.modern.mjs.map +1 -1
- package/dist/zod/zod-resolver.d.ts +2 -2
- package/dist/zod/zod-resolver.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/array-field.tsx +21 -22
- package/src/auto-form.tsx +2 -2
- package/src/field.tsx +183 -72
- package/src/form.tsx +0 -1
- package/src/step-form.tsx +13 -8
- package/src/submit-button.tsx +32 -24
- package/src/use-step-form.tsx +24 -10
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>
|
@@ -34,9 +42,7 @@ export interface UseStepFormProps<
|
|
34
42
|
TFieldValues extends FieldValues = FieldValues
|
35
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<
|
@@ -63,11 +69,12 @@ export function useStepForm<TFieldValues extends FieldValues = FieldValues>(
|
|
63
69
|
|
64
70
|
const onSubmitStep: SubmitHandler<TFieldValues> = React.useCallback(
|
65
71
|
async (data) => {
|
72
|
+
const step = steps[activeStep]
|
73
|
+
|
66
74
|
if (isLastStep) {
|
67
75
|
return props
|
68
76
|
.onSubmit?.(data)
|
69
77
|
.then(() => {
|
70
|
-
const step = steps[activeStep]
|
71
78
|
updateStep({
|
72
79
|
...step,
|
73
80
|
isCompleted: true,
|
@@ -76,9 +83,15 @@ export function useStepForm<TFieldValues extends FieldValues = FieldValues>(
|
|
76
83
|
.then(nextStep) // Show completed step
|
77
84
|
}
|
78
85
|
|
79
|
-
|
86
|
+
try {
|
87
|
+
await step.onSubmit?.(data, stepper)
|
88
|
+
|
89
|
+
nextStep()
|
90
|
+
} catch (e) {
|
91
|
+
// Step submission failed.
|
92
|
+
}
|
80
93
|
},
|
81
|
-
[activeStep, isLastStep]
|
94
|
+
[steps, activeStep, isLastStep]
|
82
95
|
)
|
83
96
|
|
84
97
|
const getFormProps = React.useCallback(() => {
|
@@ -114,16 +127,17 @@ export interface UseFormStepProps {
|
|
114
127
|
name: string
|
115
128
|
schema?: any
|
116
129
|
resolver?: any
|
130
|
+
onSubmit?: FormStepSubmitHandler
|
117
131
|
}
|
118
132
|
|
119
133
|
export function useFormStep(props: UseFormStepProps): StepState {
|
120
|
-
const { name, schema, resolver } = props
|
134
|
+
const { name, schema, resolver, onSubmit } = props
|
121
135
|
const step = useStep({ name })
|
122
136
|
|
123
137
|
const { steps, updateStep } = useStepFormContext()
|
124
138
|
|
125
139
|
React.useEffect(() => {
|
126
|
-
updateStep({ name, schema, resolver })
|
140
|
+
updateStep({ name, schema, resolver, onSubmit })
|
127
141
|
}, [name, schema])
|
128
142
|
|
129
143
|
return {
|