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

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
- import { FormGroup, FormCore } from '@bolttech/form-engine-core';
3
- import { createContext, useRef, useContext, useState, useEffect, useCallback, useMemo, Children, Suspense } from 'react';
2
+ import { FormGroup } from './src/core/index.d.js';
3
+ import { createContext, useRef, useContext } 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
 
@@ -1679,264 +1679,4 @@ const useFormGroupContext = () => {
1679
1679
  return context;
1680
1680
  };
1681
1681
 
1682
- const FieldWrapper = ({
1683
- index,
1684
- Component,
1685
- formKey,
1686
- children,
1687
- events
1688
- }) => {
1689
- var _a;
1690
- const {
1691
- formGroupInstance,
1692
- debugMode
1693
- } = useFormGroupContext();
1694
- const fieldInstance = ((_a = formGroupInstance.getForm({
1695
- key: formKey
1696
- })) === null || _a === void 0 ? void 0 : _a.getField({
1697
- key: index
1698
- })) || {};
1699
- const [valueState, setValueState] = useState(fieldInstance.stateValue);
1700
- const [state, setState] = useState({
1701
- visibility: fieldInstance.visibility,
1702
- props: fieldInstance.props,
1703
- apiResponse: fieldInstance.api
1704
- });
1705
- useEffect(() => {
1706
- fieldInstance.mountField({
1707
- valueSubscription: value => {
1708
- setValueState(value);
1709
- },
1710
- propsSubscription: ({
1711
- errors,
1712
- visibility,
1713
- apiResponse,
1714
- props
1715
- }) => {
1716
- setState(prev => Object.assign(Object.assign({}, prev), {
1717
- errors,
1718
- visibility,
1719
- apiResponse,
1720
- props
1721
- }));
1722
- }
1723
- });
1724
- return () => {
1725
- fieldInstance.destroyField();
1726
- };
1727
- }, []);
1728
- const handleChange = useCallback(event => {
1729
- fieldInstance.emitValue({
1730
- value: event,
1731
- event: 'ON_FIELD_CHANGE'
1732
- });
1733
- }, []);
1734
- const handleEvent = useCallback(event => {
1735
- fieldInstance.emitEvents({
1736
- event
1737
- });
1738
- }, []);
1739
- const mapProps = useMemo(() => {
1740
- const props = {};
1741
- if (events === null || events === void 0 ? void 0 : events.onBlur) props[events.onBlur] = () => handleEvent('ON_FIELD_BLUR');
1742
- if (events === null || events === void 0 ? void 0 : events.getValue) props[events.getValue] = handleChange;
1743
- if (events === null || events === void 0 ? void 0 : events.onFocus) props[events.onFocus] = () => handleEvent('ON_FIELD_FOCUS');
1744
- if (events === null || events === void 0 ? void 0 : events.onClick) props[events.onClick] = () => handleEvent('ON_FIELD_CLICK');
1745
- if (events === null || events === void 0 ? void 0 : events.onKeyUp) props[events.onKeyUp] = () => handleEvent('ON_FIELD_KEYUP');
1746
- if (events === null || events === void 0 ? void 0 : events.onKeyDown) props[events.onKeyDown] = () => handleEvent('ON_FIELD_KEYDOWN');
1747
- return props;
1748
- }, []);
1749
- const mapValue = useMemo(() => (events === null || events === void 0 ? void 0 : events.setValue) ? {
1750
- [events.setValue]: valueState
1751
- } : {}, [valueState]);
1752
- return (state === null || state === void 0 ? void 0 : state.visibility) ? jsxs(Fragment, {
1753
- children: [debugMode && jsxs(Fragment, {
1754
- children: [jsx("b", {
1755
- style: {
1756
- padding: '0px',
1757
- margin: '0px'
1758
- },
1759
- children: index
1760
- }), jsx("br", {}), jsx("hr", {})]
1761
- }), jsx(Component, Object.assign({}, state === null || state === void 0 ? void 0 : state.props, mapProps, mapValue, {
1762
- children: children && children
1763
- }))]
1764
- }) : jsx(Fragment, {});
1765
- };
1766
-
1767
- const BuildTree = ({
1768
- fields,
1769
- prevPath,
1770
- formKey
1771
- }) => {
1772
- return Array.from(fields).filter(([, value]) => {
1773
- return value.path === prevPath;
1774
- }).map(([, value]) => {
1775
- var _a;
1776
- const fieldName = value.name;
1777
- const mapper = (_a = fields.get(fieldName)) === null || _a === void 0 ? void 0 : _a.mapper;
1778
- const children = BuildTree({
1779
- fields,
1780
- prevPath: `${prevPath ? `${prevPath}.` : ``}${value.name}`,
1781
- formKey
1782
- });
1783
- 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,
1794
- index: fieldName,
1795
- events: mapper.events,
1796
- formKey: formKey,
1797
- children: lenght > 0 ? children : null
1798
- }, fieldName) : jsx("div", {
1799
- children: `failed to render field: ${fieldName} on tree, check mappers.. :(`
1800
- });
1801
- });
1802
- };
1803
- const BuildAsFormFieldTree = ({
1804
- children
1805
- }) => {
1806
- return Children.map(children, (child, index) => {
1807
- var _a, _b;
1808
- if (!((_a = child === null || child === void 0 ? void 0 : child.type) === null || _a === void 0 ? void 0 : _a.name) || child.type.name !== 'AsFormField') {
1809
- throw new Error('only use AsFormField inside the Form component');
1810
- // console.log(child.type.name);
1811
- }
1812
- const struct = Object.assign({}, child.props);
1813
- delete struct.children;
1814
- const childElements = BuildAsFormFieldTree({
1815
- children: (_b = child.props) === null || _b === void 0 ? void 0 : _b.children
1816
- });
1817
- return Object.assign(Object.assign({}, struct), childElements && {
1818
- children: childElements
1819
- });
1820
- });
1821
- };
1822
-
1823
- const Form = ({
1824
- schema,
1825
- index,
1826
- initialValues,
1827
- iVars,
1828
- action,
1829
- method,
1830
- onSubmit,
1831
- onData,
1832
- children
1833
- }) => {
1834
- const {
1835
- addForm,
1836
- removeForm,
1837
- getForm,
1838
- mappers,
1839
- debugMode
1840
- } = useFormGroupContext();
1841
- const [tree, setTree] = useState();
1842
- const schemaIndex = useMemo(() => index || (schema === null || schema === void 0 ? void 0 : schema.index) || 'defaultChange', [index, schema]);
1843
- useEffect(() => {
1844
- if (schemaIndex === 'defaultChange') {
1845
- console.warn('please, add an unique id to the form, otherwise multiple forms will break');
1846
- }
1847
- const formInstance = new FormCore({
1848
- schema,
1849
- initialValues: initialValues || (schema === null || schema === void 0 ? void 0 : schema.initialValues),
1850
- action: action || (schema === null || schema === void 0 ? void 0 : schema.action),
1851
- method: method || (schema === null || schema === void 0 ? void 0 : schema.method),
1852
- index: schemaIndex,
1853
- onSubmit,
1854
- onData,
1855
- mappers
1856
- });
1857
- addForm({
1858
- key: schemaIndex,
1859
- formInstance
1860
- });
1861
- /*
1862
- @TODO check if form instance is killed each time it is unmounted
1863
- the management of multiple forms needs to be planned
1864
- */
1865
- return () => removeForm({
1866
- key: index
1867
- });
1868
- }, []);
1869
- useEffect(() => {
1870
- var _a;
1871
- const schema = BuildAsFormFieldTree({
1872
- children
1873
- });
1874
- schema && getForm({
1875
- key: index
1876
- }).refreshFields(schema);
1877
- const fields = (_a = getForm({
1878
- key: index
1879
- })) === null || _a === void 0 ? void 0 : _a.fields;
1880
- if (fields) {
1881
- const buildTree = BuildTree({
1882
- fields,
1883
- formKey: index
1884
- });
1885
- setTree(buildTree);
1886
- }
1887
- }, [children]);
1888
- useEffect(() => {
1889
- if (iVars) getForm({
1890
- key: index
1891
- }).iVars = iVars;
1892
- }, [iVars]);
1893
- /*
1894
- @TODO move this logic inside form-core, add action and method onto form element,
1895
- might need a ref of the form to collect all the form parameters when there is a
1896
- normal form submition with redirect
1897
- */
1898
- const handleSubmit = event => {
1899
- const formElement = getForm({
1900
- key: schemaIndex
1901
- });
1902
- if (!(formElement === null || formElement === void 0 ? void 0 : formElement.action) && event) {
1903
- event.preventDefault();
1904
- }
1905
- formElement === null || formElement === void 0 ? void 0 : formElement.submit();
1906
- };
1907
- return jsxs(Fragment, {
1908
- children: [debugMode && jsxs(Fragment, {
1909
- children: [jsx("b", {
1910
- style: {
1911
- padding: '0px',
1912
- margin: '0px'
1913
- },
1914
- children: `form index: ${schemaIndex}`
1915
- }), jsx("button", {
1916
- onClick: () => console.log(getForm({
1917
- key: schemaIndex
1918
- })),
1919
- children: "print form instance"
1920
- }), jsx("button", {
1921
- onClick: () => {
1922
- var _a;
1923
- return (_a = getForm({
1924
- key: schemaIndex
1925
- })) === null || _a === void 0 ? void 0 : _a.printValues();
1926
- },
1927
- children: "print values"
1928
- }), jsx("br", {}), jsx("hr", {})]
1929
- }), jsx("form", {
1930
- onSubmit: handleSubmit,
1931
- children: tree && tree
1932
- })]
1933
- });
1934
- };
1935
-
1936
- const AsFormField = props => {
1937
- return jsx(Fragment, {
1938
- children: props.children
1939
- });
1940
- };
1941
-
1942
- export { AsFormField, Form, FormGroupContext, FormGroupContextProvider, useFormGroupContext };
1682
+ export { FormGroupContext, FormGroupContextProvider, useFormGroupContext };
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine",
3
- "version": "3.0.0-beta.11",
3
+ "version": "3.0.0-beta.3",
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",
10
9
  "react": "18.2.0"
