@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.
@@ -1,17 +1,17 @@
1
1
  import { useCallback } from "react";
2
2
  import {
3
- BaseKey,
4
- BaseRecord,
5
- FormWithSyncWithLocationParams,
6
- HttpError,
7
- useGo,
8
- useModal,
9
- useParsed,
10
- useResource,
11
- useUserFriendlyName,
12
- useTranslate,
13
- useWarnAboutChange,
14
- useInvalidate,
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
- 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,
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
- TQueryFnData,
31
- TError,
32
- TVariables,
33
- TContext,
34
- TData,
35
- TResponse,
36
- TResponseError
30
+ TQueryFnData,
31
+ TError,
32
+ TVariables,
33
+ TContext,
34
+ TData,
35
+ TResponse,
36
+ TResponseError
37
37
  > & {
38
- modal: {
39
- submit: (values: TVariables) => void;
40
- close: () => void;
41
- show: (id?: BaseKey) => void;
42
- visible: boolean;
43
- title: string;
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
- 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,
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
- TQueryFnData,
57
- TError,
58
- TVariables,
59
- TContext,
60
- TData,
61
- TResponse,
62
- TResponseError
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
- modalProps?: {
79
- defaultVisible?: boolean;
80
- autoSubmitClose?: boolean;
81
- autoResetForm?: boolean;
82
- };
78
+ modalProps?: {
79
+ defaultVisible?: boolean;
80
+ autoSubmitClose?: boolean;
81
+ autoResetForm?: boolean;
82
+ };
83
83
  } & FormWithSyncWithLocationParams;
84
84
 
85
85
  export const useModalForm = <
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,
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
- modalProps,
95
- refineCoreProps,
96
- syncWithLocation,
97
- ...rest
94
+ modalProps,
95
+ refineCoreProps,
96
+ syncWithLocation,
97
+ ...rest
98
98
  }: UseModalFormProps<
99
- TQueryFnData,
100
- TError,
101
- TVariables,
102
- TContext,
103
- TData,
104
- TResponse,
105
- TResponseError
99
+ TQueryFnData,
100
+ TError,
101
+ TVariables,
102
+ TContext,
103
+ TData,
104
+ TResponse,
105
+ TResponseError
106
106
  > = {}): UseModalFormReturnType<
