@refinedev/react-hook-form 4.8.14 → 4.8.15
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/esm/index.js.map +1 -1
- package/dist/iife/index.js +18 -18
- package/dist/iife/index.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/useForm/index.d.ts.map +1 -1
- package/dist/useModalForm/index.d.ts +13 -13
- package/dist/useModalForm/index.d.ts.map +1 -1
- package/dist/useStepsForm/index.d.ts +10 -10
- package/dist/useStepsForm/index.d.ts.map +1 -1
- package/package.json +32 -32
- package/src/index.ts +6 -6
- package/src/useForm/index.spec.tsx +210 -222
- package/src/useForm/index.ts +227 -229
- package/src/useModalForm/index.spec.ts +243 -243
- package/src/useModalForm/index.ts +256 -265
- package/src/useStepsForm/index.spec.ts +103 -107
- package/src/useStepsForm/index.ts +120 -120
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { useCallback } from "react";
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
BaseKey,
|
|
4
|
+
BaseRecord,
|
|
5
|
+
FormWithSyncWithLocationParams,
|
|
6
|
+
HttpError,
|
|
7
|
+
useGo,
|
|
8
|
+
useModal,
|
|
9
|
+
useParsed,
|
|
10
|
+
useResource,
|
|
11
|
+
useUserFriendlyName,
|
|
12
|
+
useTranslate,
|
|
13
|
+
useWarnAboutChange,
|
|
14
|
+
useInvalidate,
|
|
15
15
|
} from "@refinedev/core";
|
|
16
16
|
import { FieldValues } from "react-hook-form";
|
|
17
17
|
|
|
@@ -19,49 +19,49 @@ import { useForm, UseFormProps, UseFormReturnType } from "../useForm";
|
|
|
19
19
|
import React from "react";
|
|
20
20
|
|
|
21
21
|
export type UseModalFormReturnType<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
TQueryFnData extends BaseRecord = BaseRecord,
|
|
23
|
+
TError extends HttpError = HttpError,
|
|
24
|
+
TVariables extends FieldValues = FieldValues,
|
|
25
|
+
TContext extends object = {},
|
|
26
|
+
TData extends BaseRecord = TQueryFnData,
|
|
27
|
+
TResponse extends BaseRecord = TData,
|
|
28
|
+
TResponseError extends HttpError = TError,
|
|
29
29
|
> = UseFormReturnType<
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
TQueryFnData,
|
|
31
|
+
TError,
|
|
32
|
+
TVariables,
|
|
33
|
+
TContext,
|
|
34
|
+
TData,
|
|
35
|
+
TResponse,
|
|
36
|
+
TResponseError
|
|
37
37
|
> & {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
38
|
+
modal: {
|
|
39
|
+
submit: (values: TVariables) => void;
|
|
40
|
+
close: () => void;
|
|
41
|
+
show: (id?: BaseKey) => void;
|
|
42
|
+
visible: boolean;
|
|
43
|
+
title: string;
|
|
44
|
+
};
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
export type UseModalFormProps<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
TQueryFnData extends BaseRecord = BaseRecord,
|
|
49
|
+
TError extends HttpError = HttpError,
|
|
50
|
+
TVariables extends FieldValues = FieldValues,
|
|
51
|
+
TContext extends object = {},
|
|
52
|
+
TData extends BaseRecord = TQueryFnData,
|
|
53
|
+
TResponse extends BaseRecord = TData,
|
|
54
|
+
TResponseError extends HttpError = TError,
|
|
55
55
|
> = UseFormProps<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
TQueryFnData,
|
|
57
|
+
TError,
|
|
58
|
+
TVariables,
|
|
59
|
+
TContext,
|
|
60
|
+
TData,
|
|
61
|
+
TResponse,
|
|
62
|
+
TResponseError
|
|
63
63
|
> & {
|
|
64
|
-
|
|
64
|
+
/**
|
|
65
65
|
* @description Configuration object for the modal.
|
|
66
66
|
* `defaultVisible`: Initial visibility state of the modal.
|
|
67
67
|
*
|
|
@@ -75,253 +75,244 @@ export type UseModalFormProps<
|
|
|
75
75
|
}`
|
|
76
76
|
* @default `defaultVisible = false` `autoSubmitClose = true` `autoResetForm = true`
|
|
77
77
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
modalProps?: {
|
|
79
|
+
defaultVisible?: boolean;
|
|
80
|
+
autoSubmitClose?: boolean;
|
|
81
|
+
autoResetForm?: boolean;
|
|
82
|
+
};
|
|
83
83
|
} & FormWithSyncWithLocationParams;
|
|
84
84
|
|
|
85
85
|
export const useModalForm = <
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
86
|
+
TQueryFnData extends BaseRecord = BaseRecord,
|
|
87
|
+
TError extends HttpError = HttpError,
|
|
88
|
+
TVariables extends FieldValues = FieldValues,
|
|
89
|
+
TContext extends object = {},
|
|
90
|
+
TData extends BaseRecord = TQueryFnData,
|
|
91
|
+
TResponse extends BaseRecord = TData,
|
|
92
|
+
TResponseError extends HttpError = TError,
|
|
93
93
|
>({
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
modalProps,
|
|
95
|
+
refineCoreProps,
|
|
96
|
+
syncWithLocation,
|
|
97
|
+
...rest
|
|
98
98
|
}: UseModalFormProps<
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
99
|
+
TQueryFnData,
|
|
100
|
+
TError,
|
|
101
|
+
TVariables,
|
|
102
|
+
TContext,
|
|
103
|
+
TData,
|
|
104
|
+
TResponse,
|
|
105
|
+
TResponseError
|
|
106
106
|
> = {}): UseModalFormReturnType<
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
107
|
+
TQueryFnData,
|
|
108
|
+
TError,
|
|
109
|
+
TVariables,
|
|
110
|
+
TContext,
|
|
111
|
+
TData,
|
|
112
|
+
TResponse,
|
|
113
|
+
TResponseError
|
|
114
114
|
> => {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const translate = useTranslate();
|
|
115
|
+
const invalidate = useInvalidate();
|
|
116
|
+
const [initiallySynced, setInitiallySynced] = React.useState(false);
|
|
119
117
|
|
|
120
|
-
|
|
121
|
-
refineCoreProps ?? {};
|
|
118
|
+
const translate = useTranslate();
|
|
122
119
|
|
|
123
|
-
|
|
124
|
-
resource,
|
|
125
|
-
action: actionFromParams,
|
|
126
|
-
identifier,
|
|
127
|
-
} = useResource(resourceProp);
|
|
120
|
+
const { resource: resourceProp, action: actionProp } = refineCoreProps ?? {};
|
|
128
121
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
122
|
+
const {
|
|
123
|
+
resource,
|
|
124
|
+
action: actionFromParams,
|
|
125
|
+
identifier,
|
|
126
|
+
} = useResource(resourceProp);
|
|
132
127
|
|
|
133
|
-
|
|
128
|
+
const parsed = useParsed();
|
|
129
|
+
const go = useGo();
|
|
130
|
+
const getUserFriendlyName = useUserFriendlyName();
|
|
134
131
|
|
|
135
|
-
|
|
136
|
-
typeof syncWithLocation === "object" &&
|
|
137
|
-
syncWithLocation?.syncId === false
|
|
138
|
-
);
|
|
132
|
+
const action = actionProp ?? actionFromParams ?? "";
|
|
139
133
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
: resource && action && syncWithLocation
|
|
144
|
-
? `modal-${identifier}-${action}`
|
|
145
|
-
: undefined;
|
|
134
|
+
const syncingId = !(
|
|
135
|
+
typeof syncWithLocation === "object" && syncWithLocation?.syncId === false
|
|
136
|
+
);
|
|
146
137
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
138
|
+
const syncWithLocationKey =
|
|
139
|
+
typeof syncWithLocation === "object" && "key" in syncWithLocation
|
|
140
|
+
? syncWithLocation.key
|
|
141
|
+
: resource && action && syncWithLocation
|
|
142
|
+
? `modal-${identifier}-${action}`
|
|
143
|
+
: undefined;
|
|
152
144
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
TData,
|
|
159
|
-
TResponse,
|
|
160
|
-
TResponseError
|
|
161
|
-
>({
|
|
162
|
-
refineCoreProps: {
|
|
163
|
-
...refineCoreProps,
|
|
164
|
-
meta: {
|
|
165
|
-
...(syncWithLocationKey
|
|
166
|
-
? { [syncWithLocationKey]: undefined }
|
|
167
|
-
: {}),
|
|
168
|
-
...refineCoreProps?.meta,
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
...rest,
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
const {
|
|
175
|
-
reset,
|
|
176
|
-
refineCore: { onFinish, id, setId, autoSaveProps },
|
|
177
|
-
saveButtonProps,
|
|
178
|
-
handleSubmit,
|
|
179
|
-
} = useHookFormResult;
|
|
145
|
+
const {
|
|
146
|
+
defaultVisible = false,
|
|
147
|
+
autoSubmitClose = true,
|
|
148
|
+
autoResetForm = true,
|
|
149
|
+
} = modalProps ?? {};
|
|
180
150
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
151
|
+
const useHookFormResult = useForm<
|
|
152
|
+
TQueryFnData,
|
|
153
|
+
TError,
|
|
154
|
+
TVariables,
|
|
155
|
+
TContext,
|
|
156
|
+
TData,
|
|
157
|
+
TResponse,
|
|
158
|
+
TResponseError
|
|
159
|
+
>({
|
|
160
|
+
refineCoreProps: {
|
|
161
|
+
...refineCoreProps,
|
|
162
|
+
meta: {
|
|
163
|
+
...(syncWithLocationKey ? { [syncWithLocationKey]: undefined } : {}),
|
|
164
|
+
...refineCoreProps?.meta,
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
...rest,
|
|
168
|
+
});
|
|
184
169
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
} else if (typeof openStatus === "string") {
|
|
193
|
-
if (openStatus === "true") {
|
|
194
|
-
show();
|
|
195
|
-
}
|
|
196
|
-
}
|
|
170
|
+
const {
|
|
171
|
+
reset,
|
|
172
|
+
refineCore: { onFinish, id, setId, autoSaveProps },
|
|
173
|
+
saveButtonProps,
|
|
174
|
+
handleSubmit,
|
|
175
|
+
} = useHookFormResult;
|
|
197
176
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
setId?.(idFromParams);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
177
|
+
const { visible, show, close } = useModal({
|
|
178
|
+
defaultVisible,
|
|
179
|
+
});
|
|
204
180
|
|
|
205
|
-
|
|
181
|
+
React.useEffect(() => {
|
|
182
|
+
if (initiallySynced === false && syncWithLocationKey) {
|
|
183
|
+
const openStatus = parsed?.params?.[syncWithLocationKey]?.open;
|
|
184
|
+
if (typeof openStatus === "boolean") {
|
|
185
|
+
if (openStatus) {
|
|
186
|
+
show();
|
|
206
187
|
}
|
|
207
|
-
|
|
188
|
+
} else if (typeof openStatus === "string") {
|
|
189
|
+
if (openStatus === "true") {
|
|
190
|
+
show();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
208
193
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
query: {
|
|
214
|
-
[syncWithLocationKey]: {
|
|
215
|
-
...parsed?.params?.[syncWithLocationKey],
|
|
216
|
-
open: true,
|
|
217
|
-
...(syncingId && id && { id }),
|
|
218
|
-
},
|
|
219
|
-
},
|
|
220
|
-
options: { keepQuery: true },
|
|
221
|
-
type: "replace",
|
|
222
|
-
});
|
|
223
|
-
} else if (syncWithLocationKey && !visible) {
|
|
224
|
-
go({
|
|
225
|
-
query: {
|
|
226
|
-
[syncWithLocationKey]: undefined,
|
|
227
|
-
},
|
|
228
|
-
options: { keepQuery: true },
|
|
229
|
-
type: "replace",
|
|
230
|
-
});
|
|
231
|
-
}
|
|
194
|
+
if (syncingId) {
|
|
195
|
+
const idFromParams = parsed?.params?.[syncWithLocationKey]?.id;
|
|
196
|
+
if (idFromParams) {
|
|
197
|
+
setId?.(idFromParams);
|
|
232
198
|
}
|
|
233
|
-
|
|
199
|
+
}
|
|
234
200
|
|
|
235
|
-
|
|
236
|
-
|
|
201
|
+
setInitiallySynced(true);
|
|
202
|
+
}
|
|
203
|
+
}, [syncWithLocationKey, parsed, syncingId, setId]);
|
|
237
204
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
205
|
+
React.useEffect(() => {
|
|
206
|
+
if (initiallySynced === true) {
|
|
207
|
+
if (visible && syncWithLocationKey) {
|
|
208
|
+
go({
|
|
209
|
+
query: {
|
|
210
|
+
[syncWithLocationKey]: {
|
|
211
|
+
...parsed?.params?.[syncWithLocationKey],
|
|
212
|
+
open: true,
|
|
213
|
+
...(syncingId && id && { id }),
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
options: { keepQuery: true },
|
|
217
|
+
type: "replace",
|
|
218
|
+
});
|
|
219
|
+
} else if (syncWithLocationKey && !visible) {
|
|
220
|
+
go({
|
|
221
|
+
query: {
|
|
222
|
+
[syncWithLocationKey]: undefined,
|
|
223
|
+
},
|
|
224
|
+
options: { keepQuery: true },
|
|
225
|
+
type: "replace",
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}, [id, visible, show, syncWithLocationKey, syncingId]);
|
|
241
230
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
};
|
|
231
|
+
const submit = async (values: TVariables) => {
|
|
232
|
+
await onFinish(values);
|
|
246
233
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
autoSaveProps.status === "success" &&
|
|
251
|
-
refineCoreProps?.autoSave?.invalidateOnClose
|
|
252
|
-
) {
|
|
253
|
-
invalidate({
|
|
254
|
-
id,
|
|
255
|
-
invalidates: refineCoreProps.invalidates || [
|
|
256
|
-
"list",
|
|
257
|
-
"many",
|
|
258
|
-
"detail",
|
|
259
|
-
],
|
|
260
|
-
dataProviderName: refineCoreProps.dataProviderName,
|
|
261
|
-
resource: identifier,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
234
|
+
if (autoSubmitClose) {
|
|
235
|
+
close();
|
|
236
|
+
}
|
|
264
237
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
"Are you sure you want to leave? You have unsaved changes.",
|
|
270
|
-
),
|
|
271
|
-
);
|
|
238
|
+
if (autoResetForm) {
|
|
239
|
+
reset();
|
|
240
|
+
}
|
|
241
|
+
};
|
|
272
242
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
243
|
+
const { warnWhen, setWarnWhen } = useWarnAboutChange();
|
|
244
|
+
const handleClose = useCallback(() => {
|
|
245
|
+
if (
|
|
246
|
+
autoSaveProps.status === "success" &&
|
|
247
|
+
refineCoreProps?.autoSave?.invalidateOnClose
|
|
248
|
+
) {
|
|
249
|
+
invalidate({
|
|
250
|
+
id,
|
|
251
|
+
invalidates: refineCoreProps.invalidates || ["list", "many", "detail"],
|
|
252
|
+
dataProviderName: refineCoreProps.dataProviderName,
|
|
253
|
+
resource: identifier,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
279
256
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
257
|
+
if (warnWhen) {
|
|
258
|
+
const warnWhenConfirm = window.confirm(
|
|
259
|
+
translate(
|
|
260
|
+
"warnWhenUnsavedChanges",
|
|
261
|
+
"Are you sure you want to leave? You have unsaved changes.",
|
|
262
|
+
),
|
|
263
|
+
);
|
|
283
264
|
|
|
284
|
-
|
|
285
|
-
(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const hasId =
|
|
291
|
-
typeof showId !== "undefined" || typeof id !== "undefined";
|
|
292
|
-
if (needsIdToOpen ? hasId : true) {
|
|
293
|
-
show();
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
[id],
|
|
297
|
-
);
|
|
265
|
+
if (warnWhenConfirm) {
|
|
266
|
+
setWarnWhen(false);
|
|
267
|
+
} else {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
298
271
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
`${getUserFriendlyName(
|
|
303
|
-
`${actionProp} ${
|
|
304
|
-
resource?.meta?.label ??
|
|
305
|
-
resource?.options?.label ??
|
|
306
|
-
resource?.label ??
|
|
307
|
-
identifier
|
|
308
|
-
}`,
|
|
309
|
-
"singular",
|
|
310
|
-
)}`,
|
|
311
|
-
);
|
|
272
|
+
setId?.(undefined);
|
|
273
|
+
close();
|
|
274
|
+
}, [warnWhen, autoSaveProps.status]);
|
|
312
275
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
276
|
+
const handleShow = useCallback(
|
|
277
|
+
(showId?: BaseKey) => {
|
|
278
|
+
if (typeof showId !== "undefined") {
|
|
279
|
+
setId?.(showId);
|
|
280
|
+
}
|
|
281
|
+
const needsIdToOpen = action === "edit" || action === "clone";
|
|
282
|
+
const hasId = typeof showId !== "undefined" || typeof id !== "undefined";
|
|
283
|
+
if (needsIdToOpen ? hasId : true) {
|
|
284
|
+
show();
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
[id],
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const title = translate(
|
|
291
|
+
`${identifier}.titles.${actionProp}`,
|
|
292
|
+
undefined,
|
|
293
|
+
`${getUserFriendlyName(
|
|
294
|
+
`${actionProp} ${
|
|
295
|
+
resource?.meta?.label ??
|
|
296
|
+
resource?.options?.label ??
|
|
297
|
+
resource?.label ??
|
|
298
|
+
identifier
|
|
299
|
+
}`,
|
|
300
|
+
"singular",
|
|
301
|
+
)}`,
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
return {
|
|
305
|
+
modal: {
|
|
306
|
+
submit,
|
|
307
|
+
close: handleClose,
|
|
308
|
+
show: handleShow,
|
|
309
|
+
visible,
|
|
310
|
+
title,
|
|
311
|
+
},
|
|
312
|
+
...useHookFormResult,
|
|
313
|
+
saveButtonProps: {
|
|
314
|
+
...saveButtonProps,
|
|
315
|
+
onClick: (e) => handleSubmit(submit)(e),
|
|
316
|
+
},
|
|
317
|
+
};
|
|
327
318
|
};
|