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 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, string | number>;
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
- const defaultValue = (_a = element.defaultValue) !== null && _a !== void 0 ? _a : '';
54
- const parsedValue = defaultValue === 'true' ? true : defaultValue === 'false' ? false : defaultValue;
55
- setValue(element.name, parsedValue !== null && parsedValue !== void 0 ? parsedValue : '');
56
- };
57
- const autoLoadGroup = (groupFields) => {
58
- groupFields.forEach(element => {
59
- formValueHandler(element);
60
- });
61
- };
62
- const autoLoadField = (modalFields) => {
63
- if (defaultLoaded)
64
- return;
65
- modalFields.forEach(element => {
66
- if (element.elementType === 'group') {
67
- autoLoadGroup(element.groups);
68
- return;
69
- }
70
- formValueHandler(element);
71
- });
72
- setDefaultLoaded(true);
73
- };
74
- const getRender = (element, index, isEndOfRender = false) => {
75
- if (isEndOfRender && modalReady)
76
- setTimeout(() => autoLoadField(modalReady.fields), 200);
77
- const props = {
78
- control,
79
- watch,
80
- setValue
81
- };
82
- return element.elementType === 'input'
83
- ? react_1.default.createElement(make_input_1.default, Object.assign({}, props, { key: `modal-input-${index}`, element: element }))
84
- : element.elementType === 'select'
85
- ? react_1.default.createElement(make_select_1.default, Object.assign({}, props, { key: `modal-select-${index}`, element: element }))
86
- : element.elementType === 'textarea'
87
- ? react_1.default.createElement(make_textarea_1.default, Object.assign({}, props, { key: `modal-textarea-${index}`, element: element }))
88
- : element.elementType === 'toggle'
89
- ? react_1.default.createElement(make_toggle_1.default, Object.assign({}, props, { key: `modal-toggle-${index}`, element: element }))
90
- : element.elementType === 'multiselect'
91
- ? react_1.default.createElement(make_multi_select_1.default, Object.assign({}, props, { key: `modal-multiselect-${index}`, element: element }))
92
- : element.elementType === 'text'
93
- ? react_1.default.createElement(make_text_1.default, Object.assign({}, props, { key: `modal-text-${index}`, element: element }))
94
- : element.elementType === 'upload'
95
- ? react_1.default.createElement(make_upload_1.default, Object.assign({}, props, { key: `modal-upload-${index}`, element: element }))
96
- : element.elementType === 'button'
97
- ? react_1.default.createElement(make_button_1.default, Object.assign({}, props, { key: `modal-button-${index}`, element: element }))
98
- : element.elementType === 'autocomplete'
99
- ? react_1.default.createElement(make_autocomplete_1.default, Object.assign({}, props, { key: `modal-autocomplete-${index}`, element: element }))
100
- : null;
101
- };
102
- const closeHandler = () => {
103
- if ((modalReady === null || modalReady === void 0 ? void 0 : modalReady.cancel) && modalReady.cancel.action)
104
- modalReady.cancel.action();
105
- setTimeout(() => {
106
- const form = getValues();
107
- unregister(Object.keys(form));
108
- setModalReady(undefined);
109
- setDefaultLoaded(false);
110
- close();
111
- }, 200);
112
- };
113
- const actionHandler = (data) => {
114
- if ((modalReady === null || modalReady === void 0 ? void 0 : modalReady.action) && modalReady.action.action)
115
- modalReady.action.action(Object.assign(Object.assign({}, modalReady === null || modalReady === void 0 ? void 0 : modalReady.reservedData), data));
116
- closeHandler();
117
- };
118
- (0, react_1.useEffect)(() => {
119
- if (open && !modalReady)
120
- setModalReady(config);
121
- }, [config, modalReady, open]);
122
- return (modalReady
123
- ? react_1.default.createElement(portal_1.Portal, { closeTime: 200, portalOpen: open, portalTag: '#modal-portal' },
124
- 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) },
125
- react_1.default.createElement("form", { className: 'flex flex-col p-4 gap-4', autoComplete: 'off', onSubmit: handleSubmit(actionHandler) },
126
- react_1.default.createElement("h2", { className: 'text-bold text-center border-b pb-4' }, modalReady.title),
127
- react_1.default.createElement("div", { className: 'flex flex-col items-center gap-4 py-4', style: {
128
- overflowY: modalReady.overFlowBody ? 'auto' : undefined,
129
- height: modalReady.overFlowBody,
130
- minHeight: modalReady.minHeightBody
131
- } }, modalReady.fields.map((element, index) => {
132
- const isEndOfRender = index + 1 === modalReady.fields.length;
133
- if (element.elementType === 'group') {
134
- return (react_1.default.createElement("div", { key: `modal-group-${index}`, className: 'flex gap-4 w-full', style: element.style }, element.groups
135
- .filter(sub => ['input', 'select', 'toggle', 'multiselect', 'upload', 'button', 'autocomplete'].includes(sub.elementType))
136
- .map((sub, subIndex) => getRender(sub, index + subIndex, isEndOfRender))));
137
- }
138
- else {
139
- return getRender(element, index, isEndOfRender);
140
- }
141
- })),
142
- react_1.default.createElement("div", { className: 'flex items-center justify-around gap-4 py-2 border-t' },
143
- modalReady.cancel &&
144
- 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'),
145
- modalReady.action &&
146
- react_1.default.createElement(react_2.Button, { color: 'primary', type: 'submit', className: 'w-[140px]' }, (_b = modalReady.action.name) !== null && _b !== void 0 ? _b : 'Ok')))))
147
- : null);
148
- };
149
- exports.Modal = Modal;
150
- exports.default = exports.Modal;
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;
@@ -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
@@ -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'
@@ -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
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamic-modal",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "The dynamic-modal is a solution of creation differents modals into project using a json configuration file",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -51,11 +51,11 @@ export const useFieldRender = (props: IFieldRenderProps): IFieldRender => {
51
51
  }, [props.element.liveData]
52
52
  )
53
53
 
54
- const renderConditionList: string[] = useMemo(() => {
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[] = useMemo(() => {
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
 
@@ -5,8 +5,6 @@ import { IOption } from './option'
5
5
  export interface IMakeAutoComplete extends IField {
6
6
  elementType: 'autocomplete'
7
7
  options: Array<IOption>
8
- defaultOption?: boolean
9
- defaultOptionName?: string
10
8
  liveData?: IModalLiveDataCondition
11
9
  }
12
10
 
@@ -4,8 +4,6 @@ import { IModalLiveDataCondition } from './modal'
4
4
  export interface IMakeSelect extends IField {
5
5
  elementType: 'select'
6
6
  options: Array<Record<'id'|'name', string>>
7
- defaultOption?: boolean
8
- defaultOptionName?: string
9
7
  liveData?: IModalLiveDataCondition
10
8
  }
11
9
 
@@ -36,7 +36,7 @@ export interface IModalConfigProps {
36
36
  action?: () => void
37
37
  hide?: boolean;
38
38
  }
39
- reservedData?: Record<string, string | number>
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 ?? '')