107
- TQueryFnData,
108
- TError,
109
- TVariables,
110
- TContext,
111
- TData,
112
- TResponse,
113
- TResponseError
107
+ TQueryFnData,
108
+ TError,
109
+ TVariables,
110
+ TContext,
111
+ TData,
112
+ TResponse,
113
+ TResponseError
114
114
  > => {
115
- const invalidate = useInvalidate();
116
- const [initiallySynced, setInitiallySynced] = React.useState(false);
117
-
118
- const translate = useTranslate();
115
+ const invalidate = useInvalidate();
116
+ const [initiallySynced, setInitiallySynced] = React.useState(false);
119
117
 
120
- const { resource: resourceProp, action: actionProp } =
121
- refineCoreProps ?? {};
118
+ const translate = useTranslate();
122
119
 
123
- const {
124
- resource,
125
- action: actionFromParams,
126
- identifier,
127
- } = useResource(resourceProp);
120
+ const { resource: resourceProp, action: actionProp } = refineCoreProps ?? {};
128
121
 
129
- const parsed = useParsed();
130
- const go = useGo();
131
- const getUserFriendlyName = useUserFriendlyName();
122
+ const {
123
+ resource,
124
+ action: actionFromParams,
125
+ identifier,
126
+ } = useResource(resourceProp);
132
127
 
133
- const action = actionProp ?? actionFromParams ?? "";
128
+ const parsed = useParsed();
129
+ const go = useGo();
130
+ const getUserFriendlyName = useUserFriendlyName();
134
131
 
135
- const syncingId = !(
136
- typeof syncWithLocation === "object" &&
137
- syncWithLocation?.syncId === false
138
- );
132
+ const action = actionProp ?? actionFromParams ?? "";
139
133
 
140
- const syncWithLocationKey =
141
- typeof syncWithLocation === "object" && "key" in syncWithLocation
142
- ? syncWithLocation.key
143
- : resource && action && syncWithLocation
144
- ? `modal-${identifier}-${action}`
145
- : undefined;
134
+ const syncingId = !(
135
+ typeof syncWithLocation === "object" && syncWithLocation?.syncId === false
136
+ );
146
137
 
147
- const {
148
- defaultVisible = false,
149
- autoSubmitClose = true,
150
- autoResetForm = true,
151
- } = modalProps ?? {};
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
- const useHookFormResult = useForm<
154
- TQueryFnData,
155
- TError,
156
- TVariables,
157
- TContext,
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
- const { visible, show, close } = useModal({
182
- defaultVisible,
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
- React.useEffect(() => {
186
- if (initiallySynced === false && syncWithLocationKey) {
187
- const openStatus = parsed?.params?.[syncWithLocationKey]?.open;
188
- if (typeof openStatus === "boolean") {
189
- if (openStatus) {
190
- show();
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
- if (syncingId) {
199
- const idFromParams = parsed?.params?.[syncWithLocationKey]?.id;
200
- if (idFromParams) {
201
- setId?.(idFromParams);
202
- }
203
- }
177
+ const { visible, show, close } = useModal({
178
+ defaultVisible,
179
+ });
204
180
 
205
- setInitiallySynced(true);
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
- }, [syncWithLocationKey, parsed, syncingId, setId]);
188
+ } else if (typeof openStatus === "string") {
189
+ if (openStatus === "true") {
190
+ show();
191
+ }
192
+ }
208
193
 
209
- React.useEffect(() => {
210
- if (initiallySynced === true) {
211
- if (visible && syncWithLocationKey) {
212
- go({
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
- }, [id, visible, show, syncWithLocationKey, syncingId]);
199
+ }
234
200
 
235
- const submit = async (values: TVariables) => {
236
- await onFinish(values);
201
+ setInitiallySynced(true);
202
+ }
203
+ }, [syncWithLocationKey, parsed, syncingId, setId]);
237
204
 
238
- if (autoSubmitClose) {
239
- close();
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
- if (autoResetForm) {
243
- reset();
244
- }
245
- };
231
+ const submit = async (values: TVariables) => {
232
+ await onFinish(values);
246
233
 
247
- const { warnWhen, setWarnWhen } = useWarnAboutChange();
248
- const handleClose = useCallback(() => {
249
- if (
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
- if (warnWhen) {
266
- const warnWhenConfirm = window.confirm(
267
- translate(
268
- "warnWhenUnsavedChanges",
269
- "Are you sure you want to leave? You have unsaved changes.",
270
- ),
271
- );
238
+ if (autoResetForm) {
239
+ reset();
240
+ }
241
+ };
272
242
 
273
- if (warnWhenConfirm) {
274
- setWarnWhen(false);
275
- } else {
276
- return;
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
- setId?.(undefined);
281
- close();
282
- }, [warnWhen, autoSaveProps.status]);
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
- const handleShow = useCallback(
285
- (showId?: BaseKey) => {
286
- if (typeof showId !== "undefined") {
287
- setId?.(showId);
288
- }
289
- const needsIdToOpen = action === "edit" || action === "clone";
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
- const title = translate(
300
- `${identifier}.titles.${actionProp}`,
301
- undefined,
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
- return {
314
- modal: {
315
- submit,
316
- close: handleClose,
317
- show: handleShow,
318
- visible,
319
- title,
320
- },
321
- ...useHookFormResult,
322
- saveButtonProps: {
323
- ...saveButtonProps,
324
- onClick: (e) => handleSubmit(submit)(e),
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
  };