@bolttech/form-engine 3.0.0-beta.9 → 6.1.0-beta.6.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.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
2
- import { FormGroup, FormCore } from 'form-engine-core';
3
- import { createContext, useRef, useContext, useState, useEffect, useCallback, useMemo, Children } from 'react';
2
+ import { FormGroup } from 'form-engine-core';
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,256 +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 ? jsx(FieldWrapper, {
1785
- Component: mapper.component,
1786
- index: fieldName,
1787
- events: mapper.events,
1788
- formKey: formKey,
1789
- children: lenght > 0 ? children : null
1790
- }, fieldName) : jsx("div", {
1791
- children: `failed to render field: ${fieldName} on tree, check mappers.. :(`
1792
- });
1793
- });
1794
- };
1795
- const BuildAsFormFieldTree = ({
1796
- children
1797
- }) => {
1798
- return Children.map(children, (child, index) => {
1799
- var _a, _b;
1800
- if (!((_a = child === null || child === void 0 ? void 0 : child.type) === null || _a === void 0 ? void 0 : _a.name) || child.type.name !== 'AsFormField') {
1801
- throw new Error('only use AsFormField inside the Form component');
1802
- // console.log(child.type.name);
1803
- }
1804
- const struct = Object.assign({}, child.props);
1805
- delete struct.children;
1806
- const childElements = BuildAsFormFieldTree({
1807
- children: (_b = child.props) === null || _b === void 0 ? void 0 : _b.children
1808
- });
1809
- return Object.assign(Object.assign({}, struct), childElements && {
1810
- children: childElements
1811
- });
1812
- });
1813
- };
1814
-
1815
- const Form = ({
1816
- schema,
1817
- index,
1818
- initialValues,
1819
- iVars,
1820
- action,
1821
- method,
1822
- onSubmit,
1823
- onData,
1824
- children
1825
- }) => {
1826
- const {
1827
- addForm,
1828
- removeForm,
1829
- getForm,
1830
- mappers,
1831
- debugMode
1832
- } = useFormGroupContext();
1833
- const [tree, setTree] = useState();
1834
- const schemaIndex = useMemo(() => index || (schema === null || schema === void 0 ? void 0 : schema.index) || 'defaultChange', [index, schema]);
1835
- useEffect(() => {
1836
- if (schemaIndex === 'defaultChange') {
1837
- console.warn('please, add an unique id to the form, otherwise multiple forms will break');
1838
- }
1839
- const formInstance = new FormCore({
1840
- schema,
1841
- initialValues: initialValues || (schema === null || schema === void 0 ? void 0 : schema.initialValues),
1842
- action: action || (schema === null || schema === void 0 ? void 0 : schema.action),
1843
- method: method || (schema === null || schema === void 0 ? void 0 : schema.method),
1844
- index: schemaIndex,
1845
- onSubmit,
1846
- onData,
1847
- mappers
1848
- });
1849
- addForm({
1850
- key: schemaIndex,
1851
- formInstance
1852
- });
1853
- /*
1854
- @TODO check if form instance is killed each time it is unmounted
1855
- the management of multiple forms needs to be planned
1856
- */
1857
- return () => removeForm({
1858
- key: index
1859
- });
1860
- }, []);
1861
- useEffect(() => {
1862
- var _a;
1863
- const schema = BuildAsFormFieldTree({
1864
- children
1865
- });
1866
- schema && getForm({
1867
- key: index
1868
- }).refreshFields(schema);
1869
- const fields = (_a = getForm({
1870
- key: index
1871
- })) === null || _a === void 0 ? void 0 : _a.fields;
1872
- if (fields) {
1873
- const buildTree = BuildTree({
1874
- fields,
1875
- formKey: index
1876
- });
1877
- setTree(buildTree);
1878
- }
1879
- }, [children]);
1880
- useEffect(() => {
1881
- if (iVars) getForm({
1882
- key: index
1883
- }).iVars = iVars;
1884
- }, [iVars]);
1885
- /*
1886
- @TODO move this logic inside form-core, add action and method onto form element,
1887
- might need a ref of the form to collect all the form parameters when there is a
1888
- normal form submition with redirect
1889
- */
1890
- const handleSubmit = event => {
1891
- const formElement = getForm({
1892
- key: schemaIndex
1893
- });
1894
- if (!(formElement === null || formElement === void 0 ? void 0 : formElement.action) && event) {
1895
- event.preventDefault();
1896
- }
1897
- formElement === null || formElement === void 0 ? void 0 : formElement.submit();
1898
- };
1899
- return jsxs(Fragment, {
1900
- children: [debugMode && jsxs(Fragment, {
1901
- children: [jsx("b", {
1902
- style: {
1903
- padding: '0px',
1904
- margin: '0px'
1905
- },
1906
- children: `form index: ${schemaIndex}`
1907
- }), jsx("button", {
1908
- onClick: () => console.log(getForm({
1909
- key: schemaIndex
1910
- })),
1911
- children: "print form instance"
1912
- }), jsx("button", {
1913
- onClick: () => {
1914
- var _a;
1915
- return (_a = getForm({
1916
- key: schemaIndex
1917
- })) === null || _a === void 0 ? void 0 : _a.printValues();
1918
- },
1919
- children: "print values"
1920
- }), jsx("br", {}), jsx("hr", {})]
1921
- }), jsx("form", {
1922
- onSubmit: handleSubmit,
1923
- children: tree && tree
1924
- })]
1925
- });
1926
- };
1927
-
1928
- const AsFormField = props => {
1929
- return jsx(Fragment, {
1930
- children: props.children
1931
- });
1932
- };
1933
-
1934
- export { AsFormField, Form, FormGroupContext, FormGroupContextProvider, useFormGroupContext };
1682
+ export { FormGroupContext, FormGroupContextProvider, useFormGroupContext };
package/package.json CHANGED
@@ -1,12 +1,8 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine",
3
- "version": "3.0.0-beta.9",
3
+ "version": "6.1.0-beta.6.1",
4
4
  "description": "A react adapter for bolttech form engine",
