@saas-ui/forms 2.1.3 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,6 +29,8 @@ import {
29
29
  SelectProps,
30
30
  NativeSelect,
31
31
  NativeSelectProps,
32
+ SelectButtonProps,
33
+ SelectListProps,
32
34
  } from './select'
33
35
 
34
36
  import { createField } from './create-field'
@@ -83,12 +85,18 @@ export const SwitchField = createField<SwitchProps>(
83
85
  }
84
86
  )
85
87
 
86
- export const SelectField = createField<SelectProps>(
88
+ export interface SelectFieldProps extends SelectProps {
89
+ buttonProps?: SelectButtonProps
90
+ listProps?: SelectListProps
91
+ }
92
+
93
+ export const SelectField = createField<SelectFieldProps>(
87
94
  forwardRef((props, ref) => {
95
+ const { buttonProps, listProps, ...rest } = props
88
96
  return (
89
- <Select ref={ref} {...props}>
90
- <SelectButton />
91
- <SelectList />
97
+ <Select ref={ref} {...rest}>
98
+ <SelectButton {...buttonProps} />
99
+ <SelectList {...listProps} />
92
100
  </Select>
93
101
  )
94
102
  }),
@@ -10,10 +10,11 @@ import {
10
10
  import { FieldProps } from './types'
11
11
 
12
12
  export interface DisplayFieldProps
13
- extends FormControlProps,
14
- Omit<FieldProps, 'type' | 'label'> {}
13
+ extends Omit<FormControlProps, 'onChange'>,
14
+ Omit<FieldProps, 'type' | 'label' | 'onChange'> {}
15
+
15
16
  /**
16
- *
17
+ * Display a field value.
17
18
  *
18
19
  * @see Docs https://saas-ui.dev/
19
20
  */
package/src/fields.tsx CHANGED
@@ -82,7 +82,13 @@ export const AutoFields: React.FC<FieldsProps> = ({
82
82
  }
83
83
 
84
84
  return (
85
- <Field key={name} name={name} type={type as any} {...fieldProps} />
85
+ <Field
86
+ key={name}
87
+ name={name}
88
+ type={type as any}
89
+ defaultValue={defaultValue}
90
+ {...fieldProps}
91
+ />
86
92
  )
87
93
  }
88
94
  )}
package/src/index.ts CHANGED
@@ -112,6 +112,7 @@ export {
112
112
  type InputFieldProps,
113
113
  type NumberInputFieldProps,
114
114
  type PinFieldProps,
115
+ type SelectFieldProps,
115
116
  } from './default-fields'
116
117
 
117
118
  export type {
package/src/layout.tsx CHANGED
@@ -1,12 +1,6 @@
1
1
  import * as React from 'react'
2
2
 
3
- import {
4
- chakra,
5
- ResponsiveValue,
6
- SimpleGrid,
7
- SimpleGridProps,
8
- useTheme,
9
- } from '@chakra-ui/react'
3
+ import { chakra, SimpleGrid, SimpleGridProps, useTheme } from '@chakra-ui/react'
10
4
  import { cx } from '@chakra-ui/utils'
11
5
 
12
6
  export interface FormLayoutProps extends SimpleGridProps {}
@@ -47,12 +41,7 @@ export const FormLayout = ({ children, ...props }: FormLayoutProps) => {
47
41
  {...gridProps}
48
42
  className={cx('sui-form__layout', props.className)}
49
43
  >
50
- {React.Children.map(children, (child) => {
51
- if (React.isValidElement(child)) {
52
- return <FormLayoutItem>{child}</FormLayoutItem>
53
- }
54
- return child
55
- })}
44
+ {children}
56
45
  </SimpleGrid>
57
46
  )
58
47
  }
@@ -8,6 +8,7 @@ import {
8
8
  NumberIncrementStepper,
9
9
  NumberDecrementStepper,
10
10
  NumberInputProps as ChakraNumberInputProps,
11
+ NumberInputFieldProps,
11
12
  } from '@chakra-ui/react'
12
13
 
13
14
  import { ChevronDownIcon, ChevronUpIcon } from '@saas-ui/core'
@@ -25,6 +26,14 @@ interface NumberInputOptions {
25
26
  * Render a custom decrement icon.
26
27
  */
27
28
  decrementIcon?: React.ReactNode
29
+ /**
30
+ * The placeholder text when no value is selected.
31
+ */
32
+ placeholder?: string
33
+ /**
34
+ * Props to pass to the NumberInputField component.
35
+ */
36
+ fieldProps?: NumberInputFieldProps
28
37
  }
29
38
 
30
39
  export interface NumberInputProps
@@ -36,12 +45,16 @@ export const NumberInput = forwardRef<NumberInputProps, 'div'>((props, ref) => {
36
45
  hideStepper,
37
46
  incrementIcon = <ChevronUpIcon />,
38
47
  decrementIcon = <ChevronDownIcon />,
48
+ placeholder,
49
+ fieldProps: _fieldProps,
39
50
  ...rest
40
51
  } = props
41
52
 
53
+ const fieldProps = { placeholder, ..._fieldProps }
54
+
42
55
  return (
43
56
  <ChakraNumberInput {...rest} ref={ref}>
44
- <NumberInputField />
57
+ <NumberInputField {...fieldProps} />
45
58
 
46
59
  {!hideStepper && (
47
60
  <NumberInputStepper>
package/src/types.ts CHANGED
@@ -42,7 +42,7 @@ export type ArrayFieldPath<Name extends string> = Name extends string
42
42
  export interface BaseFieldProps<
43
43
  TFieldValues extends FieldValues = FieldValues,
44
44
  TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
45
- > extends Omit<FormControlProps, 'label' | 'type'> {
45
+ > extends Omit<FormControlProps, 'label' | 'type' | 'onChange'> {
46
46
  /**
47
47
  * The field name
48
48
  */
@@ -89,7 +89,13 @@ export function useStepForm<
89
89
  >(
90
90
  props: UseStepFormProps<TSteps, TFieldValues, TContext>
91
91
  ): UseStepFormReturn<TFieldValues> {
92
- const { onChange, steps: stepsOptions, resolver, ...rest } = props
92
+ const {
93
+ onChange,
94
+ steps: stepsOptions,
95
+ resolver,
96
+ fieldResolver,
97
+ ...rest
98
+ } = props
93
99
  const stepper = useStepper(rest)
94
100
 
95
101
  const [options, setOptions] = React.useState<TSteps | undefined>(stepsOptions)
@@ -98,13 +104,20 @@ export function useStepForm<
98
104
 
99
105
  const [steps, updateSteps] = React.useState<Record<string, StepState>>({})
100
106
 
107
+ const mergedData = React.useRef<TFieldValues>({} as any)
108
+
101
109
  const onSubmitStep: SubmitHandler<TFieldValues> = React.useCallback(
102
110
  async (data) => {
103
111
  try {
104
112
  const step = steps[activeStep]
105
113
 
114
+ mergedData.current = {
115
+ ...mergedData.current,
116
+ ...data,
117
+ }
118
+
106
119
  if (isLastStep) {
107
- await props.onSubmit?.(data)
120
+ await props.onSubmit?.(mergedData.current)
108
121
 
109
122
  updateStep({
110
123
  ...step,
@@ -122,7 +135,7 @@ export function useStepForm<
122
135
  // Step submission failed.
123
136
  }
124
137
  },
125
- [steps, activeStep, isLastStep]
138
+ [steps, activeStep, isLastStep, mergedData]
126
139
  )
127
140
 
128
141
  const getFormProps = React.useCallback(() => {
@@ -134,8 +147,11 @@ export function useStepForm<
134
147
  resolver: step?.schema
135
148
  ? /* @todo fix resolver type */ (resolver as any)?.(step.schema)
136
149
  : undefined,
150
+ fieldResolver: step?.schema
151
+ ? (fieldResolver as any)?.(step.schema)
152
+ : undefined,
137
153
  }
138
- }, [steps, onSubmitStep, activeStep])
154
+ }, [steps, onSubmitStep, activeStep, resolver, fieldResolver])
139
155
 
140
156
  const updateStep = React.useCallback(
141
157
  (step: StepState) => {