@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.
@@ -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
- nextStep()
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 {