dynamic-modal 1.1.22 → 1.1.24

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/src/modal.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { createElement as _createElement } from "react";
4
- import { useContext, useEffect, useState } from 'react';
4
+ import { useContext, useEffect, useMemo, useState } from 'react';
5
5
  import { useForm } from 'react-hook-form';
6
6
  import { Portal } from './components/portal/portal';
7
7
  import MakeToggle from './components/make-toggle/make-toggle';
@@ -15,8 +15,47 @@ import { ComponentStateContext } from './context/component/component-state';
15
15
  import MakeCustomUpload from './components/make-custom-upload/make-custom-upload';
16
16
  import MakeWatcher from './components/make-watcher/make-watcher';
17
17
  import MakeTable from './components/make-table/make-table';
18
- export const Modal = ({ open, close, config }) => {
18
+ const getUseSubmit = (modalConfig) => {
19
+ return modalConfig.useSubmit !== false;
20
+ };
21
+ const getFieldKey = (element, index) => {
22
+ const fieldKey = 'name' in element && element.name
23
+ ? element.name
24
+ : 'id' in element && element.id
25
+ ? element.id
26
+ : element.elementType === 'group'
27
+ ? (element.title ?? `group-${index}`)
28
+ : element.elementType === 'text'
29
+ ? (element.text ?? `text-${index}`)
30
+ : `field-${index}`;
31
+ return `${element.elementType}-${fieldKey}`;
32
+ };
33
+ const renderModalField = (field, index, { fieldProps, getValues }) => {
34
+ const { elementType, ...element } = field;
35
+ const fieldKey = getFieldKey(field, index);
36
+ return elementType === 'input' ? (_createElement(MakeInput, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'select' ? (_createElement(MakeSelect, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'textarea' ? (_createElement(MakeTextarea, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'toggle' ? (_createElement(MakeToggle, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'text' ? (_createElement(MakeDescription, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'upload' ? (_createElement(MakeUpload, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'custom-upload' ? (_createElement(MakeCustomUpload, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'watcher' ? (_createElement(MakeWatcher, { ...fieldProps, key: fieldKey, element: element })) : elementType === 'button' ? (_createElement(MakeButton, { ...fieldProps, key: fieldKey, element: element, getValues: getValues })) : elementType === 'table' ? (_createElement(MakeTable, { ...fieldProps, key: fieldKey, element: element })) : null;
37
+ };
38
+ const ModalFields = ({ modalReady, fieldProps, getValues, }) => {
39
+ return (_jsx("div", { className: "flex flex-col gap-4 py-4", style: {
40
+ overflowY: modalReady.overFlowBody ? 'auto' : undefined,
41
+ height: modalReady.overFlowBody,
42
+ minHeight: modalReady.minHeightBody,
43
+ }, children: modalReady.fields.map((element, index) => {
44
+ if (element.elementType !== 'group') {
45
+ return renderModalField(element, index, { fieldProps, getValues });
46
+ }
47
+ const groupElements = element.groups
48
+ .filter((sub) => !['group', 'watcher', 'table'].includes(sub.elementType))
49
+ .map((sub, subIndex) => renderModalField(sub, index + subIndex, { fieldProps, getValues }));
50
+ const hideDiv = groupElements.every((component) => component === null);
51
+ return (_jsxs("div", { className: `flex flex-col w-full gap-2 ${hideDiv && 'hidden'}`, children: [element.title && (_jsx("h3", { className: "font-bold border-b-2 pb-2 mb-2", children: element.title })), _jsx("div", { className: "flex gap-4 w-full", style: element.style, children: groupElements })] }, getFieldKey(element, index)));
52
+ }) }));
53
+ };
54
+ const ModalActions = ({ modalReady, closeHandler, manualSubmit, }) => {
19
55
  const { ModalButtonAction, ModalButtonCancel } = useContext(ComponentStateContext);
56
+ return (_jsxs("div", { className: "flex gap-4 items-center justify-center border-t p-2", style: modalReady.actions.containerStyle, children: [modalReady.actions.cancel && (_jsx(ModalButtonCancel, { ...modalReady.actions.cancel, onClick: closeHandler })), getUseSubmit(modalReady) ? (_jsx(ModalButtonAction, { ...modalReady.actions.action, type: "submit" })) : (_jsx(ModalButtonAction, { ...modalReady.actions.action, onClick: manualSubmit, type: "button" }))] }));
57
+ };
58
+ export const Modal = ({ open, close, config }) => {
20
59
  const [modalReady, setModalReady] = useState(undefined);
21
60
  const [defaultLoaded, setDefaultLoaded] = useState(false);
22
61
  const { control, handleSubmit, getValues, unregister, setValue, watch, trigger, reset, getFieldState, } = useForm();
@@ -31,7 +70,7 @@ export const Modal = ({ open, close, config }) => {
31
70
  'table',
32
71
  ].includes(element.elementType))
33
72
  return;
34
- if (!element.defaultValue && element.renderIf) {
73
+ if (element.defaultValue === undefined && element.renderIf) {
35
74
  unregister(element.name);
36
75
  return;
37
76
  }
@@ -60,25 +99,12 @@ export const Modal = ({ open, close, config }) => {
60
99
  });
61
100
  setDefaultLoaded(true);
62
101
  };
63
- const getUseSubmit = (modalConfig) => {
64
- const useSubmit = modalConfig.useSubmit === undefined
65
- ? true
66
- : modalConfig.useSubmit === false
67
- ? false
68
- : true;
69
- return useSubmit;
70
- };
71
- const getRender = ({ elementType, ...element }, index, isEndOfRender = false) => {
72
- if (isEndOfRender && modalReady)
73
- setTimeout(() => autoLoadField(modalReady.fields), 200);
74
- const props = {
75
- control,
76
- watch,
77
- setValue,
78
- unregister,
79
- };
80
- return elementType === 'input' ? (_createElement(MakeInput, { ...props, key: `modal-input-${index}`, element: element })) : elementType === 'select' ? (_createElement(MakeSelect, { ...props, key: `modal-select-${index}`, element: element })) : elementType === 'textarea' ? (_createElement(MakeTextarea, { ...props, key: `modal-textarea-${index}`, element: element })) : elementType === 'toggle' ? (_createElement(MakeToggle, { ...props, key: `modal-toggle-${index}`, element: element })) : elementType === 'text' ? (_createElement(MakeDescription, { ...props, key: `modal-text-${index}`, element: element })) : elementType === 'upload' ? (_createElement(MakeUpload, { ...props, key: `modal-upload-${index}`, element: element })) : elementType === 'custom-upload' ? (_createElement(MakeCustomUpload, { ...props, key: `modal-custom-upload-${index}`, element: element })) : elementType === 'watcher' ? (_createElement(MakeWatcher, { ...props, key: `modal-watcher-${index}`, element: element })) : elementType === 'button' ? (_createElement(MakeButton, { ...props, key: `modal-button-${index}`, element: element, getValues: getValues })) : elementType === 'table' ? (_createElement(MakeTable, { ...props, key: `modal-table-${index}`, element: element })) : null;
81
- };
102
+ const fieldProps = useMemo(() => ({
103
+ control,
104
+ watch,
105
+ setValue,
106
+ unregister,
107
+ }), [control, setValue, unregister, watch]);
82
108
  const closeHandler = () => {
83
109
  if (modalReady?.onClose)
84
110
  modalReady.onClose();
@@ -112,24 +138,16 @@ export const Modal = ({ open, close, config }) => {
112
138
  if (open && !modalReady)
113
139
  setModalReady(config);
114
140
  }, [config, modalReady, open]);
141
+ useEffect(() => {
142
+ if (!modalReady || defaultLoaded)
143
+ return;
144
+ const timeout = window.setTimeout(() => {
145
+ autoLoadField(modalReady.fields);
146
+ }, 0);
147
+ return () => window.clearTimeout(timeout);
148
+ }, [defaultLoaded, modalReady]);
115
149
  if (!modalReady)
116
150
  return null;
117
- return (_jsx(Portal, { closeTime: 200, portalOpen: open, portalTag: '#modal-portal', useBlur: modalReady.useBlur, children: _jsx("div", { className: `rounded bg-white relative w-auto h-auto min-h-[200px] min-w-[500px] ${modalReady.useBlur && 'shadow-md border border-gray-200'}`, style: modalReady.style, children: _jsxs("form", { className: "flex flex-col p-4 gap-4", autoComplete: "off", onSubmit: handleSubmit(actionHandler), children: [_jsx("h2", { className: "text-bold text-center border-b pb-4 font-semibold", children: modalReady.title }), _jsx("div", { className: "flex flex-col gap-4 py-4", style: {
118
- overflowY: modalReady.overFlowBody ? 'auto' : undefined,
119
- height: modalReady.overFlowBody,
120
- minHeight: modalReady.minHeightBody,
121
- }, children: modalReady.fields.map((element, index) => {
122
- const isEndOfRender = index + 1 === modalReady.fields.length;
123
- if (element.elementType === 'group') {
124
- const groupElements = element.groups
125
- .filter((sub) => !['group', 'watcher', 'table'].includes(sub.elementType))
126
- .map((sub, subIndex) => getRender(sub, index + subIndex, isEndOfRender));
127
- const hideDiv = groupElements.filter((render) => render).length === 0;
128
- return (_jsxs("div", { className: `flex flex-col w-full gap-2 ${hideDiv && 'hidden'}`, children: [element.title && (_jsx("h3", { className: "font-bold border-b-2 pb-2 mb-2", children: element.title })), _jsx("div", { className: "flex gap-4 w-full", style: element.style, children: groupElements.map((component) => component) })] }, `modal-group-${index}`));
129
- }
130
- else {
131
- return getRender(element, index, isEndOfRender);
132
- }
133
- }) }), _jsxs("div", { className: "flex gap-4 items-center justify-center border-t p-2", style: modalReady?.actions.containerStyle, children: [modalReady.actions.cancel && (_jsx(ModalButtonCancel, { ...modalReady.actions.cancel, onClick: closeHandler })), getUseSubmit(modalReady) ? (_jsx(ModalButtonAction, { ...modalReady.actions.action, type: "submit" })) : (_jsx(ModalButtonAction, { ...modalReady.actions.action, onClick: manualSubmit, type: "button" }))] })] }) }) }));
151
+ return (_jsx(Portal, { closeTime: 200, portalOpen: open, portalTag: '#modal-portal', useBlur: modalReady.useBlur, children: _jsx("div", { className: `rounded bg-white relative w-auto h-auto min-h-[200px] min-w-[500px] ${modalReady.useBlur && 'shadow-md border border-gray-200'}`, style: modalReady.style, children: _jsxs("form", { className: "flex flex-col p-4 gap-4", autoComplete: "off", onSubmit: handleSubmit(actionHandler), children: [_jsx("h2", { className: "text-bold text-center border-b pb-4 font-semibold", children: modalReady.title }), _jsx(ModalFields, { modalReady: modalReady, fieldProps: fieldProps, getValues: getValues }), _jsx(ModalActions, { modalReady: modalReady, closeHandler: closeHandler, manualSubmit: manualSubmit })] }) }) }));
134
152
  };
135
153
  export default Modal;
@@ -1,14 +1,18 @@
1
1
  'use client';
2
2
  export const liveDataAction = async (fieldData, formData, action) => {
3
- if (typeof fieldData === 'string') {
4
- const options = await action(fieldData, formData);
3
+ if (typeof fieldData === 'string' ||
4
+ typeof fieldData === 'number' ||
5
+ typeof fieldData === 'boolean') {
6
+ const options = await action(String(fieldData), formData);
5
7
  return options ?? [];
6
8
  }
7
9
  return [];
8
10
  };
9
11
  export const liveDataHandler = async (fieldData, formData, action) => {
10
- if (typeof fieldData === 'string') {
11
- const result = await action(fieldData, formData);
12
+ if (typeof fieldData === 'string' ||
13
+ typeof fieldData === 'number' ||
14
+ typeof fieldData === 'boolean') {
15
+ const result = await action(String(fieldData), formData);
12
16
  return result ?? false;
13
17
  }
14
18
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamic-modal",
3
- "version": "1.1.22",
3
+ "version": "1.1.24",
4
4
  "description": "The dynamic-modal is a solution of creation different modals into project using a json configuration file",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",