dynamic-modal 1.0.2 → 1.0.4
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/modal.js +5 -1
- 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/modal.tsx +10 -5
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 {
|
package/dist/modal.js
CHANGED
|
@@ -50,6 +50,10 @@ const Modal = ({ open, close, config }) => {
|
|
|
50
50
|
var _a;
|
|
51
51
|
if (['group', 'upload', 'text'].includes(element.elementType))
|
|
52
52
|
return;
|
|
53
|
+
if (!element.defaultValue && element.renderIf) {
|
|
54
|
+
unregister(element.name);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
53
57
|
const defaultValue = (_a = element.defaultValue) !== null && _a !== void 0 ? _a : '';
|
|
54
58
|
const parsedValue = defaultValue === 'true' ? true : defaultValue === 'false' ? false : defaultValue;
|
|
55
59
|
setValue(element.name, parsedValue !== null && parsedValue !== void 0 ? parsedValue : '');
|
|
@@ -132,7 +136,7 @@ const Modal = ({ open, close, config }) => {
|
|
|
132
136
|
const isEndOfRender = index + 1 === modalReady.fields.length;
|
|
133
137
|
if (element.elementType === 'group') {
|
|
134
138
|
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'].includes(sub.elementType))
|
|
139
|
+
.filter(sub => ['input', 'select', 'toggle', 'multiselect', 'upload', 'button', 'autocomplete'].includes(sub.elementType))
|
|
136
140
|
.map((sub, subIndex) => getRender(sub, index + subIndex, isEndOfRender))));
|
|
137
141
|
}
|
|
138
142
|
else {
|
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/modal.tsx
CHANGED
|
@@ -30,8 +30,13 @@ 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
|
-
const parsedValue: boolean | string | Array<string> |undefined = defaultValue === 'true' ? true : defaultValue === 'false' ? false : defaultValue
|
|
39
|
+
const parsedValue: boolean | string | Array<string> | undefined = defaultValue === 'true' ? true : defaultValue === 'false' ? false : defaultValue
|
|
35
40
|
setValue(element.name, parsedValue ?? '')
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -51,7 +56,7 @@ export const Modal = ({ open, close, config }: IModal) => {
|
|
|
51
56
|
}
|
|
52
57
|
formValueHandler(element as IFormField)
|
|
53
58
|
})
|
|
54
|
-
|
|
59
|
+
|
|
55
60
|
setDefaultLoaded(true)
|
|
56
61
|
}
|
|
57
62
|
|
|
@@ -102,7 +107,7 @@ export const Modal = ({ open, close, config }: IModal) => {
|
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
useEffect(() => {
|
|
105
|
-
if(open && !modalReady) setModalReady(config)
|
|
110
|
+
if (open && !modalReady) setModalReady(config)
|
|
106
111
|
}, [config, modalReady, open])
|
|
107
112
|
|
|
108
113
|
return (
|
|
@@ -121,14 +126,14 @@ export const Modal = ({ open, close, config }: IModal) => {
|
|
|
121
126
|
>
|
|
122
127
|
{
|
|
123
128
|
modalReady.fields.map((element, index) => {
|
|
124
|
-
const isEndOfRender
|
|
129
|
+
const isEndOfRender: boolean = index + 1 === modalReady.fields.length
|
|
125
130
|
|
|
126
131
|
if (element.elementType === 'group') {
|
|
127
132
|
return (
|
|
128
133
|
<div key={`modal-group-${index}`} className='flex gap-4 w-full' style={element.style} >
|
|
129
134
|
{
|
|
130
135
|
element.groups
|
|
131
|
-
.filter(sub => ['input', 'select', 'toggle', 'multiselect', 'upload', 'button'].includes(sub.elementType))
|
|
136
|
+
.filter(sub => ['input', 'select', 'toggle', 'multiselect', 'upload', 'button', 'autocomplete'].includes(sub.elementType))
|
|
132
137
|
.map((sub, subIndex) => getRender(sub, index + subIndex, isEndOfRender))
|
|
133
138
|
}
|
|
134
139
|
</div>
|