@bolttech/form-engine 3.0.7 → 3.0.9

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.
Files changed (3) hide show
  1. package/index.d.ts +68 -39
  2. package/index.esm.js +348 -348
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react from 'react';
2
2
  import { PropsWithChildren, ReactElement, ElementType, ReactNode } from 'react';
3
- import { TEventProps, TFieldEvent, TEventDataProps, TFormValues, TFormValidationPayload, TFormEntry, IComponentSchemaAsFormField, IComponentSchema, OneOf, TMapper, TFormGroupOnDataEventPayload, TFormGroupOnValidEventPayload, TFormGroupOnSubmitEventPayload, FormCore, TFormGroup, TSchemaFormConfig, TValueChangeEvent } from '@bolttech/form-engine-core';
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
4
 
5
5
  /**
6
6
  * Represents a field wrapper containing the name of the field and its index in the form.
@@ -157,43 +157,6 @@ type TAsFormFieldRepeaterProps = {
157
157
  */
158
158
  declare const AsFormFieldRepeater: ({ RepeaterComponent, addFieldName, removeFieldName, existingElements, initialElements, stateUpdater, formPrefix, RepeaterFooter, }: TAsFormFieldRepeaterProps) => ReactNode;
159
159
 
160
- /**
161
- * Represents the properties for the useForm hook, including an ID and event callback properties.
162
- *
163
- * @property {string} id - The unique identifier for the form. Deprecated, use 'index' instead
164
- * @property {string} index - The unique identifier for the form.
165
- *
166
- * @see {@link TEventsCallbackProps}
167
- * @interface
168
- */
169
- type TUseFormProps<T> = Pick<TFormEntry, 'index' | 'initialValues' | 'iVars' | 'stopEventsOnSubmit'> & TEventsCallbackProps<T> & OneOf<{
170
- id: string;
171
- index: string;
172
- }>;
173
-
174
- /**
175
- * Hook to register events callback functions
176
- */
177
- declare function useForm<T>({ id, index, onData, onSubmit, onFormMount, onValid, iVars, initialValues, stopEventsOnSubmit, ...rest }: TUseFormProps<T>, deps?: React.DependencyList): void;
178
-
179
- /**
180
- * Represents the properties for the useFormGroup hook, including a list of ID's and event callback properties.
181
- *
182
- * @property {string} ids - The unique identifier for the forms.
183
- * @property {payload: TFormGroupOnDataEventPayload<T>} onData - callback event occurring when a value is changing via input or logic.
184
- * @property {payload: TFormGroupOnValidEventPayload} onData - callback event occurring when validation status changes on the form.
185
- * @property {payload: TFormGroupOnSubmitEventPayload<T>} onData - event occurring when form submission is trigger.
186
- *
187
- */
188
- type TUseFormGroup<T> = {
189
- ids: string[];
190
- onData?: (payload: TFormGroupOnDataEventPayload<T>) => void;
191
- onValid?: (payload: TFormGroupOnValidEventPayload) => void;
192
- onSubmit?: (payload: TFormGroupOnSubmitEventPayload<T>) => void;
193
- };
194
-
195
- declare function useFormGroup<T>({ ids, onData, onValid, onSubmit }: TUseFormGroup<T>, deps?: React.DependencyList): void;
196
-
197
160
  /**
198
161
  * Represents the context for managing forms within a form group.
199
162
  *
@@ -286,6 +249,72 @@ declare const FormGroupContextProvider: ({ children, mappers, debugMode, config,
286
249
  */
287
250
  declare const useFormGroupContext: (props?: TFormContextProvider) => TFormContext;
288
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
+
289
318
  declare const defaultChangeEvent: TValueChangeEvent;
290
319
  declare const checkedChangeEvent: TValueChangeEvent;
291
320
  declare const valueChangeEvent: TValueChangeEvent;
@@ -294,4 +323,4 @@ declare const datepickerChangeEvent: TValueChangeEvent;
294
323
  declare const dropdownChangeEvent: TValueChangeEvent;
295
324
 
296
325
  export { AsFormField, AsFormFieldBuilder, AsFormFieldRepeater, Form, FormGroupContext, FormGroupContextProvider, checkedChangeEvent, datepickerChangeEvent, defaultChangeEvent, dropdownChangeEvent, numberInputChangeEvent, useForm, useFormGroup, useFormGroupContext, valueChangeEvent };
