dynamic-modal 1.1.17 → 1.1.19
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 +215 -215
- package/dist/components/make-button/make-button.js +6 -2
- package/dist/components/make-table/make-table.d.ts +4 -0
- package/dist/components/make-table/make-table.js +146 -0
- package/dist/interfaces/make-button.d.ts +5 -2
- package/dist/interfaces/make-table.d.ts +26 -0
- package/dist/interfaces/make-table.js +2 -0
- package/dist/interfaces/modal.d.ts +2 -1
- package/dist/modal.js +2 -1
- package/eslint.config.mjs +72 -72
- package/examples/enable-if.ts +127 -127
- package/examples/live-data.ts +60 -60
- package/examples/render-if.ts +128 -128
- package/examples/simple.ts +74 -74
- package/index.ts +4 -4
- package/package.json +48 -48
- package/src/components/input-upload/input-upload.tsx +82 -82
- package/src/components/make-button/make-button.tsx +25 -16
- package/src/components/make-custom-upload/make-custom-upload.tsx +100 -100
- package/src/components/make-description/make-description.tsx +21 -21
- package/src/components/make-input/make-input.tsx +100 -100
- package/src/components/make-select/make-select.tsx +125 -125
- package/src/components/make-table/make-table.tsx +201 -0
- package/src/components/make-textarea/make-textarea.tsx +100 -100
- package/src/components/make-toggle/make-toggle.tsx +100 -100
- package/src/components/make-upload/make-upload.tsx +92 -92
- package/src/components/make-watcher/make-watcher.tsx +49 -49
- package/src/components/portal/portal.tsx +41 -41
- package/src/context/component/component-state.tsx +25 -25
- package/src/hooks/modal-handler.ts +41 -41
- package/src/hooks/use-enable-if.ts +68 -68
- package/src/hooks/use-live-data.ts +47 -47
- package/src/hooks/use-render-if.ts +69 -69
- package/src/interfaces/component-state.ts +55 -56
- package/src/interfaces/custom-upload.ts +13 -13
- package/src/interfaces/field.ts +43 -43
- package/src/interfaces/input-upload.ts +23 -23
- package/src/interfaces/make-button.ts +23 -20
- package/src/interfaces/make-custom-upload.ts +9 -9
- package/src/interfaces/make-description.ts +15 -15
- package/src/interfaces/make-field-group.ts +15 -15
- package/src/interfaces/make-input.ts +15 -15
- package/src/interfaces/make-select.ts +15 -15
- package/src/interfaces/make-table.ts +29 -0
- 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/make-watcher.ts +10 -10
- package/src/interfaces/modal.ts +75 -73
- package/src/interfaces/option.ts +4 -4
- package/src/interfaces/portal.ts +9 -9
- package/src/modal.tsx +331 -322
- package/src/util/general/general.util.ts +29 -29
- package/tsconfig.json +13 -13
package/README-ES.md
CHANGED
|
@@ -1,217 +1,217 @@
|
|
|
1
|
-
# dynamic-modal
|
|
2
|
-
|
|
3
|
-
`dynamic-modal` es una librería construida en typescript para crear modales reutilizables en aplicaciones React y Next.js, utilizando JSON para configurar su estructura sin necesidad de escribir HTML. Esto facilita su construcción y personalización, permitiendo abrir y cerrar modales mediante un custom hook.
|
|
4
|
-
|
|
5
|
-
## Requisitos
|
|
6
|
-
|
|
7
|
-
Para que `dynamic-modal` funcione correctamente, debes instalar las siguientes dependencias:
|
|
8
|
-
|
|
9
|
-
- React
|
|
10
|
-
|
|
11
|
-
Además, es compatible con **Next.js**.
|
|
12
|
-
|
|
13
|
-
## Instalación
|
|
14
|
-
|
|
15
|
-
Puedes instalar la librería a través de npm:
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install dynamic-modal
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Configuración para Next.js (14 o 15)
|
|
22
|
-
Define los componentes que seran usados por el modal, crea el archivo y configura todos los componentes del modal. Aqui un ejemplo utilizando HeroUI (anteriormente NextUI):
|
|
23
|
-
|
|
24
|
-
```jsx
|
|
25
|
-
'use client'
|
|
26
|
-
import { Autocomplete, AutocompleteItem, Button, Input, Select, SelectItem, Switch, Textarea } from "@heroui/react"
|
|
27
|
-
import { IComponentState } from "dynamic-modal/src/interfaces/component-state"
|
|
28
|
-
|
|
29
|
-
export const ModalComponents: IComponentState = {
|
|
30
|
-
ModalButtonCancel: (props) => {
|
|
31
|
-
return (
|
|
32
|
-
<Button
|
|
33
|
-
{...props}
|
|
34
|
-
color={props.color as "default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined}
|
|
35
|
-
variant={'bordered'}
|
|
36
|
-
>
|
|
37
|
-
{props.text}
|
|
38
|
-
</Button>
|
|
39
|
-
)
|
|
40
|
-
},
|
|
41
|
-
ModalButtonAction: (props) => {
|
|
42
|
-
return (
|
|
43
|
-
<Button
|
|
44
|
-
{...props}
|
|
45
|
-
color={props.color as "default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined}
|
|
46
|
-
variant={'solid'}
|
|
47
|
-
>
|
|
48
|
-
{props.text}
|
|
49
|
-
</Button>)
|
|
50
|
-
},
|
|
51
|
-
Button: ({ text, ...props }) => {
|
|
52
|
-
return (
|
|
53
|
-
<Button
|
|
54
|
-
{...props}
|
|
55
|
-
color={props.color as "default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined}
|
|
56
|
-
variant={props.variant as "flat" | "bordered" | "solid" | "light" | "faded" | "shadow" | "ghost" | undefined}
|
|
57
|
-
>
|
|
58
|
-
{text}
|
|
59
|
-
</Button>)
|
|
60
|
-
},
|
|
61
|
-
Input: ({ invalid, error, disabled, onChange, ...props }) => {
|
|
62
|
-
return (
|
|
63
|
-
<Input
|
|
64
|
-
{...props}
|
|
65
|
-
onValueChange={onChange}
|
|
66
|
-
errorMessage={error?.message}
|
|
67
|
-
isInvalid={invalid}
|
|
68
|
-
isDisabled={disabled}
|
|
69
|
-
/>
|
|
70
|
-
)
|
|
71
|
-
},
|
|
72
|
-
Select: ({ options, invalid, error, isMulti, isSearch, disabled, onChange, value, ...props }) => {
|
|
73
|
-
return (
|
|
74
|
-
!isSearch ?
|
|
75
|
-
<Select
|
|
76
|
-
{...props}
|
|
77
|
-
selectedKeys={isMulti ? value : [value]}
|
|
78
|
-
onSelectionChange={onChange}
|
|
79
|
-
selectionMode={isMulti ? 'multiple' : 'single'}
|
|
80
|
-
errorMessage={error?.message}
|
|
81
|
-
isInvalid={invalid}
|
|
82
|
-
isDisabled={disabled}
|
|
83
|
-
>
|
|
84
|
-
{options.map((option) => (
|
|
85
|
-
<SelectItem key={option.id}>{option.name}</SelectItem>
|
|
86
|
-
))}
|
|
87
|
-
</Select> :
|
|
88
|
-
<Autocomplete
|
|
89
|
-
{...props}
|
|
90
|
-
selectedKey={value}
|
|
91
|
-
onSelectionChange={onChange}
|
|
92
|
-
errorMessage={error?.message}
|
|
93
|
-
isInvalid={invalid}
|
|
94
|
-
isDisabled={disabled}
|
|
95
|
-
>
|
|
96
|
-
{options.map((item) => (
|
|
97
|
-
<AutocompleteItem key={item.id}>{item.name}</AutocompleteItem>
|
|
98
|
-
))}
|
|
99
|
-
</Autocomplete>
|
|
100
|
-
)
|
|
101
|
-
},
|
|
102
|
-
Textarea: ({ invalid, error, disabled, ...props }) => {
|
|
103
|
-
return (
|
|
104
|
-
<Textarea
|
|
105
|
-
{...props}
|
|
106
|
-
errorMessage={error?.message}
|
|
107
|
-
isInvalid={invalid}
|
|
108
|
-
isDisabled={disabled}
|
|
109
|
-
/>
|
|
110
|
-
)
|
|
111
|
-
},
|
|
112
|
-
Toggle: ({ value, onChange, label, invalid, ...props }) => {
|
|
113
|
-
return(
|
|
114
|
-
<Switch {...props} isSelected={value} onValueChange={onChange}>
|
|
115
|
-
{label}
|
|
116
|
-
</Switch>
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
En el provider principal de tu aplicación Next.js, importa tus componentes del modal (definidos anteriormente) y envuelve tu aplicación con el componente `ComponentState` para que `dynamic-modal` funcione adecuadamente. Aquí un ejemplo:
|
|
123
|
-
|
|
124
|
-
```jsx
|
|
125
|
-
import { ComponentState } from 'dynamic-modal'
|
|
126
|
-
import { ModalComponents } from "@data/modal-component/modal-component"
|
|
127
|
-
|
|
128
|
-
function Provider({ children }: Readonly<{ children: ReactNode }>) {
|
|
129
|
-
return (
|
|
130
|
-
<ComponentState components={ModalComponents}>
|
|
131
|
-
<Component {...pageProps} />
|
|
132
|
-
</ComponentState>
|
|
133
|
-
)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export default Provider
|
|
137
|
-
|
|
138
|
-
```
|
|
139
|
-
En el layout principal crea el `portal` para el modal (este componente utiliza react portal)
|
|
140
|
-
|
|
141
|
-
```jsx
|
|
142
|
-
//imports...
|
|
143
|
-
|
|
144
|
-
export default function RootLayout ({
|
|
145
|
-
children
|
|
146
|
-
}: Readonly<{
|
|
147
|
-
children: ReactNode
|
|
148
|
-
}>) {
|
|
149
|
-
return (
|
|
150
|
-
<html lang="en">
|
|
151
|
-
<body className={inter.className}>
|
|
152
|
-
<Provider>
|
|
153
|
-
{children}
|
|
154
|
-
</Provider>
|
|
155
|
-
<div id='modal-portal'/>
|
|
156
|
-
</body>
|
|
157
|
-
</html>
|
|
158
|
-
)
|
|
159
|
-
}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
## Setup for Next.js 13 o mas antiguo
|
|
163
|
-
Edita el archivo llamado `_document.tsx` y crea el `portal` para el modal (este componente utiliza react portal)
|
|
164
|
-
|
|
165
|
-
```jsx
|
|
166
|
-
import { Html, Head, Main, NextScript } from 'next/document'
|
|
167
|
-
|
|
168
|
-
export default function Document () {
|
|
169
|
-
return (
|
|
170
|
-
<Html>
|
|
171
|
-
<Head />
|
|
172
|
-
<body>
|
|
173
|
-
<Main />
|
|
174
|
-
<div id='modal-portal'/>
|
|
175
|
-
<NextScript />
|
|
176
|
-
</body>
|
|
177
|
-
</Html>
|
|
178
|
-
)
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
## Uso
|
|
183
|
-
Para controlar la apertura y cierre del modal, obtén el custom hook `useModalHandler` y llama a `openModal` cuando necesites mostrar el modal.
|
|
184
|
-
|
|
185
|
-
```jsx
|
|
186
|
-
import { useModalHandler, DynamicModal } from 'dynamic-modal'
|
|
187
|
-
import { Button } from '@nextui-org/react'
|
|
188
|
-
//Crea el modal, importa y usa
|
|
189
|
-
import testModal from '@modal-config/test'
|
|
190
|
-
|
|
191
|
-
function ExampleComponent() {
|
|
192
|
-
const { openModal, modalProps } = useModalHandler()
|
|
193
|
-
|
|
194
|
-
return (
|
|
195
|
-
<>
|
|
196
|
-
<Button
|
|
197
|
-
onClick={() => {
|
|
198
|
-
openModal(testModal.default({}, (data) => {
|
|
199
|
-
console.log('modal data', data)
|
|
200
|
-
}))
|
|
201
|
-
}}
|
|
202
|
-
>
|
|
203
|
-
Open modal
|
|
204
|
-
</Button>
|
|
205
|
-
|
|
206
|
-
<DynamicModal {...modalProps} />
|
|
207
|
-
</>
|
|
208
|
-
)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export default ExampleComponent
|
|
212
|
-
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
## Ejemplos
|
|
216
|
-
La carpeta `examples` en el repositorio contiene distintos modos de configuración para ayudarte a personalizar tu modal.
|
|
217
|
-
|
|
1
|
+
# dynamic-modal
|
|
2
|
+
|
|
3
|
+
`dynamic-modal` es una librería construida en typescript para crear modales reutilizables en aplicaciones React y Next.js, utilizando JSON para configurar su estructura sin necesidad de escribir HTML. Esto facilita su construcción y personalización, permitiendo abrir y cerrar modales mediante un custom hook.
|
|
4
|
+
|
|
5
|
+
## Requisitos
|
|
6
|
+
|
|
7
|
+
Para que `dynamic-modal` funcione correctamente, debes instalar las siguientes dependencias:
|
|
8
|
+
|
|
9
|
+
- React
|
|
10
|
+
|
|
11
|
+
Además, es compatible con **Next.js**.
|
|
12
|
+
|
|
13
|
+
## Instalación
|
|
14
|
+
|
|
15
|
+
Puedes instalar la librería a través de npm:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install dynamic-modal
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configuración para Next.js (14 o 15)
|
|
22
|
+
Define los componentes que seran usados por el modal, crea el archivo y configura todos los componentes del modal. Aqui un ejemplo utilizando HeroUI (anteriormente NextUI):
|
|
23
|
+
|
|
24
|
+
```jsx
|
|
25
|
+
'use client'
|
|
26
|
+
import { Autocomplete, AutocompleteItem, Button, Input, Select, SelectItem, Switch, Textarea } from "@heroui/react"
|
|
27
|
+
import { IComponentState } from "dynamic-modal/src/interfaces/component-state"
|
|
28
|
+
|
|
29
|
+
export const ModalComponents: IComponentState = {
|
|
30
|
+
ModalButtonCancel: (props) => {
|
|
31
|
+
return (
|
|
32
|
+
<Button
|
|
33
|
+
{...props}
|
|
34
|
+
color={props.color as "default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined}
|
|
35
|
+
variant={'bordered'}
|
|
36
|
+
>
|
|
37
|
+
{props.text}
|
|
38
|
+
</Button>
|
|
39
|
+
)
|
|
40
|
+
},
|
|
41
|
+
ModalButtonAction: (props) => {
|
|
42
|
+
return (
|
|
43
|
+
<Button
|
|
44
|
+
{...props}
|
|
45
|
+
color={props.color as "default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined}
|
|
46
|
+
variant={'solid'}
|
|
47
|
+
>
|
|
48
|
+
{props.text}
|
|
49
|
+
</Button>)
|
|
50
|
+
},
|
|
51
|
+
Button: ({ text, ...props }) => {
|
|
52
|
+
return (
|
|
53
|
+
<Button
|
|
54
|
+
{...props}
|
|
55
|
+
color={props.color as "default" | "primary" | "secondary" | "success" | "warning" | "danger" | undefined}
|
|
56
|
+
variant={props.variant as "flat" | "bordered" | "solid" | "light" | "faded" | "shadow" | "ghost" | undefined}
|
|
57
|
+
>
|
|
58
|
+
{text}
|
|
59
|
+
</Button>)
|
|
60
|
+
},
|
|
61
|
+
Input: ({ invalid, error, disabled, onChange, ...props }) => {
|
|
62
|
+
return (
|
|
63
|
+
<Input
|
|
64
|
+
{...props}
|
|
65
|
+
onValueChange={onChange}
|
|
66
|
+
errorMessage={error?.message}
|
|
67
|
+
isInvalid={invalid}
|
|
68
|
+
isDisabled={disabled}
|
|
69
|
+
/>
|
|
70
|
+
)
|
|
71
|
+
},
|
|
72
|
+
Select: ({ options, invalid, error, isMulti, isSearch, disabled, onChange, value, ...props }) => {
|
|
73
|
+
return (
|
|
74
|
+
!isSearch ?
|
|
75
|
+
<Select
|
|
76
|
+
{...props}
|
|
77
|
+
selectedKeys={isMulti ? value : [value]}
|
|
78
|
+
onSelectionChange={onChange}
|
|
79
|
+
selectionMode={isMulti ? 'multiple' : 'single'}
|
|
80
|
+
errorMessage={error?.message}
|
|
81
|
+
isInvalid={invalid}
|
|
82
|
+
isDisabled={disabled}
|
|
83
|
+
>
|
|
84
|
+
{options.map((option) => (
|
|
85
|
+
<SelectItem key={option.id}>{option.name}</SelectItem>
|
|
86
|
+
))}
|
|
87
|
+
</Select> :
|
|
88
|
+
<Autocomplete
|
|
89
|
+
{...props}
|
|
90
|
+
selectedKey={value}
|
|
91
|
+
onSelectionChange={onChange}
|
|
92
|
+
errorMessage={error?.message}
|
|
93
|
+
isInvalid={invalid}
|
|
94
|
+
isDisabled={disabled}
|
|
95
|
+
>
|
|
96
|
+
{options.map((item) => (
|
|
97
|
+
<AutocompleteItem key={item.id}>{item.name}</AutocompleteItem>
|
|
98
|
+
))}
|
|
99
|
+
</Autocomplete>
|
|
100
|
+
)
|
|
101
|
+
},
|
|
102
|
+
Textarea: ({ invalid, error, disabled, ...props }) => {
|
|
103
|
+
return (
|
|
104
|
+
<Textarea
|
|
105
|
+
{...props}
|
|
106
|
+
errorMessage={error?.message}
|
|
107
|
+
isInvalid={invalid}
|
|
108
|
+
isDisabled={disabled}
|
|
109
|
+
/>
|
|
110
|
+
)
|
|
111
|
+
},
|
|
112
|
+
Toggle: ({ value, onChange, label, invalid, ...props }) => {
|
|
113
|
+
return(
|
|
114
|
+
<Switch {...props} isSelected={value} onValueChange={onChange}>
|
|
115
|
+
{label}
|
|
116
|
+
</Switch>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
En el provider principal de tu aplicación Next.js, importa tus componentes del modal (definidos anteriormente) y envuelve tu aplicación con el componente `ComponentState` para que `dynamic-modal` funcione adecuadamente. Aquí un ejemplo:
|
|
123
|
+
|
|
124
|
+
```jsx
|
|
125
|
+
import { ComponentState } from 'dynamic-modal'
|
|
126
|
+
import { ModalComponents } from "@data/modal-component/modal-component"
|
|
127
|
+
|
|
128
|
+
function Provider({ children }: Readonly<{ children: ReactNode }>) {
|
|
129
|
+
return (
|
|
130
|
+
<ComponentState components={ModalComponents}>
|
|
131
|
+
<Component {...pageProps} />
|
|
132
|
+
</ComponentState>
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export default Provider
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
En el layout principal crea el `portal` para el modal (este componente utiliza react portal)
|
|
140
|
+
|
|
141
|
+
```jsx
|
|
142
|
+
//imports...
|
|
143
|
+
|
|
144
|
+
export default function RootLayout ({
|
|
145
|
+
children
|
|
146
|
+
}: Readonly<{
|
|
147
|
+
children: ReactNode
|
|
148
|
+
}>) {
|
|
149
|
+
return (
|
|
150
|
+
<html lang="en">
|
|
151
|
+
<body className={inter.className}>
|
|
152
|
+
<Provider>
|
|
153
|
+
{children}
|
|
154
|
+
</Provider>
|
|
155
|
+
<div id='modal-portal'/>
|
|
156
|
+
</body>
|
|
157
|
+
</html>
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Setup for Next.js 13 o mas antiguo
|
|
163
|
+
Edita el archivo llamado `_document.tsx` y crea el `portal` para el modal (este componente utiliza react portal)
|
|
164
|
+
|
|
165
|
+
```jsx
|
|
166
|
+
import { Html, Head, Main, NextScript } from 'next/document'
|
|
167
|
+
|
|
168
|
+
export default function Document () {
|
|
169
|
+
return (
|
|
170
|
+
<Html>
|
|
171
|
+
<Head />
|
|
172
|
+
<body>
|
|
173
|
+
<Main />
|
|
174
|
+
<div id='modal-portal'/>
|
|
175
|
+
<NextScript />
|
|
176
|
+
</body>
|
|
177
|
+
</Html>
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Uso
|
|
183
|
+
Para controlar la apertura y cierre del modal, obtén el custom hook `useModalHandler` y llama a `openModal` cuando necesites mostrar el modal.
|
|
184
|
+
|
|
185
|
+
```jsx
|
|
186
|
+
import { useModalHandler, DynamicModal } from 'dynamic-modal'
|
|
187
|
+
import { Button } from '@nextui-org/react'
|
|
188
|
+
//Crea el modal, importa y usa
|
|
189
|
+
import testModal from '@modal-config/test'
|
|
190
|
+
|
|
191
|
+
function ExampleComponent() {
|
|
192
|
+
const { openModal, modalProps } = useModalHandler()
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<>
|
|
196
|
+
<Button
|
|
197
|
+
onClick={() => {
|
|
198
|
+
openModal(testModal.default({}, (data) => {
|
|
199
|
+
console.log('modal data', data)
|
|
200
|
+
}))
|
|
201
|
+
}}
|
|
202
|
+
>
|
|
203
|
+
Open modal
|
|
204
|
+
</Button>
|
|
205
|
+
|
|
206
|
+
<DynamicModal {...modalProps} />
|
|
207
|
+
</>
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export default ExampleComponent
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Ejemplos
|
|
216
|
+
La carpeta `examples` en el repositorio contiene distintos modos de configuración para ayudarte a personalizar tu modal.
|
|
217
|
+
|