5
5
  "dependencies": {
6
- "rxjs": "^7.8.1",
7
- "@gaignoux/currency": "^1.1.0",
8
- "credit-card-type": "^10.0.0",
9
- "lodash": "^4.17.21",
10
6
  "form-engine-core": "file:./src/core/index.d.js",
11
7
  "react": "18.2.0"
12
8
  },
@@ -1,4 +1,4 @@
1
- import { IComponentSchema } from '@form-engine-core';
1
+ import { IComponentSchema } from 'form-engine-core';
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 '@form-engine-core';
1
+ import { TFormEntry } from 'form-engine-core';
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 '@form-engine-core';
1
+ import { FormCore, TFormCore, TFormGroup, TFormValues, TMapper } from 'form-engine-core';
2
2
  import { ElementType, PropsWithChildren, ReactElement } from 'react';
3
3
  type TFormContext = {
4
4
  addForm: (payload: {
@@ -1,5 +1,5 @@
1
1
  import { ReactNode } from 'react';
2
- import { IFormField, IComponentSchema } from '@form-engine-core';
2
+ import { IFormField, IComponentSchema } from 'form-engine-core';
3
3
  declare const BuildTree: ({ fields, prevPath, formKey, }: {
4
4
  fields: Map<string, IFormField>;
5
5
  prevPath?: string | undefined;
@@ -1,4 +1,4 @@
1
- import { TComponentPropsMapping, TMapper, TFormValues, TFormEntry, TEvents, TMutationEvents, TValueChangeEvent, TApiConfig, TErrorMessages, TResetValues, TVisibilityConditions, TValidations, TMasks, TProps, TResetValueMethods, TFormatters, TValidationMethods, TEvent, TVisibility, TApiEvent, TApiResponse, TLengthValidation, TCreditCardMatch, TDocumentValidation, TCallbackValidation, TBetweenValidation, TMaskGeneric, TSplitterFormatterValue, TCurrencyMask, TDateOperatorsValidation, TConditionsValidationSet, TConditionsValidation, TMultipleValidation, TAvailableValidations, TDateValidation, TBetweenDatesValidation, TDateFormatsValidation, TDateInterval, TSubscribedTemplates } from '@form-engine-core';
1
+ import { TComponentPropsMapping } from 'form-engine-core';
2
2
  import { ElementType } from 'react';
3
3
  /**
4
4
  * @type TFieldWrapper
@@ -44,4 +44,4 @@ type TFieldWrapper = {
44
44
  formKey: string;
45
45
  events?: TComponentPropsMapping;
46
46
  };
47
- export { TFieldWrapper, TMapper, TFormValues, TFormEntry, TEvents, TMutationEvents, TValueChangeEvent, TApiConfig, TErrorMessages, TResetValues, TVisibilityConditions, TValidations, TMasks, TProps, TResetValueMethods, TFormatters, TValidationMethods, TEvent, TVisibility, TApiEvent, TApiResponse, TLengthValidation, TCreditCardMatch, TDocumentValidation, TCallbackValidation, TBetweenValidation, TMaskGeneric, TSplitterFormatterValue, TCurrencyMask, TDateOperatorsValidation, TConditionsValidationSet, TConditionsValidation, TMultipleValidation, TAvailableValidations, TDateValidation, TBetweenDatesValidation, TDateFormatsValidation, TDateInterval, TSubscribedTemplates, };
47
+ export { TFieldWrapper };