dynamic-modal 1.1.8 → 1.1.9

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.
@@ -47,33 +47,54 @@ const useFieldRender = ({ element, setValue, unregister }) => {
47
47
  return [];
48
48
  }), [element.liveData]);
49
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);
50
+ const liveHandlerProperty = element[targetHandler];
51
+ if (typeof field === 'string' && liveHandlerProperty) {
52
+ const result = liveHandlerProperty.action(field, formData);
53
53
  return result !== null && result !== void 0 ? result : false;
54
54
  }
55
55
  return false;
56
56
  }), [element.liveData]);
57
57
  const checkField = (0, react_1.useCallback)((formData_1, _a) => __awaiter(void 0, [formData_1, _a], void 0, function* (formData, { name }) {
58
- var _b, _c, _d;
58
+ var _b;
59
59
  const key = name !== null && name !== void 0 ? name : '';
60
60
  const targetField = formData[key];
61
61
  if (!targetField)
62
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);
65
- if (render !== renderStatus) {
66
- setRender(renderStatus);
67
- if (!renderStatus)
68
- unregister(element.name);
63
+ if (renderCondition) {
64
+ const renderIfConfig = element.renderIf;
65
+ const isLiveRender = !!(renderIfConfig === null || renderIfConfig === void 0 ? void 0 : renderIfConfig.action) && !!(renderIfConfig === null || renderIfConfig === void 0 ? void 0 : renderIfConfig.condition);
66
+ if (isLiveRender && renderIfConfig.condition.includes(key)) {
67
+ const renderStatus = yield liveDataHandler('renderIf', targetField, formData);
68
+ if (render !== renderStatus) {
69
+ setRender(renderStatus);
70
+ if (!renderStatus)
71
+ unregister(element.name);
72
+ }
73
+ }
74
+ else if (renderConditionList.includes(key)) {
75
+ const renderStatus = renderIfConfig[key].includes(targetField) || renderIfConfig[key].includes('*');
76
+ if (render !== renderStatus) {
77
+ setRender(renderStatus);
78
+ if (!renderStatus)
79
+ unregister(element.name);
80
+ }
69
81
  }
70
82
  }
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);
73
- if (enable !== enableStatus)
74
- setEnable(enableStatus);
83
+ else if (enableCondition) {
84
+ const enableIfConfig = element.enableIf;
85
+ const isLiveEnable = !!(enableIfConfig === null || enableIfConfig === void 0 ? void 0 : enableIfConfig.action) && !!enableIfConfig.condition;
86
+ if (isLiveEnable && enableIfConfig.condition.includes(key)) {
87
+ const enableStatus = yield liveDataHandler('enableIf', targetField, formData);
88
+ if (enable !== enableStatus)
89
+ setEnable(enableStatus);
90
+ }
91
+ else if (enableConditionList.includes(key)) {
92
+ const enableStatus = enableIfConfig[key].includes(targetField) || enableIfConfig[key].includes('*');
93
+ if (enable !== enableStatus)
94
+ setEnable(enableStatus);
95
+ }
75
96
  }
76
- if (liveDataCondition && ((_d = element.liveData) === null || _d === void 0 ? void 0 : _d.condition.includes(key))) {
97
+ if (liveDataCondition && ((_b = element.liveData) === null || _b === void 0 ? void 0 : _b.condition.includes(key))) {
77
98
  setLiveSearching(true);
78
99
  const options = yield liveDataAction(targetField, formData);
79
100
  if (liveData && JSON.stringify(liveData) !== JSON.stringify(options)) {
@@ -10,10 +10,12 @@ 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 = {
13
+ export interface IModalRenderAction {
14
14
  action: (data: string, ...args: any[]) => Promise<boolean>;
15
15
  condition: Array<string>;
16
- };
16
+ }
17
+ export type IModalRenderCriteria = Record<string, Array<string | number | boolean> | undefined>;
18
+ export type IModalRenderCondition = IModalRenderAction | IModalRenderCriteria;
17
19
  export type IModalLiveDataCondition = {
18
20
  action: (data: string, ...args: any[]) => Promise<Array<IOption>>;
19
21
  condition: Array<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamic-modal",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
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": {
@@ -3,7 +3,7 @@
3
3
  import { useCallback, useMemo, useState } from 'react'
4
4
  import { IField, IFieldProps } from '../interfaces/field'
5
5
  import { IOption } from '../interfaces/option'
6
- import { IModalLiveDataCondition, IModalRenderCondition } from '../interfaces/modal'
6
+ import { IModalLiveDataCondition, IModalRenderAction, IModalRenderCondition, IModalRenderCriteria } from '../interfaces/modal'
7
7
 
8
8
  export type IFormData = Record<string, unknown>
9
9
 
@@ -74,8 +74,10 @@ export const useFieldRender = ({ element, setValue, unregister }: IFieldRenderPr
74
74
 
75
75
  const liveDataHandler = useCallback(
76
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)
77
+ const liveHandlerProperty = element[targetHandler] as IModalRenderAction
78
+
79
+ if (typeof field === 'string' && liveHandlerProperty) {
80
+ const result = liveHandlerProperty.action(field, formData)
79
81
  return result ?? false
80
82
  }
81
83
 
@@ -89,15 +91,36 @@ export const useFieldRender = ({ element, setValue, unregister }: IFieldRenderPr
89
91
  const targetField = formData[key] as string | number
90
92
  if(!targetField) return
91
93
 
92
- if (renderCondition && element.renderIf?.condition.includes(key)) {
93
- const renderStatus: boolean = await liveDataHandler('renderIf', targetField, formData)
94
- if (render !== renderStatus) {
95
- setRender(renderStatus)
96
- if(!renderStatus) unregister(element.name as string)
94
+ if(renderCondition){
95
+ const renderIfConfig = element.renderIf
96
+ const isLiveRender: boolean = !!renderIfConfig?.action && !!renderIfConfig?.condition
97
+
98
+ if(isLiveRender && renderIfConfig!.condition!.includes(key)){
99
+ const renderStatus: boolean = await liveDataHandler('renderIf', targetField, formData)
100
+
101
+ if (render !== renderStatus) {
102
+ setRender(renderStatus)
103
+ if(!renderStatus) unregister(element.name as string)
104
+ }
105
+ }else if(renderConditionList.includes(key)){
106
+ const renderStatus: boolean = (renderIfConfig as IModalRenderCriteria)[key]!.includes(targetField) || (renderIfConfig as IModalRenderCriteria)[key]!.includes('*')
107
+
108
+ if (render !== renderStatus) {
109
+ setRender(renderStatus)
110
+ if(!renderStatus) unregister(element.name as string)
111
+ }
112
+ }
113
+ }else if (enableCondition){
114
+ const enableIfConfig = element.enableIf
115
+ const isLiveEnable: boolean = !!enableIfConfig?.action && !!enableIfConfig.condition
116
+
117
+ if(isLiveEnable && enableIfConfig!.condition!.includes(key)){
118
+ const enableStatus: boolean = await liveDataHandler('enableIf', targetField, formData)
119
+ if (enable !== enableStatus) setEnable(enableStatus)
120
+ }else if(enableConditionList.includes(key)){
121
+ const enableStatus: boolean = (enableIfConfig as IModalRenderCriteria)[key]!.includes(targetField) || (enableIfConfig as IModalRenderCriteria)[key]!.includes('*')
122
+ if (enable !== enableStatus) setEnable(enableStatus)
97
123
  }
98
- } else if (enableCondition && element.enableIf?.condition.includes(key)){
99
- const enableStatus: boolean = await liveDataHandler('enableIf', targetField, formData)
100
- if (enable !== enableStatus) setEnable(enableStatus)
101
124
  }
102
125
 
103
126
  if (liveDataCondition && element.liveData?.condition.includes(key)) {
@@ -14,11 +14,15 @@ export type IModalField = IMakeSelect | IMakeInput | IMakeFieldGroup | IMakeText
14
14
 
15
15
  export type IFormField = IMakeSelect | IMakeInput | IMakeTextarea | IMakeToggle
16
16
 
17
- export type IModalRenderCondition = {
17
+ export interface IModalRenderAction {
18
18
  action: (data: string, ...args: any[]) => Promise<boolean>
19
19
  condition: Array<string>
20
20
  }
21
21
 
22
+ export type IModalRenderCriteria = Record<string, Array<string | number | boolean> | undefined>
23
+
24
+ export type IModalRenderCondition = IModalRenderAction | IModalRenderCriteria
25
+
22
26
  export type IModalLiveDataCondition = {
23
27
  action: (data: string, ...args: any[]) => Promise<Array<IOption>>
24
28
  condition: Array<string>