@bolttech/form-engine 3.1.0-beta.1 → 3.1.0-beta.2

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 (2) hide show
  1. package/index.esm.js +23 -62
  2. package/package.json +2 -2
package/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
2
2
  import { FormGroup, FormCore } from '@bolttech/form-engine-core';
3
- import { createContext, useContext, useRef, useMemo, useState, useEffect, useCallback, Suspense, Children, Fragment as Fragment$1 } from 'react';
3
+ import { createContext, useContext, useRef, useMemo, useState, useEffect, useCallback, Suspense, Children } from 'react';
4
4
 
5
5
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
6
6
 
@@ -2523,8 +2523,9 @@ function Form({
2523
2523
  debugMode,
2524
2524
  formGroupInstance
2525
2525
  } = useFormGroupContext({});
2526
- const [schemaIndex, setSchemaIndex] = useState(`${index || (schema === null || schema === void 0 ? void 0 : schema.index) || 'defaultChange'}-ssr`);
2527
- const [formInstance, setFormInstance] = useState(new FormCore({
2526
+ const schemaIndex = useMemo(() => index || (schema === null || schema === void 0 ? void 0 : schema.index) || 'defaultChange', [index, schema === null || schema === void 0 ? void 0 : schema.index]);
2527
+ const [render, setRender] = useState(false);
2528
+ const formInstance = useRef(new FormCore({
2528
2529
  schema: Object.assign(Object.assign({}, schema), {
2529
2530
  index: schemaIndex
2530
2531
  }),
@@ -2541,7 +2542,7 @@ function Form({
2541
2542
  }
2542
2543
  if (!formGroupInstance.forms.has(schemaIndex)) addForm({
2543
2544
  key: schemaIndex,
2544
- formInstance
2545
+ formInstance: formInstance.current
2545
2546
  });
2546
2547
  /**
2547
2548
  * logic to transform AsFormFields onto JSON schema
@@ -2564,11 +2565,9 @@ function Form({
2564
2565
  fields,
2565
2566
  formIndex: schemaIndex
2566
2567
  });
2567
- return jsx(Fragment$1, {
2568
- children: buildTree
2569
- }, schemaIndex);
2568
+ return buildTree;
2570
2569
  }
2571
- }, [children, schemaIndex, getForm]);
2570
+ }, [children, getForm]);
2572
2571
  /**
2573
2572
  * iVars change tracker to update iVars onto form instance
2574
2573
  */
@@ -2589,40 +2588,23 @@ function Form({
2589
2588
  * logic to mount and manage form removal
2590
2589
  */
2591
2590
  useEffect(() => {
2592
- const newIndex = index || (schema === null || schema === void 0 ? void 0 : schema.index) || 'defaultChange';
2593
- const newInstance = new FormCore({
2594
- schema,
2595
- initialValues: initialValues || (schema === null || schema === void 0 ? void 0 : schema.initialValues),
2596
- iVars: iVars || (schema === null || schema === void 0 ? void 0 : schema.iVars),
2597
- action: action || (schema === null || schema === void 0 ? void 0 : schema.action),
2598
- method: method || (schema === null || schema === void 0 ? void 0 : schema.method),
2599
- index: newIndex,
2600
- mappers,
2601
- config: config || formGroupInstance.config
2602
- });
2603
- setSchemaIndex(newIndex);
2604
- setFormInstance(newInstance);
2605
- addForm({
2606
- key: newIndex,
2607
- formInstance: newInstance
2608
- });
2591
+ setRender(true);
2609
2592
  let subMounted;
2610
2593
  if (onFormMount) {
2611
- subMounted = newInstance.subscribeOnMount(onFormMount);
2594
+ subMounted = formInstance.current.subscribeOnMount(onFormMount);
2612
2595
  }
2613
- newInstance.mounted();
2596
+ formInstance.current.mounted();
2614
2597
  /*
2615
2598
  @TODO check if form instance is killed each time it is unmounted
2616
2599
  the management of multiple forms needs to be planned
2617
2600
  */
2618
2601
  return () => {
2619
2602
  subMounted === null || subMounted === void 0 ? void 0 : subMounted.unsubscribe();
2620
- removeForm({
2621
- key: newIndex
2622
- });
2623
- if (formGroupInstance.forms.has(`${newIndex}-ssr`)) removeForm({
2624
- key: `${newIndex}-ssr`
2625
- });
2603
+ if (render) {
2604
+ removeForm({
2605
+ key: schemaIndex
2606
+ });
2607
+ }
2626
2608
  };
2627
2609
  }, []);
2628
2610
  /**
@@ -2642,7 +2624,7 @@ function Form({
2642
2624
  onFormMount,
2643
2625
  onData,
2644
2626
  onValid
2645
- });
2627
+ }, [formInstance]);
2646
2628
  /*
2647
2629
  @TODO move this logic inside form-core, add action and method onto form element,
2648
2630
  might need a ref of the form to collect all the form parameters when there is a
@@ -2897,7 +2879,8 @@ $({ target: 'RegExp', proto: true, forced: /./.exec !== exec }, {
2897
2879
  const AsFormFieldBuilder = props => {
2898
2880
  var _a;
2899
2881
  const context = useFormGroupContext({});
2900
- const [fieldName, setFieldName] = useState(`${props.name}-ssr`);
2882
+ const [render, setRender] = useState(false);
2883
+ const fieldName = props.name;
2901
2884
  if (!((_a = context.formGroupInstance) === null || _a === void 0 ? void 0 : _a.forms.has(props.formIndex))) context.addFormWithIndex(props.formIndex);
2902
2885
  const formInstance = context.formGroupInstance.forms.get(props.formIndex);
2903
2886
  if (!(formInstance === null || formInstance === void 0 ? void 0 : formInstance.fields.has(fieldName))) {
@@ -2924,35 +2907,13 @@ const AsFormFieldBuilder = props => {
2924
2907
  * Also has the logic to remove it once this element is removed
2925
2908
  */
2926
2909
  useEffect(() => {
2927
- var _a;
2928
- setFieldName(props.name);
2929
- if (!((_a = context.formGroupInstance) === null || _a === void 0 ? void 0 : _a.forms.has(props.formIndex))) {
2930
- context.addFormWithIndex(props.formIndex);
2931
- }
2932
- const fieldSchema = Object.assign(Object.assign({}, props), {
2933
- component: props.mapper.componentName,
2934
- children: undefined
2935
- });
2936
- const formInstance = context.formGroupInstance.forms.get(props.formIndex);
2937
- formInstance === null || formInstance === void 0 ? void 0 : formInstance.addField({
2938
- fieldSchema,
2939
- mapperElement: props.mapper
2940
- });
2941
- const field = formInstance === null || formInstance === void 0 ? void 0 : formInstance.getField({
2942
- key: props.name
2943
- });
2944
- if (typeof (props === null || props === void 0 ? void 0 : props.visibility) !== 'undefined' && field) {
2945
- field.visibility = props.visibility;
2946
- }
2910
+ setRender(true);
2947
2911
  return () => {
2948
- formInstance === null || formInstance === void 0 ? void 0 : formInstance.removeField({
2949
- key: props.name
2950
- });
2951
- if (formInstance === null || formInstance === void 0 ? void 0 : formInstance.fields.has(`${props.name}-ssr`)) formInstance === null || formInstance === void 0 ? void 0 : formInstance.removeField({
2952
- key: `${props.name}-ssr`
2912
+ if (render) formInstance === null || formInstance === void 0 ? void 0 : formInstance.removeField({
2913
+ key: fieldName
2953
2914
  });
2954
2915
  };
2955
- }, [fieldName]);
2916
+ }, []);
2956
2917
  /**
2957
2918
  * expensive function to allow both external props and templating work
2958
2919
  * evaluate if templating will be done onto form-engine or not
@@ -2997,7 +2958,7 @@ const AsFormFieldBuilder = props => {
2997
2958
  props: props.props,
2998
2959
  context: !context.active ? context : null,
2999
2960
  children: props.children && props.children
3000
- }, fieldName);
2961
+ });
3001
2962
  };
3002
2963
 
3003
2964
  function useFormGroup({
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine",
3
- "version": "3.1.0-beta.1",
3
+ "version": "3.1.0-beta.2",
4
4
  "description": "A react adapter for bolttech form engine",
5
5
  "module": "./index.esm.js",
6
6
  "type": "module",
7
7
  "main": "./index.esm.js",
8
8
  "dependencies": {
9
- "@bolttech/form-engine-core": "1.0.0-beta.1",
9
+ "@bolttech/form-engine-core": "1.0.0-beta.2",
10
10
  "react": "18.2.0",
11
11
  "rxjs": "7.8.1"
12
12
  },