dynamic-modal 1.1.8 → 1.1.10

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.
Files changed (63) hide show
  1. package/README.md +0 -1
  2. package/dist/components/input-upload/input-upload.js +3 -3
  3. package/dist/components/make-button/make-button.js +19 -10
  4. package/dist/components/make-description/make-description.js +2 -2
  5. package/dist/components/make-input/make-input.js +57 -29
  6. package/dist/components/make-select/make-select.js +66 -29
  7. package/dist/components/make-textarea/make-textarea.js +57 -29
  8. package/dist/components/make-toggle/make-toggle.js +57 -29
  9. package/dist/components/make-upload/make-upload.js +72 -18
  10. package/dist/components/portal/portal.js +20 -10
  11. package/dist/context/component/component-state.d.ts +3 -3
  12. package/dist/context/component/component-state.js +20 -11
  13. package/dist/hooks/use-enable-if.d.ts +9 -0
  14. package/dist/hooks/use-enable-if.js +45 -0
  15. package/dist/hooks/use-live-data.d.ts +14 -0
  16. package/dist/hooks/use-live-data.js +42 -0
  17. package/dist/hooks/use-render-if.d.ts +12 -0
  18. package/dist/hooks/use-render-if.js +51 -0
  19. package/dist/interfaces/component-state.d.ts +8 -8
  20. package/dist/interfaces/modal.d.ts +4 -2
  21. package/dist/modal.js +70 -58
  22. package/dist/util/general/general.util.d.ts +3 -0
  23. package/dist/util/general/general.util.js +28 -0
  24. package/eslint.config.mjs +72 -14
  25. package/examples/live-data.ts +0 -1
  26. package/package.json +11 -9
  27. package/src/components/input-upload/input-upload.tsx +40 -28
  28. package/src/components/make-button/make-button.tsx +11 -13
  29. package/src/components/make-description/make-description.tsx +14 -8
  30. package/src/components/make-input/make-input.tsx +94 -47
  31. package/src/components/make-select/make-select.tsx +118 -48
  32. package/src/components/make-textarea/make-textarea.tsx +94 -47
  33. package/src/components/make-toggle/make-toggle.tsx +94 -47
  34. package/src/components/make-upload/make-upload.tsx +88 -36
  35. package/src/components/portal/portal.tsx +27 -25
  36. package/src/context/component/component-state.tsx +16 -9
  37. package/src/hooks/modal-handler.ts +17 -14
  38. package/src/hooks/use-enable-if.ts +64 -0
  39. package/src/hooks/use-live-data.ts +45 -0
  40. package/src/hooks/use-render-if.ts +59 -0
  41. package/src/interfaces/component-state.ts +38 -23
  42. package/src/interfaces/field.ts +33 -27
  43. package/src/interfaces/input-upload.ts +17 -15
  44. package/src/interfaces/make-button.ts +14 -13
  45. package/src/interfaces/make-description.ts +9 -8
  46. package/src/interfaces/make-field-group.ts +9 -8
  47. package/src/interfaces/make-input.ts +9 -8
  48. package/src/interfaces/make-select.ts +9 -9
  49. package/src/interfaces/make-textarea.ts +5 -5
  50. package/src/interfaces/make-toggle.ts +3 -3
  51. package/src/interfaces/make-upload.ts +8 -8
  52. package/src/interfaces/modal.ts +57 -39
  53. package/src/interfaces/option.ts +3 -3
  54. package/src/interfaces/portal.ts +5 -5
  55. package/src/modal.tsx +243 -147
  56. package/src/util/general/general.util.ts +27 -0
  57. package/tsconfig.json +1 -1
  58. package/dist/hooks/field-render.d.ts +0 -20
  59. package/dist/hooks/field-render.js +0 -94
  60. package/dist/tools/general.d.ts +0 -1
  61. package/dist/tools/general.js +0 -11
  62. package/src/hooks/field-render.ts +0 -120
  63. package/src/tools/general.ts +0 -8
