dynamic-modal 1.1.4 → 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.
- package/README-ES.md +217 -217
- package/README.md +216 -216
- package/dist/components/input-upload/input-upload.js +1 -1
- package/dist/components/make-button/make-button.js +7 -17
- package/dist/components/make-input/make-input.js +7 -17
- package/dist/components/make-select/make-select.js +7 -17
- package/dist/components/make-textarea/make-textarea.js +7 -17
- package/dist/components/make-toggle/make-toggle.js +7 -17
- package/dist/components/portal/portal.js +7 -17
- package/dist/context/component/component-state.js +7 -17
- package/dist/hooks/field-render.d.ts +1 -0
- package/dist/hooks/field-render.js +22 -15
- package/dist/interfaces/modal.d.ts +5 -1
- package/dist/modal.js +38 -19
- package/eslint.config.mjs +14 -14
- package/examples/enable-if.ts +127 -127
- package/examples/live-data.ts +61 -61
- package/examples/render-if.ts +128 -128
- package/examples/simple.ts +74 -74
- package/index.ts +5 -5
- package/package.json +46 -47
- package/src/components/input-upload/input-upload.tsx +67 -67
- package/src/components/make-button/make-button.tsx +18 -18
- package/src/components/make-description/make-description.tsx +15 -15
- package/src/components/make-input/make-input.tsx +53 -53
- package/src/components/make-select/make-select.tsx +55 -55
- package/src/components/make-textarea/make-textarea.tsx +53 -53
- package/src/components/make-toggle/make-toggle.tsx +53 -53
- package/src/components/make-upload/make-upload.tsx +40 -40
- package/src/components/portal/portal.tsx +37 -37
- package/src/context/component/component-state.tsx +17 -17
- package/src/hooks/field-render.ts +120 -109
- package/src/hooks/modal-handler.ts +38 -38
- package/src/interfaces/component-state.ts +33 -33
- package/src/interfaces/field.ts +37 -37
- package/src/interfaces/input-upload.ts +21 -21
- package/src/interfaces/make-button.ts +19 -19
- package/src/interfaces/make-description.ts +14 -14
- package/src/interfaces/make-field-group.ts +14 -14
- package/src/interfaces/make-input.ts +14 -14
- package/src/interfaces/make-select.ts +15 -15
- package/src/interfaces/make-textarea.ts +11 -11
- package/src/interfaces/make-toggle.ts +9 -9
- package/src/interfaces/make-upload.ts +14 -14
- package/src/interfaces/modal.ts +50 -46
- package/src/interfaces/option.ts +3 -3
- package/src/interfaces/portal.ts +8 -8
- package/src/modal.tsx +196 -164
- package/src/tools/general.ts +8 -8
- package/tsconfig.json +13 -13
|
@@ -16,23 +16,13 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
16
16
|
}) : function(o, v) {
|
|
17
17
|
o["default"] = v;
|
|
18
18
|
});
|
|
19
|
-
var __importStar = (this && this.__importStar) ||
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return ownKeys(o);
|
|
27
|
-
};
|
|
28
|
-
return function (mod) {
|
|
29
|
-
if (mod && mod.__esModule) return mod;
|
|
30
|
-
var result = {};
|
|
31
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
-
__setModuleDefault(result, mod);
|
|
33
|
-
return result;
|
|
34
|
-
};
|
|
35
|
-
})();
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
36
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
27
|
exports.ComponentState = exports.ComponentStateContext = void 0;
|
|
38
28
|
const react_1 = __importStar(require("react"));
|
|
@@ -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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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 &&
|
|
63
|
-
const enableStatus =
|
|
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 && ((
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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 =
|
|
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>;
|
|
@@ -24,6 +27,7 @@ export interface IModalConfigProps {
|
|
|
24
27
|
style?: CSSProperties;
|
|
25
28
|
overFlowBody?: string | number;
|
|
26
29
|
minHeightBody?: string | number;
|
|
30
|
+
useSubmit?: boolean;
|
|
27
31
|
actions: {
|
|
28
32
|
containerStyle?: CSSProperties;
|
|
29
33
|
cancel?: Omit<IMakeButton, 'elementType'>;
|
package/dist/modal.js
CHANGED
|
@@ -16,23 +16,22 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
16
16
|
}) : function(o, v) {
|
|
17
17
|
o["default"] = v;
|
|
18
18
|
});
|
|
19
|
-
var __importStar = (this && this.__importStar) ||
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
28
|
-
return function (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
})();
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
27
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
28
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
29
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
30
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
31
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
32
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
33
|
+
});
|
|
34
|
+
};
|
|
36
35
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
37
36
|
var t = {};
|
|
38
37
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
@@ -64,7 +63,7 @@ const Modal = ({ open, close, config }) => {
|
|
|
64
63
|
const { ModalButtonAction, ModalButtonCancel } = (0, react_1.useContext)(component_state_1.ComponentStateContext);
|
|
65
64
|
const [modalReady, setModalReady] = (0, react_1.useState)(undefined);
|
|
66
65
|
const [defaultLoaded, setDefaultLoaded] = (0, react_1.useState)(false);
|
|
67
|
-
const { control, handleSubmit, getValues, unregister, setValue, watch } = (0, react_hook_form_1.useForm)();
|
|
66
|
+
const { control, handleSubmit, getValues, unregister, setValue, watch, trigger, getFieldState } = (0, react_hook_form_1.useForm)();
|
|
68
67
|
const formValueHandler = (element) => {
|
|
69
68
|
var _a;
|
|
70
69
|
if (['group', 'upload', 'text'].includes(element.elementType))
|
|
@@ -94,6 +93,10 @@ const Modal = ({ open, close, config }) => {
|
|
|
94
93
|
});
|
|
95
94
|
setDefaultLoaded(true);
|
|
96
95
|
};
|
|
96
|
+
const getUseSubmit = (modalConfig) => {
|
|
97
|
+
const useSubmit = modalConfig.useSubmit === undefined ? true : modalConfig.useSubmit === false ? false : true;
|
|
98
|
+
return useSubmit;
|
|
99
|
+
};
|
|
97
100
|
const getRender = (_a, index, isEndOfRender) => {
|
|
98
101
|
var { elementType } = _a, element = __rest(_a, ["elementType"]);
|
|
99
102
|
if (isEndOfRender === void 0) { isEndOfRender = false; }
|
|
@@ -132,6 +135,20 @@ const Modal = ({ open, close, config }) => {
|
|
|
132
135
|
close();
|
|
133
136
|
}, 200);
|
|
134
137
|
};
|
|
138
|
+
const manualSubmit = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
139
|
+
var _a;
|
|
140
|
+
const form = getValues();
|
|
141
|
+
const fields = Object.keys(form);
|
|
142
|
+
yield trigger(fields);
|
|
143
|
+
const validations = fields.map(field => {
|
|
144
|
+
const { invalid } = getFieldState(field);
|
|
145
|
+
return invalid;
|
|
146
|
+
});
|
|
147
|
+
const result = validations.some(isInvalid => isInvalid) ? undefined : form;
|
|
148
|
+
if (!result)
|
|
149
|
+
return;
|
|
150
|
+
actionHandler(Object.assign(Object.assign({}, ((_a = modalReady === null || modalReady === void 0 ? void 0 : modalReady.reservedData) !== null && _a !== void 0 ? _a : {})), form));
|
|
151
|
+
});
|
|
135
152
|
const actionHandler = (data) => {
|
|
136
153
|
var _a;
|
|
137
154
|
modalReady === null || modalReady === void 0 ? void 0 : modalReady.out(Object.assign(Object.assign({}, ((_a = modalReady === null || modalReady === void 0 ? void 0 : modalReady.reservedData) !== null && _a !== void 0 ? _a : {})), data));
|
|
@@ -165,7 +182,9 @@ const Modal = ({ open, close, config }) => {
|
|
|
165
182
|
})),
|
|
166
183
|
react_1.default.createElement("div", { className: 'flex gap-4 items-center justify-center border-t', style: modalReady === null || modalReady === void 0 ? void 0 : modalReady.actions.containerStyle },
|
|
167
184
|
modalReady.actions.cancel && react_1.default.createElement(ModalButtonCancel, Object.assign({}, modalReady.actions.cancel, { onClick: closeHandler })),
|
|
168
|
-
|
|
185
|
+
getUseSubmit(modalReady) ?
|
|
186
|
+
react_1.default.createElement(ModalButtonAction, Object.assign({}, modalReady.actions.action, { type: 'submit' })) :
|
|
187
|
+
react_1.default.createElement(ModalButtonAction, Object.assign({}, modalReady.actions.action, { onClick: manualSubmit, type: 'button' }))))))
|
|
169
188
|
: null);
|
|
170
189
|
};
|
|
171
190
|
exports.Modal = Modal;
|
package/eslint.config.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import globals from "globals"
|
|
2
|
-
import pluginJs from "@eslint/js"
|
|
3
|
-
import tseslint from "typescript-eslint"
|
|
4
|
-
import pluginReact from "eslint-plugin-react"
|
|
5
|
-
|
|
6
|
-
export default [
|
|
7
|
-
{ files: ['**/*.{ts,tsx}'] },
|
|
8
|
-
{ languageOptions: { globals: globals.browser } },
|
|
9
|
-
{ settings: { react: { version: 'detect' } } },
|
|
10
|
-
{ ignores : ['dist/**', 'node_modules/**'] },
|
|
11
|
-
pluginJs.configs.recommended,
|
|
12
|
-
...tseslint.configs.recommended,
|
|
13
|
-
pluginReact.configs.flat.recommended,
|
|
14
|
-
]
|
|
1
|
+
import globals from "globals"
|
|
2
|
+
import pluginJs from "@eslint/js"
|
|
3
|
+
import tseslint from "typescript-eslint"
|
|
4
|
+
import pluginReact from "eslint-plugin-react"
|
|
5
|
+
|
|
6
|
+
export default [
|
|
7
|
+
{ files: ['**/*.{ts,tsx}'] },
|
|
8
|
+
{ languageOptions: { globals: globals.browser } },
|
|
9
|
+
{ settings: { react: { version: 'detect' } } },
|
|
10
|
+
{ ignores : ['dist/**', 'node_modules/**'] },
|
|
11
|
+
pluginJs.configs.recommended,
|
|
12
|
+
...tseslint.configs.recommended,
|
|
13
|
+
pluginReact.configs.flat.recommended,
|
|
14
|
+
]
|
package/examples/enable-if.ts
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
|
|
2
|
-
import { IModalConfigLoader } from '../src/interfaces/modal'
|
|
3
|
-
|
|
4
|
-
export type IncomingProps = object
|
|
5
|
-
|
|
6
|
-
type ResultProps = Record<'optionId', string> & Partial<Record<'input1ReadOnly' | 'input1WriteValue' | 'input2ReadOnly' | 'input2WriteValue', string>>
|
|
7
|
-
|
|
8
|
-
export interface IEnableIfModal {
|
|
9
|
-
default: IModalConfigLoader<IncomingProps, ResultProps>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const enableIfModal: IEnableIfModal = {
|
|
13
|
-
default: (props, action) => {
|
|
14
|
-
return {
|
|
15
|
-
reservedData: {}, //Put here any data that you want store and receive into modal output
|
|
16
|
-
fields: [ //Put here any elements for render into modal
|
|
17
|
-
{
|
|
18
|
-
elementType: 'select',
|
|
19
|
-
label: 'option',
|
|
20
|
-
defaultValue: '0',
|
|
21
|
-
options: [
|
|
22
|
-
{
|
|
23
|
-
id: '0',
|
|
24
|
-
name:'Select...'
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
id: '1',
|
|
28
|
-
name:'Option 1'
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
id: '2',
|
|
32
|
-
name:'Option 2'
|
|
33
|
-
}
|
|
34
|
-
],
|
|
35
|
-
name: 'optionId', //Element to be observed
|
|
36
|
-
validation: {
|
|
37
|
-
required: true,
|
|
38
|
-
message: 'Please select a valid option'
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
elementType: 'group', //Put here groups of element into same line and customize using styles
|
|
43
|
-
groups: [
|
|
44
|
-
{
|
|
45
|
-
elementType: 'input',
|
|
46
|
-
label: 'Input 1 (Read)',
|
|
47
|
-
name: 'input1ReadOnly',
|
|
48
|
-
styles: {
|
|
49
|
-
width: '50%'
|
|
50
|
-
},
|
|
51
|
-
validation: {
|
|
52
|
-
required: false
|
|
53
|
-
},
|
|
54
|
-
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
55
|
-
enableIf: {
|
|
56
|
-
optionId: ['1']
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
elementType: 'input',
|
|
61
|
-
label: 'Input 1',
|
|
62
|
-
name: 'input1WriteValue',
|
|
63
|
-
styles: {
|
|
64
|
-
width: '50%'
|
|
65
|
-
},
|
|
66
|
-
validation: {
|
|
67
|
-
required: true,
|
|
68
|
-
message: `Write a valid input value`
|
|
69
|
-
},
|
|
70
|
-
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
71
|
-
enableIf: {
|
|
72
|
-
optionId: ['1']
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
]
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
elementType: 'group', //Put here groups of element into same line and customize using styles
|
|
79
|
-
groups: [
|
|
80
|
-
{
|
|
81
|
-
elementType: 'input',
|
|
82
|
-
label: 'Input 2 (Read)',
|
|
83
|
-
name: 'input2ReadOnly',
|
|
84
|
-
disabled: true,
|
|
85
|
-
styles: {
|
|
86
|
-
width: '50%'
|
|
87
|
-
},
|
|
88
|
-
validation: {
|
|
89
|
-
required: false
|
|
90
|
-
},
|
|
91
|
-
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
92
|
-
enableIf: {
|
|
93
|
-
optionId: ['2']
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
elementType: 'input',
|
|
98
|
-
label: 'Input 2',
|
|
99
|
-
name: 'input2WriteValue',
|
|
100
|
-
styles: {
|
|
101
|
-
width: '50%'
|
|
102
|
-
},
|
|
103
|
-
validation: {
|
|
104
|
-
required: true,
|
|
105
|
-
message: `Write a valid input value`
|
|
106
|
-
},
|
|
107
|
-
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
108
|
-
enableIf: {
|
|
109
|
-
optionId: ['2']
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
]
|
|
113
|
-
},
|
|
114
|
-
],
|
|
115
|
-
style: { // Put here styles for modal like height or width
|
|
116
|
-
width: '500px'
|
|
117
|
-
},
|
|
118
|
-
title: `Title of enable if modal`, //Title of modal
|
|
119
|
-
out: action, //Function to receive data from modal
|
|
120
|
-
actions: { //Actions of modal, all props that you define here will be pass to ModalButtonCancel and ModalButtonAction components in your ComponentState
|
|
121
|
-
action: { text:'Action', color: 'primary' },
|
|
122
|
-
cancel: { text: 'Cancel', color: 'danger' }
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
1
|
+
|
|
2
|
+
import { IModalConfigLoader } from '../src/interfaces/modal'
|
|
3
|
+
|
|
4
|
+
export type IncomingProps = object
|
|
5
|
+
|
|
6
|
+
type ResultProps = Record<'optionId', string> & Partial<Record<'input1ReadOnly' | 'input1WriteValue' | 'input2ReadOnly' | 'input2WriteValue', string>>
|
|
7
|
+
|
|
8
|
+
export interface IEnableIfModal {
|
|
9
|
+
default: IModalConfigLoader<IncomingProps, ResultProps>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const enableIfModal: IEnableIfModal = {
|
|
13
|
+
default: (props, action) => {
|
|
14
|
+
return {
|
|
15
|
+
reservedData: {}, //Put here any data that you want store and receive into modal output
|
|
16
|
+
fields: [ //Put here any elements for render into modal
|
|
17
|
+
{
|
|
18
|
+
elementType: 'select',
|
|
19
|
+
label: 'option',
|
|
20
|
+
defaultValue: '0',
|
|
21
|
+
options: [
|
|
22
|
+
{
|
|
23
|
+
id: '0',
|
|
24
|
+
name:'Select...'
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: '1',
|
|
28
|
+
name:'Option 1'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: '2',
|
|
32
|
+
name:'Option 2'
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
name: 'optionId', //Element to be observed
|
|
36
|
+
validation: {
|
|
37
|
+
required: true,
|
|
38
|
+
message: 'Please select a valid option'
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
elementType: 'group', //Put here groups of element into same line and customize using styles
|
|
43
|
+
groups: [
|
|
44
|
+
{
|
|
45
|
+
elementType: 'input',
|
|
46
|
+
label: 'Input 1 (Read)',
|
|
47
|
+
name: 'input1ReadOnly',
|
|
48
|
+
styles: {
|
|
49
|
+
width: '50%'
|
|
50
|
+
},
|
|
51
|
+
validation: {
|
|
52
|
+
required: false
|
|
53
|
+
},
|
|
54
|
+
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
55
|
+
enableIf: {
|
|
56
|
+
optionId: ['1']
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
elementType: 'input',
|
|
61
|
+
label: 'Input 1',
|
|
62
|
+
name: 'input1WriteValue',
|
|
63
|
+
styles: {
|
|
64
|
+
width: '50%'
|
|
65
|
+
},
|
|
66
|
+
validation: {
|
|
67
|
+
required: true,
|
|
68
|
+
message: `Write a valid input value`
|
|
69
|
+
},
|
|
70
|
+
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
71
|
+
enableIf: {
|
|
72
|
+
optionId: ['1']
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
]
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
elementType: 'group', //Put here groups of element into same line and customize using styles
|
|
79
|
+
groups: [
|
|
80
|
+
{
|
|
81
|
+
elementType: 'input',
|
|
82
|
+
label: 'Input 2 (Read)',
|
|
83
|
+
name: 'input2ReadOnly',
|
|
84
|
+
disabled: true,
|
|
85
|
+
styles: {
|
|
86
|
+
width: '50%'
|
|
87
|
+
},
|
|
88
|
+
validation: {
|
|
89
|
+
required: false
|
|
90
|
+
},
|
|
91
|
+
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
92
|
+
enableIf: {
|
|
93
|
+
optionId: ['2']
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
elementType: 'input',
|
|
98
|
+
label: 'Input 2',
|
|
99
|
+
name: 'input2WriteValue',
|
|
100
|
+
styles: {
|
|
101
|
+
width: '50%'
|
|
102
|
+
},
|
|
103
|
+
validation: {
|
|
104
|
+
required: true,
|
|
105
|
+
message: `Write a valid input value`
|
|
106
|
+
},
|
|
107
|
+
//Define enable condition (element will be enabled when value of element observed match with array condition)
|
|
108
|
+
enableIf: {
|
|
109
|
+
optionId: ['2']
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
style: { // Put here styles for modal like height or width
|
|
116
|
+
width: '500px'
|
|
117
|
+
},
|
|
118
|
+
title: `Title of enable if modal`, //Title of modal
|
|
119
|
+
out: action, //Function to receive data from modal
|
|
120
|
+
actions: { //Actions of modal, all props that you define here will be pass to ModalButtonCancel and ModalButtonAction components in your ComponentState
|
|
121
|
+
action: { text:'Action', color: 'primary' },
|
|
122
|
+
cancel: { text: 'Cancel', color: 'danger' }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
128
|
export default enableIfModal
|
package/examples/live-data.ts
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { IModalConfigLoader } from '../src/interfaces/modal'
|
|
3
|
-
import { IOption } from '../src/interfaces/option'
|
|
4
|
-
|
|
5
|
-
type IncomingProps = {
|
|
6
|
-
typeList: Array<IOption>
|
|
7
|
-
|
|
8
|
-
//Define function to receive selected element and formData (if you need to use) make read or select operations and build
|
|
9
|
-
//next select array of elements
|
|
10
|
-
optionReadAction : (typeId: string, formData: any) => Promise<Array<IOption>>
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type ResultProps = Omit<IncomingProps,'optionReadAction'|'typeList'> & Record<'typeId'|'optionId', string>
|
|
14
|
-
|
|
15
|
-
export interface ILiveDataModal {
|
|
16
|
-
default: IModalConfigLoader<IncomingProps, ResultProps>
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const liveDataModal: ILiveDataModal = {
|
|
20
|
-
default: (props, action) => {
|
|
21
|
-
return {
|
|
22
|
-
reservedData: {}, //Put here any data that you want store and receive into modal output
|
|
23
|
-
fields: [ //Put here any elements for render into modal
|
|
24
|
-
{
|
|
25
|
-
elementType: 'select',
|
|
26
|
-
label: 'Type',
|
|
27
|
-
options: props.typeList,
|
|
28
|
-
name: 'typeId', //Element to be observed
|
|
29
|
-
validation: {
|
|
30
|
-
required: true,
|
|
31
|
-
message: 'Please select a valid type'
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
elementType: 'select',
|
|
36
|
-
label: 'Options',
|
|
37
|
-
options: [],
|
|
38
|
-
name: 'optionId',
|
|
39
|
-
validation: {
|
|
40
|
-
required: true,
|
|
41
|
-
message: 'Please select a valid option'
|
|
42
|
-
},
|
|
43
|
-
liveData: {
|
|
44
|
-
action: props.optionReadAction, //Define live data action (function that receive data selected from other field for execute and build other options)
|
|
45
|
-
condition: ['typeId'] //Define element into modal was to observed
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
],
|
|
49
|
-
style: { // Put here styles for modal like height or width
|
|
50
|
-
width: '500px'
|
|
51
|
-
},
|
|
52
|
-
title: `Title of enable if modal`, //Title of modal
|
|
53
|
-
out: action, //Function to receive data from modal
|
|
54
|
-
actions: { //Actions of modal, all props that you define here will be pass to ModalButtonCancel and ModalButtonAction components in your ComponentState
|
|
55
|
-
action: { text:'Action', color: 'primary' },
|
|
56
|
-
cancel: { text: 'Cancel', color: 'danger' }
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { IModalConfigLoader } from '../src/interfaces/modal'
|
|
3
|
+
import { IOption } from '../src/interfaces/option'
|
|
4
|
+
|
|
5
|
+
type IncomingProps = {
|
|
6
|
+
typeList: Array<IOption>
|
|
7
|
+
|
|
8
|
+
//Define function to receive selected element and formData (if you need to use) make read or select operations and build
|
|
9
|
+
//next select array of elements
|
|
10
|
+
optionReadAction : (typeId: string, formData: any) => Promise<Array<IOption>>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type ResultProps = Omit<IncomingProps,'optionReadAction'|'typeList'> & Record<'typeId'|'optionId', string>
|
|
14
|
+
|
|
15
|
+
export interface ILiveDataModal {
|
|
16
|
+
default: IModalConfigLoader<IncomingProps, ResultProps>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const liveDataModal: ILiveDataModal = {
|
|
20
|
+
default: (props, action) => {
|
|
21
|
+
return {
|
|
22
|
+
reservedData: {}, //Put here any data that you want store and receive into modal output
|
|
23
|
+
fields: [ //Put here any elements for render into modal
|
|
24
|
+
{
|
|
25
|
+
elementType: 'select',
|
|
26
|
+
label: 'Type',
|
|
27
|
+
options: props.typeList,
|
|
28
|
+
name: 'typeId', //Element to be observed
|
|
29
|
+
validation: {
|
|
30
|
+
required: true,
|
|
31
|
+
message: 'Please select a valid type'
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
elementType: 'select',
|
|
36
|
+
label: 'Options',
|
|
37
|
+
options: [],
|
|
38
|
+
name: 'optionId',
|
|
39
|
+
validation: {
|
|
40
|
+
required: true,
|
|
41
|
+
message: 'Please select a valid option'
|
|
42
|
+
},
|
|
43
|
+
liveData: {
|
|
44
|
+
action: props.optionReadAction, //Define live data action (function that receive data selected from other field for execute and build other options)
|
|
45
|
+
condition: ['typeId'] //Define element into modal was to observed
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
style: { // Put here styles for modal like height or width
|
|
50
|
+
width: '500px'
|
|
51
|
+
},
|
|
52
|
+
title: `Title of enable if modal`, //Title of modal
|
|
53
|
+
out: action, //Function to receive data from modal
|
|
54
|
+
actions: { //Actions of modal, all props that you define here will be pass to ModalButtonCancel and ModalButtonAction components in your ComponentState
|
|
55
|
+
action: { text:'Action', color: 'primary' },
|
|
56
|
+
cancel: { text: 'Cancel', color: 'danger' }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
62
|
export default liveDataModal
|