@saas-ui/forms 1.0.0-rc.8 → 1.0.1

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.
@@ -4,7 +4,6 @@ import { useFormContext } from 'react-hook-form'
4
4
 
5
5
  import { Button, ButtonProps } from '@saas-ui/button'
6
6
 
7
- import { forwardRef } from '@chakra-ui/system'
8
7
  import { __DEV__ } from '@chakra-ui/utils'
9
8
 
10
9
  export interface SubmitButtonProps extends ButtonProps {
@@ -24,29 +23,38 @@ export interface SubmitButtonProps extends ButtonProps {
24
23
  disableIfInvalid?: boolean
25
24
  }
26
25
 
27
- export const SubmitButton = forwardRef<SubmitButtonProps, 'button'>(
28
- (props, ref) => {
29
- const { children, disableIfUntouched, disableIfInvalid, ...rest } = props
30
- const { formState } = useFormContext()
31
-
32
- const isDisabled =
33
- (disableIfUntouched && !formState.isDirty) ||
34
- (disableIfInvalid && !formState.isValid)
35
-
36
- return (
37
- <Button
38
- type="submit"
39
- isLoading={formState.isSubmitting}
40
- colorScheme="primary"
41
- ref={ref}
42
- isDisabled={isDisabled}
43
- {...rest}
44
- >
45
- {children}
46
- </Button>
47
- )
48
- }
49
- )
26
+ export const SubmitButton = React.forwardRef<
27
+ HTMLButtonElement,
28
+ SubmitButtonProps
29
+ >((props, ref) => {
30
+ const {
31
+ children,
32
+ disableIfUntouched,
33
+ disableIfInvalid,
34
+ isDisabled: isDisabledProp,
35
+ isLoading,
36
+ ...rest
37
+ } = props
38
+ const { formState } = useFormContext()
39
+
40
+ const isDisabled =
41
+ (disableIfUntouched && !formState.isDirty) ||
42
+ (disableIfInvalid && !formState.isValid) ||
43
+ isDisabledProp
44
+
45
+ return (
46
+ <Button
47
+ {...rest}
48
+ ref={ref}
49
+ variant="primary"
50
+ type="submit"
51
+ isLoading={formState.isSubmitting || isLoading}
52
+ isDisabled={isDisabled}
53
+ >
54
+ {children}
55
+ </Button>
56
+ )
57
+ })
50
58
 
51
59
  SubmitButton.defaultProps = {
52
60
  label: 'Submit',
@@ -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 {