@bolttech/form-engine 3.1.1-beta.3 → 3.1.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/index.d.ts ADDED
@@ -0,0 +1,326 @@
1
+ import * as react from 'react';
2
+ import { PropsWithChildren, ReactElement, ElementType, ReactNode } from 'react';
3
+ import { TEventProps, TFieldEvent, TEventDataProps, TFormValues, TFormValidationPayload, TFormEntry, IComponentSchemaAsFormField, IComponentSchema, OneOf, TMapper, TFormGroupOnDataEventPayload, FormCore, TFormGroup, TSchemaFormConfig, FormField, TFormGroupOnValidEventPayload, TFormGroupOnSubmitEventPayload, TValueChangeEvent } from '@bolttech/form-engine-core';
4
+
5
+ /**
6
+ * Represents a field wrapper containing the name of the field and its index in the form.
7
+ *
8
+ * @property {string} name - The name of the field.
9
+ * @property {string} formIndex - The index of the field within the form.
10
+ */
11
+ type TFieldWrapper = {
12
+ name: string;
13
+ formIndex: string;
14
+ };
15
+ /**
16
+ * Represents the content inside onData payload action.
17
+ */
18
+ type TFormData<T> = {
19
+ field: string;
20
+ data: TFormValues<T>;
21
+ };
22
+ /**
23
+ *
24
+ * @property {(payload: TFormValues<T>) => void} onFormMount - triggered when form mounts
25
+ * @property {(payload: TFormValues<T>) => void} onData - triggered when any field change value
26
+ * @property {(payload: TFormValues<T>) => void} onSubmit - triggered when submit button is pressed
27
+ * @property {(payload: TFormValues<T>) => void} onValid - triggered when form valid status changes
28
+ * @interface
29
+ *
30
+ */
31
+ type TEventsCallbackProps<T> = Partial<Record<TEventProps, ((payload: TFieldEvent) => void) | null | undefined>> & Partial<Record<Extract<TEventDataProps, 'onFormMount' | 'onSubmit'>, (payload: TFormValues<T>) => void>> & Partial<Record<Extract<TEventDataProps, 'onData'>, (payload: {
32
+ field: string;
33
+ data: TFormValues<T>;
34
+ }) => void>> & Partial<Record<Extract<TEventDataProps, 'onValid'>, (payload: TFormValidationPayload) => void>>;
35
+
36
+ /**
37
+ * Form props, inherits the form instance constructor implementation except the mapper
38
+ * along with all event callback register props shared with other implementations
39
+ * @see {@link TFormEntry}
40
+ * @see {@link TEventsCallbackProps}
41
+ * @interface
42
+ * @example
43
+ * ```typescript
44
+ * import { Form } from '@bolttech/form-engine';
45
+ *
46
+ * <Form index={'foo'} schema={schema} onData={console.log} />
47
+ * ```
48
+ */
49
+ type TFormProps<T> = Omit<TFormEntry, 'mappers' | 'dataSubject$' | 'formValidSubject$' | 'submitSubject$'> & TEventsCallbackProps<T>;
50
+
51
+ /**
52
+ *
53
+ * @param {TFormProps} param form properties initializor
54
+ * @returns {ReactElement}
55
+ */
56
+ declare function Form<T>({ schema, index, initialValues, iVars, action, method, config, prefetchedData, onSubmit, onFormMount, onData, onBlur, onChange, onApiResponse, onClick, onFocus, onKeyDown, onKeyUp, onMount, onValid, stopEventsOnSubmit, children, }: PropsWithChildren<TFormProps<T>>): ReactElement;
57
+
58
+ /**
59
+ * AsFormField props, inherits all schema field implementation except the children
60
+ * property and has mappers property to pass a custom component, that will be a ReactNode
61
+ * @see {@link IComponentSchemaAsFormField}
62
+ */
63
+ type TAsFormFieldProps = PropsWithChildren<Omit<IComponentSchemaAsFormField<ElementType>, 'children'>>;
64
+
65
+ /**
66
+ * Component wrapper to aid building schemas with react without writting a JSON schema
67
+ * along with BuildAsFormFieldTree inside a Form component, FieldWrapper gets this props
68
+ * to build the component as it does with a JSON schema, this component only works inside
69
+ * the Form component
70
+ *
71
+ * @param {TAsFormFieldProps} props JSON schema props
72
+ * @returns {ReactNode}
73
+ */
74
+ declare const AsFormField: (props: TAsFormFieldProps) => ReactNode;
75
+
76
+ /**
77
+ * AsFormField props, inherits all schema field implementation except the children
78
+ * property, that will be a ReactNode
79
+ * also gets the formIndex for form identification and the mapper to build the component
80
+ * @property {TMapper} mapper element mapper to use
81
+ * @property {boolean} visibility element controlled visibility
82
+ * @property {(data: TFieldEvent) => void} onSelected element callback on selected option
83
+ * @see {@link TMapper}
84
+ * @see {@link IComponentSchema}
85
+ * @see {@link TFieldWrapper}
86
+ * @interface
87
+ */
88
+ type TAsFormFieldBuilderProps = PropsWithChildren<Omit<IComponentSchema, 'component' | 'children' | 'name'> & Required<TFieldWrapper> & {
89
+ onSelected?: (data: TFieldEvent) => void;
90
+ formMounted?: boolean;
91
+ } & OneOf<{
92
+ mapper: TMapper<ElementType>;
93
+ component: string;
94
+ }>>;
95
+
96
+ /**
97
+ * Component Wrapper to render form fields without the Form component, gets additional formId and mapper since
98
+ * it won't rely on them and needs to be manually declared
99
+ *
100
+ * @param {TAsFormFieldBuilderProps} props JSON schema props along with FieldWrapper props and mapper props
101
+ * @returns {ReactElement}
102
+ */
103
+ declare const AsFormFieldBuilder: (props: TAsFormFieldBuilderProps) => ReactElement;
104
+
105
+ type TRepeaterProps = {
106
+ formIndex: string;
107
+ index: number;
108
+ };
109
+ type TRepeaterFooterProps = Pick<TRepeaterProps, 'formIndex'>;
110
+ /**
111
+ * AsFormFieldRepeater props needed to build a repeater to build forms
112
+ * @property {ElementType<TRepeaterProps>} RepeaterComponent element to use as repeater
113
+ * @property {string} addFieldName field name button that will add an element on a position on a repeater
114
+ * @property {string} removeFieldName field name button that will remove an element on a position on a repeater
115
+ * @property {Record<string, unknown>[]} existingElements elements to restore a repeater list
116
+ * @property {number} initialElements elements to be pre-rendered when the form is presented
117
+ * @property {(payload: TFormGroupOnDataEventPayload<unknown>) => void} stateUpdater similar to onData for the group of form managed by the repeater
118
+ * @property {string} formPrefix prefix to use on the repeated forms ex: (person- will go person-1, person-2, ...)
119
+ * @property {ElementType<TRepeaterFooterProps>} RepeaterFooter component with a button that will add an element to the bottom of the list
120
+ * @see {@link TMapper}
121
+ * @see {@link IComponentSchema}
122
+ * @see {@link TFieldWrapper}
123
+ * @interface
124
+ * @example
125
+ * ```typescript
126
+ * import { AsFormFieldRepeater } from '@bolttech/form-engine';
127
+ *
128
+ * <AsFormFieldRepeater
129
+ * RepeaterComponent={FormElement}
130
+ * RepeaterFooter={FormElementFooter}
131
+ * addFieldName="addForm"
132
+ * removeFieldName="removeForm"
133
+ * formPrefix="insured"
134
+ * stateUpdater={(payload) => {
135
+ * console.log(payload);
136
+ * }}
137
+ * />
138
+ * ```
139
+ */
140
+ type TAsFormFieldRepeaterProps = {
141
+ RepeaterComponent: ElementType<TRepeaterProps>;
142
+ addFieldName?: string;
143
+ removeFieldName?: string;
144
+ stateUpdater?: (payload: TFormGroupOnDataEventPayload<unknown>) => void;
145
+ formPrefix?: string;
146
+ RepeaterFooter: ElementType<TRepeaterFooterProps>;
147
+ } & OneOf<{
148
+ existingElements?: Record<string, unknown>[];
149
+ initialElements?: number;
150
+ }>;
151
+
152
+ /**
153
+ * Adapter do manage repeated list elements on form
154
+ *
155
+ * @param {TAsFormFieldRepeaterProps} props Repeater properties to configure the elements repeater
156
+ * @returns {ReactElement}
157
+ */
158
+ declare const AsFormFieldRepeater: ({ RepeaterComponent, addFieldName, removeFieldName, existingElements, initialElements, stateUpdater, formPrefix, RepeaterFooter, }: TAsFormFieldRepeaterProps) => ReactNode;
159
+
160
+ /**
161
+ * Represents the context for managing forms within a form group.
162
+ *
163
+ * @property {function(string): void} addFormWithIndex - Adds a form to the form group by its index.
164
+ * @property {function({ key: string, formInstance: TFormCore }): void} addForm - Adds a form to the form group using a payload containing the key and form instance.
165
+ * @property {function({ key: string }): (FormCore | undefined)} getForm - Retrieves a form from the form group using a payload containing the key.
166
+ * @property {function({ key: string }): void} removeForm - Removes a form from the form group using a payload containing the key.
167
+ * @property {TFormGroup} formGroupInstance - The instance of the form group.
168
+ * @property {TMapper<ElementType>[]} [mappers] - Optional array of mappers for elements.
169
+ * @property {function(): void} printFormGroupInstance - Prints the form group instance.
170
+ * @property {function(string[]): TFormValues} submitMultipleFormsByIndex - Submits multiple forms by their indexes and returns the form values.
171
+ * @property {boolean} debugMode - Indicates if debug mode is active.
172
+ * @property {boolean} active - Indicates if the form context is active (if the context provider is defined above).
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * import React from 'react';
177
+ * import { useFormGroupContext } from '@bolttech/form-engine';
178
+ *
179
+ * const FormComponent = (): React.ReactElement => {
180
+ * const {
181
+ * addForm,
182
+ * removeForm,
183
+ * getForm,
184
+ * printFormGroupInstance,
185
+ * submitMultipleFormsByIndex,
186
+ * debugMode
187
+ * } = useFormGroupContext();
188
+ *
189
+ * // (...)
190
+ * };
191
+ * ```
192
+ *
193
+ */
194
+ type TFormContext = {
195
+ addFormWithIndex: (index: string) => void;
196
+ addForm: (payload: {
197
+ key: string;
198
+ params: TFormEntry;
199
+ }) => void;
200
+ getForm: (payload: {
201
+ key: string;
202
+ }) => FormCore | undefined;
203
+ removeForm: (payload: {
204
+ key: string;
205
+ }) => void;
206
+ formGroupInstance: TFormGroup<ElementType>;
207
+ mappers?: TMapper<ElementType>[];
208
+ printFormGroupInstance: () => void;
209
+ submitMultipleFormsByIndex: <T>(indexes: string[], callback?: (payload: TFormValues<T>) => void) => void;
210
+ debugMode: boolean;
211
+ active: boolean;
212
+ config?: TSchemaFormConfig;
213
+ };
214
+ /**
215
+ * Represents the props for a form context provider.
216
+ *
217
+ * @property {TMapper<ElementType>[]} [mappers] - Optional array of mappers for elements.
218
+ * @property {boolean} [debugMode] - Optional flag indicating if debug mode should be enabled.
219
+ * @property {TSchemaFormConfig} config - Optional config settings for debounce times and client logging
220
+ *
221
+ * * @example
222
+ * ```tsx
223
+ * import { FormGroupContextProvider } from '@bolttech/form-engine';
224
+ *
225
+ * <FormGroupContextProvider mappers={mappers} debugMode={true}>
226
+ * {children}
227
+ * </FormGroupContextProvider>
228
+ * ```
229
+ */
230
+ type TFormContextProvider = {
231
+ mappers?: TMapper<ElementType>[];
232
+ debugMode?: boolean;
233
+ config?: TSchemaFormConfig;
234
+ };
235
+
236
+ declare const FormGroupContext: react.Context<TFormContext>;
237
+ /**
238
+ * context provider to wrap form-engine adapter elements
239
+ *
240
+ * @param {PropsWithChildren<TFormContextProvider>} param context parameters
241
+ * @returns {ReactElement}
242
+ */
243
+ declare const FormGroupContextProvider: ({ children, mappers, debugMode, config, }: PropsWithChildren<TFormContextProvider>) => ReactElement;
244
+ /**
245
+ * FormGroup context hook to handle context or isolated context implementations
246
+ *
247
+ * @param {TFormContextProvider} props form group context parameters
248
+ * @returns {TFormContext}
249
+ */
250
+ declare const useFormGroupContext: (props?: TFormContextProvider) => TFormContext;
251
+
252
+ /**
253
+ * Represents the props for a field wrapper component, including children.
254
+ *
255
+ * @property {Record<string, unknown>} [props] - Additional properties for the field.
256
+ * @property {TFormContext | null} [context] - The context of the form, which may be null.
257
+ * @property {React.ReactNode} children - The child elements of the component.
258
+ * @see {@link TFieldWrapper}
259
+ */
260
+ type TFieldWrapperProps = PropsWithChildren<TFieldWrapper & {
261
+ props?: Record<string, unknown>;
262
+ context?: TFormContext | null;
263
+ mounted?: boolean;
264
+ mapper?: TMapper<ElementType>;
265
+ component?: string;
266
+ visibility?: boolean;
267
+ }>;
268
+ /**
269
+ * Represents the props for rendering a field wrapper component, including children.
270
+ *
271
+ * @property {Record<string, unknown>} props - Additional properties for the field.
272
+ * @property {FormField} [fieldInstance] - The instance of the form field, which may be undefined.
273
+ * @property {React.ReactNode} children - The child elements of the component.
274
+ */
275
+ type TFieldWrapperComponentRenderProps = PropsWithChildren<{
276
+ props: Record<string, unknown>;
277
+ fieldInstance?: FormField;
278
+ mapper?: TMapper<ElementType>;
279
+ }>;
280
+
281
+ /**
282
+ * Represents the properties for the useForm hook, including an ID and event callback properties.
283
+ *
284
+ * @property {string} id - The unique identifier for the form. Deprecated, use 'index' instead
285
+ * @property {string} index - The unique identifier for the form.
286
+ *
287
+ * @see {@link TEventsCallbackProps}
288
+ * @interface
289
+ */
290
+ type TUseFormProps<T> = Pick<TFormEntry, 'index' | 'initialValues' | 'iVars' | 'stopEventsOnSubmit'> & TEventsCallbackProps<T> & OneOf<{
291
+ id: string;
292
+ index: string;
293
+ }>;
294
+
295
+ /**
296
+ * Hook to register events callback functions
297
+ */
298
+ declare function useForm<T>({ id, index, onData, onSubmit, onFormMount, onValid, iVars, initialValues, stopEventsOnSubmit, ...rest }: TUseFormProps<T>, deps?: React.DependencyList): void;
299
+
300
+ /**
301
+ * Represents the properties for the useFormGroup hook, including a list of ID's and event callback properties.
302
+ *
303
+ * @property {string} ids - The unique identifier for the forms.
304
+ * @property {payload: TFormGroupOnDataEventPayload<T>} onData - callback event occurring when a value is changing via input or logic.
305
+ * @property {payload: TFormGroupOnValidEventPayload} onData - callback event occurring when validation status changes on the form.
306
+ * @property {payload: TFormGroupOnSubmitEventPayload<T>} onData - event occurring when form submission is trigger.
307
+ *
308
+ */
309
+ type TUseFormGroup<T> = {
310
+ ids: string[];
311
+ onData?: (payload: TFormGroupOnDataEventPayload<T>) => void;
312
+ onValid?: (payload: TFormGroupOnValidEventPayload) => void;
313
+ onSubmit?: (payload: TFormGroupOnSubmitEventPayload<T>) => void;
314
+ };
315
+
316
+ declare function useFormGroup<T>({ ids, onData, onValid, onSubmit }: TUseFormGroup<T>, deps?: React.DependencyList): void;
317
+
318
+ declare const defaultChangeEvent: TValueChangeEvent;
319
+ declare const checkedChangeEvent: TValueChangeEvent;
320
+ declare const valueChangeEvent: TValueChangeEvent;
321
+ declare const numberInputChangeEvent: TValueChangeEvent;
322
+ declare const datepickerChangeEvent: TValueChangeEvent;
323
+ declare const dropdownChangeEvent: TValueChangeEvent;
324
+
325
+ export { AsFormField, AsFormFieldBuilder, AsFormFieldRepeater, Form, FormGroupContext, FormGroupContextProvider, checkedChangeEvent, datepickerChangeEvent, defaultChangeEvent, dropdownChangeEvent, numberInputChangeEvent, useForm, useFormGroup, useFormGroupContext, valueChangeEvent };
326
+ export type { TAsFormFieldBuilderProps, TAsFormFieldProps, TAsFormFieldRepeaterProps, TEventsCallbackProps, TFieldWrapper, TFieldWrapperComponentRenderProps, TFieldWrapperProps, TFormContext, TFormContextProvider, TFormData, TFormProps, TRepeaterFooterProps, TRepeaterProps };