@saas-ui/forms 2.4.1 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
package/src/types.ts CHANGED
@@ -20,10 +20,10 @@ export type ShallowMerge<A, B> = Omit<A, keyof B> & B
20
20
  type Split<S extends string, D extends string> = string extends S
21
21
  ? string[]
22
22
  : S extends ''
23
- ? []
24
- : S extends `${infer T}${D}${infer U}`
25
- ? [T, ...Split<U, D>]
26
- : [S]
23
+ ? []
24
+ : S extends `${infer T}${D}${infer U}`
25
+ ? [T, ...Split<U, D>]
26
+ : [S]
27
27
 
28
28
  type MapPath<T extends string[]> = T extends [infer U, ...infer R]
29
29
  ? U extends string
@@ -41,7 +41,7 @@ export type ArrayFieldPath<Name extends string> = Name extends string
41
41
 
42
42
  export interface BaseFieldProps<
43
43
  TFieldValues extends FieldValues = FieldValues,
44
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
44
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
45
45
  > extends Omit<FormControlProps, 'label' | 'type' | 'onChange'> {
46
46
  /**
47
47
  * The field name
@@ -79,28 +79,43 @@ export interface BaseFieldProps<
79
79
  placeholder?: string
80
80
  }
81
81
 
82
+ export type GetBaseField<TProps extends object = object> = () => {
83
+ extraProps: string[]
84
+ BaseField: React.FC<
85
+ Omit<BaseFieldProps, 'name'> & {
86
+ name: string
87
+ children: React.ReactNode
88
+ } & TProps
89
+ >
90
+ }
91
+
82
92
  type FieldPathWithArray<
83
93
  TFieldValues extends FieldValues,
84
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
94
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
85
95
  > = TName | ArrayFieldPath<TName>
86
96
 
87
97
  export type MergeFieldProps<
88
98
  FieldDefs,
89
99
  TFieldValues extends FieldValues = FieldValues,
90
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
100
+ TExtraFieldProps extends object = object,
101
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
91
102
  > = ValueOf<{
92
103
  [K in keyof FieldDefs]: FieldDefs[K] extends React.FC<infer Props>
93
- ? { type?: K } & ShallowMerge<Props, BaseFieldProps<TFieldValues, TName>>
104
+ ? { type?: K } & ShallowMerge<Props, BaseFieldProps<TFieldValues, TName>> &
105
+ TExtraFieldProps
94
106
  : never
95
107
  }>
96
108
 
97
- export type FieldProps<TFieldValues extends FieldValues = FieldValues> =
98
- MergeFieldProps<DefaultFields, TFieldValues>
109
+ export type FieldProps<
110
+ TFieldValues extends FieldValues = FieldValues,
111
+ TExtraFieldProps extends object = object,
112
+ > = MergeFieldProps<DefaultFields, TFieldValues, TExtraFieldProps>
99
113
 
100
114
  export type FormChildren<
101
115
  FieldDefs,
102
116
  TFieldValues extends FieldValues = FieldValues,
103
- TContext extends object = object
117
+ TContext extends object = object,
118
+ TExtraFieldProps extends object = object,
104
119
  > = MaybeRenderProp<
105
120
  FormRenderContext<
106
121
  TFieldValues,
@@ -109,7 +124,8 @@ export type FormChildren<
109
124
  FieldDefs extends never
110
125
  ? DefaultFields
111
126
  : ShallowMerge<DefaultFields, FieldDefs>,
112
- TFieldValues
127
+ TFieldValues,
128
+ TExtraFieldProps
113
129
  >
114
130
  >
115
131
  >
@@ -122,7 +138,7 @@ export type DefaultFieldOverrides = {
122
138
  type MergeOverrideFieldProps<
123
139
  FieldDefs,
124
140
  TFieldValues extends FieldValues = FieldValues,
125
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
141
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
126
142
  > = ValueOf<{
127
143
  [K in keyof FieldDefs]: FieldDefs[K] extends React.FC<infer Props>
128
144
  ? { type?: K } & Omit<
@@ -135,7 +151,7 @@ type MergeOverrideFieldProps<
135
151
  export type FieldOverrides<
136
152
  FieldDefs,
137
153
  TFieldValues extends FieldValues = FieldValues,
138
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
154
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
139
155
  > = {
140
156
  [K in FieldPathWithArray<TFieldValues, TName>]?:
141
157
  | MergeOverrideFieldProps<
@@ -155,28 +171,35 @@ export type FieldOverrides<
155
171
  }
156
172
 
157
173
  export type WithFields<
158
- TFormProps extends FormProps<any, any, any, any>,
174
+ TFormProps extends FormProps<any, any, any, any, any>,
159
175
  FieldDefs,
160
- ExtraOverrides = object
161
- > = TFormProps extends FormProps<
162
- infer TSchema,
163
- infer TFieldValues,
164
- infer TContext
165
- >
166
- ? Omit<TFormProps, 'children' | 'fields'> & {
167
- children?: FormChildren<FieldDefs, TFieldValues, TContext>
168
- fields?: FieldOverrides<FieldDefs, TFieldValues> & {
169
- submit?: SubmitButtonProps
170
- } & ExtraOverrides
171
- }
172
- : never
176
+ ExtraOverrides = object,
177
+ > =
178
+ TFormProps extends FormProps<
179
+ infer TSchema,
180
+ infer TFieldValues,
181
+ infer TContext,
182
+ infer TExtraFieldProps
183
+ >
184
+ ? Omit<TFormProps, 'children' | 'fields'> & {
185
+ children?: FormChildren<
186
+ FieldDefs,
187
+ TFieldValues,
188
+ TContext,
189
+ TExtraFieldProps
190
+ >
191
+ fields?: FieldOverrides<FieldDefs, TFieldValues> & {
192
+ submit?: SubmitButtonProps
193
+ } & ExtraOverrides
194
+ }
195
+ : never
173
196
 
174
197
  // StepForm types
175
198
  export type StepFormChildren<
176
199
  FieldDefs,
177
200
  TSteps extends StepsOptions<any> = StepsOptions<any>,
178
201
  TFieldValues extends FieldValues = FieldValues,
179
- TContext extends object = object
202
+ TContext extends object = object,
180
203
  > = MaybeRenderProp<
181
204
  StepFormRenderContext<
182
205
  TSteps,
@@ -194,16 +217,17 @@ export type StepFormChildren<
194
217
  export type WithStepFields<
195
218
  TStepFormProps extends UseStepFormProps<any, any, any>,
196
219
  FieldDefs,
197
- ExtraOverrides = object
198
- > = TStepFormProps extends UseStepFormProps<
199
- infer TSteps,
200
- infer TFieldValues,
201
- infer TContext
202
- >
203
- ? Omit<TStepFormProps, 'children' | 'fields'> & {
204
- children?: StepFormChildren<FieldDefs, TSteps, TFieldValues, TContext>
205
- fields?: FieldOverrides<FieldDefs, TFieldValues> & {
206
- submit?: SubmitButtonProps
207
- } & ExtraOverrides
208
- }
209
- : never
220
+ ExtraOverrides = object,
221
+ > =
222
+ TStepFormProps extends UseStepFormProps<
223
+ infer TSteps,
224
+ infer TFieldValues,
225
+ infer TContext
226
+ >
227
+ ? Omit<TStepFormProps, 'children' | 'fields'> & {
228
+ children?: StepFormChildren<FieldDefs, TSteps, TFieldValues, TContext>
229
+ fields?: FieldOverrides<FieldDefs, TFieldValues> & {
230
+ submit?: SubmitButtonProps
231
+ } & ExtraOverrides
232
+ }
233
+ : never
@@ -64,7 +64,7 @@ export const [ArrayFieldRowProvider, useArrayFieldRowContext] =
64
64
 
65
65
  export interface ArrayFieldOptions<
66
66
  TFieldValues extends FieldValues = FieldValues,
67
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
67
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
68
68
  > {
69
69
  /**
70
70
  * The field name
@@ -91,12 +91,13 @@ export const useArrayField = ({
91
91
  max,
92
92
  }: ArrayFieldOptions) => {
93
93
  const { control } = useFormContext()
94
+
94
95
  const context = useFieldArray({
95
96
  control,
96
97
  name,
97
98
  keyName,
98
99
  })
99
-
100
+ console.log(context)
100
101
  return {
101
102
  ...context,
102
103
  name,