@refinedev/react-hook-form 5.0.0 → 5.0.2
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/index.cjs +417 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +404 -1
- package/dist/index.mjs.map +1 -1
- package/dist/useModalForm/index.d.cts.map +1 -1
- package/dist/useModalForm/index.d.ts.map +1 -1
- package/package.json +7 -8
- package/src/useForm/index.spec.tsx +2 -2
- package/src/useModalForm/index.spec.ts +27 -6
- package/src/useModalForm/index.ts +18 -4
- package/src/useStepsForm/index.spec.ts +2 -2
|
@@ -236,8 +236,8 @@ describe("useModalForm Hook", () => {
|
|
|
236
236
|
});
|
|
237
237
|
|
|
238
238
|
it("should `meta[syncWithLocationKey]` overrided by default", async () => {
|
|
239
|
-
const mockGetOne =
|
|
240
|
-
const mockUpdate =
|
|
239
|
+
const mockGetOne = vi.fn().mockResolvedValue({ data: { id: 5 } });
|
|
240
|
+
const mockUpdate = vi.fn().mockResolvedValue({ data: { id: 5 } });
|
|
241
241
|
|
|
242
242
|
const { result } = renderHook(
|
|
243
243
|
() =>
|
|
@@ -291,11 +291,16 @@ describe("useModalForm Hook", () => {
|
|
|
291
291
|
|
|
292
292
|
await act(async () => {
|
|
293
293
|
result.current.modal.show();
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
await act(async () => {
|
|
294
297
|
// register values to form
|
|
295
298
|
result.current.register("test");
|
|
296
299
|
result.current.setValue("test", "test");
|
|
297
300
|
});
|
|
298
301
|
|
|
302
|
+
expect(result.current.getValues()).toStrictEqual({ test: "test" });
|
|
303
|
+
|
|
299
304
|
await act(async () => {
|
|
300
305
|
result.current.modal.close();
|
|
301
306
|
});
|
|
@@ -350,20 +355,36 @@ describe("useModalForm Hook", () => {
|
|
|
350
355
|
},
|
|
351
356
|
}),
|
|
352
357
|
{
|
|
353
|
-
wrapper: TestWrapper({
|
|
358
|
+
wrapper: TestWrapper({
|
|
359
|
+
dataProvider: {
|
|
360
|
+
...MockJSONServer,
|
|
361
|
+
getOne: () =>
|
|
362
|
+
Promise.resolve({ data: { id: 5, title: "default-title" } }),
|
|
363
|
+
},
|
|
364
|
+
}),
|
|
354
365
|
},
|
|
355
366
|
);
|
|
356
367
|
|
|
357
368
|
await act(async () => {
|
|
358
369
|
result.current.modal.show();
|
|
359
|
-
|
|
360
|
-
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
await act(async () => {
|
|
373
|
+
result.current.setValue("title", "new-title");
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
expect(result.current.getValues()).toStrictEqual({
|
|
377
|
+
id: 5,
|
|
378
|
+
title: "new-title",
|
|
361
379
|
});
|
|
362
380
|
|
|
363
381
|
await act(async () => {
|
|
364
382
|
result.current.modal.close();
|
|
365
383
|
});
|
|
366
384
|
|
|
367
|
-
expect(result.current.getValues()).toStrictEqual({
|
|
385
|
+
expect(result.current.getValues()).toStrictEqual({
|
|
386
|
+
id: 5,
|
|
387
|
+
title: "default-title",
|
|
388
|
+
});
|
|
368
389
|
});
|
|
369
390
|
});
|
|
@@ -64,11 +64,11 @@ export type UseModalFormProps<
|
|
|
64
64
|
/**
|
|
65
65
|
* @description Configuration object for the modal.
|
|
66
66
|
* `defaultVisible`: Initial visibility state of the modal.
|
|
67
|
-
*
|
|
67
|
+
*
|
|
68
68
|
* `autoSubmitClose`: Whether the form should be submitted when the modal is closed.
|
|
69
|
-
*
|
|
69
|
+
*
|
|
70
70
|
* `autoResetForm`: Whether the form should be reset when the form is submitted.
|
|
71
|
-
*
|
|
71
|
+
*
|
|
72
72
|
* `autoResetFormWhenClose`: Whether the form should be reset to defaultValues when the modal is closed.
|
|
73
73
|
* @type `{
|
|
74
74
|
defaultVisible?: boolean;
|
|
@@ -176,7 +176,7 @@ export const useModalForm = <
|
|
|
176
176
|
|
|
177
177
|
const {
|
|
178
178
|
reset,
|
|
179
|
-
refineCore: { onFinish, id, setId, autoSaveProps },
|
|
179
|
+
refineCore: { onFinish, id, setId, autoSaveProps, query },
|
|
180
180
|
saveButtonProps,
|
|
181
181
|
handleSubmit,
|
|
182
182
|
} = useHookFormResult;
|
|
@@ -185,6 +185,20 @@ export const useModalForm = <
|
|
|
185
185
|
defaultVisible,
|
|
186
186
|
});
|
|
187
187
|
|
|
188
|
+
// compensate for setting of initial form values in useForm since it doesnt track modal visibility
|
|
189
|
+
React.useEffect(() => {
|
|
190
|
+
if (!visible || !query?.data?.data) return;
|
|
191
|
+
|
|
192
|
+
const formData = query.data.data;
|
|
193
|
+
if (!formData) return;
|
|
194
|
+
|
|
195
|
+
reset(formData as any, {
|
|
196
|
+
...(!autoResetFormWhenClose && {
|
|
197
|
+
keepDirtyValues: true,
|
|
198
|
+
}),
|
|
199
|
+
});
|
|
200
|
+
}, [visible, query?.data?.data, autoResetFormWhenClose]);
|
|
201
|
+
|
|
188
202
|
React.useEffect(() => {
|
|
189
203
|
if (initiallySynced === false && syncWithLocationKey) {
|
|
190
204
|
const openStatus = parsed?.params?.[syncWithLocationKey]?.open;
|
|
@@ -81,10 +81,10 @@ describe("useStepsForm Hook", () => {
|
|
|
81
81
|
async (scenario) => {
|
|
82
82
|
const mockData = { field1: "field1", field2: "field2" };
|
|
83
83
|
const mockDirtyFields = scenario.mockDirtyFields;
|
|
84
|
-
const setValue =
|
|
84
|
+
const setValue = vi.fn();
|
|
85
85
|
const getValues = () => mockData;
|
|
86
86
|
|
|
87
|
-
(
|
|
87
|
+
(vi.spyOn(UseForm, "useForm") as vi.Mock).mockReturnValueOnce({
|
|
88
88
|
setValue,
|
|
89
89
|
getValues,
|
|
90
90
|
formState: { dirtyFields: mockDirtyFields },
|