dynamic-modal 1.0.3 → 1.0.5
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 +2 -0
- package/README.md +2 -0
- package/dist/interfaces/make-autocomplete.d.ts +0 -2
- package/dist/interfaces/make-select.d.ts +0 -2
- package/dist/interfaces/modal.d.ts +1 -1
- package/dist/modal.js +154 -150
- package/examples/enable-if.ts +130 -1
- package/examples/live-data.ts +1 -1
- package/examples/render-if.ts +131 -1
- package/examples/simple.ts +3 -2
- package/package.json +1 -1
- package/src/hooks/field-render.ts +2 -2
- package/src/interfaces/make-autocomplete.ts +0 -2
- package/src/interfaces/make-select.ts +0 -2
- package/src/interfaces/modal.ts +1 -1
- package/src/modal.tsx +5 -0
package/README-ES.md
CHANGED
|
@@ -87,6 +87,8 @@ Para controlar la apertura y cierre del modal, obtén el custom hook `useModalHa
|
|
|
87
87
|
```jsx
|
|
88
88
|
import { useModalHandler, DynamicModal } from 'dynamic-modal';
|
|
89
89
|
import { Button } from '@nextui-org/react';
|
|
90
|
+
//Crea el modal, importa y usa
|
|
91
|
+
import testModal from '@modal-config/test';
|
|
90
92
|
|
|
91
93
|
function ExampleComponent() {
|
|
92
94
|
const { openModal, modalProps } = useModalHandler();
|
package/README.md
CHANGED
|
@@ -87,6 +87,8 @@ To control the modal’s open and close states, use the `useModalHandler` custom
|
|
|
87
87
|
```jsx
|
|
88
88
|
import { useModalHandler, DynamicModal } from 'dynamic-modal';
|
|
89
89
|
import { Button } from '@nextui-org/react';
|
|
90
|
+
//Create your modal, import and use
|
|
91
|
+
import testModal from '@modal-config/test';
|
|
90
92
|
|
|
91
93
|
function ExampleComponent() {
|
|
92
94
|
const { openModal, modalProps } = useModalHandler();
|
|
@@ -4,8 +4,6 @@ import { IOption } from './option';
|
|
|
4
4
|
export interface IMakeAutoComplete extends IField {
|
|
5
5
|
elementType: 'autocomplete';
|
|
6
6
|
options: Array<IOption>;
|
|
7
|
-
defaultOption?: boolean;
|
|
8
|
-
defaultOptionName?: string;
|
|
9
7
|
liveData?: IModalLiveDataCondition;
|
|
10
8
|
}
|
|
11
9
|
export interface IMakeAutoCompleteProps extends IFieldProps {
|
|
@@ -3,8 +3,6 @@ import { IModalLiveDataCondition } from './modal';
|
|
|
3
3
|
export interface IMakeSelect extends IField {
|
|
4
4
|
elementType: 'select';
|
|
5
5
|
options: Array<Record<'id' | 'name', string>>;
|
|
6
|
-
defaultOption?: boolean;
|
|
7
|
-
defaultOptionName?: string;
|
|
8
6
|
liveData?: IModalLiveDataCondition;
|
|
9
7
|
}
|
|
10
8
|
export interface IMakeSelectProps extends IFieldProps {
|
|
@@ -30,7 +30,7 @@ export interface IModalConfigProps {
|
|
|
30
30
|
action?: () => void;
|
|
31
31
|
hide?: boolean;
|
|
32
32
|
};
|
|
33
|
-
reservedData?: Record<string,
|
|
33
|
+
reservedData?: Record<string, any>;
|
|
34
34
|
styles?: CSSProperties;
|
|
35
35
|
overFlowBody?: string | number;
|
|
36
36
|
minHeightBody?: string | number;
|
package/dist/modal.js
CHANGED
|
@@ -1,150 +1,154 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
'use client';
|
|
3
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
-
if (k2 === undefined) k2 = k;
|
|
5
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
-
}
|
|
9
|
-
Object.defineProperty(o, k2, desc);
|
|
10
|
-
}) : (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
o[k2] = m[k];
|
|
13
|
-
}));
|
|
14
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
-
}) : function(o, v) {
|
|
17
|
-
o["default"] = v;
|
|
18
|
-
});
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
-
};
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Modal = void 0;
|
|
31
|
-
const react_1 = __importStar(require("react"));
|
|
32
|
-
const react_hook_form_1 = require("react-hook-form");
|
|
33
|
-
const react_2 = require("@nextui-org/react");
|
|
34
|
-
const portal_1 = require("./components/portal/portal");
|
|
35
|
-
const make_toggle_1 = __importDefault(require("./components/make-toggle/make-toggle"));
|
|
36
|
-
const make_input_1 = __importDefault(require("./components/make-input/make-input"));
|
|
37
|
-
const make_select_1 = __importDefault(require("./components/make-select/make-select"));
|
|
38
|
-
const make_textarea_1 = __importDefault(require("./components/make-textarea/make-textarea"));
|
|
39
|
-
const make_multi_select_1 = __importDefault(require("./components/make-multi-select/make-multi-select"));
|
|
40
|
-
const make_text_1 = __importDefault(require("./components/make-text/make-text"));
|
|
41
|
-
const make_upload_1 = __importDefault(require("./components/make-upload/make-upload"));
|
|
42
|
-
const make_button_1 = __importDefault(require("./components/make-button/make-button"));
|
|
43
|
-
const make_autocomplete_1 = __importDefault(require("./components/make-autocomplete/make-autocomplete"));
|
|
44
|
-
const Modal = ({ open, close, config }) => {
|
|
45
|
-
var _a, _b;
|
|
46
|
-
const [modalReady, setModalReady] = (0, react_1.useState)(undefined);
|
|
47
|
-
const [defaultLoaded, setDefaultLoaded] = (0, react_1.useState)(false);
|
|
48
|
-
const { control, handleSubmit, getValues, unregister, setValue, watch } = (0, react_hook_form_1.useForm)();
|
|
49
|
-
const formValueHandler = (element) => {
|
|
50
|
-
var _a;
|
|
51
|
-
if (['group', 'upload', 'text'].includes(element.elementType))
|
|
52
|
-
return;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.Modal = void 0;
|
|
31
|
+
const react_1 = __importStar(require("react"));
|
|
32
|
+
const react_hook_form_1 = require("react-hook-form");
|
|
33
|
+
const react_2 = require("@nextui-org/react");
|
|
34
|
+
const portal_1 = require("./components/portal/portal");
|
|
35
|
+
const make_toggle_1 = __importDefault(require("./components/make-toggle/make-toggle"));
|
|
36
|
+
const make_input_1 = __importDefault(require("./components/make-input/make-input"));
|
|
37
|
+
const make_select_1 = __importDefault(require("./components/make-select/make-select"));
|
|
38
|
+
const make_textarea_1 = __importDefault(require("./components/make-textarea/make-textarea"));
|
|
39
|
+
const make_multi_select_1 = __importDefault(require("./components/make-multi-select/make-multi-select"));
|
|
40
|
+
const make_text_1 = __importDefault(require("./components/make-text/make-text"));
|
|
41
|
+
const make_upload_1 = __importDefault(require("./components/make-upload/make-upload"));
|
|
42
|
+
const make_button_1 = __importDefault(require("./components/make-button/make-button"));
|
|
43
|
+
const make_autocomplete_1 = __importDefault(require("./components/make-autocomplete/make-autocomplete"));
|
|
44
|
+
const Modal = ({ open, close, config }) => {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
const [modalReady, setModalReady] = (0, react_1.useState)(undefined);
|
|
47
|
+
const [defaultLoaded, setDefaultLoaded] = (0, react_1.useState)(false);
|
|
48
|
+
const { control, handleSubmit, getValues, unregister, setValue, watch } = (0, react_hook_form_1.useForm)();
|
|
49
|
+
const formValueHandler = (element) => {
|
|
50
|
+
var _a;
|
|
51
|
+
if (['group', 'upload', 'text'].includes(element.elementType))
|
|
52
|
+
return;
|
|
53
|
+
if (!element.defaultValue && element.renderIf) {
|
|
54
|
+
unregister(element.name);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const defaultValue = (_a = element.defaultValue) !== null && _a !== void 0 ? _a : '';
|
|
58
|
+
const parsedValue = defaultValue === 'true' ? true : defaultValue === 'false' ? false : defaultValue;
|
|
59
|
+
setValue(element.name, parsedValue !== null && parsedValue !== void 0 ? parsedValue : '');
|
|
60
|
+
};
|
|
61
|
+
const autoLoadGroup = (groupFields) => {
|
|
62
|
+
groupFields.forEach(element => {
|
|
63
|
+
formValueHandler(element);
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const autoLoadField = (modalFields) => {
|
|
67
|
+
if (defaultLoaded)
|
|
68
|
+
return;
|
|
69
|
+
modalFields.forEach(element => {
|
|
70
|
+
if (element.elementType === 'group') {
|
|
71
|
+
autoLoadGroup(element.groups);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
formValueHandler(element);
|
|
75
|
+
});
|
|
76
|
+
setDefaultLoaded(true);
|
|
77
|
+
};
|
|
78
|
+
const getRender = (element, index, isEndOfRender = false) => {
|
|
79
|
+
if (isEndOfRender && modalReady)
|
|
80
|
+
setTimeout(() => autoLoadField(modalReady.fields), 200);
|
|
81
|
+
const props = {
|
|
82
|
+
control,
|
|
83
|
+
watch,
|
|
84
|
+
setValue
|
|
85
|
+
};
|
|
86
|
+
return element.elementType === 'input'
|
|
87
|
+
? react_1.default.createElement(make_input_1.default, Object.assign({}, props, { key: `modal-input-${index}`, element: element }))
|
|
88
|
+
: element.elementType === 'select'
|
|
89
|
+
? react_1.default.createElement(make_select_1.default, Object.assign({}, props, { key: `modal-select-${index}`, element: element }))
|
|
90
|
+
: element.elementType === 'textarea'
|
|
91
|
+
? react_1.default.createElement(make_textarea_1.default, Object.assign({}, props, { key: `modal-textarea-${index}`, element: element }))
|
|
92
|
+
: element.elementType === 'toggle'
|
|
93
|
+
? react_1.default.createElement(make_toggle_1.default, Object.assign({}, props, { key: `modal-toggle-${index}`, element: element }))
|
|
94
|
+
: element.elementType === 'multiselect'
|
|
95
|
+
? react_1.default.createElement(make_multi_select_1.default, Object.assign({}, props, { key: `modal-multiselect-${index}`, element: element }))
|
|
96
|
+
: element.elementType === 'text'
|
|
97
|
+
? react_1.default.createElement(make_text_1.default, Object.assign({}, props, { key: `modal-text-${index}`, element: element }))
|
|
98
|
+
: element.elementType === 'upload'
|
|
99
|
+
? react_1.default.createElement(make_upload_1.default, Object.assign({}, props, { key: `modal-upload-${index}`, element: element }))
|
|
100
|
+
: element.elementType === 'button'
|
|
101
|
+
? react_1.default.createElement(make_button_1.default, Object.assign({}, props, { key: `modal-button-${index}`, element: element }))
|
|
102
|
+
: element.elementType === 'autocomplete'
|
|
103
|
+
? react_1.default.createElement(make_autocomplete_1.default, Object.assign({}, props, { key: `modal-autocomplete-${index}`, element: element }))
|
|
104
|
+
: null;
|
|
105
|
+
};
|
|
106
|
+
const closeHandler = () => {
|
|
107
|
+
if ((modalReady === null || modalReady === void 0 ? void 0 : modalReady.cancel) && modalReady.cancel.action)
|
|
108
|
+
modalReady.cancel.action();
|
|
109
|
+
setTimeout(() => {
|
|
110
|
+
const form = getValues();
|
|
111
|
+
unregister(Object.keys(form));
|
|
112
|
+
setModalReady(undefined);
|
|
113
|
+
setDefaultLoaded(false);
|
|
114
|
+
close();
|
|
115
|
+
}, 200);
|
|
116
|
+
};
|
|
117
|
+
const actionHandler = (data) => {
|
|
118
|
+
if ((modalReady === null || modalReady === void 0 ? void 0 : modalReady.action) && modalReady.action.action)
|
|
119
|
+
modalReady.action.action(Object.assign(Object.assign({}, modalReady === null || modalReady === void 0 ? void 0 : modalReady.reservedData), data));
|
|
120
|
+
closeHandler();
|
|
121
|
+
};
|
|
122
|
+
(0, react_1.useEffect)(() => {
|
|
123
|
+
if (open && !modalReady)
|
|
124
|
+
setModalReady(config);
|
|
125
|
+
}, [config, modalReady, open]);
|
|
126
|
+
return (modalReady
|
|
127
|
+
? react_1.default.createElement(portal_1.Portal, { closeTime: 200, portalOpen: open, portalTag: '#modal-portal' },
|
|
128
|
+
react_1.default.createElement("div", { className: 'rounded bg-white relative w-auto h-auto min-h-[200px] min-w-[500px]', style: Object.assign({}, modalReady.styles) },
|
|
129
|
+
react_1.default.createElement("form", { className: 'flex flex-col p-4 gap-4', autoComplete: 'off', onSubmit: handleSubmit(actionHandler) },
|
|
130
|
+
react_1.default.createElement("h2", { className: 'text-bold text-center border-b pb-4' }, modalReady.title),
|
|
131
|
+
react_1.default.createElement("div", { className: 'flex flex-col items-center gap-4 py-4', style: {
|
|
132
|
+
overflowY: modalReady.overFlowBody ? 'auto' : undefined,
|
|
133
|
+
height: modalReady.overFlowBody,
|
|
134
|
+
minHeight: modalReady.minHeightBody
|
|
135
|
+
} }, modalReady.fields.map((element, index) => {
|
|
136
|
+
const isEndOfRender = index + 1 === modalReady.fields.length;
|
|
137
|
+
if (element.elementType === 'group') {
|
|
138
|
+
return (react_1.default.createElement("div", { key: `modal-group-${index}`, className: 'flex gap-4 w-full', style: element.style }, element.groups
|
|
139
|
+
.filter(sub => ['input', 'select', 'toggle', 'multiselect', 'upload', 'button', 'autocomplete'].includes(sub.elementType))
|
|
140
|
+
.map((sub, subIndex) => getRender(sub, index + subIndex, isEndOfRender))));
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return getRender(element, index, isEndOfRender);
|
|
144
|
+
}
|
|
145
|
+
})),
|
|
146
|
+
react_1.default.createElement("div", { className: 'flex items-center justify-around gap-4 py-2 border-t' },
|
|
147
|
+
modalReady.cancel &&
|
|
148
|
+
react_1.default.createElement(react_2.Button, { variant: 'bordered', onClick: closeHandler, type: 'reset', className: 'w-[140px]' }, (_a = modalReady.cancel.name) !== null && _a !== void 0 ? _a : 'Close'),
|
|
149
|
+
modalReady.action &&
|
|
150
|
+
react_1.default.createElement(react_2.Button, { color: 'primary', type: 'submit', className: 'w-[140px]' }, (_b = modalReady.action.name) !== null && _b !== void 0 ? _b : 'Ok')))))
|
|
151
|
+
: null);
|
|
152
|
+
};
|
|
153
|
+
exports.Modal = Modal;
|
|
154
|
+
exports.default = exports.Modal;
|
package/examples/enable-if.ts
CHANGED
|
@@ -1 +1,130 @@
|
|
|
1
|
-
|
|
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
|
+
styles: { // Put here styles for modal like height or width
|
|
116
|
+
width: '500px'
|
|
117
|
+
},
|
|
118
|
+
title: `Title of enable if modal`, //Title of modal
|
|
119
|
+
action: {
|
|
120
|
+
name: 'Save', //Name of action button
|
|
121
|
+
action //Function to receive data from modal
|
|
122
|
+
},
|
|
123
|
+
cancel: {
|
|
124
|
+
name: 'Cancel' //Name of cancel button
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export default enableIfModal
|
package/examples/live-data.ts
CHANGED
|
@@ -25,7 +25,7 @@ const liveDataModal: ILiveDataModal = {
|
|
|
25
25
|
elementType: 'select',
|
|
26
26
|
label: 'Type',
|
|
27
27
|
options: props.typeList,
|
|
28
|
-
name: 'typeId', //Element observed
|
|
28
|
+
name: 'typeId', //Element to be observed
|
|
29
29
|
validation: {
|
|
30
30
|
required: true,
|
|
31
31
|
message: 'Please select a valid type'
|
package/examples/render-if.ts
CHANGED
|
@@ -1 +1,131 @@
|
|
|
1
|
-
|
|
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 IRenderIfModal {
|
|
9
|
+
default: IModalConfigLoader<IncomingProps, ResultProps>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const renderIfModal: IRenderIfModal = {
|
|
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
|
+
disabled: true,
|
|
49
|
+
styles: {
|
|
50
|
+
width: '50%'
|
|
51
|
+
},
|
|
52
|
+
validation: {
|
|
53
|
+
required: false
|
|
54
|
+
},
|
|
55
|
+
//Define render condition (element will be show when value of element observed match with array condition)
|
|
56
|
+
renderIf: {
|
|
57
|
+
optionId: ['1']
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
elementType: 'input',
|
|
62
|
+
label: 'Input 1',
|
|
63
|
+
name: 'input1WriteValue',
|
|
64
|
+
styles: {
|
|
65
|
+
width: '50%'
|
|
66
|
+
},
|
|
67
|
+
validation: {
|
|
68
|
+
required: true,
|
|
69
|
+
message: `Write a valid input value`
|
|
70
|
+
},
|
|
71
|
+
//Define render condition (element will be show when value of element observed match with array condition)
|
|
72
|
+
renderIf: {
|
|
73
|
+
optionId: ['1']
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
elementType: 'group', //Put here groups of element into same line and customize using styles
|
|
80
|
+
groups: [
|
|
81
|
+
{
|
|
82
|
+
elementType: 'input',
|
|
83
|
+
label: 'Input 2 (Read)',
|
|
84
|
+
name: 'input2ReadOnly',
|
|
85
|
+
disabled: true,
|
|
86
|
+
styles: {
|
|
87
|
+
width: '50%'
|
|
88
|
+
},
|
|
89
|
+
validation: {
|
|
90
|
+
required: false
|
|
91
|
+
},
|
|
92
|
+
//Define render condition (element will be show when value of element observed match with array condition)
|
|
93
|
+
renderIf: {
|
|
94
|
+
optionId: ['2']
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
elementType: 'input',
|
|
99
|
+
label: 'Input 2',
|
|
100
|
+
name: 'input2WriteValue',
|
|
101
|
+
styles: {
|
|
102
|
+
width: '50%'
|
|
103
|
+
},
|
|
104
|
+
validation: {
|
|
105
|
+
required: true,
|
|
106
|
+
message: `Write a valid input value`
|
|
107
|
+
},
|
|
108
|
+
//Define render condition (element will be show when value of element observed match with array condition)
|
|
109
|
+
renderIf: {
|
|
110
|
+
optionId: ['2']
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
styles: { // Put here styles for modal like height or width
|
|
117
|
+
width: '500px'
|
|
118
|
+
},
|
|
119
|
+
title: `Title of render if modal`, //Title of modal
|
|
120
|
+
action: {
|
|
121
|
+
name: 'Save', //Name of action button
|
|
122
|
+
action //Function to receive data from modal
|
|
123
|
+
},
|
|
124
|
+
cancel: {
|
|
125
|
+
name: 'Cancel' //Name of cancel button
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export default renderIfModal
|
package/examples/simple.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IModalConfigLoader } from '../src/interfaces/modal'
|
|
2
2
|
|
|
3
3
|
type IncomingProps = {
|
|
4
|
+
reserved: string
|
|
4
5
|
input1: string
|
|
5
6
|
store?: boolean
|
|
6
7
|
clear?: boolean
|
|
@@ -15,7 +16,7 @@ export interface ISimpleModal {
|
|
|
15
16
|
const simpleModal: ISimpleModal = {
|
|
16
17
|
default: (props, action) => {
|
|
17
18
|
return {
|
|
18
|
-
reservedData: {}, //Put here any data that you want store and receive into modal output
|
|
19
|
+
reservedData: { reserved: props.reserved }, //Put here any data that you want store and receive into modal output
|
|
19
20
|
fields: [ //Put here any elements for render into modal
|
|
20
21
|
{
|
|
21
22
|
elementType: 'input',
|
|
@@ -67,7 +68,7 @@ const simpleModal: ISimpleModal = {
|
|
|
67
68
|
action //Function to receive data from modal
|
|
68
69
|
},
|
|
69
70
|
cancel: {
|
|
70
|
-
name: 'Cancel' //Name of cancel button
|
|
71
|
+
name: 'Cancel', //Name of cancel button
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
}
|
package/package.json
CHANGED
|
@@ -51,11 +51,11 @@ export const useFieldRender = (props: IFieldRenderProps): IFieldRender => {
|
|
|
51
51
|
}, [props.element.liveData]
|
|
52
52
|
)
|
|
53
53
|
|
|
54
|
-
const renderConditionList: string
|
|
54
|
+
const renderConditionList: Array<string> = useMemo(() => {
|
|
55
55
|
return renderCondition ? Object.keys(props.element.renderIf as IModalRenderCondition) : []
|
|
56
56
|
}, [props.element.renderIf, renderCondition])
|
|
57
57
|
|
|
58
|
-
const enableConditionList: string
|
|
58
|
+
const enableConditionList: Array<string> = useMemo(() => {
|
|
59
59
|
return enableCondition ? Object.keys(props.element.enableIf as IModalRenderCondition) : []
|
|
60
60
|
}, [enableCondition, props.element.enableIf])
|
|
61
61
|
|
package/src/interfaces/modal.ts
CHANGED
|
@@ -36,7 +36,7 @@ export interface IModalConfigProps {
|
|
|
36
36
|
action?: () => void
|
|
37
37
|
hide?: boolean;
|
|
38
38
|
}
|
|
39
|
-
reservedData?: Record<string,
|
|
39
|
+
reservedData?: Record<string, any>
|
|
40
40
|
styles?: CSSProperties;
|
|
41
41
|
overFlowBody?: string | number
|
|
42
42
|
minHeightBody?: string | number
|
package/src/modal.tsx
CHANGED
|
@@ -30,6 +30,11 @@ export const Modal = ({ open, close, config }: IModal) => {
|
|
|
30
30
|
|
|
31
31
|
const formValueHandler = (element: IFormField) => {
|
|
32
32
|
if (['group', 'upload', 'text'].includes(element.elementType)) return
|
|
33
|
+
if(!element.defaultValue && element.renderIf) {
|
|
34
|
+
unregister(element.name)
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
33
38
|
const defaultValue = element.defaultValue ?? ''
|
|
34
39
|
const parsedValue: boolean | string | Array<string> | undefined = defaultValue === 'true' ? true : defaultValue === 'false' ? false : defaultValue
|
|
35
40
|
setValue(element.name, parsedValue ?? '')
|