@boxcustodia/library 2.0.0-alpha.19 → 2.0.0-alpha.20
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/components/button/button.cjs.js +1 -1
- package/dist/components/button/button.es.js +19 -18
- package/dist/components/button/components/base-button.cjs.js +1 -1
- package/dist/components/button/components/base-button.es.js +20 -20
- package/dist/components/calendar/calendar.cjs.js +1 -1
- package/dist/components/calendar/calendar.es.js +1 -0
- package/dist/components/date-picker/date-input.cjs.js +1 -1
- package/dist/components/date-picker/date-input.es.js +92 -75
- package/dist/components/date-picker/date-picker.cjs.js +1 -1
- package/dist/components/date-picker/date-picker.es.js +104 -95
- package/dist/components/date-picker/date-picker.utils.cjs.js +1 -1
- package/dist/components/date-picker/date-picker.utils.es.js +51 -43
- package/dist/components/date-picker/use-hidden-field-value.cjs.js +1 -0
- package/dist/components/date-picker/use-hidden-field-value.es.js +11 -0
- package/dist/components/menu/menu.es.js +1 -9
- package/dist/components/otp/otp.cjs.js +2 -0
- package/dist/components/otp/otp.es.js +93 -0
- package/dist/components/password/password.cjs.js +1 -1
- package/dist/components/password/password.es.js +2 -2
- package/dist/components/select/select.cjs.js +1 -1
- package/dist/components/select/select.es.js +68 -60
- package/dist/hooks/internal/is-apple-device.cjs.js +1 -0
- package/dist/hooks/internal/is-apple-device.es.js +9 -0
- package/dist/hooks/internal/use-latest-ref.cjs.js +1 -0
- package/dist/hooks/internal/use-latest-ref.es.js +11 -0
- package/dist/hooks/use-array/use-array.cjs.js +1 -1
- package/dist/hooks/use-array/use-array.es.js +54 -42
- package/dist/hooks/use-async/use-async.cjs.js +1 -1
- package/dist/hooks/use-async/use-async.es.js +53 -20
- package/dist/hooks/use-boolean/use-boolean.cjs.js +1 -0
- package/dist/hooks/use-boolean/use-boolean.es.js +25 -0
- package/dist/hooks/use-click-outside/use-click-outside.cjs.js +1 -1
- package/dist/hooks/use-click-outside/use-click-outside.es.js +26 -12
- package/dist/hooks/use-debounce-callback/use-debounced-callback.cjs.js +1 -1
- package/dist/hooks/use-debounce-callback/use-debounced-callback.es.js +27 -10
- package/dist/hooks/use-debounce-value/use-debounced-value.cjs.js +1 -1
- package/dist/hooks/use-debounce-value/use-debounced-value.es.js +7 -9
- package/dist/hooks/use-disclosure/use-disclosure.cjs.js +1 -1
- package/dist/hooks/use-disclosure/use-disclosure.es.js +21 -11
- package/dist/hooks/use-document-title/use-document-title.cjs.js +1 -1
- package/dist/hooks/use-document-title/use-document-title.es.js +14 -12
- package/dist/hooks/use-event-listener/use-event-listener.cjs.js +1 -1
- package/dist/hooks/use-event-listener/use-event-listener.es.js +17 -9
- package/dist/hooks/use-hotkey/use-hotkey.cjs.js +1 -1
- package/dist/hooks/use-hotkey/use-hotkey.es.js +30 -14
- package/dist/hooks/use-hotkey/utils/is-input-field.cjs.js +1 -1
- package/dist/hooks/use-hotkey/utils/is-input-field.es.js +4 -2
- package/dist/hooks/use-hotkey/utils/match-and-run.cjs.js +1 -0
- package/dist/hooks/use-hotkey/utils/match-and-run.es.js +12 -0
- package/dist/hooks/use-hotkey/utils/match-key-modifiers.cjs.js +1 -1
- package/dist/hooks/use-hotkey/utils/match-key-modifiers.es.js +13 -12
- package/dist/hooks/use-hover/use-hover.cjs.js +1 -1
- package/dist/hooks/use-hover/use-hover.es.js +32 -17
- package/dist/hooks/use-is-visible/use-is-visible.cjs.js +1 -1
- package/dist/hooks/use-is-visible/use-is-visible.es.js +31 -27
- package/dist/hooks/use-local-storage/use-local-storage.cjs.js +1 -1
- package/dist/hooks/use-local-storage/use-local-storage.es.js +52 -20
- package/dist/hooks/use-media-query/use-media-query.cjs.js +1 -1
- package/dist/hooks/use-media-query/use-media-query.es.js +21 -11
- package/dist/hooks/use-mutation/use-mutation.cjs.js +1 -1
- package/dist/hooks/use-mutation/use-mutation.es.js +36 -22
- package/dist/hooks/use-object/use-object.cjs.js +1 -1
- package/dist/hooks/use-object/use-object.es.js +26 -22
- package/dist/hooks/use-prevent-page-close/use-prevent-page-close.cjs.js +1 -0
- package/dist/hooks/use-prevent-page-close/use-prevent-page-close.es.js +14 -0
- package/dist/hooks/use-step/use-step.cjs.js +1 -1
- package/dist/hooks/use-step/use-step.es.js +25 -24
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +308 -300
- package/dist/src/components/date-picker/date-picker.utils.d.ts +17 -0
- package/dist/src/components/date-picker/use-hidden-field-value.d.ts +12 -0
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/hooks/index.d.ts +2 -2
- package/dist/src/hooks/internal/index.d.ts +2 -0
- package/dist/src/hooks/internal/is-apple-device.d.ts +12 -0
- package/dist/src/hooks/internal/use-latest-ref.d.ts +12 -0
- package/dist/src/hooks/use-array/use-array.d.ts +24 -11
- package/dist/src/hooks/use-async/use-async.d.ts +16 -13
- package/dist/src/hooks/use-boolean/index.d.ts +1 -0
- package/dist/src/hooks/use-boolean/use-boolean.d.ts +15 -0
- package/dist/src/hooks/use-boolean/use-boolean.test.d.ts +1 -0
- package/dist/src/hooks/use-click-outside/use-click-outside.d.ts +23 -1
- package/dist/src/hooks/use-debounce-callback/use-debounced-callback.d.ts +19 -1
- package/dist/src/hooks/use-debounce-value/use-debounced-value.d.ts +10 -1
- package/dist/src/hooks/use-disclosure/use-disclosure.d.ts +17 -8
- package/dist/src/hooks/use-document-title/use-document-title.d.ts +11 -0
- package/dist/src/hooks/use-event-listener/use-event-listener.d.ts +18 -1
- package/dist/src/hooks/use-hotkey/index.d.ts +2 -1
- package/dist/src/hooks/use-hotkey/use-hotkey.d.ts +62 -5
- package/dist/src/hooks/use-hotkey/utils/index.d.ts +4 -3
- package/dist/src/hooks/use-hotkey/utils/is-input-field.d.ts +12 -2
- package/dist/src/hooks/use-hotkey/utils/is-input-field.test.d.ts +1 -0
- package/dist/src/hooks/use-hotkey/utils/match-and-run.d.ts +36 -0
- package/dist/src/hooks/use-hotkey/utils/match-and-run.test.d.ts +1 -0
- package/dist/src/hooks/use-hotkey/utils/match-key-modifiers.d.ts +20 -6
- package/dist/src/hooks/use-hotkey/utils/match-key-modifiers.test.d.ts +1 -0
- package/dist/src/hooks/use-hover/use-hover.d.ts +8 -4
- package/dist/src/hooks/use-is-visible/use-is-visible.d.ts +28 -4
- package/dist/src/hooks/use-local-storage/use-local-storage.d.ts +13 -2
- package/dist/src/hooks/use-media-query/use-media-query.d.ts +10 -1
- package/dist/src/hooks/use-media-query/use-media-query.test.d.ts +1 -0
- package/dist/src/hooks/use-mutation/use-mutation.d.ts +18 -11
- package/dist/src/hooks/use-object/use-object.d.ts +15 -6
- package/dist/src/hooks/use-prevent-page-close/index.d.ts +1 -0
- package/dist/src/hooks/use-prevent-page-close/use-prevent-page-close.d.ts +10 -0
- package/dist/src/hooks/use-prevent-page-close/use-prevent-page-close.test.d.ts +1 -0
- package/dist/src/hooks/use-step/use-step.d.ts +18 -11
- package/dist/src/utils/form.d.ts +10 -0
- package/package.json +1 -1
- package/src/components/alert-dialog/alert-dialog.test.tsx +13 -9
- package/src/components/auto-complete/auto-complete.test.tsx +4 -14
- package/src/components/avatar/avatar.test.tsx +7 -12
- package/src/components/button/button.test.tsx +10 -15
- package/src/components/button/button.tsx +14 -9
- package/src/components/button/components/base-button.tsx +2 -4
- package/src/components/calendar/calendar.test.tsx +12 -19
- package/src/components/calendar/calendar.tsx +4 -0
- package/src/components/card/card.test.tsx +4 -6
- package/src/components/checkbox/checkbox.test.tsx +12 -8
- package/src/components/checkbox-group/checkbox-group.test.tsx +7 -8
- package/src/components/combobox/combobox.test.tsx +24 -21
- package/src/components/date-picker/date-input-form.test.tsx +77 -0
- package/src/components/date-picker/date-input.stories.tsx +30 -18
- package/src/components/date-picker/date-input.tsx +77 -44
- package/src/components/date-picker/date-picker.stories.tsx +31 -1
- package/src/components/date-picker/date-picker.test.tsx +3 -13
- package/src/components/date-picker/date-picker.tsx +35 -16
- package/src/components/date-picker/date-picker.utils.test.ts +32 -14
- package/src/components/date-picker/date-picker.utils.ts +33 -0
- package/src/components/date-picker/use-date-input-popover.test.ts +3 -1
- package/src/components/date-picker/use-hidden-field-value.ts +23 -0
- package/src/components/dialog/dialog.test.tsx +10 -8
- package/src/components/dropzone/dropzone.test.tsx +11 -13
- package/src/components/empty/empty.test.tsx +4 -3
- package/src/components/field/field.test.tsx +12 -13
- package/src/components/form/form.stories.tsx +16 -1
- package/src/components/index.ts +1 -0
- package/src/components/label/label.test.tsx +3 -3
- package/src/components/menu/menu.tsx +1 -5
- package/src/components/number-input/number-input.test.tsx +6 -2
- package/src/components/password/password.test.tsx +20 -6
- package/src/components/password/password.tsx +2 -2
- package/src/components/popover/popover.test.tsx +4 -4
- package/src/components/progress/progress.test.tsx +7 -8
- package/src/components/radio-group/radio-group.test.tsx +17 -11
- package/src/components/select/select.test.tsx +10 -10
- package/src/components/select/select.tsx +9 -1
- package/src/components/stepper/stepper.stories.tsx +11 -15
- package/src/components/stepper/stepper.test.tsx +6 -4
- package/src/components/switch/switch.test.tsx +3 -3
- package/src/components/table/table.test.tsx +9 -3
- package/src/components/tabs/tabs.test.tsx +6 -2
- package/src/components/tag/tag.test.tsx +1 -3
- package/src/components/textarea/textarea.test.tsx +4 -1
- package/src/components/timeline/timeline.test.tsx +10 -5
- package/src/components/toast/toast.test.tsx +11 -14
- package/src/components/tooltip/tooltip.test.tsx +1 -5
- package/src/components/tree/tree.test.tsx +3 -1
- package/src/hooks/index.ts +2 -2
- package/src/hooks/internal/index.ts +2 -0
- package/src/hooks/internal/is-apple-device.test.ts +41 -0
- package/src/hooks/internal/is-apple-device.ts +33 -0
- package/src/hooks/internal/use-isomorphic-layout-effect.ts +3 -1
- package/src/hooks/internal/use-latest-ref.ts +21 -0
- package/src/hooks/use-array/use-array.stories.tsx +435 -64
- package/src/hooks/use-array/use-array.test.tsx +398 -15
- package/src/hooks/use-array/use-array.ts +105 -66
- package/src/hooks/use-async/use-async.stories.tsx +255 -131
- package/src/hooks/use-async/use-async.test.ts +397 -0
- package/src/hooks/use-async/use-async.ts +117 -39
- package/src/hooks/use-boolean/index.ts +1 -0
- package/src/hooks/use-boolean/use-boolean.stories.tsx +377 -0
- package/src/hooks/use-boolean/use-boolean.test.tsx +177 -0
- package/src/hooks/use-boolean/use-boolean.ts +50 -0
- package/src/hooks/use-click-outside/use-click-outside.stories.tsx +188 -18
- package/src/hooks/use-click-outside/use-click-outside.test.tsx +89 -10
- package/src/hooks/use-click-outside/use-click-outside.ts +62 -16
- package/src/hooks/use-debounce-callback/use-debounced-callback.stories.tsx +141 -41
- package/src/hooks/use-debounce-callback/use-debounced-callback.test.ts +217 -9
- package/src/hooks/use-debounce-callback/use-debounced-callback.ts +71 -11
- package/src/hooks/use-debounce-value/use-debounced-value.stories.tsx +247 -47
- package/src/hooks/use-debounce-value/use-debounced-value.test.ts +105 -10
- package/src/hooks/use-debounce-value/use-debounced-value.ts +19 -10
- package/src/hooks/use-disclosure/use-disclosure.stories.tsx +305 -14
- package/src/hooks/use-disclosure/use-disclosure.test.ts +198 -50
- package/src/hooks/use-disclosure/use-disclosure.ts +49 -29
- package/src/hooks/use-document-title/use-document-title.stories.tsx +54 -0
- package/src/hooks/use-document-title/use-document-title.test.tsx +26 -0
- package/src/hooks/use-document-title/{use-document-title.tsx → use-document-title.ts} +17 -3
- package/src/hooks/use-event-listener/use-event-listener.stories.tsx +105 -9
- package/src/hooks/use-event-listener/use-event-listener.test.tsx +77 -10
- package/src/hooks/use-event-listener/use-event-listener.ts +71 -11
- package/src/hooks/use-focus-trap/use-focus-trap.test.ts +31 -6
- package/src/hooks/use-focus-trap/use-focus-trap.ts +3 -2
- package/src/hooks/use-hotkey/index.ts +9 -1
- package/src/hooks/use-hotkey/use-hotkey.stories.tsx +279 -74
- package/src/hooks/use-hotkey/use-hotkey.test.tsx +286 -34
- package/src/hooks/use-hotkey/use-hotkey.ts +141 -17
- package/src/hooks/use-hotkey/utils/index.ts +8 -3
- package/src/hooks/use-hotkey/utils/is-input-field.test.ts +78 -0
- package/src/hooks/use-hotkey/utils/is-input-field.ts +31 -10
- package/src/hooks/use-hotkey/utils/match-and-run.test.ts +203 -0
- package/src/hooks/use-hotkey/utils/match-and-run.ts +62 -0
- package/src/hooks/use-hotkey/utils/match-key-modifiers.test.ts +65 -0
- package/src/hooks/use-hotkey/utils/match-key-modifiers.ts +39 -12
- package/src/hooks/use-hover/use-hover.stories.tsx +258 -80
- package/src/hooks/use-hover/use-hover.test.tsx +266 -26
- package/src/hooks/use-hover/use-hover.tsx +93 -28
- package/src/hooks/use-is-visible/use-is-visible.stories.tsx +193 -46
- package/src/hooks/use-is-visible/use-is-visible.test.tsx +235 -7
- package/src/hooks/use-is-visible/use-is-visible.ts +114 -0
- package/src/hooks/use-local-storage/use-local-storage.stories.tsx +129 -29
- package/src/hooks/use-local-storage/use-local-storage.test.ts +106 -41
- package/src/hooks/use-local-storage/use-local-storage.ts +100 -31
- package/src/hooks/use-media-query/use-media-query.stories.tsx +86 -26
- package/src/hooks/use-media-query/use-media-query.test.ts +132 -0
- package/src/hooks/use-media-query/use-media-query.ts +39 -14
- package/src/hooks/use-memoized-fn/use-memoized-fn.ts +0 -1
- package/src/hooks/use-mutation/use-mutation.stories.tsx +260 -94
- package/src/hooks/use-mutation/use-mutation.test.ts +359 -0
- package/src/hooks/use-mutation/use-mutation.ts +97 -0
- package/src/hooks/use-object/use-object.stories.tsx +310 -79
- package/src/hooks/use-object/use-object.test.tsx +235 -56
- package/src/hooks/use-object/use-object.ts +59 -0
- package/src/hooks/use-pagination/use-pagination.tsx +0 -1
- package/src/hooks/use-prevent-page-close/index.ts +1 -0
- package/src/hooks/use-prevent-page-close/use-prevent-page-close.stories.tsx +39 -0
- package/src/hooks/use-prevent-page-close/use-prevent-page-close.test.ts +89 -0
- package/src/hooks/use-prevent-page-close/use-prevent-page-close.ts +27 -0
- package/src/hooks/use-range-pagination/use-range-pagination.test.tsx +1 -1
- package/src/hooks/use-range-pagination/use-range-pagination.tsx +1 -1
- package/src/hooks/use-selection/use-selection.ts +0 -1
- package/src/hooks/use-step/use-step.stories.tsx +178 -65
- package/src/hooks/use-step/use-step.test.ts +178 -53
- package/src/hooks/use-step/use-step.ts +57 -49
- package/src/utils/form.test.tsx +13 -8
- package/src/utils/form.tsx +10 -0
- package/src/utils/functions/getFormData.test.ts +1 -1
- package/dist/hooks/use-hotkey/utils/create-hotkey-listener.cjs.js +0 -1
- package/dist/hooks/use-hotkey/utils/create-hotkey-listener.es.js +0 -10
- package/dist/hooks/use-prevent-close-window/use-prevent-close-window.cjs.js +0 -1
- package/dist/hooks/use-prevent-close-window/use-prevent-close-window.es.js +0 -15
- package/dist/hooks/use-toggle/use-toggle.cjs.js +0 -1
- package/dist/hooks/use-toggle/use-toggle.es.js +0 -10
- package/dist/src/hooks/use-hotkey/utils/create-hotkey-listener.d.ts +0 -1
- package/dist/src/hooks/use-prevent-close-window/index.d.ts +0 -1
- package/dist/src/hooks/use-prevent-close-window/use-prevent-close-window.d.ts +0 -13
- package/dist/src/hooks/use-toggle/index.d.ts +0 -1
- package/dist/src/hooks/use-toggle/use-toggle.d.ts +0 -3
- package/src/hooks/use-async/use-async.test.tsx +0 -68
- package/src/hooks/use-hotkey/utils/create-hotkey-listener.ts +0 -25
- package/src/hooks/use-is-visible/use-is-visible.tsx +0 -49
- package/src/hooks/use-mutation/use-mutation.test.tsx +0 -83
- package/src/hooks/use-mutation/use-mutation.tsx +0 -59
- package/src/hooks/use-object/use-object.tsx +0 -46
- package/src/hooks/use-prevent-close-window/index.ts +0 -1
- package/src/hooks/use-prevent-close-window/use-prevent-close-window.stories.tsx +0 -32
- package/src/hooks/use-prevent-close-window/use-prevent-close-window.test.ts +0 -79
- package/src/hooks/use-prevent-close-window/use-prevent-close-window.ts +0 -33
- package/src/hooks/use-toggle/index.ts +0 -1
- package/src/hooks/use-toggle/use-toggle.stories.tsx +0 -25
- package/src/hooks/use-toggle/use-toggle.test.tsx +0 -64
- package/src/hooks/use-toggle/use-toggle.ts +0 -14
- /package/dist/src/{hooks/use-prevent-close-window/use-prevent-close-window.test.d.ts → components/date-picker/date-input-form.test.d.ts} +0 -0
- /package/dist/src/hooks/{use-toggle/use-toggle.test.d.ts → internal/is-apple-device.test.d.ts} +0 -0
|
@@ -1,111 +1,277 @@
|
|
|
1
|
-
import { Meta } from "@storybook/react-vite";
|
|
2
|
-
import {
|
|
3
|
-
import { Button
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { Button } from "../../components";
|
|
4
|
+
import { type UseMutationOptions, useMutation } from "./use-mutation";
|
|
5
|
+
|
|
6
|
+
// ─── Demo component ──────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
interface DemoVariables {
|
|
9
|
+
value: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface DemoData {
|
|
13
|
+
message: string;
|
|
14
|
+
timestamp: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type MutationDemoProps = UseMutationOptions<DemoVariables, DemoData> & {
|
|
18
|
+
/** Simulated async delay in ms */
|
|
19
|
+
delay?: number;
|
|
20
|
+
/** When true the mutation will reject with an error */
|
|
21
|
+
shouldFail?: boolean;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const MutationDemo = ({
|
|
25
|
+
delay = 800,
|
|
26
|
+
shouldFail = false,
|
|
27
|
+
onMutate,
|
|
28
|
+
onSuccess,
|
|
29
|
+
onError,
|
|
30
|
+
onSettled,
|
|
31
|
+
}: MutationDemoProps) => {
|
|
32
|
+
const [log, setLog] = useState<string[]>([]);
|
|
33
|
+
|
|
34
|
+
const addLog = (msg: string) =>
|
|
35
|
+
setLog((prev) => [
|
|
36
|
+
`[${new Date().toISOString().slice(11, 23)}] ${msg}`,
|
|
37
|
+
...prev,
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
const mutation = useMutation<DemoVariables, DemoData>({
|
|
41
|
+
fn: async (vars) => {
|
|
42
|
+
await new Promise((res, rej) =>
|
|
43
|
+
setTimeout(
|
|
44
|
+
() =>
|
|
45
|
+
shouldFail ? rej(new Error("Simulated failure")) : res(undefined),
|
|
46
|
+
delay,
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
return { message: `Created: ${vars.value}`, timestamp: Date.now() };
|
|
50
|
+
},
|
|
51
|
+
onMutate: (vars) => {
|
|
52
|
+
addLog(`onMutate — vars: ${JSON.stringify(vars)}`);
|
|
53
|
+
onMutate?.(vars);
|
|
54
|
+
},
|
|
55
|
+
onSuccess: (data, vars) => {
|
|
56
|
+
addLog(`onSuccess — data: ${data.message}`);
|
|
57
|
+
onSuccess?.(data, vars);
|
|
58
|
+
},
|
|
59
|
+
onError: (err, vars) => {
|
|
60
|
+
addLog(`onError — ${err.message}`);
|
|
61
|
+
onError?.(err, vars);
|
|
62
|
+
},
|
|
63
|
+
onSettled: (data, err, vars) => {
|
|
64
|
+
addLog(
|
|
65
|
+
`onSettled — data: ${data?.message ?? "null"}, error: ${err?.message ?? "null"}`,
|
|
66
|
+
);
|
|
67
|
+
onSettled?.(data, err, vars);
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div className="flex flex-col gap-4 max-w-sm">
|
|
73
|
+
<div className="flex gap-2">
|
|
74
|
+
<Button
|
|
75
|
+
onClick={() => mutation.mutate({ value: "item-1" })}
|
|
76
|
+
disabled={mutation.isPending}
|
|
77
|
+
>
|
|
78
|
+
{mutation.isPending ? "Running…" : "Mutate"}
|
|
79
|
+
</Button>
|
|
80
|
+
<Button
|
|
81
|
+
variant="outline"
|
|
82
|
+
onClick={() => mutation.reset()}
|
|
83
|
+
disabled={mutation.isPending}
|
|
84
|
+
>
|
|
85
|
+
Reset
|
|
86
|
+
</Button>
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
<pre className="rounded-md bg-slate-950 p-3 text-xs text-white leading-relaxed">
|
|
90
|
+
{JSON.stringify(
|
|
91
|
+
{
|
|
92
|
+
status: mutation.status,
|
|
93
|
+
data: mutation.data,
|
|
94
|
+
error: mutation.error?.message ?? null,
|
|
95
|
+
},
|
|
96
|
+
null,
|
|
97
|
+
2,
|
|
98
|
+
)}
|
|
99
|
+
</pre>
|
|
100
|
+
|
|
101
|
+
{log.length > 0 && (
|
|
102
|
+
<div className="rounded-md border bg-muted p-3 text-xs font-mono space-y-0.5">
|
|
103
|
+
{log.map((entry, i) => (
|
|
104
|
+
<div key={i}>{entry}</div>
|
|
105
|
+
))}
|
|
106
|
+
</div>
|
|
107
|
+
)}
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// ─── Meta ─────────────────────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
const meta: Meta<typeof MutationDemo> = {
|
|
25
115
|
title: "hooks/useMutation",
|
|
116
|
+
component: MutationDemo,
|
|
26
117
|
parameters: {
|
|
27
118
|
docs: {
|
|
28
|
-
|
|
119
|
+
description: {
|
|
120
|
+
component: `
|
|
121
|
+
\`useMutation\` manages async mutation lifecycle with stable callback identity.
|
|
122
|
+
|
|
123
|
+
**API summary**
|
|
124
|
+
|
|
125
|
+
\`\`\`ts
|
|
126
|
+
const { mutate, mutateAsync, reset, status, data, error, isPending, isSuccess, isError, isIdle } =
|
|
127
|
+
useMutation({ fn, onMutate?, onSuccess?, onError?, onSettled? });
|
|
128
|
+
\`\`\`
|
|
129
|
+
|
|
130
|
+
**Lifecycle order (success):** \`onMutate\` → \`fn\` → \`onSuccess\` → \`onSettled\`
|
|
131
|
+
|
|
132
|
+
**Lifecycle order (error):** \`onMutate\` → \`fn\` throws → \`onError\` → \`onSettled\`
|
|
133
|
+
|
|
134
|
+
**Key guarantees**
|
|
135
|
+
- \`mutate\` is fire-and-forget: returns \`void\`, never throws.
|
|
136
|
+
- \`mutateAsync\` returns a \`Promise\` that re-throws on failure.
|
|
137
|
+
- \`onSettled\` is always called — even if \`onError\` throws.
|
|
138
|
+
- All callbacks read through \`useLatestRef\`, so inline functions never recreate \`mutate\`.
|
|
139
|
+
`.trim(),
|
|
140
|
+
},
|
|
29
141
|
},
|
|
30
142
|
},
|
|
143
|
+
argTypes: {
|
|
144
|
+
delay: {
|
|
145
|
+
control: { type: "range", min: 0, max: 3000, step: 100 },
|
|
146
|
+
description:
|
|
147
|
+
"Simulated async delay in ms — demo control only, not a hook option.",
|
|
148
|
+
table: { category: "Demo controls" },
|
|
149
|
+
},
|
|
150
|
+
shouldFail: {
|
|
151
|
+
control: "boolean",
|
|
152
|
+
description:
|
|
153
|
+
"When true the demo's fn rejects — simulates an API error. Demo control only, not a hook option.",
|
|
154
|
+
table: { category: "Demo controls" },
|
|
155
|
+
},
|
|
156
|
+
onMutate: {
|
|
157
|
+
action: "onMutate",
|
|
158
|
+
description:
|
|
159
|
+
"Called synchronously with `variables` before `fn` is awaited. Use for optimistic updates.",
|
|
160
|
+
table: {
|
|
161
|
+
category: "Hook options",
|
|
162
|
+
type: { summary: "(variables: TVariables) => void" },
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
onSuccess: {
|
|
166
|
+
action: "onSuccess",
|
|
167
|
+
description:
|
|
168
|
+
"Called with `(data, variables)` after `fn` resolves successfully.",
|
|
169
|
+
table: {
|
|
170
|
+
category: "Hook options",
|
|
171
|
+
type: { summary: "(data: TData, variables: TVariables) => void" },
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
onError: {
|
|
175
|
+
action: "onError",
|
|
176
|
+
description: "Called with `(error, variables)` when `fn` rejects.",
|
|
177
|
+
table: {
|
|
178
|
+
category: "Hook options",
|
|
179
|
+
type: { summary: "(error: Error, variables: TVariables) => void" },
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
onSettled: {
|
|
183
|
+
action: "onSettled",
|
|
184
|
+
description:
|
|
185
|
+
"Always called after `onSuccess` or `onError`, even if one of them throws.",
|
|
186
|
+
table: {
|
|
187
|
+
category: "Hook options",
|
|
188
|
+
type: {
|
|
189
|
+
summary:
|
|
190
|
+
"(data: TData | null, error: Error | null, variables: TVariables) => void",
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
render: (args) => <MutationDemo {...args} />,
|
|
31
196
|
};
|
|
32
197
|
|
|
33
198
|
export default meta;
|
|
199
|
+
type Story = StoryObj<typeof MutationDemo>;
|
|
34
200
|
|
|
35
|
-
|
|
36
|
-
userId: number;
|
|
37
|
-
id: number;
|
|
38
|
-
title: string;
|
|
39
|
-
completed: boolean;
|
|
40
|
-
}
|
|
201
|
+
// ─── Stories ──────────────────────────────────────────────────────────────────
|
|
41
202
|
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
203
|
+
export const Idle: Story = {
|
|
204
|
+
args: {
|
|
205
|
+
delay: 800,
|
|
206
|
+
shouldFail: false,
|
|
207
|
+
},
|
|
208
|
+
parameters: {
|
|
209
|
+
docs: {
|
|
210
|
+
description: {
|
|
211
|
+
story:
|
|
212
|
+
"Default idle state before any mutation is triggered. All booleans are false except `isIdle`.",
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
},
|
|
48
216
|
};
|
|
49
217
|
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
},
|
|
62
|
-
}
|
|
63
|
-
.then((response) => response.json())
|
|
64
|
-
.then((json) => json);
|
|
218
|
+
export const Pending: Story = {
|
|
219
|
+
args: {
|
|
220
|
+
delay: 3000,
|
|
221
|
+
shouldFail: false,
|
|
222
|
+
},
|
|
223
|
+
parameters: {
|
|
224
|
+
docs: {
|
|
225
|
+
description: {
|
|
226
|
+
story:
|
|
227
|
+
"Click **Mutate** to see `isPending=true` while the operation runs. The button disables until the promise settles.",
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
},
|
|
65
231
|
};
|
|
66
232
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
233
|
+
export const Success: Story = {
|
|
234
|
+
args: {
|
|
235
|
+
delay: 600,
|
|
236
|
+
shouldFail: false,
|
|
237
|
+
},
|
|
238
|
+
parameters: {
|
|
239
|
+
docs: {
|
|
240
|
+
description: {
|
|
241
|
+
story:
|
|
242
|
+
"After the mutation resolves, `status` becomes `'success'` and `data` is populated. `error` remains null.",
|
|
74
243
|
},
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
<Result loading={loading} data={data} error={error?.message} />
|
|
108
|
-
</div>
|
|
109
|
-
);
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export const ErrorState: Story = {
|
|
249
|
+
name: "Error",
|
|
250
|
+
args: {
|
|
251
|
+
delay: 600,
|
|
252
|
+
shouldFail: true,
|
|
253
|
+
},
|
|
254
|
+
parameters: {
|
|
255
|
+
docs: {
|
|
256
|
+
description: {
|
|
257
|
+
story:
|
|
258
|
+
"Toggle **shouldFail** in controls to simulate a failing API call. `status` becomes `'error'`, `error` is set, and `data` stays null. `mutate` swallows the rejection silently — use `mutateAsync` if you need to catch it.",
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
export const ResetAfterSuccess: Story = {
|
|
265
|
+
args: {
|
|
266
|
+
delay: 600,
|
|
267
|
+
shouldFail: false,
|
|
268
|
+
},
|
|
269
|
+
parameters: {
|
|
270
|
+
docs: {
|
|
271
|
+
description: {
|
|
272
|
+
story:
|
|
273
|
+
"After a successful mutation, click **Reset** to return to `idle` and clear `data`. `reset()` does not abort in-flight mutations — it only clears state.",
|
|
274
|
+
},
|
|
275
|
+
},
|
|
110
276
|
},
|
|
111
277
|
};
|