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.
- package/dist/hooks/field-render.js +36 -15
- package/dist/interfaces/modal.d.ts +4 -2
- package/package.json +1 -1
- package/src/hooks/field-render.ts +34 -11
- package/src/interfaces/modal.ts +5 -1
|
@@ -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
|
-
|
|
51
|
-
if (typeof field === 'string' &&
|
|
52
|
-
const result =
|
|
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
|
|
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
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
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 && ((
|
|
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
|
|
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
|
@@ -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
|
-
|
|
78
|
-
|
|
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
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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)) {
|
package/src/interfaces/modal.ts
CHANGED
|
@@ -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
|
|
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>
|