@rilaykit/forms 3.0.0 → 5.0.0
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/dist/index.d.mts +469 -70
- package/dist/index.d.ts +469 -70
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,124 +1,536 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { ril,
|
|
2
|
+
import { ril, FieldValidationConfig, FormFieldConfig, FormFieldRow, FormValidationConfig, FormValidator, FormConfiguration, ComponentRendererBaseProps, FormBodyRendererProps, ValidationError, ValidationResult, FormRowRendererProps, FormSubmitButtonRendererProps } from '@rilaykit/core';
|
|
3
3
|
export * from '@rilaykit/core';
|
|
4
|
-
|
|
5
|
-
import * as React$1 from 'react';
|
|
6
|
-
import React__default from 'react';
|
|
4
|
+
import React$1 from 'react';
|
|
7
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for a form field with type safety
|
|
8
|
+
*
|
|
9
|
+
* @template C - The component configuration map
|
|
10
|
+
* @template T - The specific component type key
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const fieldConfig: FieldConfig<MyComponents, 'text'> = {
|
|
15
|
+
* type: 'text',
|
|
16
|
+
* props: { placeholder: 'Enter your name' },
|
|
17
|
+
* validation: {
|
|
18
|
+
* validators: [required(), minLength(2)],
|
|
19
|
+
* validateOnChange: true,
|
|
20
|
+
* validateOnBlur: true
|
|
21
|
+
* }
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
8
25
|
type FieldConfig<C extends Record<string, any>, T extends keyof C> = {
|
|
9
|
-
|
|
26
|
+
/** Unique identifier for the field. Auto-generated if not provided */
|
|
27
|
+
id?: string;
|
|
28
|
+
/** Component type from the registered components */
|
|
10
29
|
type: T;
|
|
30
|
+
/** Component-specific properties */
|
|
11
31
|
props?: Partial<C[T]>;
|
|
12
|
-
|
|
13
|
-
|
|
32
|
+
/** Validation configuration for this field */
|
|
33
|
+
validation?: FieldValidationConfig;
|
|
14
34
|
};
|
|
15
|
-
interface RowOptions {
|
|
16
|
-
spacing?: 'tight' | 'normal' | 'loose';
|
|
17
|
-
alignment?: 'start' | 'center' | 'end' | 'stretch';
|
|
18
|
-
}
|
|
19
35
|
/**
|
|
20
|
-
* Form builder class for creating form configurations
|
|
21
|
-
*
|
|
36
|
+
* Form builder class for creating type-safe form configurations
|
|
37
|
+
*
|
|
38
|
+
* This class provides a fluent API for building forms with automatic validation,
|
|
39
|
+
* type safety, and flexible layout options. It manages field registration,
|
|
40
|
+
* row organization, and form configuration generation.
|
|
41
|
+
*
|
|
42
|
+
* @template C - Component configuration map defining available component types
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // Create a form builder with typed components
|
|
47
|
+
* const formBuilder = form.create(rilConfig, 'user-registration')
|
|
48
|
+
* .add({ type: 'text', props: { label: 'Name' } })
|
|
49
|
+
* .add(
|
|
50
|
+
* { type: 'email', props: { label: 'Email' } },
|
|
51
|
+
* { type: 'password', props: { label: 'Password' } }
|
|
52
|
+
* )
|
|
53
|
+
* .build();
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* - Supports up to 3 fields per row for optimal layout
|
|
58
|
+
* - Automatically generates unique IDs for fields and rows
|
|
59
|
+
* - Maintains type safety throughout the building process
|
|
22
60
|
*/
|
|
23
61
|
declare class form<C extends Record<string, any> = Record<string, never>> {
|
|
62
|
+
/** The ril configuration instance containing component definitions */
|
|
24
63
|
private config;
|
|
64
|
+
/** Array of form rows containing field configurations */
|
|
25
65
|
private rows;
|
|
66
|
+
/** Unique identifier for this form */
|
|
26
67
|
private formId;
|
|
27
|
-
|
|
68
|
+
/** Generator for creating unique IDs */
|
|
69
|
+
private idGenerator;
|
|
70
|
+
/** Form-level validation configuration */
|
|
71
|
+
private formValidation?;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a new form builder instance
|
|
74
|
+
*
|
|
75
|
+
* @param config - The ril configuration containing component definitions
|
|
76
|
+
* @param formId - Optional unique identifier for the form. Auto-generated if not provided
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const builder = new form(rilConfig, 'my-form');
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
28
83
|
constructor(config: ril<C>, formId?: string);
|
|
84
|
+
/**
|
|
85
|
+
* Static factory method to create a new form builder
|
|
86
|
+
*
|
|
87
|
+
* @template Cm - Component configuration map
|
|
88
|
+
* @param config - The ril configuration instance
|
|
89
|
+
* @param formId - Optional form identifier
|
|
90
|
+
* @returns A new form builder instance
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const builder = form.create(rilConfig, 'registration-form');
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
29
97
|
static create<Cm extends Record<string, any> = Record<string, never>>(config: ril<Cm>, formId?: string): form<Cm>;
|
|
30
98
|
/**
|
|
31
|
-
*
|
|
99
|
+
* Converts a FieldConfig to a FormFieldConfig
|
|
100
|
+
*
|
|
101
|
+
* This internal method handles the transformation from the builder's field
|
|
102
|
+
* configuration format to the final form field configuration, including
|
|
103
|
+
* component lookup, prop merging, ID generation, and validation setup.
|
|
104
|
+
*
|
|
105
|
+
* The validation system combines component-level validation (defined in the component config)
|
|
106
|
+
* with field-level validation (defined in the field config). Component validators are
|
|
107
|
+
* applied first, followed by field validators.
|
|
108
|
+
*
|
|
109
|
+
* @template T - The component type
|
|
110
|
+
* @param fieldConfig - The field configuration to convert
|
|
111
|
+
* @returns A complete FormFieldConfig ready for use
|
|
112
|
+
* @throws Error if the specified component type is not registered
|
|
113
|
+
*
|
|
114
|
+
* @internal
|
|
32
115
|
*/
|
|
33
116
|
private createFormField;
|
|
34
117
|
/**
|
|
35
|
-
*
|
|
118
|
+
* Creates a form row with the specified fields and options
|
|
119
|
+
*
|
|
120
|
+
* This internal method handles row creation,
|
|
121
|
+
* proper spacing, and alignment configuration.
|
|
122
|
+
*
|
|
123
|
+
* @template T - The component type
|
|
124
|
+
* @param fieldConfigs - Array of field configurations for the row
|
|
125
|
+
* @param rowOptions - Optional row layout configuration
|
|
126
|
+
* @returns A complete FormFieldRow configuration
|
|
127
|
+
* @throws Error if no fields provided or more than 3 fields specified
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
36
130
|
*/
|
|
37
131
|
private createRow;
|
|
38
132
|
/**
|
|
39
|
-
*
|
|
133
|
+
* Universal method for adding fields to the form
|
|
134
|
+
*
|
|
135
|
+
* This is the primary method for adding fields to your form. It supports multiple
|
|
136
|
+
* usage patterns for maximum flexibility:
|
|
137
|
+
*
|
|
138
|
+
* - Single field: Creates a new row with one field
|
|
139
|
+
* - Multiple fields (≤3): Creates one row with all fields
|
|
140
|
+
* - Multiple fields (>3): Creates separate rows for each field
|
|
141
|
+
* - Array with options: Explicit control over row configuration
|
|
142
|
+
*
|
|
143
|
+
* @template T - The component type
|
|
144
|
+
* @param fields - Field configurations (variadic or array)
|
|
145
|
+
* @returns The form builder instance for method chaining
|
|
146
|
+
* @throws Error if no fields provided or invalid configuration
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* // Single field on its own row
|
|
151
|
+
* builder.add({ type: 'text', props: { label: 'Name' } });
|
|
152
|
+
*
|
|
153
|
+
* // Multiple fields on same row (max 3)
|
|
154
|
+
* builder.add(
|
|
155
|
+
* { type: 'text', props: { label: 'First Name' } },
|
|
156
|
+
* { type: 'text', props: { label: 'Last Name' } }
|
|
157
|
+
* );
|
|
158
|
+
*
|
|
159
|
+
* // Array syntax with row options
|
|
160
|
+
* builder.add([
|
|
161
|
+
* { type: 'email', props: { label: 'Email' } },
|
|
162
|
+
* { type: 'phone', props: { label: 'Phone' } }
|
|
163
|
+
* ], { spacing: 'loose', alignment: 'center' });
|
|
164
|
+
* ```
|
|
40
165
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}): this;
|
|
166
|
+
add<T extends keyof C & string>(...fields: FieldConfig<C, T>[]): this;
|
|
167
|
+
add<T extends keyof C & string>(fields: FieldConfig<C, T>[]): this;
|
|
44
168
|
/**
|
|
45
|
-
*
|
|
169
|
+
* Adds multiple fields on separate rows
|
|
170
|
+
*
|
|
171
|
+
* This method is useful when you want to ensure each field gets its own row,
|
|
172
|
+
* regardless of the number of fields. It's an alternative to the add() method
|
|
173
|
+
* when you need explicit control over row separation.
|
|
174
|
+
*
|
|
175
|
+
* @template T - The component type
|
|
176
|
+
* @param fieldConfigs - Array of field configurations
|
|
177
|
+
* @param rowOptions - Optional row layout configuration applied to all rows
|
|
178
|
+
* @returns The form builder instance for method chaining
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* // Each field will be on its own row
|
|
183
|
+
* builder.addSeparateRows([
|
|
184
|
+
* { type: 'text', props: { label: 'Field 1' } },
|
|
185
|
+
* { type: 'text', props: { label: 'Field 2' } },
|
|
186
|
+
* { type: 'text', props: { label: 'Field 3' } }
|
|
187
|
+
* ]);
|
|
188
|
+
* ```
|
|
46
189
|
*/
|
|
47
|
-
|
|
190
|
+
addSeparateRows<T extends keyof C & string>(fieldConfigs: FieldConfig<C, T>[]): this;
|
|
48
191
|
/**
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
192
|
+
* Sets the form identifier
|
|
193
|
+
*
|
|
194
|
+
* @param id - The new form identifier
|
|
195
|
+
* @returns The form builder instance for method chaining
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* builder.setId('user-profile-form');
|
|
200
|
+
* ```
|
|
54
201
|
*/
|
|
55
202
|
setId(id: string): this;
|
|
56
203
|
/**
|
|
57
|
-
*
|
|
204
|
+
* Updates an existing field's configuration
|
|
205
|
+
*
|
|
206
|
+
* This method allows you to modify field properties after the field has been added to the form.
|
|
207
|
+
*
|
|
208
|
+
* @param fieldId - The unique identifier of the field to update
|
|
209
|
+
* @param updates - Partial field configuration with updates to apply
|
|
210
|
+
* @returns The form builder instance for method chaining
|
|
211
|
+
* @throws Error if the field with the specified ID is not found
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* builder.updateField('email-field', {
|
|
216
|
+
* props: { placeholder: 'Enter your email address' },
|
|
217
|
+
* });
|
|
218
|
+
* ```
|
|
58
219
|
*/
|
|
59
220
|
updateField(fieldId: string, updates: Partial<Omit<FormFieldConfig, 'id'>>): this;
|
|
60
221
|
/**
|
|
61
|
-
*
|
|
222
|
+
* Finds a field by its unique identifier
|
|
223
|
+
*
|
|
224
|
+
* This internal method searches through all rows to locate a field
|
|
225
|
+
* with the specified ID.
|
|
226
|
+
*
|
|
227
|
+
* @param fieldId - The field identifier to search for
|
|
228
|
+
* @returns The field configuration if found, null otherwise
|
|
229
|
+
*
|
|
230
|
+
* @internal
|
|
62
231
|
*/
|
|
63
232
|
private findField;
|
|
64
233
|
/**
|
|
65
|
-
*
|
|
234
|
+
* Removes a field from the form
|
|
235
|
+
*
|
|
236
|
+
* This method removes the specified field and cleans up any empty rows
|
|
237
|
+
* that result from the removal. The form structure is automatically
|
|
238
|
+
* reorganized to maintain consistency.
|
|
239
|
+
*
|
|
240
|
+
* @param fieldId - The unique identifier of the field to remove
|
|
241
|
+
* @returns The form builder instance for method chaining
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* builder.removeField('unwanted-field-id');
|
|
246
|
+
* ```
|
|
66
247
|
*/
|
|
67
248
|
removeField(fieldId: string): this;
|
|
68
249
|
/**
|
|
69
|
-
*
|
|
250
|
+
* Retrieves a field configuration by its ID
|
|
251
|
+
*
|
|
252
|
+
* @param fieldId - The unique identifier of the field
|
|
253
|
+
* @returns The field configuration if found, undefined otherwise
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* const emailField = builder.getField('email-field');
|
|
258
|
+
* if (emailField) {
|
|
259
|
+
* console.log('Email field props:', emailField.props);
|
|
260
|
+
* }
|
|
261
|
+
* ```
|
|
70
262
|
*/
|
|
71
263
|
getField(fieldId: string): FormFieldConfig | undefined;
|
|
72
264
|
/**
|
|
73
|
-
*
|
|
265
|
+
* Gets all fields as a flat array
|
|
266
|
+
*
|
|
267
|
+
* This method flattens the row structure to provide a simple array
|
|
268
|
+
* of all field configurations in the form, maintaining their order.
|
|
269
|
+
*
|
|
270
|
+
* @returns Array of all field configurations in the form
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* const allFields = builder.getFields();
|
|
275
|
+
* console.log(`Form has ${allFields.length} fields`);
|
|
276
|
+
* ```
|
|
74
277
|
*/
|
|
75
278
|
getFields(): FormFieldConfig[];
|
|
76
279
|
/**
|
|
77
|
-
*
|
|
280
|
+
* Gets all rows in the form
|
|
281
|
+
*
|
|
282
|
+
* Returns a copy of the internal rows array to prevent external
|
|
283
|
+
* modification while allowing inspection of the form structure.
|
|
284
|
+
*
|
|
285
|
+
* @returns Array of all form rows
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```typescript
|
|
289
|
+
* const rows = builder.getRows();
|
|
290
|
+
* console.log(`Form has ${rows.length} rows`);
|
|
291
|
+
* ```
|
|
78
292
|
*/
|
|
79
293
|
getRows(): FormFieldRow[];
|
|
80
294
|
/**
|
|
81
|
-
*
|
|
295
|
+
* Clears all fields and rows from the form
|
|
296
|
+
*
|
|
297
|
+
* This method resets the form to an empty state and resets the ID generator
|
|
298
|
+
* to ensure clean ID generation for subsequent fields.
|
|
299
|
+
*
|
|
300
|
+
* @returns The form builder instance for method chaining
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* builder.clear().add({ type: 'text', props: { label: 'New start' } });
|
|
305
|
+
* ```
|
|
82
306
|
*/
|
|
83
307
|
clear(): this;
|
|
84
308
|
/**
|
|
85
|
-
*
|
|
309
|
+
* Configures validation for the entire form
|
|
310
|
+
*
|
|
311
|
+
* This method sets up form-level validation that will be applied when the
|
|
312
|
+
* form is submitted or when validation is explicitly triggered. Form validators
|
|
313
|
+
* receive all form data and can perform cross-field validation.
|
|
314
|
+
*
|
|
315
|
+
* @param validationConfig - Form validation configuration
|
|
316
|
+
* @returns The form builder instance for method chaining
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```typescript
|
|
320
|
+
* builder.setValidation({
|
|
321
|
+
* validators: [
|
|
322
|
+
* (formData, context) => {
|
|
323
|
+
* if (!formData.email && !formData.phone) {
|
|
324
|
+
* return createErrorResult('Either email or phone is required');
|
|
325
|
+
* }
|
|
326
|
+
* return createSuccessResult();
|
|
327
|
+
* }
|
|
328
|
+
* ],
|
|
329
|
+
* validateOnSubmit: true
|
|
330
|
+
* });
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
setValidation(validationConfig: FormValidationConfig): this;
|
|
334
|
+
/**
|
|
335
|
+
* Adds validators to the form-level validation
|
|
336
|
+
*
|
|
337
|
+
* This method allows adding validators to an existing validation configuration
|
|
338
|
+
* without replacing the entire configuration.
|
|
339
|
+
*
|
|
340
|
+
* @param validators - Array of form validators to add
|
|
341
|
+
* @returns The form builder instance for method chaining
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```typescript
|
|
345
|
+
* builder.addValidators([
|
|
346
|
+
* customFormValidator,
|
|
347
|
+
* anotherFormValidator
|
|
348
|
+
* ]);
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
addValidators(validators: FormValidator[]): this;
|
|
352
|
+
/**
|
|
353
|
+
* Adds validation to a specific field by ID
|
|
354
|
+
*
|
|
355
|
+
* This method allows adding validation to a field after it has been created,
|
|
356
|
+
* useful for dynamic validation requirements.
|
|
357
|
+
*
|
|
358
|
+
* @param fieldId - The ID of the field to add validation to
|
|
359
|
+
* @param validationConfig - Field validation configuration
|
|
360
|
+
* @returns The form builder instance for method chaining
|
|
361
|
+
* @throws Error if the field with the specified ID is not found
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* builder.addFieldValidation('email', {
|
|
366
|
+
* validators: [required(), email()],
|
|
367
|
+
* validateOnBlur: true
|
|
368
|
+
* });
|
|
369
|
+
* ```
|
|
370
|
+
*/
|
|
371
|
+
addFieldValidation(fieldId: string, validationConfig: FieldValidationConfig): this;
|
|
372
|
+
/**
|
|
373
|
+
* Creates a deep copy of the current form builder
|
|
374
|
+
*
|
|
375
|
+
* This method creates a completely independent copy of the form builder,
|
|
376
|
+
* including all field configurations and internal state. The cloned
|
|
377
|
+
* builder can be modified without affecting the original.
|
|
378
|
+
*
|
|
379
|
+
* @param newFormId - Optional new form ID for the clone
|
|
380
|
+
* @returns A new form builder instance with copied configuration
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```typescript
|
|
384
|
+
* const originalForm = builder.clone();
|
|
385
|
+
* const modifiedForm = builder.clone('modified-form')
|
|
386
|
+
* .add({ type: 'text', props: { label: 'Additional field' } });
|
|
387
|
+
* ```
|
|
86
388
|
*/
|
|
87
389
|
clone(newFormId?: string): form<C>;
|
|
88
390
|
/**
|
|
89
|
-
*
|
|
391
|
+
* Checks the current form configuration for basic structural issues.
|
|
392
|
+
*
|
|
393
|
+
* @returns Array of error messages (empty if valid)
|
|
90
394
|
*/
|
|
91
395
|
validate(): string[];
|
|
92
396
|
/**
|
|
93
|
-
*
|
|
397
|
+
* Builds the final form configuration
|
|
398
|
+
*
|
|
399
|
+
* This method creates the complete form
|
|
400
|
+
* configuration object ready for rendering. It includes all field
|
|
401
|
+
* configurations, render settings, validation configuration, and metadata.
|
|
402
|
+
*
|
|
403
|
+
* @returns Complete form configuration ready for use
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```typescript
|
|
407
|
+
* const formConfig = builder.build();
|
|
408
|
+
* // Use formConfig with your form renderer
|
|
409
|
+
* ```
|
|
410
|
+
*
|
|
411
|
+
* @remarks
|
|
412
|
+
* The returned configuration includes:
|
|
413
|
+
* - Form ID and metadata
|
|
414
|
+
* - All rows with their field configurations
|
|
415
|
+
* - Flattened array of all fields for easy access
|
|
416
|
+
* - Component configuration reference
|
|
417
|
+
* - Render configuration for customization
|
|
418
|
+
* - Form-level validation configuration
|
|
94
419
|
*/
|
|
95
420
|
build(): FormConfiguration;
|
|
96
421
|
/**
|
|
97
|
-
*
|
|
422
|
+
* Exports the form configuration as JSON
|
|
423
|
+
*
|
|
424
|
+
* This method serializes the form configuration to a plain JavaScript
|
|
425
|
+
* object suitable for storage, transmission, or debugging.
|
|
426
|
+
*
|
|
427
|
+
* @returns Plain object representation of the form
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* ```typescript
|
|
431
|
+
* const formJson = builder.toJSON();
|
|
432
|
+
* localStorage.setItem('savedForm', JSON.stringify(formJson));
|
|
433
|
+
* ```
|
|
98
434
|
*/
|
|
99
435
|
toJSON(): any;
|
|
100
436
|
/**
|
|
101
|
-
*
|
|
437
|
+
* Imports form configuration from JSON
|
|
438
|
+
*
|
|
439
|
+
* This method restores form state from a previously exported JSON
|
|
440
|
+
* configuration. It's useful for loading saved forms or restoring
|
|
441
|
+
* form state from external sources.
|
|
442
|
+
*
|
|
443
|
+
* @param json - The JSON object containing form configuration
|
|
444
|
+
* @returns The form builder instance for method chaining
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```typescript
|
|
448
|
+
* const savedForm = JSON.parse(localStorage.getItem('savedForm'));
|
|
449
|
+
* builder.fromJSON(savedForm);
|
|
450
|
+
* ```
|
|
451
|
+
*
|
|
452
|
+
* @remarks
|
|
453
|
+
* - Only imports basic form structure (ID and rows)
|
|
454
|
+
* - Does not validate imported configuration
|
|
455
|
+
* - Existing form content is replaced
|
|
102
456
|
*/
|
|
103
457
|
fromJSON(json: any): this;
|
|
104
458
|
/**
|
|
105
|
-
*
|
|
459
|
+
* Gets comprehensive statistics about the form
|
|
460
|
+
*
|
|
461
|
+
* This method provides useful metrics about the form structure,
|
|
462
|
+
* helpful for analytics, debugging, or UI display purposes.
|
|
463
|
+
*
|
|
464
|
+
* @returns Object containing form statistics
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```typescript
|
|
468
|
+
* const stats = builder.getStats();
|
|
469
|
+
* console.log(`Form has ${stats.totalFields} fields in ${stats.totalRows} rows`);
|
|
470
|
+
* console.log(`Average fields per row: ${stats.averageFieldsPerRow.toFixed(1)}`);
|
|
471
|
+
* ```
|
|
472
|
+
*
|
|
473
|
+
* @remarks
|
|
474
|
+
* Statistics include:
|
|
475
|
+
* - Total number of fields and rows
|
|
476
|
+
* - Average fields per row
|
|
477
|
+
* - Maximum and minimum fields in any row
|
|
478
|
+
* - Useful for form complexity analysis
|
|
106
479
|
*/
|
|
107
480
|
getStats(): {
|
|
481
|
+
/** Total number of fields across all rows */
|
|
108
482
|
totalFields: number;
|
|
483
|
+
/** Total number of rows in the form */
|
|
109
484
|
totalRows: number;
|
|
485
|
+
/** Average number of fields per row */
|
|
110
486
|
averageFieldsPerRow: number;
|
|
487
|
+
/** Maximum number of fields in any single row */
|
|
111
488
|
maxFieldsInRow: number;
|
|
489
|
+
/** Minimum number of fields in any single row */
|
|
112
490
|
minFieldsInRow: number;
|
|
113
491
|
};
|
|
114
492
|
}
|
|
115
493
|
/**
|
|
116
494
|
* Factory function to create a form builder directly
|
|
495
|
+
*
|
|
496
|
+
* This is a convenience function that provides an alternative to using
|
|
497
|
+
* the class constructor or static create method. It's particularly useful
|
|
498
|
+
* for functional programming styles or when you prefer function calls
|
|
499
|
+
* over class instantiation.
|
|
500
|
+
*
|
|
501
|
+
* @template C - Component configuration map
|
|
502
|
+
* @param config - The ril configuration instance
|
|
503
|
+
* @param formId - Optional form identifier
|
|
504
|
+
* @returns A new form builder instance
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```typescript
|
|
508
|
+
* const builder = createForm(rilConfig, 'contact-form')
|
|
509
|
+
* .add({ type: 'text', props: { label: 'Name' } })
|
|
510
|
+
* .add({ type: 'email', props: { label: 'Email' } });
|
|
511
|
+
* ```
|
|
117
512
|
*/
|
|
118
513
|
declare function createForm<C extends Record<string, any>>(config: ril<C>, formId?: string): form<C>;
|
|
514
|
+
/**
|
|
515
|
+
* Module augmentation to add createForm method to ril instances
|
|
516
|
+
*
|
|
517
|
+
* This declaration extends the ril interface to include the createForm
|
|
518
|
+
* method, allowing for a more integrated API experience.
|
|
519
|
+
*/
|
|
119
520
|
declare module '@rilaykit/core' {
|
|
120
521
|
interface ril<C extends Record<string, any> = Record<string, never>> {
|
|
121
|
-
|
|
522
|
+
/**
|
|
523
|
+
* Creates a new form builder using this ril configuration
|
|
524
|
+
*
|
|
525
|
+
* @param formId - Optional form identifier
|
|
526
|
+
* @returns A new form builder instance
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```typescript
|
|
530
|
+
* const builder = rilConfig.createForm('my-form');
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
form(formId?: string): form<C>;
|
|
122
534
|
}
|
|
123
535
|
}
|
|
124
536
|
|
|
@@ -132,12 +544,7 @@ interface FormProps {
|
|
|
132
544
|
}
|
|
133
545
|
declare function Form({ formConfig, defaultValues, onSubmit, onFieldChange, children }: FormProps): react_jsx_runtime.JSX.Element;
|
|
134
546
|
|
|
135
|
-
|
|
136
|
-
className?: string;
|
|
137
|
-
children?: React.ReactNode | RendererChildrenFunction<FormBodyRendererProps>;
|
|
138
|
-
renderAs?: 'default' | 'children' | boolean;
|
|
139
|
-
}
|
|
140
|
-
declare function FormBody({ className, children, renderAs }: FormBodyProps): string | number | boolean | React$1.ReactElement<any, string | React$1.JSXElementConstructor<any>> | Iterable<React$1.ReactNode> | null | undefined;
|
|
547
|
+
declare function FormBody({ className, ...props }: ComponentRendererBaseProps<FormBodyRendererProps>): react_jsx_runtime.JSX.Element;
|
|
141
548
|
|
|
142
549
|
interface FormFieldProps {
|
|
143
550
|
fieldId: string;
|
|
@@ -145,32 +552,32 @@ interface FormFieldProps {
|
|
|
145
552
|
customProps?: Record<string, any>;
|
|
146
553
|
className?: string;
|
|
147
554
|
}
|
|
148
|
-
declare function FormField({ fieldId, disabled, customProps, className, }: FormFieldProps): react_jsx_runtime.JSX.Element
|
|
555
|
+
declare function FormField({ fieldId, disabled, customProps, className, }: FormFieldProps): react_jsx_runtime.JSX.Element;
|
|
149
556
|
|
|
150
557
|
interface FormState {
|
|
151
558
|
values: Record<string, any>;
|
|
152
559
|
errors: Record<string, ValidationError[]>;
|
|
153
|
-
|
|
154
|
-
|
|
560
|
+
validationState: Record<string, 'idle' | 'validating' | 'valid' | 'invalid'>;
|
|
561
|
+
touched: Record<string, boolean>;
|
|
155
562
|
isDirty: boolean;
|
|
156
|
-
isValid: boolean;
|
|
157
563
|
isSubmitting: boolean;
|
|
564
|
+
isValid: boolean;
|
|
158
565
|
}
|
|
159
566
|
interface FormContextValue {
|
|
160
567
|
formState: FormState;
|
|
161
568
|
formConfig: FormConfiguration;
|
|
162
569
|
setValue: (fieldId: string, value: any) => void;
|
|
163
|
-
|
|
164
|
-
clearError: (fieldId: string) => void;
|
|
165
|
-
markFieldTouched: (fieldId: string) => void;
|
|
166
|
-
setFieldValidating: (fieldId: string, isValidating: boolean) => void;
|
|
570
|
+
setFieldTouched: (fieldId: string, touched?: boolean) => void;
|
|
167
571
|
validateField: (fieldId: string, value?: any) => Promise<ValidationResult>;
|
|
168
|
-
|
|
572
|
+
validateForm: () => Promise<ValidationResult>;
|
|
573
|
+
isFormValid: () => boolean;
|
|
169
574
|
reset: (values?: Record<string, any>) => void;
|
|
170
|
-
submit: (event?:
|
|
575
|
+
submit: (event?: React$1.FormEvent) => Promise<boolean>;
|
|
576
|
+
setError: (fieldId: string, errors: ValidationError[]) => void;
|
|
577
|
+
clearError: (fieldId: string) => void;
|
|
171
578
|
}
|
|
172
579
|
interface FormProviderProps {
|
|
173
|
-
children:
|
|
580
|
+
children: React$1.ReactNode;
|
|
174
581
|
formConfig: FormConfiguration;
|
|
175
582
|
defaultValues?: Record<string, any>;
|
|
176
583
|
onSubmit?: (data: Record<string, any>) => void | Promise<void>;
|
|
@@ -180,19 +587,11 @@ interface FormProviderProps {
|
|
|
180
587
|
declare function FormProvider({ children, formConfig, defaultValues, onSubmit, onFieldChange, className, }: FormProviderProps): react_jsx_runtime.JSX.Element;
|
|
181
588
|
declare function useFormContext(): FormContextValue;
|
|
182
589
|
|
|
183
|
-
interface FormRowProps {
|
|
590
|
+
interface FormRowProps extends ComponentRendererBaseProps<FormRowRendererProps> {
|
|
184
591
|
row: FormFieldRow;
|
|
185
|
-
className?: string;
|
|
186
|
-
children?: React.ReactNode | RendererChildrenFunction<FormRowRendererProps>;
|
|
187
|
-
renderAs?: 'default' | 'children' | boolean;
|
|
188
592
|
}
|
|
189
|
-
declare function FormRow({ row, className,
|
|
593
|
+
declare function FormRow({ row, className, ...props }: FormRowProps): react_jsx_runtime.JSX.Element;
|
|
190
594
|
|
|
191
|
-
|
|
192
|
-
className?: string;
|
|
193
|
-
children?: React__default.ReactNode | RendererChildrenFunction<FormSubmitButtonRendererProps>;
|
|
194
|
-
renderAs?: 'default' | 'children' | boolean;
|
|
195
|
-
}
|
|
196
|
-
declare function FormSubmitButton({ className, children, renderAs }: FormSubmitButtonProps): string | number | boolean | React__default.ReactElement<any, string | React__default.JSXElementConstructor<any>> | Iterable<React__default.ReactNode> | null | undefined;
|
|
595
|
+
declare function FormSubmitButton({ className, ...props }: ComponentRendererBaseProps<FormSubmitButtonRendererProps>): react_jsx_runtime.JSX.Element;
|
|
197
596
|
|
|
198
|
-
export { type FieldConfig, Form, FormBody,
|
|
597
|
+
export { type FieldConfig, Form, FormBody, form as FormBuilder, FormField, FormProvider, FormRow, FormSubmitButton, createForm, form, useFormContext };
|