@bolttech/form-engine 3.0.0-beta.11 → 3.0.0-beta.13

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.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, useRef, useContext, useState, useEffect, useCallback, useMemo, Children, Suspense } from 'react';
3
+ import { createContext, useRef, useContext, useState, useEffect, useCallback, useMemo, 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
 
@@ -1624,6 +1624,12 @@ const FormGroupContextProvider = ({
1624
1624
  debugMode: _debugMode = false
1625
1625
  }) => {
1626
1626
  const formGroupInstance = useRef(new FormGroup());
1627
+ const addFormWithIndex = index => {
1628
+ formGroupInstance.current.createFormWithIndex({
1629
+ index,
1630
+ mappers
1631
+ });
1632
+ };
1627
1633
  const addForm = ({
1628
1634
  key,
1629
1635
  formInstance
@@ -1640,6 +1646,9 @@ const FormGroupContextProvider = ({
1640
1646
  key
1641
1647
  });
1642
1648
  };
1649
+ const removeField = payload => {
1650
+ formGroupInstance.current.removeField(payload);
1651
+ };
1643
1652
  const getForm = ({
1644
1653
  key
1645
1654
  }) => formGroupInstance.current.getForm({
@@ -1652,9 +1661,11 @@ const FormGroupContextProvider = ({
1652
1661
  return formGroupInstance.current.submitMultipleFormsByIndex(indexes);
1653
1662
  };
1654
1663
  const contextValue = {
1664
+ addFormWithIndex,
1655
1665
  addForm,
1656
1666
  getForm,
1657
1667
  removeForm,
1668
+ removeField,
1658
1669
  mappers,
1659
1670
  formGroupInstance: formGroupInstance.current,
1660
1671
  printFormGroupInstance,
@@ -1679,12 +1690,29 @@ const useFormGroupContext = () => {
1679
1690
  return context;
1680
1691
  };
1681
1692
 
1693
+ const FieldWrapperComponentRender = ({
1694
+ props,
1695
+ fieldInstance,
1696
+ children
1697
+ }) => {
1698
+ const Component = fieldInstance.mapper.component;
1699
+ const Asynccomponent = fieldInstance.mapper.asynccomponent;
1700
+ if (Component) return jsx(Component, Object.assign({}, props, {
1701
+ children: children && children
1702
+ }));
1703
+ if (Asynccomponent) return jsx(Suspense, {
1704
+ children: jsx(Asynccomponent, Object.assign({}, props, {
1705
+ children: children && children
1706
+ }))
1707
+ });
1708
+ return jsx("div", {
1709
+ children: `failed to render field ${fieldInstance.name} with ${fieldInstance.mapper.componentName}, please check mappers`
1710
+ });
1711
+ };
1682
1712
  const FieldWrapper = ({
1683
1713
  index,
1684
- Component,
1685
1714
  formKey,
1686
- children,
1687
- events
1715
+ children
1688
1716
  }) => {
1689
1717
  var _a;
1690
1718
  const {
@@ -1737,6 +1765,7 @@ const FieldWrapper = ({
1737
1765
  });
1738
1766
  }, []);
1739
1767
  const mapProps = useMemo(() => {
1768
+ const events = fieldInstance.mapper.events;
1740
1769
  const props = {};
1741
1770
  if (events === null || events === void 0 ? void 0 : events.onBlur) props[events.onBlur] = () => handleEvent('ON_FIELD_BLUR');
1742
1771
  if (events === null || events === void 0 ? void 0 : events.getValue) props[events.getValue] = handleChange;
@@ -1746,9 +1775,12 @@ const FieldWrapper = ({
1746
1775
  if (events === null || events === void 0 ? void 0 : events.onKeyDown) props[events.onKeyDown] = () => handleEvent('ON_FIELD_KEYDOWN');
1747
1776
  return props;
1748
1777
  }, []);
1749
- const mapValue = useMemo(() => (events === null || events === void 0 ? void 0 : events.setValue) ? {
1750
- [events.setValue]: valueState
1751
- } : {}, [valueState]);
1778
+ const mapValue = useMemo(() => {
1779
+ const events = fieldInstance.mapper.events;
1780
+ return (events === null || events === void 0 ? void 0 : events.setValue) ? {
1781
+ [events.setValue]: valueState
1782
+ } : {};
1783
+ }, [valueState]);
1752
1784
  return (state === null || state === void 0 ? void 0 : state.visibility) ? jsxs(Fragment, {
1753
1785
  children: [debugMode && jsxs(Fragment, {
1754
1786
  children: [jsx("b", {
@@ -1758,9 +1790,11 @@ const FieldWrapper = ({
1758
1790
  },
1759
1791
  children: index
1760
1792
  }), jsx("br", {}), jsx("hr", {})]
1761
- }), jsx(Component, Object.assign({}, state === null || state === void 0 ? void 0 : state.props, mapProps, mapValue, {
1793
+ }), jsx(FieldWrapperComponentRender, {
1794
+ props: Object.assign(Object.assign(Object.assign({}, state.props), mapProps), mapValue),
1795
+ fieldInstance: fieldInstance,
1762
1796
  children: children && children
1763
- }))]
1797
+ })]
1764
1798
  }) : jsx(Fragment, {});
1765
1799
  };
1766
1800
 
@@ -1772,31 +1806,17 @@ const BuildTree = ({
1772
1806
  return Array.from(fields).filter(([, value]) => {
1773
1807
  return value.path === prevPath;
1774
1808
  }).map(([, value]) => {
1775
- var _a;
1776
1809
  const fieldName = value.name;
1777
- const mapper = (_a = fields.get(fieldName)) === null || _a === void 0 ? void 0 : _a.mapper;
1778
1810
  const children = BuildTree({
1779
1811
  fields,
1780
1812
  prevPath: `${prevPath ? `${prevPath}.` : ``}${value.name}`,
1781
1813
  formKey
1782
1814
  });
1783
1815
  const lenght = Children.count(children);
1784
- return mapper && mapper.asynccomponent ? jsx(Suspense, {
1785
- children: jsx(FieldWrapper, {
1786
- Component: mapper.asynccomponent,
1787
- index: fieldName,
1788
- events: mapper.events,
1789
- formKey: formKey,
1790
- children: lenght > 0 ? children : null
1791
- })
1792
- }, fieldName) : mapper && mapper.component ? jsx(FieldWrapper, {
1793
- Component: mapper.component,
1816
+ return jsx(FieldWrapper, {
1794
1817
  index: fieldName,
1795
- events: mapper.events,
1796
1818
  formKey: formKey,
1797
1819
  children: lenght > 0 ? children : null
1798
- }, fieldName) : jsx("div", {
1799
- children: `failed to render field: ${fieldName} on tree, check mappers.. :(`
1800
1820
  });
1801
1821
  });
1802
1822
  };
@@ -1939,4 +1959,34 @@ const AsFormField = props => {
1939
1959
  });
1940
1960
  };
1941
1961
 
1942
- export { AsFormField, Form, FormGroupContext, FormGroupContextProvider, useFormGroupContext };
1962
+ const AsFormFieldBuilder = props => {
1963
+ const {
1964
+ formGroupInstance,
1965
+ addFormWithIndex,
1966
+ removeField
1967
+ } = useFormGroupContext();
1968
+ const [field, setField] = useState(jsx(Fragment, {}));
1969
+ useEffect(() => {
1970
+ var _a;
1971
+ if (!formGroupInstance.forms.has(props.formIndex)) {
1972
+ addFormWithIndex(props.formIndex);
1973
+ }
1974
+ const fieldInstanceProps = Object.assign(Object.assign({}, props), {
1975
+ children: undefined
1976
+ });
1977
+ (_a = formGroupInstance.forms.get(props.formIndex)) === null || _a === void 0 ? void 0 : _a.addField(fieldInstanceProps);
1978
+ const field = jsx(FieldWrapper, {
1979
+ formKey: props.formIndex,
1980
+ index: props.name,
1981
+ children: props.children && props.children
1982
+ });
1983
+ setField(field);
1984
+ return () => removeField({
1985
+ formIndex: props.formIndex,
1986
+ fieldIndex: props.name
1987
+ });
1988
+ }, []);
1989
+ return field;
1990
+ };
1991
+
1992
+ export { AsFormField, AsFormFieldBuilder, Form, FormGroupContext, FormGroupContextProvider, useFormGroupContext };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine",
3
- "version": "3.0.0-beta.11",
3
+ "version": "3.0.0-beta.13",
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": "0.0.1-beta.2",
9
+ "@bolttech/form-engine-core": "0.0.1-beta.4",
10
10
  "react": "18.2.0"
11
11
  },
12
12
  "peerDependencies": {}
@@ -0,0 +1,6 @@
1
+ import { IComponentSchema } from '@bolttech/form-engine-core';
2
+ import { PropsWithChildren, ReactElement } from 'react';
3
+ declare const AsFormFieldBuilder: (props: PropsWithChildren<Omit<IComponentSchema, 'children'> & {
4
+ formIndex: string;
5
+ }>) => ReactElement;
6
+ export default AsFormFieldBuilder;
@@ -1,4 +1,4 @@
1
1
  import { PropsWithChildren, ReactElement } from 'react';
2
2
  import { TFieldWrapper } from '../../types';
3
- declare const FieldWrapper: ({ index, Component, formKey, children, events, }: PropsWithChildren<TFieldWrapper>) => ReactElement;
3
+ declare const FieldWrapper: ({ index, formKey, children, }: PropsWithChildren<TFieldWrapper>) => ReactElement;
4
4
  export default FieldWrapper;
@@ -1,2 +1,3 @@
1
1
  export { default as Form } from './Form/Form';
2
2
  export { default as AsFormField } from './AsFormField/AsFormField';
3
+ export { default as AsFormFieldBuilder } from './AsFormFieldBuilder/AsFormFieldBuilder';
@@ -1,6 +1,7 @@
1
1
  import { FormCore, TFormCore, TFormGroup, TFormValues, TMapper } from '@bolttech/form-engine-core';
2
2
  import { ElementType, PropsWithChildren, ReactElement } from 'react';
3
3
  type TFormContext = {
4
+ addFormWithIndex: (index: string) => void;
4
5
  addForm: (payload: {
5
6
  key: string;
6
7
  formInstance: TFormCore;
@@ -11,6 +12,10 @@ type TFormContext = {
11
12
  removeForm: (payload: {
12
13
  key: string;
13
14
  }) => void;
15
+ removeField: (payload: {
16
+ formIndex: string;
17
+ fieldIndex: string;
18
+ }) => void;
14
19
  formGroupInstance: TFormGroup;
15
20
  mappers: TMapper<ElementType>[];
16
21
  printFormGroupInstance: () => void;
@@ -1,5 +1,3 @@
1
- import { TComponentPropsMapping } from '@bolttech/form-engine-core';
2
- import { ElementType } from 'react';
3
1
  /**
4
2
  * @type TFieldWrapper
5
3
  * Represents the wrapper for a form field, including the component,
@@ -40,8 +38,6 @@ import { ElementType } from 'react';
40
38
  */
41
39
  type TFieldWrapper = {
42
40
  index: string;
43
- Component: ElementType;
44
41
  formKey: string;
45
- events?: TComponentPropsMapping;
46
42
  };
47
43
  export { TFieldWrapper };