@saas-ui/forms 2.1.3 → 2.2.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.
- package/CHANGELOG.md +22 -0
- package/dist/index.d.ts +41 -29
- package/dist/index.js +72 -46
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +73 -51
- package/dist/index.mjs.map +1 -1
- package/dist/yup/index.d.ts +57 -41
- package/dist/yup/index.js.map +1 -1
- package/dist/yup/index.mjs.map +1 -1
- package/dist/zod/index.d.ts +58 -46
- package/dist/zod/index.js +17 -5
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/index.mjs +17 -5
- package/dist/zod/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/create-field.tsx +7 -3
- package/src/create-form.tsx +9 -3
- package/src/default-fields.tsx +12 -4
- package/src/display-field.tsx +4 -3
- package/src/fields.tsx +7 -1
- package/src/index.ts +1 -0
- package/src/layout.tsx +2 -13
- package/src/number-input/number-input.tsx +14 -1
- package/src/types.ts +1 -1
- package/src/use-step-form.tsx +20 -4
package/src/default-fields.tsx
CHANGED
@@ -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
|
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} {...
|
90
|
-
<SelectButton />
|
91
|
-
<SelectList />
|
97
|
+
<Select ref={ref} {...rest}>
|
98
|
+
<SelectButton {...buttonProps} />
|
99
|
+
<SelectList {...listProps} />
|
92
100
|
</Select>
|
93
101
|
)
|
94
102
|
}),
|
package/src/display-field.tsx
CHANGED
@@ -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
|
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
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
|
-
{
|
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
|
*/
|
package/src/use-step-form.tsx
CHANGED
@@ -89,7 +89,13 @@ export function useStepForm<
|
|
89
89
|
>(
|
90
90
|
props: UseStepFormProps<TSteps, TFieldValues, TContext>
|
91
91
|
): UseStepFormReturn<TFieldValues> {
|
92
|
-
const {
|
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?.(
|
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) => {
|