297
- export type { TEventsCallbackProps, TFieldWrapper, TFormContext, TFormData };
326
+ export type { TAsFormFieldBuilderProps, TAsFormFieldProps, TAsFormFieldRepeaterProps, TEventsCallbackProps, TFieldWrapper, TFieldWrapperComponentRenderProps, TFieldWrapperProps, TFormContext, TFormContextProvider, TFormData, TFormProps, TRepeaterFooterProps, TRepeaterProps };
package/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use client';
2
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
- import { createContext, useContext, useRef, useEffect, useState, useMemo, useCallback, Suspense } from 'react';
2
+ import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
3
+ import { createContext, useContext, useRef, useEffect, useMemo, useState, useCallback, Suspense } from 'react';
4
4
  import { FormGroup, FormField } from '@bolttech/form-engine-core';
5
5
 
6
6
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -2185,35 +2185,6 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
2185
2185
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
2186
2186
  };
2187
2187
 
2188
- const BuildSchemaAsFields = ({
2189
- components,
2190
- mappers,
2191
- formIndex,
2192
- mountedForm
2193
- }) => {
2194
- return components && components.map(_a => {
2195
- var {
2196
- component
2197
- } = _a,
2198
- componentEl = __rest(_a, ["component"]);
2199
- const mapper = mappers === null || mappers === void 0 ? void 0 : mappers.find(el => el.componentName === component);
2200
- return mapper ? jsx(AsFormFieldBuilder, Object.assign({
2201
- formIndex: formIndex,
2202
- mapper: mapper,
2203
- formMounted: mountedForm
2204
- }, componentEl, {
2205
- children: componentEl.children && componentEl.children.length > 0 && jsx(BuildSchemaAsFields, {
2206
- formIndex: formIndex,
2207
- mappers: mappers,
2208
- components: componentEl.children,
2209
- mountedForm: mountedForm
2210
- })
2211
- }), componentEl.name) : jsx("div", {
2212
- children: `component mapper not found for ${component} from field name ${componentEl.name} on form ${formIndex}`
2213
- }, componentEl.name);
2214
- });
2215
- };
2216
-
2217
2188
  const FormGroupContext = /*#__PURE__*/createContext({});
2218
2189
  /**
2219
2190
  * context interface to be used isolated or with the context provider
@@ -2338,68 +2309,356 @@ const useFormGroupContext = props => {
2338
2309
  return context;
2339
2310
  };
2340
2311
 
2341
- var NATIVE_BIND = functionBindNative;
2342
-
2343
- var FunctionPrototype = Function.prototype;
2344
- var apply$2 = FunctionPrototype.apply;
2345
- var call$2 = FunctionPrototype.call;
2346
-
2347
- // eslint-disable-next-line es/no-function-prototype-bind, es/no-reflect -- safe
2348
- var functionApply = typeof Reflect == 'object' && Reflect.apply || (NATIVE_BIND ? call$2.bind(apply$2) : function () {
2349
- return call$2.apply(apply$2, arguments);
2350
- });
2351
-
2352
- var defineProperty = objectDefineProperty.f;
2353
-
2354
- var proxyAccessor$1 = function (Target, Source, key) {
2355
- key in Target || defineProperty(Target, key, {
2356
- configurable: true,
2357
- get: function () { return Source[key]; },
2358
- set: function (it) { Source[key] = it; }
2312
+ /**
2313
+ * Renders the React element defined on the mappers configuration
2314
+ *
2315
+ * @param param component props, field instance and children to render
2316
+ * @returns
2317
+ */
2318
+ const FieldWrapperComponentRender = ({
2319
+ props,
2320
+ fieldInstance,
2321
+ children,
2322
+ mapper
2323
+ }) => {
2324
+ var _a, _b, _c;
2325
+ const Component = (mapper === null || mapper === void 0 ? void 0 : mapper.component) || ((_a = fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mapper) === null || _a === void 0 ? void 0 : _a.component);
2326
+ const Asynccomponent = (mapper === null || mapper === void 0 ? void 0 : mapper.asynccomponent) || ((_b = fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mapper) === null || _b === void 0 ? void 0 : _b.asynccomponent);
2327
+ if (Component) return jsx(Component, Object.assign({}, props, {
2328
+ children: children && children
2329
+ }));
2330
+ if (Asynccomponent) return jsx(Suspense, {
2331
+ children: jsx(Asynccomponent, Object.assign({}, props, {
2332
+ children: children && children
2333
+ }))
2334
+ });
2335
+ return jsx("div", {
2336
+ children: `failed to render field ${fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.name} with componentName:${(_c = fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mapper) === null || _c === void 0 ? void 0 : _c.componentName}, please check mappers`
2359
2337
  });
2360
2338
  };