11
10
  },
12
11
  "peerDependencies": {}
@@ -1,4 +1,4 @@
1
- import { IComponentSchema } from '@bolttech/form-engine-core';
1
+ import { IComponentSchema } from '../../core/index.d';
2
2
  import { PropsWithChildren, ReactElement } from 'react';
3
3
  declare const AsFormField: (props: PropsWithChildren<Omit<IComponentSchema, 'children'>>) => ReactElement;
4
4
  export default AsFormField;
@@ -1,4 +1,4 @@
1
- import { TFormEntry } from '@bolttech/form-engine-core';
1
+ import { TFormEntry } from '../../core/index.d';
2
2
  import { PropsWithChildren } from 'react';
3
3
  declare const Form: ({ schema, index, initialValues, iVars, action, method, onSubmit, onData, children, }: PropsWithChildren<Omit<TFormEntry, 'mappers'>>) => import("react/jsx-runtime").JSX.Element;
4
4
  export default Form;
@@ -1,2 +1,2 @@
1
- export { default as Form } from './Form/Form';
2
- export { default as AsFormField } from './AsFormField/AsFormField';
1
+ export * from './Form/Form';
2
+ export * from './AsFormField/AsFormField';
@@ -1,4 +1,4 @@
1
- import { FormCore, TFormCore, TFormGroup, TFormValues, TMapper } from '@bolttech/form-engine-core';
1
+ import { FormCore, TFormCore, TFormGroup, TFormValues, TMapper } from '../core/index.d';
2
2
  import { ElementType, PropsWithChildren, ReactElement } from 'react';
3
3
  type TFormContext = {
4
4
  addForm: (payload: {