@jasperoosthoek/react-toolbox 0.9.0 → 0.9.1
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/package.json
CHANGED
|
@@ -5,20 +5,27 @@ import { FormModal, ModalTitle, Width } from './FormModal';
|
|
|
5
5
|
import { FormValue } from './FormFields';
|
|
6
6
|
import { ButtonProps, CreateButton, EditButton } from '../buttons/IconButtons';
|
|
7
7
|
|
|
8
|
+
// Add the IncludeData type from master version
|
|
9
|
+
export type IncludeData<T extends FormFields> = {
|
|
10
|
+
[key in Exclude<string, keyof T>]: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
8
13
|
export type ShowCreateModal = (show?: boolean) => void;
|
|
9
|
-
export type ShowEditModal<T
|
|
14
|
+
export type ShowEditModal<T, K> = (state: ({ [key in keyof T]: FormValue } & K) | null) => void;
|
|
10
15
|
|
|
11
|
-
type FormModalContextType<T
|
|
16
|
+
type FormModalContextType<T, K> = {
|
|
12
17
|
showCreateModal: ShowCreateModal;
|
|
13
|
-
showEditModal: ShowEditModal<T>;
|
|
18
|
+
showEditModal: ShowEditModal<T, K>;
|
|
14
19
|
hasProvider: boolean;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
type T = any;
|
|
23
|
+
type K = any;
|
|
24
|
+
const defaultErrorState: FormModalContextType<T, K> = {
|
|
18
25
|
showCreateModal: () => {
|
|
19
26
|
console.error('The showCreateModal function should only be used in a child of the FormModalProvider component.');
|
|
20
27
|
},
|
|
21
|
-
showEditModal: () => {
|
|
28
|
+
showEditModal: (state: ({ [key in keyof T]: FormValue } & K) | null) => {
|
|
22
29
|
console.error('The showEditModal function should only be used in a child of the FormModalProvider component.');
|
|
23
30
|
},
|
|
24
31
|
hasProvider: false,
|
|
@@ -26,7 +33,7 @@ const defaultErrorState: FormModalContextType<any> = {
|
|
|
26
33
|
|
|
27
34
|
export const FormModalContext = React.createContext(defaultErrorState);
|
|
28
35
|
|
|
29
|
-
export const useFormModal =
|
|
36
|
+
export const useFormModal = () => useContext(FormModalContext);
|
|
30
37
|
|
|
31
38
|
export type FormCreateModalButton = ButtonProps;
|
|
32
39
|
export const FormCreateModalButton = ({ onClick, ...props }: ButtonProps) => {
|
|
@@ -52,11 +59,11 @@ export const FormCreateModalButton = ({ onClick, ...props }: ButtonProps) => {
|
|
|
52
59
|
)
|
|
53
60
|
}
|
|
54
61
|
|
|
55
|
-
export interface FormEditModalButtonProps<T
|
|
56
|
-
state:
|
|
62
|
+
export interface FormEditModalButtonProps<T, K> extends ButtonProps {
|
|
63
|
+
state: { [key in keyof T]: FormValue } & K;
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
export const FormEditModalButton =
|
|
66
|
+
export const FormEditModalButton = ({ state, onClick, ...props }: FormEditModalButtonProps<T, K>) => {
|
|
60
67
|
const context = useContext(FormModalContext);
|
|
61
68
|
|
|
62
69
|
return (
|
|
@@ -79,12 +86,16 @@ export const FormEditModalButton = <T extends FormFields>({ state, onClick, ...p
|
|
|
79
86
|
)
|
|
80
87
|
}
|
|
81
88
|
|
|
82
|
-
export type FormModalProviderProps<
|
|
89
|
+
export type FormModalProviderProps<
|
|
90
|
+
T extends FormFields,
|
|
91
|
+
K extends IncludeData<T>
|
|
92
|
+
> = {
|
|
83
93
|
formFields: T;
|
|
84
|
-
initialState
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
94
|
+
initialState: InitialState<T> | K;
|
|
95
|
+
includeData?: K;
|
|
96
|
+
onSave?: (state: { [key in keyof T]: FormValue } & K, callback?: () => void) => void;
|
|
97
|
+
onCreate?: (state: { [key in keyof T]: FormValue } & K, callback?: () => void) => void;
|
|
98
|
+
onUpdate?: (state: { [key in keyof T]: FormValue } & K, callback?: () => void) => void;
|
|
88
99
|
validate?: Validate;
|
|
89
100
|
createModalTitle?: ModalTitle;
|
|
90
101
|
editModalTitle?: ModalTitle;
|
|
@@ -94,11 +105,12 @@ export type FormModalProviderProps<T extends FormFields> = {
|
|
|
94
105
|
children: ReactNode;
|
|
95
106
|
}
|
|
96
107
|
|
|
97
|
-
export const FormModalProvider
|
|
108
|
+
export const FormModalProvider: React.FC<FormModalProviderProps<T, K>> = ({
|
|
98
109
|
createModalTitle,
|
|
99
110
|
editModalTitle,
|
|
100
111
|
formFields,
|
|
101
|
-
initialState
|
|
112
|
+
initialState,
|
|
113
|
+
includeData,
|
|
102
114
|
validate,
|
|
103
115
|
loading = false,
|
|
104
116
|
onCreate,
|
|
@@ -107,21 +119,29 @@ export const FormModalProvider = <T extends FormFields>({
|
|
|
107
119
|
dialogClassName,
|
|
108
120
|
width,
|
|
109
121
|
children,
|
|
110
|
-
}
|
|
122
|
+
}) => {
|
|
111
123
|
const [createModalActive, setCreateModalActive] = useState(false);
|
|
112
|
-
const [editModalState, setEditModalState] = useState<
|
|
124
|
+
const [editModalState, setEditModalState] = useState<({ [key in keyof T]: FormValue } & K) | null>(null);
|
|
113
125
|
const { strings } = useLocalization();
|
|
126
|
+
|
|
127
|
+
// Provide default values
|
|
128
|
+
const safeIncludeData = (includeData || {}) as K;
|
|
114
129
|
|
|
115
130
|
const handleCreateSubmit: OnSubmit<T> = (state, callback) => {
|
|
116
131
|
// Priority: onCreate > onSave fallback
|
|
117
132
|
const submitHandler = onCreate || onSave;
|
|
118
133
|
if (submitHandler) {
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
134
|
+
// Merge form state with includeData
|
|
135
|
+
submitHandler(
|
|
136
|
+
{
|
|
137
|
+
...state,
|
|
138
|
+
...safeIncludeData,
|
|
139
|
+
} as { [key in keyof T]: FormValue } & K,
|
|
140
|
+
() => {
|
|
141
|
+
setCreateModalActive(false);
|
|
142
|
+
if (callback) callback();
|
|
143
|
+
}
|
|
144
|
+
);
|
|
125
145
|
} else {
|
|
126
146
|
// No handler provided, just close modal
|
|
127
147
|
setCreateModalActive(false);
|
|
@@ -133,10 +153,17 @@ export const FormModalProvider = <T extends FormFields>({
|
|
|
133
153
|
// Priority: onUpdate > onSave fallback
|
|
134
154
|
const submitHandler = onUpdate || onSave;
|
|
135
155
|
if (submitHandler) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
156
|
+
// Merge form state with includeData
|
|
157
|
+
submitHandler(
|
|
158
|
+
{
|
|
159
|
+
...state,
|
|
160
|
+
...safeIncludeData,
|
|
161
|
+
} as { [key in keyof T]: FormValue } & K,
|
|
162
|
+
() => {
|
|
163
|
+
setEditModalState(null);
|
|
164
|
+
if (callback) callback();
|
|
165
|
+
}
|
|
166
|
+
);
|
|
140
167
|
} else {
|
|
141
168
|
// No handler provided, just close modal
|
|
142
169
|
setEditModalState(null);
|
|
@@ -144,14 +171,20 @@ export const FormModalProvider = <T extends FormFields>({
|
|
|
144
171
|
}
|
|
145
172
|
};
|
|
146
173
|
|
|
174
|
+
// Convert entity state to form initial state
|
|
175
|
+
const getFormInitialState = (entityState: ({ [key in keyof T]: FormValue } & K) | null): InitialState<T> => {
|
|
176
|
+
if (!entityState) {
|
|
177
|
+
return initialState as InitialState<T>;
|
|
178
|
+
}
|
|
179
|
+
return entityState as unknown as InitialState<T>;
|
|
180
|
+
};
|
|
181
|
+
|
|
147
182
|
return (
|
|
148
183
|
<FormModalContext.Provider
|
|
149
184
|
value={{
|
|
150
185
|
showCreateModal: (show?: boolean) =>
|
|
151
186
|
setCreateModalActive(typeof show === 'undefined' ? true : show),
|
|
152
|
-
showEditModal: (
|
|
153
|
-
(state: InitialState<T> | null) => setEditModalState(state)
|
|
154
|
-
) as (state: Partial<{ [x: string]: FormValue; }> | null) => void,
|
|
187
|
+
showEditModal: (state: ({ [key in keyof T]: FormValue } & K) | null) => setEditModalState(state),
|
|
155
188
|
hasProvider: true,
|
|
156
189
|
}}
|
|
157
190
|
>
|
|
@@ -160,7 +193,7 @@ export const FormModalProvider = <T extends FormFields>({
|
|
|
160
193
|
{createModalActive && (onCreate || onSave) && (
|
|
161
194
|
<FormProvider
|
|
162
195
|
formFields={formFields}
|
|
163
|
-
initialState={initialState}
|
|
196
|
+
initialState={initialState as InitialState<T>}
|
|
164
197
|
onSubmit={handleCreateSubmit}
|
|
165
198
|
validate={validate}
|
|
166
199
|
loading={loading}
|
|
@@ -179,7 +212,7 @@ export const FormModalProvider = <T extends FormFields>({
|
|
|
179
212
|
{editModalState && (onUpdate || onSave) && (
|
|
180
213
|
<FormProvider
|
|
181
214
|
formFields={formFields}
|
|
182
|
-
initialState={editModalState}
|
|
215
|
+
initialState={getFormInitialState(editModalState)}
|
|
183
216
|
onSubmit={handleEditSubmit}
|
|
184
217
|
validate={validate}
|
|
185
218
|
loading={loading}
|