dynamic-modal 1.1.7 → 1.1.8

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.
@@ -16,4 +16,5 @@ export interface IFieldRender {
16
16
  export interface IFieldRenderProps extends Pick<IFieldProps, 'setValue' | 'unregister'> {
17
17
  element: Partial<Pick<IField, 'enableIf' | 'renderIf' | 'name'>> & Partial<Record<'liveData', IModalLiveDataCondition>>;
18
18
  }
19
+ export type ILiveHandlerProps = Pick<IField, 'enableIf' | 'renderIf'>;
19
20
  export declare const useFieldRender: ({ element, setValue, unregister }: IFieldRenderProps) => IFieldRender;
@@ -46,34 +46,41 @@ const useFieldRender = ({ element, setValue, unregister }) => {
46
46
  }
47
47
  return [];
48
48
  }), [element.liveData]);
49
+ const liveDataHandler = (0, react_1.useCallback)((targetHandler, field, formData) => __awaiter(void 0, void 0, void 0, function* () {
50
+ var _a;
51
+ if (typeof field === 'string' && ((_a = element[targetHandler]) === null || _a === void 0 ? void 0 : _a.action)) {
52
+ const result = element[targetHandler].action(field, formData);
53
+ return result !== null && result !== void 0 ? result : false;
54
+ }
55
+ return false;
56
+ }), [element.liveData]);
49
57
  const checkField = (0, react_1.useCallback)((formData_1, _a) => __awaiter(void 0, [formData_1, _a], void 0, function* (formData, { name }) {
50
- var _b;
58
+ var _b, _c, _d;
51
59
  const key = name !== null && name !== void 0 ? name : '';
52
60
  const targetField = formData[key];
53
- const fieldValidValue = targetField !== undefined && targetField !== null && targetField !== '';
54
- if (renderCondition && renderConditionList.includes(key) && fieldValidValue) {
55
- const renderStatus = element.renderIf[key].includes(targetField) || element.renderIf[key].includes('*');
61
+ if (!targetField)
62
+ return;
63
+ if (renderCondition && ((_b = element.renderIf) === null || _b === void 0 ? void 0 : _b.condition.includes(key))) {
64
+ const renderStatus = yield liveDataHandler('renderIf', targetField, formData);
56
65
  if (render !== renderStatus) {
57
66
  setRender(renderStatus);
58
67
  if (!renderStatus)
59
68
  unregister(element.name);
60
69
  }
61
70
  }
62
- else if (enableCondition && enableConditionList.includes(key) && fieldValidValue) {
63
- const enableStatus = element.enableIf[key].includes(targetField) || element.enableIf[key].includes('*');
71
+ else if (enableCondition && ((_c = element.enableIf) === null || _c === void 0 ? void 0 : _c.condition.includes(key))) {
72
+ const enableStatus = yield liveDataHandler('enableIf', targetField, formData);
64
73
  if (enable !== enableStatus)
65
74
  setEnable(enableStatus);
66
75
  }
67
- if (liveDataCondition && ((_b = element.liveData) === null || _b === void 0 ? void 0 : _b.condition.includes(key))) {
68
- if (targetField) {
69
- setLiveSearching(true);
70
- const options = yield liveDataAction(targetField, formData);
71
- if (liveData && JSON.stringify(liveData) !== JSON.stringify(options)) {
72
- setValue(element.name, options);
73
- }
74
- setLiveData(options);
75
- setLiveSearching(false);
76
+ if (liveDataCondition && ((_d = element.liveData) === null || _d === void 0 ? void 0 : _d.condition.includes(key))) {
77
+ setLiveSearching(true);
78
+ const options = yield liveDataAction(targetField, formData);
79
+ if (liveData && JSON.stringify(liveData) !== JSON.stringify(options)) {
80
+ setValue(element.name, options);
76
81
  }
82
+ setLiveData(options);
83
+ setLiveSearching(false);
77
84
  }
78
85
  }), [enable, enableCondition, enableConditionList, liveData, liveDataAction, liveDataCondition, render, renderCondition, renderConditionList]);
79
86
  return {
@@ -10,7 +10,10 @@ import { IOption } from './option';
10
10
  import { IMakeButton } from './make-button';
11
11
  export type IModalField = IMakeSelect | IMakeInput | IMakeFieldGroup | IMakeTextarea | IMakeToggle | IMakeDescription | IMakeUpload | IMakeButton;
12
12
  export type IFormField = IMakeSelect | IMakeInput | IMakeTextarea | IMakeToggle;
13
- export type IModalRenderCondition = Record<string, Array<string | number | boolean> | undefined>;
13
+ export type IModalRenderCondition = {
14
+ action: (data: string, ...args: any[]) => Promise<boolean>;
15
+ condition: Array<string>;
16
+ };
14
17
  export type IModalLiveDataCondition = {
15
18
  action: (data: string, ...args: any[]) => Promise<Array<IOption>>;
16
19
  condition: Array<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamic-modal",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "The dynamic-modal is a solution of creation different modals into project using a json configuration file",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -24,6 +24,8 @@ export interface IFieldRenderProps extends Pick<IFieldProps, 'setValue' | 'unreg
24
24
  element: Partial<Pick<IField, 'enableIf' | 'renderIf' | 'name' >> & Partial<Record<'liveData', IModalLiveDataCondition>>
25
25
  }
26
26
 
27
+ export type ILiveHandlerProps = Pick<IField, 'enableIf' | 'renderIf'>
28
+
27
29
  export const useFieldRender = ({ element, setValue, unregister }: IFieldRenderProps): IFieldRender => {
28
30
  const [render, setRender] = useState<boolean|null>(null)
29
31
  const [enable, setEnable] = useState<boolean|null>(null)
@@ -61,7 +63,7 @@ export const useFieldRender = ({ element, setValue, unregister }: IFieldRenderPr
61
63
  }, [enableCondition, element.enableIf])
62
64
 
63
65
  const liveDataAction = useCallback(
64
- async (field: string |number | Array<unknown>, formData: IFormData) => {
66
+ async (field: string | number | Array<unknown>, formData: IFormData) => {
65
67
  if (typeof field === 'string' && element.liveData?.action) {
66
68
  const options = element.liveData.action(field, formData)
67
69
  return options ?? []
@@ -70,31 +72,40 @@ export const useFieldRender = ({ element, setValue, unregister }: IFieldRenderPr
70
72
  }, [element.liveData]
71
73
  )
72
74
 
75
+ const liveDataHandler = useCallback(
76
+ async (targetHandler: keyof ILiveHandlerProps, field: string | number | Array<unknown>, formData: IFormData) => {
77
+ if (typeof field === 'string' && element[targetHandler]?.action) {
78
+ const result = element[targetHandler].action(field, formData)
79
+ return result ?? false
80
+ }
81
+
82
+ return false
83
+ }, [element.liveData]
84
+ )
85
+
73
86
  const checkField = useCallback(
74
87
  async (formData: IFormData, { name }: IWatchEvent) => {
75
88
  const key: string = name ?? ''
76
89
  const targetField = formData[key] as string | number
77
- const fieldValidValue: boolean = targetField !== undefined && targetField !== null && targetField !== ''
90
+ if(!targetField) return
78
91
 
79
- if (renderCondition && renderConditionList.includes(key) && fieldValidValue) {
80
- const renderStatus: boolean = (element.renderIf as IModalRenderCondition)[key]!.includes(targetField) || (element.renderIf as IModalRenderCondition)[key]!.includes('*')
92
+ if (renderCondition && element.renderIf?.condition.includes(key)) {
93
+ const renderStatus: boolean = await liveDataHandler('renderIf', targetField, formData)
81
94
  if (render !== renderStatus) {
82
95
  setRender(renderStatus)
83
96
  if(!renderStatus) unregister(element.name as string)
84
97
  }
85
- } else if (enableCondition && enableConditionList.includes(key) && fieldValidValue) {
86
- const enableStatus: boolean = (element.enableIf as IModalRenderCondition)[key]!.includes(targetField) || (element.enableIf as IModalRenderCondition)[key]!.includes('*')
98
+ } else if (enableCondition && element.enableIf?.condition.includes(key)){
99
+ const enableStatus: boolean = await liveDataHandler('enableIf', targetField, formData)
87
100
  if (enable !== enableStatus) setEnable(enableStatus)
88
101
  }
89
102
 
90
103
  if (liveDataCondition && element.liveData?.condition.includes(key)) {
91
- if (targetField) {
92
- setLiveSearching(true)
93
- const options: Array<IOption> = await liveDataAction(targetField, formData)
94
- if (liveData && JSON.stringify(liveData) !== JSON.stringify(options)) { setValue(element.name as string, options) }
95
- setLiveData(options)
96
- setLiveSearching(false)
97
- }
104
+ setLiveSearching(true)
105
+ const options: Array<IOption> = await liveDataAction(targetField, formData)
106
+ if (liveData && JSON.stringify(liveData) !== JSON.stringify(options)) { setValue(element.name as string, options) }
107
+ setLiveData(options)
108
+ setLiveSearching(false)
98
109
  }
99
110
  }, [enable, enableCondition, enableConditionList, liveData, liveDataAction, liveDataCondition, render, renderCondition, renderConditionList]
100
111
  )
@@ -14,7 +14,10 @@ export type IModalField = IMakeSelect | IMakeInput | IMakeFieldGroup | IMakeText
14
14
 
15
15
  export type IFormField = IMakeSelect | IMakeInput | IMakeTextarea | IMakeToggle
16
16
 
17
- export type IModalRenderCondition = Record<string, Array<string | number | boolean> | undefined>
17
+ export type IModalRenderCondition = {
18
+ action: (data: string, ...args: any[]) => Promise<boolean>
19
+ condition: Array<string>
20
+ }
18
21
 
19
22
  export type IModalLiveDataCondition = {
20
23
  action: (data: string, ...args: any[]) => Promise<Array<IOption>>