@idem.agency/form-builder 0.0.11 → 0.0.12

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 (71) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +922 -12
  3. package/dist/index.cjs +272 -88
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +18 -16
  6. package/dist/index.d.mts +18 -16
  7. package/dist/index.mjs +273 -89
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +2 -2
  10. package/public/main.tsx +17 -10
  11. package/src/app/index.test.tsx +137 -0
  12. package/src/app/index.tsx +25 -71
  13. package/src/entity/dynamicBuilder/element.tsx +71 -0
  14. package/src/entity/dynamicBuilder/index.tsx +21 -0
  15. package/src/entity/dynamicBuilder/model/createBuilderContext.tsx +52 -0
  16. package/src/entity/dynamicBuilder/model/index.ts +7 -0
  17. package/src/entity/inputs/ui/group/index.test.tsx +46 -0
  18. package/src/entity/inputs/ui/group/index.tsx +2 -4
  19. package/src/entity/inputs/ui/input/index.test.tsx +73 -0
  20. package/src/index.ts +19 -5
  21. package/src/plugins/validation/index.ts +39 -0
  22. package/src/plugins/validation/rules/confirm.test.ts +23 -0
  23. package/src/plugins/validation/rules/confirm.ts +11 -0
  24. package/src/plugins/validation/rules/email.test.ts +20 -0
  25. package/src/plugins/validation/rules/email.ts +10 -0
  26. package/src/plugins/validation/rules/index.ts +5 -0
  27. package/src/plugins/validation/rules/required.test.ts +24 -0
  28. package/src/plugins/validation/rules/required.ts +9 -0
  29. package/src/plugins/validation/types/index.ts +7 -0
  30. package/src/plugins/validation/validation.test.ts +69 -0
  31. package/src/plugins/validation/validation.ts +57 -0
  32. package/src/{shared/lib/VisibleCore.ts → plugins/visibility/core.ts} +24 -11
  33. package/src/plugins/visibility/index.ts +25 -0
  34. package/src/plugins/visibility/types.ts +20 -0
  35. package/src/shared/model/plugins/EventBus.ts +15 -0
  36. package/src/shared/model/plugins/FieldRegistry.ts +15 -0
  37. package/src/shared/model/plugins/HookRegistry.ts +21 -0
  38. package/src/shared/model/plugins/MiddlewareRegistry.ts +17 -0
  39. package/src/shared/model/plugins/PipelineRegistry.ts +73 -0
  40. package/src/shared/model/plugins/PluginManager.ts +84 -0
  41. package/src/shared/model/plugins/context.tsx +10 -0
  42. package/src/shared/model/plugins/hooks.ts +22 -0
  43. package/src/shared/model/plugins/index.ts +23 -0
  44. package/src/shared/model/plugins/types.ts +99 -0
  45. package/src/shared/model/store/createStoreContext.tsx +15 -19
  46. package/src/shared/model/store/index.test.tsx +113 -0
  47. package/src/shared/model/store/index.tsx +96 -0
  48. package/src/shared/model/store/store.test.ts +64 -0
  49. package/src/shared/model/store/store.ts +12 -1
  50. package/src/shared/test-utils.tsx +33 -0
  51. package/src/shared/types/common.ts +7 -41
  52. package/src/shared/utils.test.ts +55 -0
  53. package/src/shared/utils.ts +26 -15
  54. package/src/widgets/form/form.tsx +59 -0
  55. package/vite.config.ts +1 -0
  56. package/src/app/debug.tsx +0 -0
  57. package/src/shared/lib/VisibleCore.spec.ts +0 -103
  58. package/src/shared/lib/validation/core.spec.ts +0 -103
  59. package/src/shared/lib/validation/core.ts +0 -79
  60. package/src/shared/lib/validation/rules/base.ts +0 -10
  61. package/src/shared/lib/validation/rules/confirm.spec.ts +0 -17
  62. package/src/shared/lib/validation/rules/confirm.ts +0 -32
  63. package/src/shared/lib/validation/rules/email.spec.ts +0 -12
  64. package/src/shared/lib/validation/rules/email.ts +0 -13
  65. package/src/shared/lib/validation/rules/require.spec.ts +0 -13
  66. package/src/shared/lib/validation/rules/require.ts +0 -12
  67. package/src/shared/model/builder/createContext.tsx +0 -40
  68. package/src/shared/model/builder/index.ts +0 -6
  69. package/src/shared/model/store/index.ts +0 -46
  70. package/src/widgets/dynamicBuilder/element.tsx +0 -31
  71. package/src/widgets/dynamicBuilder/index.tsx +0 -33