2361
-
2362
- var isCallable = isCallable$g;
2363
- var isObject$1 = isObject$a;
2364
- var setPrototypeOf$1 = objectSetPrototypeOf;
2365
-
2366
- // makes subclassing work correct for wrapped built-ins
2367
- var inheritIfRequired$1 = function ($this, dummy, Wrapper) {
2368
- var NewTarget, NewTargetPrototype;
2369
- if (
2370
- // it can work only with native `setPrototypeOf`
2371
- setPrototypeOf$1 &&
2372
- // we haven't completely correct pre-ES6 way for getting `new.target`, so use this
2373
- isCallable(NewTarget = dummy.constructor) &&
2374
- NewTarget !== Wrapper &&
2375
- isObject$1(NewTargetPrototype = NewTarget.prototype) &&
2376
- NewTargetPrototype !== Wrapper.prototype
2377
- ) setPrototypeOf$1($this, NewTargetPrototype);
2378
- return $this;
2379
- };
2380
-
2381
- var classof$2 = classof$4;
2382
-
2383
- var $String = String;
2384
-
2385
- var toString$2 = function (argument) {
2386
- if (classof$2(argument) === 'Symbol') throw new TypeError('Cannot convert a Symbol value to a string');
2387
- return $String(argument);
2388
- };
2389
-
2390
- var toString$1 = toString$2;
2391
-
2392
- var normalizeStringArgument$1 = function (argument, $default) {
2393
- return argument === undefined ? arguments.length < 2 ? '' : $default : toString$1(argument);
2339
+ /**
2340
+ * Base field Wrapper to render the component with the necessary configurations from the schema
2341
+ * and mapper configuration
2342
+ *
2343
+ * @param {TFieldWrapperProps} param FieldWrapper params
2344
+ * @returns {ReactElement}
2345
+ */
2346
+ const FieldWrapper = ({
2347
+ name,
2348
+ formIndex,
2349
+ children,
2350
+ props,
2351
+ context,
2352
+ mounted,
2353
+ mapper,
2354
+ visibility,
2355
+ component
2356
+ }) => {
2357
+ var _a, _b, _c;
2358
+ const localContext = useFormGroupContext({});
2359
+ const fieldMapper = mapper || ((_a = localContext.mappers) === null || _a === void 0 ? void 0 : _a.find(mapper => mapper.componentName === component));
2360
+ /**
2361
+ * picks the right context prioritizing the context passed as prop
2362
+ */
2363
+ const {
2364
+ formGroupInstance,
2365
+ debugMode
2366
+ } = useMemo(() => context ? context : localContext, [context, localContext]);
2367
+ /**
2368
+ * retrieves the field instance when it's mounted
2369
+ */
2370
+ const fieldInstance = useMemo(() => {
2371
+ var _a;
2372
+ return (_a = formGroupInstance.getForm({
2373
+ key: formIndex
2374
+ })) === null || _a === void 0 ? void 0 : _a.getField({
2375
+ key: name
2376
+ });
2377
+ }, [mounted]);
2378
+ /**
2379
+ * props with templates stripped until the field ins't ready on the instance
2380
+ */
2381
+ const filteredProps = useMemo(() => {
2382
+ return FormField.filterProps(props);
2383
+ }, []);
2384
+ /**
2385
+ * sends props changed on the adapter for comparison to effectively change props for the instance
2386
+ */
2387
+ useEffect(() => {
2388
+ if (fieldInstance && props) fieldInstance.adapterProps = props;
2389
+ }, [props]);
2390
+ const [valueState, setValueState] = useState((fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.stateValue) || {
2391
+ [((_b = fieldMapper === null || fieldMapper === void 0 ? void 0 : fieldMapper.events) === null || _b === void 0 ? void 0 : _b.setValue) || 'value']: ''
2392
+ });
2393
+ const [state, setState] = useState({
2394
+ visibility: typeof visibility === 'boolean' ? visibility : true,
2395
+ props: filteredProps
2396
+ });
2397
+ /**
2398
+ * handles the mounting and unmounting logic onto the field instance
2399
+ */
2400
+ useEffect(() => {
2401
+ if (!fieldInstance || (fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mounted)) return;
2402
+ fieldInstance.mountField({
2403
+ valueSubscription: value => {
2404
+ setValueState(value);
2405
+ },
2406
+ propsSubscription: ({
2407
+ visibility,
2408
+ props,
2409
+ errors
2410
+ }) => {
2411
+ setState(prev => Object.assign(Object.assign({}, prev), {
2412
+ visibility,
2413
+ props,
2414
+ errors
2415
+ }));
2416
+ }
2417
+ });
2418
+ }, [fieldInstance]);
2419
+ /**
2420
+ * recycle effect to remove the field Subscriptions due to memory leaks
2421
+ */
2422
+ useEffect(() => {
2423
+ return () => {
2424
+ (fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mounted) && (fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.destroyField());
2425
+ };
2426
+ }, []);
2427
+ /**
2428
+ * handles the value change onto the field instance
2429
+ */
2430
+ const handleChange = useCallback(event => {
2431
+ if (!mounted) return;
2432
+ fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.emitValue({
2433
+ value: event,
2434
+ event: 'ON_FIELD_CHANGE'
2435
+ });
2436
+ }, [mounted]);
2437
+ /**
2438
+ * handles the event emission onto the field instance
2439
+ */
2440
+ const handleEvent = useCallback(event => {
2441
+ if (!mounted) return;
2442
+ fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.emitEvents({
2443
+ event
2444
+ });
2445
+ }, [mounted]);
2446
+ /**
2447
+ * handles the mappers configuration to bind the event submission callback
2448
+ * to the corresponding prop defined on the mappers
2449
+ */
2450
+ const mapProps = useMemo(() => {
2451
+ const events = fieldMapper === null || fieldMapper === void 0 ? void 0 : fieldMapper.events;
2452
+ const props = {};
2453
+ if (events === null || events === void 0 ? void 0 : events.onBlur) props[events.onBlur] = () => handleEvent('ON_FIELD_BLUR');
2454
+ if (events === null || events === void 0 ? void 0 : events.getValue) props[events.getValue] = handleChange;
2455
+ if (events === null || events === void 0 ? void 0 : events.onFocus) props[events.onFocus] = () => handleEvent('ON_FIELD_FOCUS');
2456
+ if (events === null || events === void 0 ? void 0 : events.onClick) props[events.onClick] = () => handleEvent('ON_FIELD_CLICK');
2457
+ if (events === null || events === void 0 ? void 0 : events.onSubmit) props[events.onSubmit] = () => handleEvent('ON_FORM_SUBMIT');
2458
+ if (events === null || events === void 0 ? void 0 : events.onKeyUp) props[events.onKeyUp] = () => handleEvent('ON_FIELD_KEYUP');
2459
+ if (events === null || events === void 0 ? void 0 : events.onKeyDown) props[events.onKeyDown] = () => handleEvent('ON_FIELD_KEYDOWN');
2460
+ return props;
2461
+ }, [mounted]);
2462
+ return state.visibility ? jsxs(Fragment, {
2463
+ children: [debugMode && jsxs("div", {
2464
+ style: {
2465
+ width: '100%',
2466
+ display: 'flex',
2467
+ flexDirection: 'column'
2468
+ },
2469
+ children: [jsxs("b", {
2470
+ style: {
2471
+ padding: '0px',
2472
+ margin: '0px'
2473
+ },
2474
+ children: ["name:", name]
2475
+ }), jsx("br", {}), jsx("hr", {})]
2476
+ }), jsx(FieldWrapperComponentRender, {
2477
+ props: Object.assign(Object.assign(Object.assign(Object.assign({}, mapProps), state.props), state.errors), valueState),
2478
+ fieldInstance: fieldInstance,
2479
+ mapper: fieldMapper,
2480
+ children: children ? children : ((_c = state === null || state === void 0 ? void 0 : state.props) === null || _c === void 0 ? void 0 : _c.children) ? state === null || state === void 0 ? void 0 : state.props.children : null
2481
+ })]
2482
+ }) : jsx(Fragment, {});
2394
2483
  };
2395
2484
 
2396
- var isObject = isObject$a;
2397
- var createNonEnumerableProperty$2 = createNonEnumerableProperty$8;
2398
-
2399
- // `InstallErrorCause` abstract operation
2400
- // https://tc39.es/ecma262/#sec-installerrorcause
2401
- var installErrorCause$1 = function (O, options) {
2402
- if (isObject(options) && 'cause' in options) {
2485
+ /**
2486
+ * Component Wrapper to render form fields without the Form component, gets additional formId and mapper since
2487
+ * it won't rely on them and needs to be manually declared
2488
+ *
2489
+ * @param {TAsFormFieldBuilderProps} props JSON schema props along with FieldWrapper props and mapper props
2490
+ * @returns {ReactElement}
2491
+ */
2492
+ const AsFormFieldBuilder = props => {
2493
+ const context = useFormGroupContext({});
2494
+ /**
2495
+ * state to track the field instance creation process
2496
+ */
2497
+ const [mounted, setMounted] = useState(false);
2498
+ const mountedRef = useRef(false);
2499
+ /**
2500
+ * initializer to create or add a form instance to the formGroup by it's formId
2501
+ * and add the field to the form instance
2502
+ * Also has the logic to remove it once this element is removed
2503
+ */
2504
+ useEffect(() => {
2505
+ var _a, _b;
2506
+ if (mountedRef.current) return;
2507
+ if (typeof props.formMounted === 'undefined' && !((_a = context.formGroupInstance) === null || _a === void 0 ? void 0 : _a.forms.has(props.formIndex))) {
2508
+ context.addFormWithIndex(props.formIndex);
2509
+ }
2510
+ if (props.formMounted || typeof props.formMounted === 'undefined') {
2511
+ const fieldSchema = Object.assign(Object.assign({}, props), {
2512
+ component: ((_b = props.mapper) === null || _b === void 0 ? void 0 : _b.componentName) || props.component,
2513
+ //forcing 1 option with OneOf
2514
+ children: undefined
2515
+ });
2516
+ const formInstance = context.formGroupInstance.forms.get(props.formIndex);
2517
+ formInstance === null || formInstance === void 0 ? void 0 : formInstance.addField({
2518
+ fieldSchema,
2519
+ mapperElement: props.mapper
2520
+ });
2521
+ setMounted(true);
2522
+ mountedRef.current = true;
2523
+ }
2524
+ }, [props.formMounted]);
2525
+ /**
2526
+ * recycle effect to remove the field from the form instance when the field leaves the vDOM
2527
+ * and the subscriptions
2528
+ */
2529
+ useEffect(() => {
2530
+ return () => {
2531
+ var _a, _b;
2532
+ if ((_b = (_a = context.getForm({
2533
+ key: props.formIndex
2534
+ })) === null || _a === void 0 ? void 0 : _a.getField({
2535
+ key: props.name
2536
+ })) === null || _b === void 0 ? void 0 : _b.mounted) context.formGroupInstance.removeField({
2537
+ formIndex: props.formIndex,
2538
+ fieldIndex: props.name
2539
+ });
2540
+ };
2541
+ }, []);
2542
+ /**
2543
+ * allows to control field selected value on each event
2544
+ */
2545
+ useEffect(() => {
2546
+ var _a;
2547
+ if (!props.onSelected) return;
2548
+ const callback = payload => {
2549
+ if (props.onSelected) {
2550
+ props.onSelected(payload);
2551
+ }
2552
+ };
2553
+ const sub = (_a = context.formGroupInstance.forms.get(props.formIndex)) === null || _a === void 0 ? void 0 : _a.subscribeFieldEvent({
2554
+ callback
2555
+ });
2556
+ return () => sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
2557
+ }, [props.onSelected]);
2558
+ return jsx(FieldWrapper, {
2559
+ formIndex: props.formIndex,
2560
+ name: props.name,
2561
+ props: props.props,
2562
+ context: !context.active ? context : null,
2563
+ mounted: mounted,
2564
+ mapper: props.mapper,
2565
+ visibility: props.visibility,
2566
+ component: props.component,
2567
+ children: props.children && props.children
2568
+ });
2569
+ };
2570
+
2571
+ const BuildSchemaAsFields = ({
2572
+ components,
2573
+ mappers,
2574
+ formIndex,
2575
+ mountedForm
2576
+ }) => {
2577
+ return components && components.map(_a => {
2578
+ var {
2579
+ component
2580
+ } = _a,
2581
+ componentEl = __rest(_a, ["component"]);
2582
+ const mapper = mappers === null || mappers === void 0 ? void 0 : mappers.find(el => el.componentName === component);
2583
+ return mapper ? jsx(AsFormFieldBuilder, Object.assign({
2584
+ formIndex: formIndex,
2585
+ mapper: mapper,
2586
+ formMounted: mountedForm
2587
+ }, componentEl, {
2588
+ children: componentEl.children && componentEl.children.length > 0 && jsx(BuildSchemaAsFields, {
2589
+ formIndex: formIndex,
2590
+ mappers: mappers,
2591
+ components: componentEl.children,
2592
+ mountedForm: mountedForm
2593
+ })
2594
+ }), componentEl.name) : jsx("div", {
2595
+ children: `component mapper not found for ${component} from field name ${componentEl.name} on form ${formIndex}`
2596
+ }, componentEl.name);
2597
+ });
2598
+ };
2599
+
2600
+ var NATIVE_BIND = functionBindNative;
2601
+
2602
+ var FunctionPrototype = Function.prototype;
2603
+ var apply$2 = FunctionPrototype.apply;
2604
+ var call$2 = FunctionPrototype.call;
2605
+
2606
+ // eslint-disable-next-line es/no-function-prototype-bind, es/no-reflect -- safe
2607
+ var functionApply = typeof Reflect == 'object' && Reflect.apply || (NATIVE_BIND ? call$2.bind(apply$2) : function () {
2608
+ return call$2.apply(apply$2, arguments);
2609
+ });
2610
+
2611
+ var defineProperty = objectDefineProperty.f;
2612
+
2613
+ var proxyAccessor$1 = function (Target, Source, key) {
2614
+ key in Target || defineProperty(Target, key, {
2615
+ configurable: true,
2616
+ get: function () { return Source[key]; },
2617
+ set: function (it) { Source[key] = it; }
2618
+ });
2619
+ };
2620
+
2621
+ var isCallable = isCallable$g;
2622
+ var isObject$1 = isObject$a;
2623
+ var setPrototypeOf$1 = objectSetPrototypeOf;
2624
+
2625
+ // makes subclassing work correct for wrapped built-ins
2626
+ var inheritIfRequired$1 = function ($this, dummy, Wrapper) {
2627
+ var NewTarget, NewTargetPrototype;
2628
+ if (
2629
+ // it can work only with native `setPrototypeOf`
2630
+ setPrototypeOf$1 &&
2631
+ // we haven't completely correct pre-ES6 way for getting `new.target`, so use this
2632
+ isCallable(NewTarget = dummy.constructor) &&
2633
+ NewTarget !== Wrapper &&
2634
+ isObject$1(NewTargetPrototype = NewTarget.prototype) &&
2635
+ NewTargetPrototype !== Wrapper.prototype
2636
+ ) setPrototypeOf$1($this, NewTargetPrototype);
2637
+ return $this;
2638
+ };
2639
+
2640
+ var classof$2 = classof$4;
2641
+
2642
+ var $String = String;
2643
+
2644
+ var toString$2 = function (argument) {
2645
+ if (classof$2(argument) === 'Symbol') throw new TypeError('Cannot convert a Symbol value to a string');
2646
+ return $String(argument);
2647
+ };
2648
+
2649
+ var toString$1 = toString$2;
2650
+
2651
+ var normalizeStringArgument$1 = function (argument, $default) {
2652
+ return argument === undefined ? arguments.length < 2 ? '' : $default : toString$1(argument);
2653
+ };
2654
+
2655
+ var isObject = isObject$a;
2656
+ var createNonEnumerableProperty$2 = createNonEnumerableProperty$8;
2657
+
2658
+ // `InstallErrorCause` abstract operation
2659
+ // https://tc39.es/ecma262/#sec-installerrorcause
2660
+ var installErrorCause$1 = function (O, options) {
2661
+ if (isObject(options) && 'cause' in options) {
2403
2662
  createNonEnumerableProperty$2(O, 'cause', options.cause);
2404
2663
  }
2405
2664
  };
@@ -3180,265 +3439,6 @@ const AsFormField = props => {
3180
3439
  return props.children;
3181
3440
  };
3182
3441
 
3183
- /**
3184
- * Renders the React element defined on the mappers configuration
3185
- *
3186
- * @param param component props, field instance and children to render
3187
- * @returns
3188
- */
3189
- const FieldWrapperComponentRender = ({
3190
- props,
3191
- fieldInstance,
3192
- children,
3193
- mapper
3194
- }) => {
3195
- var _a, _b, _c;
3196
- const Component = (mapper === null || mapper === void 0 ? void 0 : mapper.component) || ((_a = fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mapper) === null || _a === void 0 ? void 0 : _a.component);
3197
- const Asynccomponent = (mapper === null || mapper === void 0 ? void 0 : mapper.asynccomponent) || ((_b = fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mapper) === null || _b === void 0 ? void 0 : _b.asynccomponent);
3198
- if (Component) return jsx(Component, Object.assign({}, props, {
3199
- children: children && children
3200
- }));
3201
- if (Asynccomponent) return jsx(Suspense, {
3202
- children: jsx(Asynccomponent, Object.assign({}, props, {
3203
- children: children && children
3204
- }))
3205
- });
3206
- return jsx("div", {
3207
- children: `failed to render field ${fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.name} with componentName:${(_c = fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mapper) === null || _c === void 0 ? void 0 : _c.componentName}, please check mappers`
3208
- });
3209
- };
3210
- /**
3211
- * Base field Wrapper to render the component with the necessary configurations from the schema
3212
- * and mapper configuration
3213
- *
3214
- * @param {TFieldWrapperProps} param FieldWrapper params
3215
- * @returns {ReactElement}
3216
- */
3217
- const FieldWrapper = ({
3218
- name,
3219
- formIndex,
3220
- children,
3221
- props,
3222
- context,
3223
- mounted,
3224
- mapper,
3225
- visibility,
3226
- component
3227
- }) => {
3228
- var _a, _b, _c;
3229
- const localContext = useFormGroupContext({});
3230
- const fieldMapper = mapper || ((_a = localContext.mappers) === null || _a === void 0 ? void 0 : _a.find(mapper => mapper.componentName === component));
3231
- /**
3232
- * picks the right context prioritizing the context passed as prop
3233
- */
3234
- const {
3235
- formGroupInstance,
3236
- debugMode
3237
- } = useMemo(() => context ? context : localContext, [context, localContext]);
3238
- /**
3239
- * retrieves the field instance when it's mounted
3240
- */
3241
- const fieldInstance = useMemo(() => {
3242
- var _a;
3243
- return (_a = formGroupInstance.getForm({
3244
- key: formIndex
3245
- })) === null || _a === void 0 ? void 0 : _a.getField({
3246
- key: name
3247
- });
3248
- }, [mounted]);
3249
- /**
3250
- * props with templates stripped until the field ins't ready on the instance
3251
- */
3252
- const filteredProps = useMemo(() => {
3253
- return FormField.filterProps(props);
3254
- }, []);
3255
- /**
3256
- * sends props changed on the adapter for comparison to effectively change props for the instance
3257
- */
3258
- useEffect(() => {
3259
- if (fieldInstance && props) fieldInstance.adapterProps = props;
3260
- }, [props]);
3261
- const [valueState, setValueState] = useState((fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.stateValue) || {
3262
- [((_b = fieldMapper === null || fieldMapper === void 0 ? void 0 : fieldMapper.events) === null || _b === void 0 ? void 0 : _b.setValue) || 'value']: ''
3263
- });
3264
- const [state, setState] = useState({
3265
- visibility: typeof visibility === 'boolean' ? visibility : true,
3266
- props: filteredProps
3267
- });
3268
- /**
3269
- * handles the mounting and unmounting logic onto the field instance
3270
- */
3271
- useEffect(() => {
3272
- if (!fieldInstance || (fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mounted)) return;
3273
- fieldInstance.mountField({
3274
- valueSubscription: value => {
3275
- setValueState(value);
3276
- },
3277
- propsSubscription: ({
3278
- visibility,
3279
- props,
3280
- errors
3281
- }) => {
3282
- setState(prev => Object.assign(Object.assign({}, prev), {
3283
- visibility,
3284
- props,
3285
- errors
3286
- }));
3287
- }
3288
- });
3289
- }, [fieldInstance]);
3290
- /**
3291
- * recycle effect to remove the field Subscriptions due to memory leaks
3292
- */
3293
- useEffect(() => {
3294
- return () => {
3295
- (fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.mounted) && (fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.destroyField());
3296
- };
3297
- }, []);
3298
- /**
3299
- * handles the value change onto the field instance
3300
- */
3301
- const handleChange = useCallback(event => {
3302
- if (!mounted) return;
3303
- fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.emitValue({
3304
- value: event,
3305
- event: 'ON_FIELD_CHANGE'
3306
- });
3307
- }, [mounted]);
3308
- /**
3309
- * handles the event emission onto the field instance
3310
- */
3311
- const handleEvent = useCallback(event => {
3312
- if (!mounted) return;
3313
- fieldInstance === null || fieldInstance === void 0 ? void 0 : fieldInstance.emitEvents({
3314
- event
3315
- });
3316
- }, [mounted]);
3317
- /**
3318
- * handles the mappers configuration to bind the event submission callback
3319
- * to the corresponding prop defined on the mappers
3320
- */
3321
- const mapProps = useMemo(() => {
3322
- const events = fieldMapper === null || fieldMapper === void 0 ? void 0 : fieldMapper.events;
3323
- const props = {};
3324
- if (events === null || events === void 0 ? void 0 : events.onBlur) props[events.onBlur] = () => handleEvent('ON_FIELD_BLUR');
3325
- if (events === null || events === void 0 ? void 0 : events.getValue) props[events.getValue] = handleChange;
3326
- if (events === null || events === void 0 ? void 0 : events.onFocus) props[events.onFocus] = () => handleEvent('ON_FIELD_FOCUS');
3327
- if (events === null || events === void 0 ? void 0 : events.onClick) props[events.onClick] = () => handleEvent('ON_FIELD_CLICK');
3328
- if (events === null || events === void 0 ? void 0 : events.onSubmit) props[events.onSubmit] = () => handleEvent('ON_FORM_SUBMIT');
3329
- if (events === null || events === void 0 ? void 0 : events.onKeyUp) props[events.onKeyUp] = () => handleEvent('ON_FIELD_KEYUP');
3330
- if (events === null || events === void 0 ? void 0 : events.onKeyDown) props[events.onKeyDown] = () => handleEvent('ON_FIELD_KEYDOWN');
3331
- return props;
3332
- }, [mounted]);
3333
- return state.visibility ? jsxs(Fragment, {
3334
- children: [debugMode && jsxs("div", {
3335
- style: {
3336
- width: '100%',
3337
- display: 'flex',
3338
- flexDirection: 'column'
3339
- },
3340
- children: [jsxs("b", {
3341
- style: {
3342
- padding: '0px',
3343
- margin: '0px'
3344
- },
3345
- children: ["name:", name]
3346
- }), jsx("br", {}), jsx("hr", {})]
3347
- }), jsx(FieldWrapperComponentRender, {
3348
- props: Object.assign(Object.assign(Object.assign(Object.assign({}, mapProps), state.props), state.errors), valueState),
3349
- fieldInstance: fieldInstance,
3350
- mapper: fieldMapper,
3351
- children: children ? children : ((_c = state === null || state === void 0 ? void 0 : state.props) === null || _c === void 0 ? void 0 : _c.children) ? state === null || state === void 0 ? void 0 : state.props.children : null
3352
- })]
3353
- }) : jsx(Fragment, {});
3354
- };
3355
-
3356
- /**
3357
- * Component Wrapper to render form fields without the Form component, gets additional formId and mapper since
3358
- * it won't rely on them and needs to be manually declared
3359
- *
3360
- * @param {TAsFormFieldBuilderProps} props JSON schema props along with FieldWrapper props and mapper props
3361
- * @returns {ReactElement}
3362
- */
3363
- const AsFormFieldBuilder = props => {
3364
- const context = useFormGroupContext({});
3365
- /**
3366
- * state to track the field instance creation process
3367
- */
3368
- const [mounted, setMounted] = useState(false);
3369
- const mountedRef = useRef(false);
3370
- /**
3371
- * initializer to create or add a form instance to the formGroup by it's formId
3372
- * and add the field to the form instance
3373
- * Also has the logic to remove it once this element is removed
3374
- */
3375
- useEffect(() => {
3376
- var _a, _b;
3377
- if (mountedRef.current) return;
3378
- if (typeof props.formMounted === 'undefined' && !((_a = context.formGroupInstance) === null || _a === void 0 ? void 0 : _a.forms.has(props.formIndex))) {
3379
- context.addFormWithIndex(props.formIndex);
3380
- }
3381
- if (props.formMounted || typeof props.formMounted === 'undefined') {
3382
- const fieldSchema = Object.assign(Object.assign({}, props), {
3383
- component: ((_b = props.mapper) === null || _b === void 0 ? void 0 : _b.componentName) || props.component,
3384
- //forcing 1 option with OneOf
3385
- children: undefined
3386
- });
3387
- const formInstance = context.formGroupInstance.forms.get(props.formIndex);
3388
- formInstance === null || formInstance === void 0 ? void 0 : formInstance.addField({
3389
- fieldSchema,
3390
- mapperElement: props.mapper
3391
- });
3392
- setMounted(true);
3393
- mountedRef.current = true;
3394
- }
3395
- }, [props.formMounted]);
3396
- /**
3397
- * recycle effect to remove the field from the form instance when the field leaves the vDOM
3398
- * and the subscriptions
3399
- */
3400
- useEffect(() => {
3401
- return () => {
3402
- var _a, _b;
3403
- if ((_b = (_a = context.getForm({
3404
- key: props.formIndex
3405
- })) === null || _a === void 0 ? void 0 : _a.getField({
3406
- key: props.name
3407
- })) === null || _b === void 0 ? void 0 : _b.mounted) context.formGroupInstance.removeField({
3408
- formIndex: props.formIndex,
3409
- fieldIndex: props.name
3410
- });
3411
- };
3412
- }, []);
3413
- /**
3414
- * allows to control field selected value on each event
3415
- */
3416
- useEffect(() => {
3417
- var _a;
3418
- if (!props.onSelected) return;
3419
- const callback = payload => {
3420
- if (props.onSelected) {
3421
- props.onSelected(payload);
3422
- }
3423
- };
3424
- const sub = (_a = context.formGroupInstance.forms.get(props.formIndex)) === null || _a === void 0 ? void 0 : _a.subscribeFieldEvent({
3425
- callback
3426
- });
3427
- return () => sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
3428
- }, [props.onSelected]);
3429
- return jsx(FieldWrapper, {
3430
- formIndex: props.formIndex,
3431
- name: props.name,
3432
- props: props.props,
3433
- context: !context.active ? context : null,
3434
- mounted: mounted,
3435
- mapper: props.mapper,
3436
- visibility: props.visibility,
3437
- component: props.component,
3438
- children: props.children && props.children
3439
- });
3440
- };
3441
-
3442
3442
  var classof$1 = classofRaw$2;
3443
3443
 
3444
3444
  // `IsArray` abstract operation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine",
3
- "version": "3.0.7",
3
+ "version": "3.0.9",
4
4
  "description": "A react adapter for bolttech form engine",
5
5
  "dependencies": {
6
6
  "@bolttech/form-engine-core": "1.0.5"