@@ -1,120 +0,0 @@
1
- 'use client'
2
-
3
- import { useCallback, useMemo, useState } from 'react'
4
- import { IField, IFieldProps } from '../interfaces/field'
5
- import { IOption } from '../interfaces/option'
6
- import { IModalLiveDataCondition, IModalRenderCondition } from '../interfaces/modal'
7
-
8
- export type IFormData = Record<string, unknown>
9
-
10
- export interface IWatchEvent {
11
- name: string | undefined
12
- type: string | undefined
13
- }
14
-
15
- export interface IFieldRender {
16
- render: boolean | null
17
- enable: boolean | null
18
- checkField: (formData: IFormData, { name, type }: IWatchEvent) => void
19
- liveData?: Array<IOption>
20
- liveSearching?: boolean
21
- }
22
-
23
- export interface IFieldRenderProps extends Pick<IFieldProps, 'setValue' | 'unregister'> {
24
- element: Partial<Pick<IField, 'enableIf' | 'renderIf' | 'name' >> & Partial<Record<'liveData', IModalLiveDataCondition>>
25
- }
26
-
27
- export type ILiveHandlerProps = Pick<IField, 'enableIf' | 'renderIf'>
28
-
29
- export const useFieldRender = ({ element, setValue, unregister }: IFieldRenderProps): IFieldRender => {
30
- const [render, setRender] = useState<boolean|null>(null)
31
- const [enable, setEnable] = useState<boolean|null>(null)
32
- const [liveSearching, setLiveSearching] = useState<boolean>(false)
33
- const [liveData, setLiveData] = useState<Array<IOption> | undefined>(undefined)
34
-
35
- const renderCondition = useMemo<boolean>(
36
- () => {
37
- const isRender: boolean = element.renderIf !== undefined
38
- if (render === null) setRender(!isRender)
39
- return isRender
40
- }, [element.renderIf, render]
41
- )
42
-
43
- const enableCondition = useMemo<boolean>(
44
- () => {
45
- const isEnable: boolean = element.enableIf !== undefined
46
- if (enable === null) setEnable(!isEnable)
47
- return isEnable
48
- }, [element.enableIf, enable]
49
- )
50
-
51
- const liveDataCondition = useMemo<boolean>(
52
- () => {
53
- return element.liveData !== undefined
54
- }, [element.liveData]
55
- )
56
-
57
- const renderConditionList: Array<string> = useMemo(() => {
58
- return renderCondition ? Object.keys(element.renderIf as IModalRenderCondition) : []
59
- }, [element.renderIf, renderCondition])
60
-
61
- const enableConditionList: Array<string> = useMemo(() => {
62
- return enableCondition ? Object.keys(element.enableIf as IModalRenderCondition) : []
63
- }, [enableCondition, element.enableIf])
64
-
65
- const liveDataAction = useCallback(
66
- async (field: string | number | Array<unknown>, formData: IFormData) => {
67
- if (typeof field === 'string' && element.liveData?.action) {
68
- const options = element.liveData.action(field, formData)
69
- return options ?? []
70
- }
71
- return [] as Array<IOption>
72
- }, [element.liveData]
73
- )
74
-
75
- const liveDataHandler = useCallback(
76
- async (targetHandler: keyof ILiveHandlerProps, field: string | number | Array<unknown>, formData: IFormData) => {
77
- if (typeof field === 'string' && element[targetHandler]?.action) {
78
- const result = element[targetHandler].action(field, formData)
79
- return result ?? false
80
- }
81
-
82
- return false
83
- }, [element.liveData]
84
- )
85
-
86
- const checkField = useCallback(
87
- async (formData: IFormData, { name }: IWatchEvent) => {
88
- const key: string = name ?? ''
89
- const targetField = formData[key] as string | number
90
- if(!targetField) return
91
-
92
- if (renderCondition && element.renderIf?.condition.includes(key)) {
93
- const renderStatus: boolean = await liveDataHandler('renderIf', targetField, formData)
94
- if (render !== renderStatus) {
95
- setRender(renderStatus)
96
- if(!renderStatus) unregister(element.name as string)
97
- }
98
- } else if (enableCondition && element.enableIf?.condition.includes(key)){
99
- const enableStatus: boolean = await liveDataHandler('enableIf', targetField, formData)
100
- if (enable !== enableStatus) setEnable(enableStatus)
101
- }
102
-
103
- if (liveDataCondition && element.liveData?.condition.includes(key)) {
104
- setLiveSearching(true)
105
- const options: Array<IOption> = await liveDataAction(targetField, formData)
106
- if (liveData && JSON.stringify(liveData) !== JSON.stringify(options)) { setValue(element.name as string, options) }
107
- setLiveData(options)
108
- setLiveSearching(false)
109
- }
110
- }, [enable, enableCondition, enableConditionList, liveData, liveDataAction, liveDataCondition, render, renderCondition, renderConditionList]
111
- )
112
-
113
- return {
114
- render,
115
- enable,
116
- checkField,
117
- liveData,
118
- liveSearching
119
- }
120
- }
@@ -1,8 +0,0 @@
1
- 'use client'
2
-
3
- export const generateId = (): string => {
4
- return Math.random()
5
- .toString(36)
6
- .split('.')[1]
7
- .substring(0, 6)
8
- }