@@ -1,40 +0,0 @@
1
- import React, {createContext, type ReactNode, useContext, useRef,} from "react";
2
- import {DynamicBuilder} from "@/widgets/dynamicBuilder";
3
-
4
- export function createStoreContext() {
5
- const BuilderContext = createContext<((layout: any, path: string[]|undefined, children?: ReactNode) => ReactNode) | null>(null);
6
-
7
- const Provider: React.FC<{ children: React.ReactNode, plugins: any }> = ({
8
- plugins,
9
- children
10
- }) => {
11
- const storeRef = useRef<(layout: any, path: string[]|undefined, children?: ReactNode) => ReactNode>(null);
12
-
13
- if (!storeRef.current) {
14
- storeRef.current = (layout: any, path: string[]|undefined, children?: ReactNode) => {
15
- return <DynamicBuilder layout={layout} path={path} plugins={plugins}>{children}</DynamicBuilder>
16
- };
17
- }
18
-
19
- return (
20
- <BuilderContext.Provider value={storeRef.current}>
21
- {children}
22
- </BuilderContext.Provider>
23
- );
24
- };
25
-
26
- function useBuilder(): (layout: any, path: string[]|undefined, children?: ReactNode) => ReactNode {
27
- const store = useContext(BuilderContext);
28
-
29
- if (!store) {
30
- throw new Error("StoreProvider missing");
31
- }
32
-
33
- return store
34
- }
35
-
36
- return {
37
- Provider,
38
- useBuilder
39
- };
40
- }
@@ -1,6 +0,0 @@
1
- import {createStoreContext} from "@/shared/model/builder/createContext";
2
-
3
- export const {
4
- Provider: BuilderProvider,
5
- useBuilder,
6
- } = createStoreContext();
@@ -1,46 +0,0 @@
1
- import { createStoreContext } from "./createStoreContext";
2
- import type {FormData} from "@/shared/types/common";
3
- import {updateNestedValue} from "@/shared/utils";
4
-
5
- type State = {
6
- formData: FormData
7
- errors: Record<string, any>
8
- };
9
-
10
- type Action =
11
- | { type: "setValue"; path: string[]; value: unknown }
12
- | { type: "setError"; path: string[]; value?: string }
13
- | { type: "reset"; }
14
- | { type: "setErrors"; errors?: object };
15
-
16
- const initialState: State = {
17
- formData: {},
18
- errors: {}
19
- };
20
-
21
- function reducer(state: State, action: Action): State {
22
- const newData = {...state};
23
- switch (action.type) {
24
- case 'setValue':
25
- newData.formData = updateNestedValue(newData.formData, action.path, action.value);
26
- break;
27
- case 'setError':
28
- newData.errors = updateNestedValue(newData.errors, action.path, action.value);
29
- break;
30
- case 'reset':
31
- newData.formData = {};
32
- newData.errors = {};
33
- break;
34
- case 'setErrors':
35
- newData.errors = {...action.errors};
36
- break;
37
- }
38
- return newData;
39
- }
40
-
41
- export const {
42
- Provider: FormStoreProvider,
43
- useStore: useFormStore,
44
- useDispatch: useFormDispatch,
45
- useSubmit,
46
- } = createStoreContext(reducer, initialState);
@@ -1,31 +0,0 @@
1
- import {useFormStore, useFormDispatch} from "@/shared/model/store";
2
- import {getNestedValue} from "@/shared/utils";
3
-
4
- export const BuilderElement = (props: any) => {
5
- const Element = props.element;
6
- const field = props.field;
7
- const currentFieldPath = [...props.path, field.name];
8
- const value = useFormStore((s=> getNestedValue(s.formData, currentFieldPath)));
9
- const errors = useFormStore((s=> getNestedValue(s.errors, currentFieldPath)));
10
- const dispatch= useFormDispatch();
11
- return <Element
12
- field={{...field}}
13
- builder={props.DynamicBuilder}
14
- path={currentFieldPath}
15
- plugins={props.plugins}
16
- value={value}
17
- errors={errors}
18
- onChange={(value: any) => {
19
- dispatch({
20
- type: 'setValue',
21
- path: currentFieldPath,
22
- value
23
- });
24
- dispatch({
25
- type: 'setError',
26
- path: currentFieldPath,
27
- value: ''
28
- });
29
- }}
30
- />;
31
- }
@@ -1,33 +0,0 @@
1
- import type {TDynamicBuilder} from "@/shared/types/common";
2
- import {BuilderElement} from "@/widgets/dynamicBuilder/element";
3
- // import {getNestedValue} from "@/shared/utils";
4
- // import {useCallback} from "react";
5
- // import {VisibleCore} from "@/shared/lib/VisibleCore";
6
- // import {use} from "@/shared/model/store";
7
-
8
- export const DynamicBuilder: TDynamicBuilder = (props) => {
9
- const path = props.path ?? [];
10
-
11
- // const isShow = useCallback((field: FormFieldConfig) => {
12
- // let result = true;
13
- //
14
- // if (field.viewConfig) {
15
- // result = VisibleCore.isVisible(field.viewConfig, state.formData);
16
- // }
17
- // return result;
18
- // }, [state]);
19
-
20
-
21
- return props.layout.map((field, index) => {
22
- const FormElement = props.plugins[field.type];
23
-
24
- if (!FormElement) {
25
- console.warn(`Неизвестный тип поля: ${field.type}. Проверьте formRegistry.`);
26
- return null;
27
- }
28
-
29
- // const currentValue = getNestedValue(state.formData, currentFieldPath);
30
- // const currentErrors = getNestedValue(state.errors, currentFieldPath) as (Record<string, string> | undefined);
31
- return <BuilderElement key={`${field.name}${index}`} element={FormElement} path={path} field={field}/>
32
- })
33
- }