@boxcustodia/library 2.0.0-alpha.10 → 2.0.0-alpha.12
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 +70 -70
- package/dist/index.css +2 -0
- package/dist/index.d.ts +420 -272
- package/dist/index.es.js +34448 -27816
- package/dist/theme.css +1 -1
- package/package.json +11 -6
- 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 +475 -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 +892 -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 +70 -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 +230 -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 +71 -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
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
|
2
|
+
import { CalendarIcon } from "lucide-react";
|
|
3
|
+
import { ReactNode, useRef, useState } from "react";
|
|
4
|
+
import {
|
|
5
|
+
Calendar,
|
|
6
|
+
FieldControl,
|
|
7
|
+
inputBaseClasses,
|
|
8
|
+
PopoverPopup,
|
|
9
|
+
PopoverRoot,
|
|
10
|
+
PopoverTrigger,
|
|
11
|
+
} from "../../components";
|
|
12
|
+
import { cn } from "../../lib";
|
|
13
|
+
import type {
|
|
14
|
+
DatePickerFooterProps,
|
|
15
|
+
DatePickerProps,
|
|
16
|
+
DateRange,
|
|
17
|
+
MultipleDatePickerProps,
|
|
18
|
+
RangeDatePickerProps,
|
|
19
|
+
SingleDatePickerProps,
|
|
20
|
+
} from "./date-picker.model";
|
|
21
|
+
import {
|
|
22
|
+
formatMultipleDates,
|
|
23
|
+
formatRangeDate,
|
|
24
|
+
formatSingleDate,
|
|
25
|
+
} from "./date-picker.utils";
|
|
26
|
+
|
|
27
|
+
export const DatePicker = (props: DatePickerProps) => {
|
|
28
|
+
if (props.mode === "range") return <RangePicker {...props} />;
|
|
29
|
+
if (props.mode === "multiple") return <MultiplePicker {...props} />;
|
|
30
|
+
return <SinglePicker {...props} />;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// ─── Shared popover shell ────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
type PickerShellProps = {
|
|
36
|
+
open: boolean;
|
|
37
|
+
onOpenChange: (open: boolean) => void;
|
|
38
|
+
displayText: string;
|
|
39
|
+
hasValue: boolean;
|
|
40
|
+
disabled?: boolean;
|
|
41
|
+
className?: string;
|
|
42
|
+
children: ReactNode;
|
|
43
|
+
footer?: ReactNode;
|
|
44
|
+
slot: string;
|
|
45
|
+
formValue: string;
|
|
46
|
+
required?: boolean;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const PickerShell = ({
|
|
50
|
+
open,
|
|
51
|
+
onOpenChange,
|
|
52
|
+
displayText,
|
|
53
|
+
hasValue,
|
|
54
|
+
disabled,
|
|
55
|
+
className,
|
|
56
|
+
children,
|
|
57
|
+
footer,
|
|
58
|
+
slot,
|
|
59
|
+
formValue,
|
|
60
|
+
required,
|
|
61
|
+
}: PickerShellProps) => {
|
|
62
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<PopoverRoot data-slot={slot} open={open} onOpenChange={onOpenChange}>
|
|
66
|
+
<PopoverTrigger
|
|
67
|
+
render={
|
|
68
|
+
<button
|
|
69
|
+
ref={triggerRef}
|
|
70
|
+
disabled={disabled}
|
|
71
|
+
className={cn(
|
|
72
|
+
inputBaseClasses,
|
|
73
|
+
"inline-flex items-center gap-2 h-10 cursor-pointer text-left",
|
|
74
|
+
"focus-visible:border-ring",
|
|
75
|
+
"aria-invalid:border-error focus-visible:aria-invalid:ring-error/20",
|
|
76
|
+
"group-data-[invalid]:border-error focus-visible:group-data-[invalid]:ring-error/20",
|
|
77
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
78
|
+
!hasValue && "text-muted-foreground",
|
|
79
|
+
className,
|
|
80
|
+
)}
|
|
81
|
+
/>
|
|
82
|
+
}
|
|
83
|
+
>
|
|
84
|
+
<CalendarIcon className="shrink-0" />
|
|
85
|
+
<span className="truncate">{displayText}</span>
|
|
86
|
+
</PopoverTrigger>
|
|
87
|
+
{/* Hidden input registered with Field for validity tracking and FormData. */}
|
|
88
|
+
<FieldControl
|
|
89
|
+
render={
|
|
90
|
+
<input
|
|
91
|
+
type="text"
|
|
92
|
+
tabIndex={-1}
|
|
93
|
+
aria-hidden="true"
|
|
94
|
+
className="sr-only"
|
|
95
|
+
value={formValue}
|
|
96
|
+
onChange={() => {}}
|
|
97
|
+
required={required}
|
|
98
|
+
onFocus={() => triggerRef.current?.focus()}
|
|
99
|
+
/>
|
|
100
|
+
}
|
|
101
|
+
/>
|
|
102
|
+
<PopoverPopup
|
|
103
|
+
className="w-auto p-0"
|
|
104
|
+
align="start"
|
|
105
|
+
data-slot={`${slot}-content`}
|
|
106
|
+
>
|
|
107
|
+
{children}
|
|
108
|
+
{footer && (
|
|
109
|
+
<div className="p-2" data-slot={`${slot}-footer`}>
|
|
110
|
+
{footer}
|
|
111
|
+
</div>
|
|
112
|
+
)}
|
|
113
|
+
</PopoverPopup>
|
|
114
|
+
</PopoverRoot>
|
|
115
|
+
);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// ─── Single mode ─────────────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
const SinglePicker = ({
|
|
121
|
+
value: valueProp,
|
|
122
|
+
defaultValue,
|
|
123
|
+
onChange,
|
|
124
|
+
placeholder = "Seleccionar fecha",
|
|
125
|
+
disabled,
|
|
126
|
+
disabledDate,
|
|
127
|
+
renderFooter,
|
|
128
|
+
className,
|
|
129
|
+
required,
|
|
130
|
+
}: SingleDatePickerProps) => {
|
|
131
|
+
const [open, setOpen] = useState(false);
|
|
132
|
+
|
|
133
|
+
const [value, setValue] = useControllableState<Date | null>({
|
|
134
|
+
prop: valueProp,
|
|
135
|
+
onChange: (next) => onChange?.(next ?? null),
|
|
136
|
+
defaultProp: defaultValue ?? null,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const footerProps: DatePickerFooterProps = {
|
|
140
|
+
mode: "single",
|
|
141
|
+
value: value ?? null,
|
|
142
|
+
clear: () => setValue(null),
|
|
143
|
+
selectToday: () => {
|
|
144
|
+
const today = new Date();
|
|
145
|
+
if (disabledDate?.(today)) return;
|
|
146
|
+
setValue(today);
|
|
147
|
+
},
|
|
148
|
+
setValue: (next) => setValue(next),
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<PickerShell
|
|
153
|
+
slot="date-picker"
|
|
154
|
+
open={open}
|
|
155
|
+
onOpenChange={setOpen}
|
|
156
|
+
displayText={formatSingleDate(value, placeholder)}
|
|
157
|
+
hasValue={!!value}
|
|
158
|
+
disabled={disabled}
|
|
159
|
+
className={className}
|
|
160
|
+
footer={renderFooter?.(footerProps)}
|
|
161
|
+
formValue={formatSingleDate(value, "")}
|
|
162
|
+
required={required}
|
|
163
|
+
>
|
|
164
|
+
<Calendar
|
|
165
|
+
className="border-none"
|
|
166
|
+
mode="single"
|
|
167
|
+
selected={value ?? undefined}
|
|
168
|
+
onSelect={(next) => {
|
|
169
|
+
if (disabledDate && next && disabledDate(next)) return;
|
|
170
|
+
setValue(next ?? null);
|
|
171
|
+
setOpen(false);
|
|
172
|
+
}}
|
|
173
|
+
disabled={disabledDate}
|
|
174
|
+
/>
|
|
175
|
+
</PickerShell>
|
|
176
|
+
);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// ─── Range mode ──────────────────────────────────────────────────────────────
|
|
180
|
+
|
|
181
|
+
const EMPTY_RANGE: DateRange = { start: null, end: null };
|
|
182
|
+
|
|
183
|
+
const RangePicker = ({
|
|
184
|
+
value: valueProp,
|
|
185
|
+
defaultValue,
|
|
186
|
+
onChange,
|
|
187
|
+
placeholder = "Seleccionar rango",
|
|
188
|
+
disabled,
|
|
189
|
+
disabledDate,
|
|
190
|
+
renderFooter,
|
|
191
|
+
className,
|
|
192
|
+
required,
|
|
193
|
+
}: RangeDatePickerProps) => {
|
|
194
|
+
const [open, setOpen] = useState(false);
|
|
195
|
+
|
|
196
|
+
const [value, setValue] = useControllableState<DateRange>({
|
|
197
|
+
prop: valueProp,
|
|
198
|
+
onChange: (next) => onChange?.(next ?? EMPTY_RANGE),
|
|
199
|
+
defaultProp: defaultValue ?? EMPTY_RANGE,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const range = value ?? EMPTY_RANGE;
|
|
203
|
+
|
|
204
|
+
const footerProps: DatePickerFooterProps = {
|
|
205
|
+
mode: "range",
|
|
206
|
+
value: range,
|
|
207
|
+
clear: () => setValue(EMPTY_RANGE),
|
|
208
|
+
setValue: (next) => setValue(next),
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<PickerShell
|
|
213
|
+
slot="date-picker"
|
|
214
|
+
open={open}
|
|
215
|
+
onOpenChange={setOpen}
|
|
216
|
+
displayText={formatRangeDate(
|
|
217
|
+
range.start || range.end ? range : undefined,
|
|
218
|
+
placeholder,
|
|
219
|
+
)}
|
|
220
|
+
hasValue={!!(range.start || range.end)}
|
|
221
|
+
disabled={disabled}
|
|
222
|
+
className={className}
|
|
223
|
+
footer={renderFooter?.(footerProps)}
|
|
224
|
+
formValue={formatRangeDate(
|
|
225
|
+
range.start || range.end ? range : undefined,
|
|
226
|
+
"",
|
|
227
|
+
)}
|
|
228
|
+
required={required}
|
|
229
|
+
>
|
|
230
|
+
<Calendar
|
|
231
|
+
className="border-none"
|
|
232
|
+
mode="range"
|
|
233
|
+
selected={{
|
|
234
|
+
from: range.start ?? undefined,
|
|
235
|
+
to: range.end ?? undefined,
|
|
236
|
+
}}
|
|
237
|
+
onSelect={(next) => {
|
|
238
|
+
setValue({ start: next?.from ?? null, end: next?.to ?? null });
|
|
239
|
+
}}
|
|
240
|
+
disabled={disabledDate}
|
|
241
|
+
/>
|
|
242
|
+
</PickerShell>
|
|
243
|
+
);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// ─── Multiple mode ───────────────────────────────────────────────────────────
|
|
247
|
+
|
|
248
|
+
const MultiplePicker = ({
|
|
249
|
+
value: valueProp,
|
|
250
|
+
defaultValue,
|
|
251
|
+
onChange,
|
|
252
|
+
placeholder = "Seleccionar fechas",
|
|
253
|
+
disabled,
|
|
254
|
+
disabledDate,
|
|
255
|
+
renderFooter,
|
|
256
|
+
className,
|
|
257
|
+
required,
|
|
258
|
+
}: MultipleDatePickerProps) => {
|
|
259
|
+
const [open, setOpen] = useState(false);
|
|
260
|
+
|
|
261
|
+
const [value, setValue] = useControllableState<Date[]>({
|
|
262
|
+
prop: valueProp,
|
|
263
|
+
onChange: (next) => onChange?.(next ?? []),
|
|
264
|
+
defaultProp: defaultValue ?? [],
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const footerProps: DatePickerFooterProps = {
|
|
268
|
+
mode: "multiple",
|
|
269
|
+
value: value ?? [],
|
|
270
|
+
clear: () => setValue([]),
|
|
271
|
+
setValue: (next) => setValue(next),
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
return (
|
|
275
|
+
<PickerShell
|
|
276
|
+
slot="date-picker"
|
|
277
|
+
open={open}
|
|
278
|
+
onOpenChange={setOpen}
|
|
279
|
+
displayText={formatMultipleDates(value, placeholder)}
|
|
280
|
+
hasValue={(value?.length ?? 0) > 0}
|
|
281
|
+
disabled={disabled}
|
|
282
|
+
className={className}
|
|
283
|
+
footer={renderFooter?.(footerProps)}
|
|
284
|
+
formValue={formatMultipleDates(value, "")}
|
|
285
|
+
required={required}
|
|
286
|
+
>
|
|
287
|
+
<Calendar
|
|
288
|
+
className="border-none"
|
|
289
|
+
mode="multiple"
|
|
290
|
+
selected={value ?? []}
|
|
291
|
+
onSelect={(next) => {
|
|
292
|
+
setValue(next ?? []);
|
|
293
|
+
}}
|
|
294
|
+
disabled={disabledDate}
|
|
295
|
+
/>
|
|
296
|
+
</PickerShell>
|
|
297
|
+
);
|
|
298
|
+
};
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { format, isDate } from "date-fns";
|
|
2
|
+
import type { DateRange } from "./date-picker.model";
|
|
3
|
+
|
|
4
|
+
export const DISPLAY_DATE_FORMAT = "dd/MM/yyyy";
|
|
5
|
+
|
|
6
|
+
export const formatSingleDate = (
|
|
7
|
+
date: Date | null | undefined,
|
|
8
|
+
placeholder: string,
|
|
9
|
+
): string => {
|
|
10
|
+
if (!date || !isDate(date)) return placeholder;
|
|
11
|
+
return format(date, DISPLAY_DATE_FORMAT);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const formatRangeDate = (
|
|
15
|
+
range: DateRange | undefined,
|
|
16
|
+
placeholder: string,
|
|
17
|
+
): string => {
|
|
18
|
+
if (!range) return placeholder;
|
|
19
|
+
const { start, end } = range;
|
|
20
|
+
if (!start && !end) return placeholder;
|
|
21
|
+
const startStr =
|
|
22
|
+
start && isDate(start) ? format(start, DISPLAY_DATE_FORMAT) : "...";
|
|
23
|
+
const endStr = end && isDate(end) ? format(end, DISPLAY_DATE_FORMAT) : "...";
|
|
24
|
+
return `${startStr} → ${endStr}`;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const formatMultipleDates = (
|
|
28
|
+
dates: Date[] | undefined,
|
|
29
|
+
placeholder: string,
|
|
30
|
+
): string => {
|
|
31
|
+
if (!dates || dates.length === 0) return placeholder;
|
|
32
|
+
if (dates.length === 1) return format(dates[0], DISPLAY_DATE_FORMAT);
|
|
33
|
+
return `${dates.length} fechas seleccionadas`;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const formatDate = (date: Date | null): string => {
|
|
37
|
+
if (!isDate(date)) return "";
|
|
38
|
+
return format(date, DISPLAY_DATE_FORMAT);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const parseInputToDate = (raw: string): Date | null => {
|
|
42
|
+
const trimmed = raw.trim();
|
|
43
|
+
if (!trimmed) return null;
|
|
44
|
+
|
|
45
|
+
const match = /^(\d{1,2})\/(\d{1,2})\/(\d{1,4})$/.exec(trimmed);
|
|
46
|
+
if (!match) return null;
|
|
47
|
+
|
|
48
|
+
const [, dd, mm, yyyy] = match;
|
|
49
|
+
const day = Number(dd);
|
|
50
|
+
const month = Number(mm);
|
|
51
|
+
const rawYear = yyyy;
|
|
52
|
+
let year = Number(rawYear);
|
|
53
|
+
|
|
54
|
+
if (Number.isNaN(day) || Number.isNaN(month) || Number.isNaN(year)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Año corto → asumimos 2000 + yy (ej: 25 → 2025)
|
|
59
|
+
if (rawYear.length <= 2) {
|
|
60
|
+
year = 2000 + year;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (day < 1 || day > 31 || month < 1 || month > 12) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const parsed = new Date(year, month - 1, day);
|
|
68
|
+
|
|
69
|
+
if (
|
|
70
|
+
parsed.getFullYear() !== year ||
|
|
71
|
+
parsed.getMonth() !== month - 1 ||
|
|
72
|
+
parsed.getDate() !== day
|
|
73
|
+
) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return parsed;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type DateMaskContext = {
|
|
81
|
+
lastKey: string | null;
|
|
82
|
+
previousValue: string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const maskDateInput = (
|
|
86
|
+
raw: string,
|
|
87
|
+
context: DateMaskContext,
|
|
88
|
+
): string => {
|
|
89
|
+
const isDeleting =
|
|
90
|
+
(context.lastKey === "Backspace" || context.lastKey === "Delete") &&
|
|
91
|
+
raw.length < context.previousValue.length;
|
|
92
|
+
const rawHasSlash = raw.includes("/");
|
|
93
|
+
const endsWithSlash = raw.trim().endsWith("/");
|
|
94
|
+
|
|
95
|
+
// When both separators are present, use segment-based parsing regardless of
|
|
96
|
+
// whether we're inserting or deleting. Strip-and-rebuild merges digits across
|
|
97
|
+
// segment boundaries incorrectly in both directions:
|
|
98
|
+
// delete: "01/0/2025" → digits "0102025" → "01/02/025" ✗
|
|
99
|
+
// insert: "01/1/2025" → digits "0112025" → month "12", year "025" ✗
|
|
100
|
+
const slashCount = (raw.match(/\//g) ?? []).length;
|
|
101
|
+
if (slashCount >= 2) {
|
|
102
|
+
const parts = raw.split("/");
|
|
103
|
+
const dayPart = (parts[0] ?? "").replace(/\D/g, "").slice(0, 2);
|
|
104
|
+
const monthPart = (parts[1] ?? "").replace(/\D/g, "").slice(0, 2);
|
|
105
|
+
const yearPart = (parts[2] ?? "").replace(/\D/g, "").slice(0, 4);
|
|
106
|
+
|
|
107
|
+
if (!dayPart && !monthPart && !yearPart) return "";
|
|
108
|
+
return `${dayPart}/${monthPart}/${yearPart}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const digits = raw.replace(/\D/g, "").slice(0, 8);
|
|
112
|
+
|
|
113
|
+
if (!digits) {
|
|
114
|
+
return "";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Día
|
|
118
|
+
if (digits.length <= 2) {
|
|
119
|
+
let maskedDay = digits;
|
|
120
|
+
|
|
121
|
+
if (digits.length === 1 && (rawHasSlash || endsWithSlash) && !isDeleting) {
|
|
122
|
+
let dayNumber = Number(maskedDay);
|
|
123
|
+
|
|
124
|
+
if (Number.isNaN(dayNumber)) {
|
|
125
|
+
dayNumber = 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (dayNumber < 1) {
|
|
129
|
+
dayNumber = 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (dayNumber > 31) {
|
|
133
|
+
dayNumber = 31;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
maskedDay = dayNumber.toString().padStart(2, "0");
|
|
137
|
+
maskedDay += "/";
|
|
138
|
+
} else if (digits.length === 2) {
|
|
139
|
+
if (!isDeleting) {
|
|
140
|
+
let dayNumber = Number(maskedDay);
|
|
141
|
+
|
|
142
|
+
if (Number.isNaN(dayNumber)) {
|
|
143
|
+
dayNumber = 1;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (dayNumber < 1) {
|
|
147
|
+
dayNumber = 1;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (dayNumber > 31) {
|
|
151
|
+
dayNumber = 31;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
maskedDay = dayNumber.toString().padStart(2, "0");
|
|
155
|
+
maskedDay += "/";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return maskedDay;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Día completo (ya hay más de 2 dígitos): normalizamos siempre
|
|
163
|
+
const dayDigitsRaw = digits.slice(0, 2);
|
|
164
|
+
let dayNumber = Number(dayDigitsRaw);
|
|
165
|
+
|
|
166
|
+
if (Number.isNaN(dayNumber)) {
|
|
167
|
+
dayNumber = 1;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (dayNumber < 1) {
|
|
171
|
+
dayNumber = 1;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (dayNumber > 31) {
|
|
175
|
+
dayNumber = 31;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const day = dayNumber.toString().padStart(2, "0");
|
|
179
|
+
const monthDigitsRaw = digits.slice(2, 4);
|
|
180
|
+
const yearDigits = digits.slice(4);
|
|
181
|
+
|
|
182
|
+
let monthText = "";
|
|
183
|
+
|
|
184
|
+
if (monthDigitsRaw.length === 0) {
|
|
185
|
+
monthText = "";
|
|
186
|
+
} else if (monthDigitsRaw.length === 1 && yearDigits.length === 0) {
|
|
187
|
+
// Un solo dígito de mes sin año todavía:
|
|
188
|
+
// - si es "0" o "1", permitimos que el usuario complete (ej: "08")
|
|
189
|
+
// - si es 1-9, lo consideramos un mes completo y lo normalizamos con padding
|
|
190
|
+
const shouldPadMonth =
|
|
191
|
+
endsWithSlash && !isDeleting && monthDigitsRaw.length === 1;
|
|
192
|
+
|
|
193
|
+
if (monthDigitsRaw === "0" || monthDigitsRaw === "1") {
|
|
194
|
+
if (shouldPadMonth) {
|
|
195
|
+
let monthNumber = Number(monthDigitsRaw);
|
|
196
|
+
|
|
197
|
+
if (Number.isNaN(monthNumber)) {
|
|
198
|
+
monthNumber = 1;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (monthNumber < 1) {
|
|
202
|
+
monthNumber = 1;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (monthNumber > 12) {
|
|
206
|
+
monthNumber = 12;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
monthText = monthNumber.toString().padStart(2, "0");
|
|
210
|
+
} else {
|
|
211
|
+
monthText = monthDigitsRaw;
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
let monthNumber = Number(monthDigitsRaw);
|
|
215
|
+
|
|
216
|
+
if (Number.isNaN(monthNumber)) {
|
|
217
|
+
monthNumber = 1;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (monthNumber < 1) {
|
|
221
|
+
monthNumber = 1;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (monthNumber > 12) {
|
|
225
|
+
monthNumber = 12;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
monthText = monthNumber.toString().padStart(2, "0");
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
// Mes completo (2 dígitos o ya empezamos a escribir el año): normalizamos
|
|
232
|
+
let monthNumber = Number(monthDigitsRaw);
|
|
233
|
+
|
|
234
|
+
if (Number.isNaN(monthNumber)) {
|
|
235
|
+
monthNumber = 1;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (monthNumber < 1) {
|
|
239
|
+
monthNumber = 1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (monthNumber > 12) {
|
|
243
|
+
monthNumber = 12;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
monthText = monthNumber.toString().padStart(2, "0");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const base = `${day}/${monthText}`;
|
|
250
|
+
const shouldAddYearSlash =
|
|
251
|
+
!yearDigits && monthText.length === 2 && !isDeleting;
|
|
252
|
+
|
|
253
|
+
const masked = yearDigits
|
|
254
|
+
? `${base}/${yearDigits}`
|
|
255
|
+
: shouldAddYearSlash
|
|
256
|
+
? `${base}/`
|
|
257
|
+
: base;
|
|
258
|
+
|
|
259
|
+
return masked;
|
|
260
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { KeyboardEvent, useCallback, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
type UseDateInputPopoverOptions = {
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type UseDateInputPopoverResult = {
|
|
8
|
+
open: boolean;
|
|
9
|
+
setOpen: (open: boolean) => void;
|
|
10
|
+
handleInputKeyDown: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
11
|
+
inputRef: React.RefObject<HTMLInputElement | null>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const useDateInputPopover = (
|
|
15
|
+
options: UseDateInputPopoverOptions = {},
|
|
16
|
+
): UseDateInputPopoverResult => {
|
|
17
|
+
const { disabled } = options;
|
|
18
|
+
|
|
19
|
+
const [open, setOpen] = useState(false);
|
|
20
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
21
|
+
|
|
22
|
+
const handleInputKeyDown = useCallback(
|
|
23
|
+
(event: KeyboardEvent<HTMLInputElement>) => {
|
|
24
|
+
if (disabled) return;
|
|
25
|
+
|
|
26
|
+
if (event.key === " " || event.key === "Spacebar") {
|
|
27
|
+
event.preventDefault();
|
|
28
|
+
setOpen(true);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (event.key === "Escape") {
|
|
33
|
+
setOpen(false);
|
|
34
|
+
requestAnimationFrame(() => {
|
|
35
|
+
inputRef.current?.focus();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
[disabled],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
open,
|
|
44
|
+
setOpen,
|
|
45
|
+
handleInputKeyDown,
|
|
46
|
+
inputRef,
|
|
47
|
+
};
|
|
48
|
+
};
|