@boxcustodia/library 2.0.0-alpha.11 → 2.0.0-alpha.13
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.js +38 -38
- package/dist/index.css +1 -1
- package/dist/index.d.ts +28 -29
- package/dist/index.es.js +6804 -6792
- package/package.json +4 -3
- package/src/__doc__/Changelog.mdx +6 -0
- package/src/__doc__/Components.mdx +73 -0
- package/src/__doc__/Examples.tsx +69 -0
- package/src/__doc__/Icons.mdx +41 -0
- package/src/__doc__/Intro.mdx +138 -0
- package/src/__doc__/MCP.mdx +71 -0
- package/src/__doc__/Migration.mdx +451 -0
- package/src/__doc__/Theme.mdx +132 -0
- package/src/__doc__/Types.mdx +252 -0
- package/src/components/alert/alert.stories.tsx +142 -0
- package/src/components/alert/alert.tsx +109 -0
- package/src/components/alert/index.ts +7 -0
- package/src/components/alert-dialog/alert-dialog.stories.tsx +173 -0
- package/src/components/alert-dialog/alert-dialog.test.tsx +49 -0
- package/src/components/alert-dialog/alert-dialog.tsx +265 -0
- package/src/components/alert-dialog/index.ts +1 -0
- package/src/components/auto-complete/auto-complete-primitives.tsx +155 -0
- package/src/components/auto-complete/auto-complete.stories.tsx +241 -0
- package/src/components/auto-complete/auto-complete.tsx +82 -0
- package/src/components/auto-complete/index.ts +2 -0
- package/src/components/avatar/avatar.stories.tsx +84 -0
- package/src/components/avatar/avatar.test.tsx +61 -0
- package/src/components/avatar/avatar.tsx +104 -0
- package/src/components/avatar/index.ts +1 -0
- package/src/components/background-image/background-image.stories.tsx +21 -0
- package/src/components/background-image/background-image.test.tsx +29 -0
- package/src/components/background-image/background-image.tsx +23 -0
- package/src/components/background-image/index.ts +1 -0
- package/src/components/button/button.stories.tsx +396 -0
- package/src/components/button/button.test.tsx +58 -0
- package/src/components/button/button.tsx +31 -0
- package/src/components/button/button.variants.ts +44 -0
- package/src/components/button/components/base-button.tsx +86 -0
- package/src/components/button/components/loader-overlay.tsx +21 -0
- package/src/components/button/components/loading-icon.tsx +47 -0
- package/src/components/button/index.ts +3 -0
- package/src/components/calendar/calendar.model.ts +86 -0
- package/src/components/calendar/calendar.stories.tsx +155 -0
- package/src/components/calendar/calendar.test.tsx +12 -0
- package/src/components/calendar/calendar.tsx +185 -0
- package/src/components/calendar/components/calendar-navigation.tsx +141 -0
- package/src/components/calendar/components/day.tsx +61 -0
- package/src/components/calendar/components/decade-view.tsx +45 -0
- package/src/components/calendar/components/index.ts +6 -0
- package/src/components/calendar/components/month-view.tsx +58 -0
- package/src/components/calendar/components/week-days.tsx +27 -0
- package/src/components/calendar/components/year-view.tsx +29 -0
- package/src/components/calendar/hooks/index.ts +4 -0
- package/src/components/calendar/hooks/use-calendar-navigation.ts +79 -0
- package/src/components/calendar/hooks/use-calendar.ts +90 -0
- package/src/components/calendar/hooks/use-multiple-calendar.ts +34 -0
- package/src/components/calendar/hooks/use-range-calendar.ts +91 -0
- package/src/components/calendar/hooks/use-single-calendar.ts +18 -0
- package/src/components/calendar/index.ts +1 -0
- package/src/components/calendar/utils/typeguards.ts +7 -0
- package/src/components/card/card.stories.tsx +116 -0
- package/src/components/card/card.tsx +74 -0
- package/src/components/card/index.ts +1 -0
- package/src/components/center/center.stories.tsx +81 -0
- package/src/components/center/center.tsx +24 -0
- package/src/components/center/index.ts +1 -0
- package/src/components/checkbox/checkbox.stories.tsx +307 -0
- package/src/components/checkbox/checkbox.tsx +273 -0
- package/src/components/checkbox/index.ts +1 -0
- package/src/components/checkbox-group/checkbox-group.stories.tsx +104 -0
- package/src/components/checkbox-group/checkbox-group.tsx +16 -0
- package/src/components/checkbox-group/index.ts +1 -0
- package/src/components/combobox/combobox.stories.tsx +339 -0
- package/src/components/combobox/combobox.tsx +898 -0
- package/src/components/combobox/index.ts +1 -0
- package/src/components/date-picker/date-input.stories.tsx +158 -0
- package/src/components/date-picker/date-input.tsx +163 -0
- package/src/components/date-picker/date-picker.model.ts +90 -0
- package/src/components/date-picker/date-picker.stories.tsx +200 -0
- package/src/components/date-picker/date-picker.test.tsx +23 -0
- package/src/components/date-picker/date-picker.tsx +298 -0
- package/src/components/date-picker/date-picker.utils.ts +260 -0
- package/src/components/date-picker/index.ts +3 -0
- package/src/components/date-picker/use-date-input-popover.ts +48 -0
- package/src/components/date-picker/use-date-input.ts +125 -0
- package/src/components/dialog/dialog.stories.tsx +171 -0
- package/src/components/dialog/dialog.test.tsx +68 -0
- package/src/components/dialog/dialog.tsx +277 -0
- package/src/components/dialog/index.ts +1 -0
- package/src/components/divider/divider.stories.tsx +139 -0
- package/src/components/divider/divider.test.tsx +22 -0
- package/src/components/divider/divider.tsx +23 -0
- package/src/components/divider/index.ts +1 -0
- package/src/components/dropzone/dropzone.stories.tsx +210 -0
- package/src/components/dropzone/dropzone.tsx +154 -0
- package/src/components/dropzone/file-types.ts +64 -0
- package/src/components/dropzone/index.ts +3 -0
- package/src/components/dropzone/upload-primitives.tsx +310 -0
- package/src/components/dropzone/use-dropzone.ts +122 -0
- package/src/components/empty-state/empty-state.stories.tsx +56 -0
- package/src/components/empty-state/empty-state.tsx +39 -0
- package/src/components/empty-state/index.ts +1 -0
- package/src/components/field/field.stories.tsx +223 -0
- package/src/components/field/field.tsx +229 -0
- package/src/components/field/index.ts +1 -0
- package/src/components/form/form.stories.tsx +594 -0
- package/src/components/form/form.tsx +30 -0
- package/src/components/form/index.ts +1 -0
- package/src/components/heading/heading.stories.tsx +74 -0
- package/src/components/heading/heading.tsx +28 -0
- package/src/components/heading/heading.variants.ts +27 -0
- package/src/components/heading/index.ts +1 -0
- package/src/components/index.ts +46 -0
- package/src/components/input/index.ts +1 -0
- package/src/components/input/input.stories.tsx +104 -0
- package/src/components/input/input.tsx +75 -0
- package/src/components/kbd/index.ts +1 -0
- package/src/components/kbd/kbd.stories.tsx +40 -0
- package/src/components/kbd/kbd.tsx +31 -0
- package/src/components/kbd/kbd.variants.ts +26 -0
- package/src/components/label/index.ts +1 -0
- package/src/components/label/label.stories.tsx +68 -0
- package/src/components/label/label.test.tsx +61 -0
- package/src/components/label/label.tsx +62 -0
- package/src/components/loader/index.ts +1 -0
- package/src/components/loader/loader.stories.tsx +60 -0
- package/src/components/loader/loader.test.tsx +26 -0
- package/src/components/loader/loader.tsx +60 -0
- package/src/components/menu/index.ts +2 -0
- package/src/components/menu/menu-primitives.tsx +248 -0
- package/src/components/menu/menu.stories.tsx +203 -0
- package/src/components/menu/menu.tsx +100 -0
- package/src/components/menu/util/render-menu-item.tsx +54 -0
- package/src/components/multi-select/hooks/use-multi-select.ts +66 -0
- package/src/components/multi-select/index.ts +1 -0
- package/src/components/multi-select/multi-select.stories.tsx +294 -0
- package/src/components/multi-select/multi-select.tsx +300 -0
- package/src/components/multi-select/multi-select.variants.ts +22 -0
- package/src/components/number-input/index.ts +1 -0
- package/src/components/number-input/number-input.stories.tsx +209 -0
- package/src/components/number-input/number-input.test.tsx +87 -0
- package/src/components/number-input/number-input.tsx +232 -0
- package/src/components/pagination/components/pagination-option.tsx +27 -0
- package/src/components/pagination/index.ts +1 -0
- package/src/components/pagination/pagination.stories.tsx +80 -0
- package/src/components/pagination/pagination.test.tsx +76 -0
- package/src/components/pagination/pagination.tsx +102 -0
- package/src/components/password/index.ts +1 -0
- package/src/components/password/password.stories.tsx +104 -0
- package/src/components/password/password.tsx +75 -0
- package/src/components/popover/index.ts +1 -0
- package/src/components/popover/popover.stories.tsx +213 -0
- package/src/components/popover/popover.tsx +203 -0
- package/src/components/progress/index.ts +1 -0
- package/src/components/progress/progress.stories.tsx +124 -0
- package/src/components/progress/progress.test.tsx +25 -0
- package/src/components/progress/progress.tsx +124 -0
- package/src/components/scroll-area/index.ts +1 -0
- package/src/components/scroll-area/scroll-area.stories.tsx +166 -0
- package/src/components/scroll-area/scroll-area.tsx +64 -0
- package/src/components/select/index.ts +1 -0
- package/src/components/select/select.stories.tsx +253 -0
- package/src/components/select/select.tsx +430 -0
- package/src/components/show/index.ts +1 -0
- package/src/components/show/show.stories.tsx +197 -0
- package/src/components/show/show.test.tsx +41 -0
- package/src/components/show/show.tsx +16 -0
- package/src/components/skeleton/index.ts +1 -0
- package/src/components/skeleton/skeleton.stories.tsx +36 -0
- package/src/components/skeleton/skeleton.test.tsx +14 -0
- package/src/components/skeleton/skeleton.tsx +15 -0
- package/src/components/stack/index.ts +1 -0
- package/src/components/stack/stack.stories.tsx +194 -0
- package/src/components/stack/stack.tsx +52 -0
- package/src/components/stepper/Stepper.tsx +190 -0
- package/src/components/stepper/context/stepper-context.tsx +11 -0
- package/src/components/stepper/index.ts +1 -0
- package/src/components/stepper/stepper.stories.tsx +130 -0
- package/src/components/stepper/stepper.test.tsx +91 -0
- package/src/components/switch/index.ts +1 -0
- package/src/components/switch/switch.stories.tsx +122 -0
- package/src/components/switch/switch.test.tsx +30 -0
- package/src/components/switch/switch.tsx +86 -0
- package/src/components/table/index.ts +3 -0
- package/src/components/table/table-primitives.tsx +122 -0
- package/src/components/table/table.model.ts +20 -0
- package/src/components/table/table.stories.tsx +169 -0
- package/src/components/table/table.test.tsx +91 -0
- package/src/components/table/table.tsx +109 -0
- package/src/components/table-pagination/index.ts +2 -0
- package/src/components/table-pagination/table-pagination.model.ts +2 -0
- package/src/components/table-pagination/table-pagination.stories.tsx +23 -0
- package/src/components/table-pagination/table-pagination.test.tsx +32 -0
- package/src/components/table-pagination/table-pagination.tsx +108 -0
- package/src/components/tabs/context/tabs-context.tsx +14 -0
- package/src/components/tabs/index.ts +1 -0
- package/src/components/tabs/tabs.stories.tsx +182 -0
- package/src/components/tabs/tabs.test.tsx +61 -0
- package/src/components/tabs/tabs.tsx +175 -0
- package/src/components/tag/index.ts +2 -0
- package/src/components/tag/tag.stories.tsx +170 -0
- package/src/components/tag/tag.test.tsx +18 -0
- package/src/components/tag/tag.tsx +99 -0
- package/src/components/tag/tag.variants.ts +31 -0
- package/src/components/textarea/index.ts +1 -0
- package/src/components/textarea/textarea.stories.tsx +73 -0
- package/src/components/textarea/textarea.tsx +105 -0
- package/src/components/timeline/index.ts +1 -0
- package/src/components/timeline/timeline-status.ts +5 -0
- package/src/components/timeline/timeline.stories.tsx +84 -0
- package/src/components/timeline/timeline.tsx +147 -0
- package/src/components/toast/index.ts +1 -0
- package/src/components/toast/toast.stories.tsx +392 -0
- package/src/components/toast/toast.test.tsx +50 -0
- package/src/components/toast/toast.tsx +411 -0
- package/src/components/tooltip/index.ts +1 -0
- package/src/components/tooltip/tooltip.stories.tsx +226 -0
- package/src/components/tooltip/tooltip.test.tsx +46 -0
- package/src/components/tooltip/tooltip.tsx +171 -0
- package/src/components/tree/hooks/use-controllable-tree-state.ts +80 -0
- package/src/components/tree/index.ts +2 -0
- package/src/components/tree/tree-primitives.tsx +126 -0
- package/src/components/tree/tree.stories.tsx +468 -0
- package/src/components/tree/tree.tsx +42 -0
- package/src/hooks/index.ts +26 -0
- package/src/hooks/useArray/__doc__/useArray.stories.tsx +100 -0
- package/src/hooks/useArray/__test__/useArray.test.tsx +88 -0
- package/src/hooks/useArray/index.ts +1 -0
- package/src/hooks/useArray/useArray.ts +76 -0
- package/src/hooks/useAsync/__doc__/useAsync.stories.tsx +149 -0
- package/src/hooks/useAsync/__test__/useAsync.test.tsx +68 -0
- package/src/hooks/useAsync/index.ts +1 -0
- package/src/hooks/useAsync/useAsync.ts +58 -0
- package/src/hooks/useClickOutside/__doc__/useClickOutside.stories.tsx +40 -0
- package/src/hooks/useClickOutside/__test__/useClickOutside.test.tsx +33 -0
- package/src/hooks/useClickOutside/index.ts +1 -0
- package/src/hooks/useClickOutside/useClickOutside.ts +26 -0
- package/src/hooks/useClipboard/__doc__/useClipboard.stories.tsx +45 -0
- package/src/hooks/useClipboard/__test__/useClipboard.test.tsx +19 -0
- package/src/hooks/useClipboard/index.ts +1 -0
- package/src/hooks/useClipboard/useClipboard.tsx +28 -0
- package/src/hooks/useDebounceCallback/__doc__/useDebouncedCallback.stories.tsx +84 -0
- package/src/hooks/useDebounceCallback/index.ts +1 -0
- package/src/hooks/useDebounceCallback/useDebouncedCallback.ts +23 -0
- package/src/hooks/useDebounceValue/__doc__/useDebouncedValue.stories.tsx +75 -0
- package/src/hooks/useDebounceValue/index.ts +1 -0
- package/src/hooks/useDebounceValue/useDebouncedValue.ts +17 -0
- package/src/hooks/useDisclosure/__doc__/useDisclosure.stories.tsx +39 -0
- package/src/hooks/useDisclosure/__test__/useDisclosure.test.ts +43 -0
- package/src/hooks/useDisclosure/index.ts +1 -0
- package/src/hooks/useDisclosure/useDisclosure.ts +37 -0
- package/src/hooks/useDocumentTitle/__doc__/useDocumentTitle.stories.tsx +26 -0
- package/src/hooks/useDocumentTitle/index.ts +1 -0
- package/src/hooks/useDocumentTitle/useDocumentTitle.tsx +11 -0
- package/src/hooks/useEventListener/__doc__/useEventListener.stories.tsx +28 -0
- package/src/hooks/useEventListener/__test__/useEventListener.test.tsx +26 -0
- package/src/hooks/useEventListener/index.ts +1 -0
- package/src/hooks/useEventListener/useEventListener.ts +25 -0
- package/src/hooks/useFocusTrap/__doc__/useFocusTrap.stories.tsx +37 -0
- package/src/hooks/useFocusTrap/index.ts +1 -0
- package/src/hooks/useFocusTrap/scopeTab.ts +38 -0
- package/src/hooks/useFocusTrap/tabbable.ts +70 -0
- package/src/hooks/useFocusTrap/useFocusTrap.ts +78 -0
- package/src/hooks/useHotkey/__docs__/useHotkey.stories.tsx +116 -0
- package/src/hooks/useHotkey/__test__/useHotkey.test.tsx +105 -0
- package/src/hooks/useHotkey/__utils__/create-hotkey-listener.ts +25 -0
- package/src/hooks/useHotkey/__utils__/index.ts +3 -0
- package/src/hooks/useHotkey/__utils__/is-input-field.ts +14 -0
- package/src/hooks/useHotkey/__utils__/match-key-modifiers.ts +25 -0
- package/src/hooks/useHotkey/index.ts +1 -0
- package/src/hooks/useHotkey/useHotkey.ts +34 -0
- package/src/hooks/useHover/__doc__/useHover.stories.tsx +41 -0
- package/src/hooks/useHover/__test__/useHover.test.tsx +45 -0
- package/src/hooks/useHover/index.ts +1 -0
- package/src/hooks/useHover/useHover.tsx +40 -0
- package/src/hooks/useIsVisible/__doc__/useIsVisible.stories.tsx +60 -0
- package/src/hooks/useIsVisible/index.ts +1 -0
- package/src/hooks/useIsVisible/useIsVisible.tsx +50 -0
- package/src/hooks/useLocalStorage/__doc__/useLocalStorage.stories.tsx +86 -0
- package/src/hooks/useLocalStorage/__test__/useLocalStorage.test.ts +85 -0
- package/src/hooks/useLocalStorage/index.ts +1 -0
- package/src/hooks/useLocalStorage/useLocalStorage.ts +57 -0
- package/src/hooks/useMediaQuery/__doc__/useMediaQuery.stories.tsx +39 -0
- package/src/hooks/useMediaQuery/index.ts +1 -0
- package/src/hooks/useMediaQuery/useMediaQuery.ts +22 -0
- package/src/hooks/useMemoizedFn/index.ts +1 -0
- package/src/hooks/useMemoizedFn/useMemoizedFn.ts +32 -0
- package/src/hooks/useMutation/__doc__/useMutation.stories.tsx +111 -0
- package/src/hooks/useMutation/__test__/useMutation.test.tsx +83 -0
- package/src/hooks/useMutation/index.ts +1 -0
- package/src/hooks/useMutation/useMutation.tsx +60 -0
- package/src/hooks/useObject/__doc__/useObject.stories.tsx +119 -0
- package/src/hooks/useObject/__test__/useObject.test.tsx +87 -0
- package/src/hooks/useObject/index.ts +1 -0
- package/src/hooks/useObject/useObject.tsx +48 -0
- package/src/hooks/usePagination/__doc__/usePagination.stories.tsx +72 -0
- package/src/hooks/usePagination/__test__/usePagination.test.tsx +98 -0
- package/src/hooks/usePagination/index.ts +2 -0
- package/src/hooks/usePagination/usePagination.tsx +74 -0
- package/src/hooks/usePortal/__doc__/usePortal.stories.tsx +19 -0
- package/src/hooks/usePortal/__test__/usePortal.test.tsx +20 -0
- package/src/hooks/usePortal/index.ts +1 -0
- package/src/hooks/usePortal/usePortal.ts +40 -0
- package/src/hooks/usePreventCloseWindow/__doc__/usePreventCloseWindow.stories.tsx +32 -0
- package/src/hooks/usePreventCloseWindow/index.ts +1 -0
- package/src/hooks/usePreventCloseWindow/usePreventCloseWindow.ts +33 -0
- package/src/hooks/useRangePagination/__test__/useRangePagination.test.tsx +63 -0
- package/src/hooks/useRangePagination/index.ts +2 -0
- package/src/hooks/useRangePagination/useRangePagination.tsx +72 -0
- package/src/hooks/useSelection/__doc__/useSelection.stories.tsx +140 -0
- package/src/hooks/useSelection/__test__/useSelection.test.tsx +57 -0
- package/src/hooks/useSelection/index.ts +1 -0
- package/src/hooks/useSelection/useSelection.ts +121 -0
- package/src/hooks/useStep/__doc__/useStep.stories.tsx +98 -0
- package/src/hooks/useStep/__test__/useStep.test.ts +51 -0
- package/src/hooks/useStep/index.ts +1 -0
- package/src/hooks/useStep/useStep.ts +57 -0
- package/src/hooks/useToggle/__doc__/useToggle.stories.tsx +25 -0
- package/src/hooks/useToggle/__test__/useToggle.test.tsx +43 -0
- package/src/hooks/useToggle/index.ts +1 -0
- package/src/hooks/useToggle/useToggle.ts +16 -0
- package/src/index.ts +6 -0
- package/src/lib/cn.ts +8 -0
- package/src/lib/index.ts +1 -0
- package/src/models/Generic.model.ts +67 -0
- package/src/models/index.ts +1 -0
- package/src/providers/index.ts +2 -0
- package/src/providers/library-provider.tsx +44 -0
- package/src/providers/theme/ThemeProvider.tsx +25 -0
- package/src/providers/theme/index.ts +3 -0
- package/src/providers/theme/types.ts +11 -0
- package/src/providers/theme/useThemeProps.ts +25 -0
- package/src/stores/theme.store.ts +31 -0
- package/src/styles/components.css +4 -0
- package/src/styles/index.css +2 -0
- package/src/styles/library.css +2 -0
- package/src/styles/theme.css +232 -0
- package/src/utils/dates/parseDateRange.utility.ts +39 -0
- package/src/utils/form.tsx +91 -0
- package/src/utils/functions/createSafeContext.ts +17 -0
- package/src/utils/functions/ensureReactElement.tsx +30 -0
- package/src/utils/functions/getFormData.ts +19 -0
- package/src/utils/functions/index.ts +4 -0
- package/src/utils/functions/mergeRefs.ts +18 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/strings/extractInitials.utility.ts +10 -0
- package/src/utils/strings/index.ts +1 -0
- package/src/utils/tests/click.ts +3 -0
- package/src/utils/tests/index.ts +2 -0
- package/src/utils/tests/keyboard.ts +21 -0
- package/src/utils/tests/type.ts +6 -0
- package/dist/components.css +0 -2
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import useArray from "../useArray";
|
|
4
|
+
|
|
5
|
+
describe("useArray hook", () => {
|
|
6
|
+
it("should initialize with default value", () => {
|
|
7
|
+
const defaultList = ["First", "Second", "Third"];
|
|
8
|
+
const { result } = renderHook(() => useArray(defaultList));
|
|
9
|
+
const [list] = result.current;
|
|
10
|
+
|
|
11
|
+
expect(list).toEqual(defaultList);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should insert item at specified index", () => {
|
|
15
|
+
const initialList = ["First", "Second", "Third"];
|
|
16
|
+
const { result } = renderHook(() => useArray(initialList));
|
|
17
|
+
const [, { insertAt }] = result.current;
|
|
18
|
+
|
|
19
|
+
act(() => insertAt(1, "Inserted"));
|
|
20
|
+
|
|
21
|
+
expect(result.current[0]).toEqual(["First", "Inserted", "Second", "Third"]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should remove item at specified index", () => {
|
|
25
|
+
const initialList = ["First", "Second", "Third"];
|
|
26
|
+
const { result } = renderHook(() => useArray(initialList));
|
|
27
|
+
const [, { removeAt }] = result.current;
|
|
28
|
+
|
|
29
|
+
act(() => removeAt(1));
|
|
30
|
+
|
|
31
|
+
expect(result.current[0]).toEqual(["First", "Third"]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should update item at specified index", () => {
|
|
35
|
+
const initialList = ["First", "Second", "Third"];
|
|
36
|
+
const { result } = renderHook(() => useArray(initialList));
|
|
37
|
+
const [, { updateAt }] = result.current;
|
|
38
|
+
|
|
39
|
+
act(() => updateAt(1, "Updated"));
|
|
40
|
+
|
|
41
|
+
expect(result.current[0]).toEqual(["First", "Updated", "Third"]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should clear the list", () => {
|
|
45
|
+
const initialList = ["First", "Second", "Third"];
|
|
46
|
+
const { result } = renderHook(() => useArray(initialList));
|
|
47
|
+
const [, { clear }] = result.current;
|
|
48
|
+
|
|
49
|
+
act(() => clear());
|
|
50
|
+
|
|
51
|
+
expect(result.current[0]).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should reset to default value", () => {
|
|
55
|
+
const defaultList = ["First", "Second", "Third"];
|
|
56
|
+
const newList = ["New", "List"];
|
|
57
|
+
const { result } = renderHook((props) => useArray(props), {
|
|
58
|
+
initialProps: defaultList,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const [, { reset, push }] = result.current;
|
|
62
|
+
|
|
63
|
+
act(() => push(newList[0]));
|
|
64
|
+
act(() => reset());
|
|
65
|
+
|
|
66
|
+
expect(result.current[0]).toEqual(defaultList);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should push item to end of list", () => {
|
|
70
|
+
const initialList = ["First", "Second"];
|
|
71
|
+
const { result } = renderHook(() => useArray(initialList));
|
|
72
|
+
const [, { push }] = result.current;
|
|
73
|
+
|
|
74
|
+
act(() => push("New"));
|
|
75
|
+
|
|
76
|
+
expect(result.current[0]).toEqual(["First", "Second", "New"]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should update item based on predicate", () => {
|
|
80
|
+
const initialList = ["First", "Second", "Third"];
|
|
81
|
+
const { result } = renderHook(() => useArray(initialList));
|
|
82
|
+
const [, { update }] = result.current;
|
|
83
|
+
|
|
84
|
+
act(() => update((item) => item === "Second", "Updated"));
|
|
85
|
+
|
|
86
|
+
expect(result.current[0]).toEqual(["First", "Updated", "Third"]);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useArray } from "./useArray";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { findIndex, isFunction, ListIterateeCustom } from "lodash";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
|
|
4
|
+
function useArray<T>(defaultProp: T[]) {
|
|
5
|
+
const [list = [], setList] = useState(defaultProp);
|
|
6
|
+
|
|
7
|
+
const insertAt = (index: number, item: T) => {
|
|
8
|
+
setList((prevList = []) => {
|
|
9
|
+
const newList = [...prevList];
|
|
10
|
+
newList.splice(index, 0, item);
|
|
11
|
+
return newList;
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const removeAt = (index: number) => {
|
|
16
|
+
setList((prevList = []) => {
|
|
17
|
+
const newList = [...prevList];
|
|
18
|
+
newList.splice(index, 1);
|
|
19
|
+
return newList;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const updateAt = (index: number, newItem: T) => {
|
|
24
|
+
setList((prevList = []) => {
|
|
25
|
+
const newList = [...prevList];
|
|
26
|
+
newList[index] = newItem;
|
|
27
|
+
return newList;
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const clear = () => setList([]);
|
|
32
|
+
|
|
33
|
+
const reset = () => setList(defaultProp);
|
|
34
|
+
|
|
35
|
+
const push = (item: T) => setList((prevList = []) => [...prevList, item]);
|
|
36
|
+
|
|
37
|
+
const set = (newList: T[]) => setList(newList);
|
|
38
|
+
|
|
39
|
+
const pop = () => {
|
|
40
|
+
setList((prevList = []) => {
|
|
41
|
+
const newList = [...prevList];
|
|
42
|
+
newList.pop();
|
|
43
|
+
return newList;
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const update = (
|
|
48
|
+
predicate: ListIterateeCustom<T, boolean>,
|
|
49
|
+
newItem: T | ((item: T) => T),
|
|
50
|
+
) => {
|
|
51
|
+
setList((prevList = []) => {
|
|
52
|
+
const newList = [...prevList];
|
|
53
|
+
const index = findIndex(newList, predicate);
|
|
54
|
+
if (index === -1) return newList;
|
|
55
|
+
newList[index] = isFunction(newItem) ? newItem(newList[index]) : newItem;
|
|
56
|
+
return newList;
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return [
|
|
61
|
+
list,
|
|
62
|
+
{
|
|
63
|
+
insertAt,
|
|
64
|
+
removeAt,
|
|
65
|
+
updateAt,
|
|
66
|
+
clear,
|
|
67
|
+
reset,
|
|
68
|
+
push,
|
|
69
|
+
set,
|
|
70
|
+
pop,
|
|
71
|
+
update,
|
|
72
|
+
},
|
|
73
|
+
] as const;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default useArray;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { isUndefined } from "lodash";
|
|
3
|
+
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { createToastManager, ToastProvider } from "@/components/toast";
|
|
6
|
+
import { Button, Input } from "../../../components";
|
|
7
|
+
import useDebouncedValue from "../../useDebounceValue/useDebouncedValue";
|
|
8
|
+
import useAsync from "../useAsync";
|
|
9
|
+
|
|
10
|
+
const toastManager = createToastManager();
|
|
11
|
+
|
|
12
|
+
const meta: Meta = {
|
|
13
|
+
title: "hooks/useAsync",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default meta;
|
|
17
|
+
type Story = StoryObj;
|
|
18
|
+
|
|
19
|
+
const fetchPokemon = async (name: string) => {
|
|
20
|
+
if (!name) throw new Error("Name is required");
|
|
21
|
+
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`);
|
|
22
|
+
if (!response.ok) throw new Error("Pokemon not found");
|
|
23
|
+
return response.json();
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const Basic = {
|
|
27
|
+
render: () => {
|
|
28
|
+
const [pokemonName, setPokemonName] = useState<string>("pikachu");
|
|
29
|
+
const debouncedName = useDebouncedValue(pokemonName, 600);
|
|
30
|
+
const { loading, error, data } = useAsync({
|
|
31
|
+
fn: () => fetchPokemon(debouncedName),
|
|
32
|
+
dependencies: [debouncedName],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div className="space-y-4">
|
|
37
|
+
<Input
|
|
38
|
+
type="text"
|
|
39
|
+
value={pokemonName}
|
|
40
|
+
onChange={setPokemonName}
|
|
41
|
+
placeholder="Enter Pokémon name"
|
|
42
|
+
/>
|
|
43
|
+
{loading && <p>Loading...</p>}
|
|
44
|
+
{error && <p>Error: {error.message}</p>}
|
|
45
|
+
{data && (
|
|
46
|
+
<div className="border w-fit mx-auto rounded-lg bg-muted">
|
|
47
|
+
<h3 className="capitalize text-center font-semibold">
|
|
48
|
+
{data.name}
|
|
49
|
+
</h3>
|
|
50
|
+
<img src={data.sprites.front_default} alt={data.name} />
|
|
51
|
+
</div>
|
|
52
|
+
)}
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const fetchPokemonById = async (id: number) => {
|
|
59
|
+
if (isUndefined(id)) throw new Error("id is required");
|
|
60
|
+
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
|
|
61
|
+
if (!response.ok) throw new Error("Pokemon not found");
|
|
62
|
+
return response.json();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* ```tsx
|
|
67
|
+
* interface UseAsyncOptions<T> {
|
|
68
|
+
* onError?: (error: Error) => void;
|
|
69
|
+
* onSuccess?: (data: T) => void;
|
|
70
|
+
* onFinish?: () => void;
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
export const Advanced: Story = {
|
|
76
|
+
decorators: [
|
|
77
|
+
(Story) => (
|
|
78
|
+
<ToastProvider toastManager={toastManager}>
|
|
79
|
+
<Story />
|
|
80
|
+
</ToastProvider>
|
|
81
|
+
),
|
|
82
|
+
],
|
|
83
|
+
render: () => {
|
|
84
|
+
const [pokemonId, setPokemonId] = useState<number>(1);
|
|
85
|
+
const [log, setLog] = useState<string[]>([]);
|
|
86
|
+
|
|
87
|
+
const handleSuccess = (data: any) => {
|
|
88
|
+
setLog((prevLog) => [...prevLog, `✅ Fetched Pokémon: ${data.name}`]);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const handleError = (error: Error) => {
|
|
92
|
+
toastManager.add({ variant: "error", description: error.message });
|
|
93
|
+
setLog((prevLog) => [...prevLog, `❌ Error: ${error.message}`]);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const handleSettled = () => {
|
|
97
|
+
setLog((prevLog) => [...prevLog, `🏁 Request settled`]);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const { loading, error, data } = useAsync({
|
|
101
|
+
fn: () => fetchPokemonById(pokemonId),
|
|
102
|
+
dependencies: [pokemonId],
|
|
103
|
+
onSuccess: handleSuccess,
|
|
104
|
+
onError: handleError,
|
|
105
|
+
onFinish: handleSettled,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<div className="p-4">
|
|
110
|
+
<div className="flex justify-center items-center gap-x-4">
|
|
111
|
+
<Button
|
|
112
|
+
size="icon"
|
|
113
|
+
variant="ghost"
|
|
114
|
+
onClick={() => setPokemonId(pokemonId - 1)}
|
|
115
|
+
>
|
|
116
|
+
<ChevronLeft />
|
|
117
|
+
</Button>
|
|
118
|
+
<div className="font-bold">{pokemonId}</div>
|
|
119
|
+
<Button
|
|
120
|
+
size="icon"
|
|
121
|
+
variant="ghost"
|
|
122
|
+
onClick={() => setPokemonId(pokemonId + 1)}
|
|
123
|
+
>
|
|
124
|
+
<ChevronRight />
|
|
125
|
+
</Button>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{loading && <p>Loading...</p>}
|
|
129
|
+
{error && <p>Error: {error.message}</p>}
|
|
130
|
+
{data && (
|
|
131
|
+
<div className="border w-fit mx-auto rounded-lg bg-muted">
|
|
132
|
+
<h3 className="capitalize text-center font-semibold">
|
|
133
|
+
{data.name}
|
|
134
|
+
</h3>
|
|
135
|
+
<img src={data.sprites.front_default} alt={data.name} />
|
|
136
|
+
</div>
|
|
137
|
+
)}
|
|
138
|
+
<div className="mt-4">
|
|
139
|
+
<h4 className="font-semibold">Log:</h4>
|
|
140
|
+
<ul className="list-disc">
|
|
141
|
+
{log.map((entry, index) => (
|
|
142
|
+
<li key={index}>{entry}</li>
|
|
143
|
+
))}
|
|
144
|
+
</ul>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
},
|
|
149
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import useAsync from "../useAsync";
|
|
4
|
+
|
|
5
|
+
const mockFetchSuccess = async (data: any) => {
|
|
6
|
+
return new Promise((resolve) => setTimeout(() => resolve(data), 100));
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const mockFetchError = async () => {
|
|
10
|
+
return new Promise((_, reject) =>
|
|
11
|
+
setTimeout(() => reject(new Error("Fetch error")), 100),
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
describe("useAsync hook", () => {
|
|
16
|
+
it("should initialize with default state", () => {
|
|
17
|
+
const { result } = renderHook(() =>
|
|
18
|
+
useAsync({ fn: () => Promise.resolve("test") }),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
expect(result.current.loading).toBe(true);
|
|
22
|
+
expect(result.current.error).toBeNull();
|
|
23
|
+
expect(result.current.data).toBeNull();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should handle success", async () => {
|
|
27
|
+
const { result } = renderHook(() =>
|
|
28
|
+
useAsync({ fn: () => mockFetchSuccess("success") }),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
await waitFor(() => result.current.data);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle error", async () => {
|
|
35
|
+
const { result } = renderHook(() =>
|
|
36
|
+
useAsync({ fn: () => mockFetchError() }),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
await waitFor(() => expect(result.current.error).not.toBeNull());
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should call onSuccess callback", async () => {
|
|
43
|
+
const onSuccess = vi.fn();
|
|
44
|
+
renderHook(() =>
|
|
45
|
+
useAsync({ fn: () => mockFetchSuccess("success"), onSuccess }),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
await waitFor(() => expect(onSuccess).toHaveBeenCalledWith("success"));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should call onError callback", async () => {
|
|
52
|
+
const onError = vi.fn();
|
|
53
|
+
renderHook(() => useAsync({ fn: () => mockFetchError(), onError }));
|
|
54
|
+
|
|
55
|
+
await waitFor(() =>
|
|
56
|
+
expect(onError).toHaveBeenCalledWith(expect.any(Error)),
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should call onFinish callback", async () => {
|
|
61
|
+
const onFinish = vi.fn();
|
|
62
|
+
renderHook(() =>
|
|
63
|
+
useAsync({ fn: () => mockFetchSuccess("success"), onFinish }),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
await waitFor(() => expect(onFinish).toHaveBeenCalled());
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useAsync } from "./useAsync";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DependencyList, useCallback, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
interface UseAsyncOptions<T> {
|
|
4
|
+
onError?: (error: Error) => void;
|
|
5
|
+
onSuccess?: (data: T) => void;
|
|
6
|
+
onFinish?: () => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface AsyncState<T> {
|
|
10
|
+
loading: boolean;
|
|
11
|
+
error: Error | null;
|
|
12
|
+
data: T | null;
|
|
13
|
+
refetch: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Props<T> extends UseAsyncOptions<T> {
|
|
17
|
+
fn: () => Promise<T>;
|
|
18
|
+
dependencies?: DependencyList;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function useAsync<T>({
|
|
22
|
+
fn,
|
|
23
|
+
dependencies = [],
|
|
24
|
+
onError,
|
|
25
|
+
onSuccess,
|
|
26
|
+
onFinish,
|
|
27
|
+
}: Props<T>): AsyncState<T> {
|
|
28
|
+
const [loading, setLoading] = useState<boolean>(true);
|
|
29
|
+
const [error, setError] = useState<Error | null>(null);
|
|
30
|
+
const [data, setData] = useState<T | null>(null);
|
|
31
|
+
|
|
32
|
+
const fnMemoized = useCallback(() => {
|
|
33
|
+
setLoading(true);
|
|
34
|
+
setError(null);
|
|
35
|
+
setData(null);
|
|
36
|
+
fn()
|
|
37
|
+
.then((result) => {
|
|
38
|
+
onSuccess?.(result);
|
|
39
|
+
setData(result);
|
|
40
|
+
})
|
|
41
|
+
.catch((err: Error) => {
|
|
42
|
+
onError?.(err);
|
|
43
|
+
setError(err);
|
|
44
|
+
})
|
|
45
|
+
.finally(() => {
|
|
46
|
+
onFinish?.();
|
|
47
|
+
setLoading(false);
|
|
48
|
+
});
|
|
49
|
+
}, dependencies);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
fnMemoized();
|
|
53
|
+
}, [fnMemoized]);
|
|
54
|
+
|
|
55
|
+
return { loading, error, data, refetch: fnMemoized };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default useAsync;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Meta } from "@storybook/react-vite";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { Button } from "../../../components";
|
|
4
|
+
import useClickOutside from "../useClickOutside";
|
|
5
|
+
|
|
6
|
+
const meta: Meta = {
|
|
7
|
+
title: "hooks/useClickOutside",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default meta;
|
|
11
|
+
|
|
12
|
+
export const Default = {
|
|
13
|
+
render: () => {
|
|
14
|
+
const [isOpen, setIsOpen] = useState(true);
|
|
15
|
+
|
|
16
|
+
const handleClose = () => {
|
|
17
|
+
setIsOpen(false);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const ref = useClickOutside<HTMLDivElement>(handleClose);
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<div className="flex gap-2">
|
|
24
|
+
<Button onClick={() => setIsOpen(true)} disabled={isOpen}>
|
|
25
|
+
Abrir
|
|
26
|
+
</Button>
|
|
27
|
+
<Button onClick={() => setIsOpen(false)} disabled={!isOpen}>
|
|
28
|
+
Cerrar
|
|
29
|
+
</Button>
|
|
30
|
+
<div
|
|
31
|
+
ref={ref}
|
|
32
|
+
className={`relative p-4 bg-white shadow-md ${isOpen ? "" : "hidden"}`}
|
|
33
|
+
>
|
|
34
|
+
<p>Haz clic fuera de este cuadro para cerrarlo.</p>
|
|
35
|
+
<Button onClick={() => setIsOpen(false)}>Cerrar desde dentro</Button>
|
|
36
|
+
</div>
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
},
|
|
40
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import useClickOutside from "../useClickOutside";
|
|
4
|
+
|
|
5
|
+
describe("useSelection hook", () => {
|
|
6
|
+
const Component = (mock: any) => {
|
|
7
|
+
const ref: any = useClickOutside(mock);
|
|
8
|
+
return (
|
|
9
|
+
<div data-testid="wrapper">
|
|
10
|
+
<div ref={ref} data-testid="target" />
|
|
11
|
+
</div>
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
it("should close on outside click", () => {
|
|
16
|
+
const mock = vi.fn();
|
|
17
|
+
render(<Component mock={mock} />);
|
|
18
|
+
|
|
19
|
+
// click item
|
|
20
|
+
const target = screen.getByTestId("target");
|
|
21
|
+
act(() => target.click());
|
|
22
|
+
waitFor(() => {
|
|
23
|
+
expect(mock).not.toHaveBeenCalled();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// click outside
|
|
27
|
+
const wrapper = screen.getByTestId("wrapper");
|
|
28
|
+
wrapper.click();
|
|
29
|
+
waitFor(() => {
|
|
30
|
+
expect(mock).toHaveBeenCalled();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useClickOutside } from "./useClickOutside";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
const useClickOutside = <T extends HTMLElement = HTMLElement>(
|
|
4
|
+
callback: () => void,
|
|
5
|
+
watch: boolean = true,
|
|
6
|
+
) => {
|
|
7
|
+
const ref = useRef<T>(null);
|
|
8
|
+
|
|
9
|
+
const handleClickOutside = (event: MouseEvent) => {
|
|
10
|
+
if (ref.current && !ref.current.contains(event.target as Node)) {
|
|
11
|
+
callback();
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!watch) return;
|
|
17
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
18
|
+
return () => {
|
|
19
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
20
|
+
};
|
|
21
|
+
}, [watch]);
|
|
22
|
+
|
|
23
|
+
return ref;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default useClickOutside;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { Button } from "../../../components";
|
|
3
|
+
import useClipboard from "../useClipboard";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Permite copiar texto al portapapeles del usuario. Devuelve el texto copiado actualmente y una función para copiar nuevo texto. Incluye manejo de compatibilidad y errores.
|
|
7
|
+
*/
|
|
8
|
+
const meta: Meta = {
|
|
9
|
+
title: "hooks/useClipboard",
|
|
10
|
+
tags: ["autodocs"],
|
|
11
|
+
parameters: {
|
|
12
|
+
docs: {
|
|
13
|
+
source: { type: "code" },
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default meta;
|
|
19
|
+
type Story = StoryObj<typeof meta>;
|
|
20
|
+
|
|
21
|
+
export const Default: Story = {
|
|
22
|
+
name: "Copia al portapapeles",
|
|
23
|
+
args: {
|
|
24
|
+
textoACopiar: "Hello world",
|
|
25
|
+
},
|
|
26
|
+
render: (args) => {
|
|
27
|
+
const [copiedText, copy] = useClipboard();
|
|
28
|
+
return (
|
|
29
|
+
<div className="space-y-2">
|
|
30
|
+
<pre className="rounded-md bg-slate-950 p-4 text-white">
|
|
31
|
+
<code className="block">
|
|
32
|
+
Texto copiado:{" "}
|
|
33
|
+
<span className="text-blue-300">
|
|
34
|
+
{JSON.stringify(copiedText, null, 2)}
|
|
35
|
+
</span>
|
|
36
|
+
</code>
|
|
37
|
+
</pre>
|
|
38
|
+
|
|
39
|
+
<Button showLoader={false} onClick={() => copy(args.textoACopiar)}>
|
|
40
|
+
Copy
|
|
41
|
+
</Button>
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import useClipboard from "../useClipboard";
|
|
4
|
+
|
|
5
|
+
describe("useClipboard", () => {
|
|
6
|
+
it("should be defined", () => {
|
|
7
|
+
expect(useClipboard).toBeDefined();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should copy to clipboard", () => {
|
|
11
|
+
const { result } = renderHook(useClipboard);
|
|
12
|
+
const [copiedValue, copy] = result.current;
|
|
13
|
+
|
|
14
|
+
copy("test");
|
|
15
|
+
waitFor(() => {
|
|
16
|
+
expect(copiedValue).toEqual("test");
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useClipboard } from "./useClipboard";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
|
|
3
|
+
type CopiedValue = string | null;
|
|
4
|
+
type CopyFunction = (text: string) => Promise<boolean>;
|
|
5
|
+
|
|
6
|
+
function useCopyToClipboard(): [CopiedValue, CopyFunction] {
|
|
7
|
+
const [copiedText, setCopiedText] = useState<CopiedValue>(null);
|
|
8
|
+
|
|
9
|
+
const copy = useCallback(async (text: string) => {
|
|
10
|
+
if (!navigator?.clipboard) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await navigator.clipboard.writeText(text);
|
|
16
|
+
setCopiedText(text);
|
|
17
|
+
return true;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error(error);
|
|
20
|
+
setCopiedText(null);
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}, []);
|
|
24
|
+
|
|
25
|
+
return [copiedText, copy];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default useCopyToClipboard;
|