@dbcdk/react-components 0.0.103 → 0.0.104
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/assets/logo.cjs +87 -0
- package/dist/assets/logo.js +85 -0
- package/dist/client.cjs +587 -0
- package/dist/client.d.ts +83 -0
- package/dist/client.js +84 -0
- package/dist/components/accordion/Accordion.cjs +103 -0
- package/dist/components/accordion/Accordion.js +97 -0
- package/dist/components/accordion/Accordion.module.css +41 -0
- package/dist/components/accordion/components/AccordionRow.cjs +115 -0
- package/dist/components/accordion/components/AccordionRow.js +109 -0
- package/dist/components/accordion/components/AccordionRow.module.css +99 -0
- package/dist/components/alert/Alert.cjs +52 -0
- package/dist/components/alert/Alert.d.ts +2 -1
- package/dist/components/alert/Alert.js +46 -0
- package/dist/components/alert/Alert.module.css +97 -0
- package/dist/components/app-header/AppHeader.cjs +14 -0
- package/dist/components/app-header/AppHeader.js +8 -0
- package/dist/components/app-header/AppHeader.module.css +74 -0
- package/dist/components/attribute-chip/AttributeChip.cjs +22 -0
- package/dist/components/attribute-chip/AttributeChip.js +16 -0
- package/dist/components/attribute-chip/AttributeChip.module.css +65 -0
- package/dist/components/avatar/Avatar.cjs +111 -0
- package/dist/components/avatar/Avatar.js +86 -0
- package/dist/components/avatar/Avatar.module.css +91 -0
- package/dist/components/breadcrumbs/Breadcrumbs.cjs +24 -0
- package/dist/components/breadcrumbs/Breadcrumbs.js +18 -0
- package/dist/components/breadcrumbs/Breadcrumbs.module.css +80 -0
- package/dist/components/button/Button.cjs +149 -0
- package/dist/components/button/Button.js +124 -0
- package/dist/components/button/Button.module.css +260 -0
- package/dist/components/button-select/ButtonSelect.cjs +35 -0
- package/dist/components/button-select/ButtonSelect.js +29 -0
- package/dist/components/button-select/ButtonSelect.module.css +40 -0
- package/dist/components/card/Card.cjs +112 -0
- package/dist/components/card/Card.d.ts +2 -1
- package/dist/components/card/Card.js +106 -0
- package/dist/components/card/Card.module.css +187 -0
- package/dist/components/card/components/CardMeta.cjs +62 -0
- package/dist/components/card/components/CardMeta.js +55 -0
- package/dist/components/card/components/CardMeta.module.css +55 -0
- package/dist/components/card-container/CardContainer.cjs +34 -0
- package/dist/components/card-container/CardContainer.js +28 -0
- package/dist/components/card-container/CardContainer.module.css +63 -0
- package/dist/components/chip/Chip.cjs +65 -0
- package/dist/components/chip/Chip.js +59 -0
- package/dist/components/chip/Chip.module.css +236 -0
- package/dist/components/circle/Circle.cjs +33 -0
- package/dist/components/circle/Circle.js +27 -0
- package/dist/components/circle/Circle.module.css +128 -0
- package/dist/components/clear-button/ClearButton.cjs +33 -0
- package/dist/components/clear-button/ClearButton.d.ts +2 -1
- package/dist/components/clear-button/ClearButton.js +27 -0
- package/dist/components/clear-button/ClearButton.module.css +26 -0
- package/dist/components/code-block/CodeBlock.cjs +97 -0
- package/dist/components/code-block/CodeBlock.js +91 -0
- package/dist/components/code-block/CodeBlock.module.css +124 -0
- package/dist/components/copy-button/CopyButton.cjs +139 -0
- package/dist/components/copy-button/CopyButton.js +133 -0
- package/dist/components/copy-button/CopyButton.module.css +22 -0
- package/dist/components/datetime-picker/DateTimePicker.cjs +559 -0
- package/dist/components/datetime-picker/DateTimePicker.js +553 -0
- package/dist/components/datetime-picker/DateTimePicker.module.css +155 -0
- package/dist/components/datetime-picker/dateTimeHelpers.cjs +179 -0
- package/dist/components/datetime-picker/dateTimeHelpers.js +160 -0
- package/dist/components/divider/Divider.cjs +22 -0
- package/dist/components/divider/Divider.js +20 -0
- package/dist/components/filter-field/FilterField.cjs +296 -0
- package/dist/components/filter-field/FilterField.js +289 -0
- package/dist/components/filter-field/FilterField.module.css +379 -0
- package/dist/components/filtering/chip-multi-toggle/ChipMultiToggle.cjs +115 -0
- package/dist/components/filtering/chip-multi-toggle/ChipMultiToggle.js +108 -0
- package/dist/components/filtering/chip-multi-toggle/ChipMultiToggle.module.css +59 -0
- package/dist/components/forms/checkbox/Checkbox.cjs +88 -0
- package/dist/components/forms/checkbox/Checkbox.js +82 -0
- package/dist/components/forms/checkbox/Checkbox.module.css +104 -0
- package/dist/components/forms/checkbox-group/CheckboxGroup.cjs +121 -0
- package/dist/components/forms/checkbox-group/CheckboxGroup.js +115 -0
- package/dist/components/forms/checkbox-group/CheckboxGroup.module.css +115 -0
- package/dist/components/forms/form-select/FormSelect.cjs +191 -0
- package/dist/components/forms/form-select/FormSelect.js +166 -0
- package/dist/components/forms/form-select/FormSelect.module.css +236 -0
- package/dist/components/forms/input/Input.cjs +188 -0
- package/dist/components/forms/input/Input.d.ts +1 -0
- package/dist/components/forms/input/Input.js +182 -0
- package/dist/components/forms/input/Input.module.css +468 -0
- package/dist/components/forms/input-container/InputContainer.cjs +72 -0
- package/dist/components/forms/input-container/InputContainer.js +66 -0
- package/dist/components/forms/input-container/InputContainer.module.css +59 -0
- package/dist/components/forms/multi-select/MultiSelect.cjs +227 -0
- package/dist/components/forms/multi-select/MultiSelect.js +225 -0
- package/dist/components/forms/radio-buttons/RadioButton.cjs +91 -0
- package/dist/components/forms/radio-buttons/RadioButton.js +85 -0
- package/dist/components/forms/radio-buttons/RadioButtonGroup.cjs +74 -0
- package/dist/components/forms/radio-buttons/RadioButtonGroup.js +72 -0
- package/dist/components/forms/radio-buttons/RadioButtons.module.css +118 -0
- package/dist/components/forms/select/Select.cjs +286 -0
- package/dist/components/forms/select/Select.js +280 -0
- package/dist/components/forms/select/Select.module.css +32 -0
- package/dist/components/forms/text-area/Textarea.cjs +116 -0
- package/dist/components/forms/text-area/Textarea.js +110 -0
- package/dist/components/forms/text-area/Textarea.module.css +70 -0
- package/dist/components/forms/typeahead/Typeahead.cjs +792 -0
- package/dist/components/forms/typeahead/Typeahead.js +768 -0
- package/dist/components/forms/typeahead/Typeahead.module.css +38 -0
- package/dist/components/grid/Grid.cjs +45 -0
- package/dist/components/grid/Grid.js +38 -0
- package/dist/components/grid/Grid.module.css +35 -0
- package/dist/components/headline/CollapsibleHeadline.cjs +80 -0
- package/dist/components/headline/CollapsibleHeadline.js +74 -0
- package/dist/components/headline/Headline.cjs +59 -0
- package/dist/components/headline/Headline.js +53 -0
- package/dist/components/headline/Headline.module.css +185 -0
- package/dist/components/hyperlink/Hyperlink.cjs +107 -0
- package/dist/components/hyperlink/Hyperlink.js +82 -0
- package/dist/components/hyperlink/Hyperlink.module.css +121 -0
- package/dist/components/icon/Icon.cjs +27 -0
- package/dist/components/icon/Icon.js +20 -0
- package/dist/components/icon/Icon.module.css +36 -0
- package/dist/components/inline-status/InlineStatus.cjs +39 -0
- package/dist/components/inline-status/InlineStatus.js +33 -0
- package/dist/components/inline-status/InlineStatus.module.css +92 -0
- package/dist/components/interval-select/IntervalSelect.cjs +203 -0
- package/dist/components/interval-select/IntervalSelect.js +201 -0
- package/dist/components/json-viewer/HighlightedText.cjs +13 -0
- package/dist/components/json-viewer/HighlightedText.js +11 -0
- package/dist/components/json-viewer/JsonNode.cjs +124 -0
- package/dist/components/json-viewer/JsonNode.js +118 -0
- package/dist/components/json-viewer/JsonViewer.cjs +164 -0
- package/dist/components/json-viewer/JsonViewer.js +158 -0
- package/dist/components/json-viewer/JsonViewer.module.css +346 -0
- package/dist/components/json-viewer/types.cjs +2 -0
- package/dist/components/json-viewer/types.js +1 -0
- package/dist/components/json-viewer/useClipboardStatus.cjs +15 -0
- package/dist/components/json-viewer/useClipboardStatus.js +13 -0
- package/dist/components/json-viewer/utils.cjs +126 -0
- package/dist/components/json-viewer/utils.js +111 -0
- package/dist/components/media-card/MediaCard.cjs +60 -0
- package/dist/components/media-card/MediaCard.d.ts +19 -0
- package/dist/components/media-card/MediaCard.js +54 -0
- package/dist/components/media-card/MediaCard.module.css +92 -0
- package/dist/components/menu/Menu.cjs +287 -0
- package/dist/components/menu/Menu.js +262 -0
- package/dist/components/menu/Menu.module.css +220 -0
- package/dist/components/meta-bar/MetaBar.cjs +28 -0
- package/dist/components/meta-bar/MetaBar.js +22 -0
- package/dist/components/meta-bar/MetaBar.module.css +27 -0
- package/dist/components/nav-bar/NavBar.cjs +95 -0
- package/dist/components/nav-bar/NavBar.d.ts +1 -1
- package/dist/components/nav-bar/NavBar.js +89 -0
- package/dist/components/nav-bar/NavBar.module.css +225 -0
- package/dist/components/overlay/fade-overlay/FadeOverlay.cjs +30 -0
- package/dist/components/overlay/fade-overlay/FadeOverlay.js +24 -0
- package/dist/components/overlay/fade-overlay/FadeOverlay.module.css +54 -0
- package/dist/components/overlay/modal/Modal.cjs +189 -0
- package/dist/components/overlay/modal/Modal.d.ts +2 -1
- package/dist/components/overlay/modal/Modal.js +183 -0
- package/dist/components/overlay/modal/Modal.module.css +112 -0
- package/dist/components/overlay/modal/provider/ModalProvider.cjs +98 -0
- package/dist/components/overlay/modal/provider/ModalProvider.js +95 -0
- package/dist/components/overlay/side-panel/SidePanel.cjs +173 -0
- package/dist/components/overlay/side-panel/SidePanel.js +167 -0
- package/dist/components/overlay/side-panel/SidePanel.module.css +177 -0
- package/dist/components/overlay/side-panel/useSidePanel.cjs +16 -0
- package/dist/components/overlay/side-panel/useSidePanel.js +14 -0
- package/dist/components/overlay/tooltip/Tooltip.cjs +34 -0
- package/dist/components/overlay/tooltip/Tooltip.js +32 -0
- package/dist/components/overlay/tooltip/Tooltip.module.css +104 -0
- package/dist/components/overlay/tooltip/TooltipProvider.cjs +238 -0
- package/dist/components/overlay/tooltip/TooltipProvider.js +231 -0
- package/dist/components/overlay/tooltip/useTooltipTrigger.cjs +117 -0
- package/dist/components/overlay/tooltip/useTooltipTrigger.js +115 -0
- package/dist/components/page/Page.cjs +63 -0
- package/dist/components/page/Page.js +57 -0
- package/dist/components/page/Page.module.css +95 -0
- package/dist/components/page-layout/PageLayout.cjs +97 -0
- package/dist/components/page-layout/PageLayout.d.ts +0 -5
- package/dist/components/page-layout/PageLayout.js +91 -0
- package/dist/components/page-layout/PageLayout.module.css +236 -0
- package/dist/components/page-layout/components/footer/Footer.cjs +53 -0
- package/dist/components/page-layout/components/footer/Footer.js +47 -0
- package/dist/components/page-layout/components/footer/Footer.module.css +89 -0
- package/dist/components/pagination/Pagination.cjs +160 -0
- package/dist/components/pagination/Pagination.js +154 -0
- package/dist/components/pagination/Pagination.module.css +25 -0
- package/dist/components/panel/Panel.cjs +38 -0
- package/dist/components/panel/Panel.js +32 -0
- package/dist/components/panel/Panel.module.css +30 -0
- package/dist/components/popover/Popover.cjs +331 -0
- package/dist/components/popover/Popover.js +307 -0
- package/dist/components/popover/Popover.module.css +52 -0
- package/dist/components/search-box/SearchBox.cjs +274 -0
- package/dist/components/search-box/SearchBox.js +268 -0
- package/dist/components/search-box/SearchBox.module.css +27 -0
- package/dist/components/segmented-progress-bar/SegmentedProgressBar.cjs +127 -0
- package/dist/components/segmented-progress-bar/SegmentedProgressBar.js +121 -0
- package/dist/components/segmented-progress-bar/SegmentedProgressBar.module.css +174 -0
- package/dist/components/sidebar/Sidebar.cjs +39 -0
- package/dist/components/sidebar/Sidebar.js +37 -0
- package/dist/components/sidebar/components/SidebarItem.cjs +29 -0
- package/dist/components/sidebar/components/SidebarItem.js +27 -0
- package/dist/components/sidebar/components/SidebarItem.module.css +0 -0
- package/dist/components/sidebar/components/expandable-sidebar-item/ExpandableSidebarItem.cjs +133 -0
- package/dist/components/sidebar/components/expandable-sidebar-item/ExpandableSidebarItem.js +127 -0
- package/dist/components/sidebar/components/expandable-sidebar-item/ExpandableSidebarItem.module.css +29 -0
- package/dist/components/sidebar/components/sidebar-container/SidebarContainer.cjs +214 -0
- package/dist/components/sidebar/components/sidebar-container/SidebarContainer.js +207 -0
- package/dist/components/sidebar/components/sidebar-container/SidebarContainer.module.css +260 -0
- package/dist/components/sidebar/components/sidebar-item-content/SidebarItemContent.cjs +54 -0
- package/dist/components/sidebar/components/sidebar-item-content/SidebarItemContent.js +47 -0
- package/dist/components/sidebar/components/sidebar-item-content/SidebarItemContent.module.css +106 -0
- package/dist/components/sidebar/components/sidebar-items/SidebarItems.cjs +61 -0
- package/dist/components/sidebar/components/sidebar-items/SidebarItems.js +55 -0
- package/dist/components/sidebar/components/sidebar-items/SidebarItems.module.css +20 -0
- package/dist/components/sidebar/components/sidenav-filteirng/SidenavFiltering.cjs +97 -0
- package/dist/components/sidebar/components/sidenav-filteirng/SidenavFiltering.js +91 -0
- package/dist/components/sidebar/providers/SidebarProvider.cjs +223 -0
- package/dist/components/sidebar/providers/SidebarProvider.js +220 -0
- package/dist/components/skeleton-loader/SkeletonLoader.cjs +442 -0
- package/dist/components/skeleton-loader/SkeletonLoader.js +440 -0
- package/dist/components/skeleton-loader/skeleton-loader-item/SkeletonLoaderItem.cjs +42 -0
- package/dist/components/skeleton-loader/skeleton-loader-item/SkeletonLoaderItem.js +36 -0
- package/dist/components/skeleton-loader/skeleton-loader-item/SkeletonLoaderItem.module.css +51 -0
- package/dist/components/split-button/SplitButton.cjs +46 -0
- package/dist/components/split-button/SplitButton.js +40 -0
- package/dist/components/split-button/SplitButton.module.css +32 -0
- package/dist/components/split-pane/SplitPane.cjs +177 -0
- package/dist/components/split-pane/SplitPane.js +168 -0
- package/dist/components/split-pane/SplitPane.module.css +111 -0
- package/dist/components/split-pane/provider/SplitPaneContext.cjs +129 -0
- package/dist/components/split-pane/provider/SplitPaneContext.js +121 -0
- package/dist/components/stack/Stack.cjs +47 -0
- package/dist/components/stack/Stack.d.ts +1 -1
- package/dist/components/stack/Stack.js +41 -0
- package/dist/components/stack/Stack.module.css +61 -0
- package/dist/components/state-page/StatePage.cjs +48 -0
- package/dist/components/state-page/StatePage.js +42 -0
- package/dist/components/state-page/StatePage.module.css +9 -0
- package/dist/components/state-page/empty.cjs +664 -0
- package/dist/components/state-page/empty.js +662 -0
- package/dist/components/state-page/error.cjs +310 -0
- package/dist/components/state-page/error.js +308 -0
- package/dist/components/state-page/notFound.cjs +243 -0
- package/dist/components/state-page/notFound.js +241 -0
- package/dist/components/sticky-footer-layout/StickyFooterLayout.cjs +98 -0
- package/dist/components/sticky-footer-layout/StickyFooterLayout.js +96 -0
- package/dist/components/table/Table.cjs +216 -0
- package/dist/components/table/Table.js +210 -0
- package/dist/components/table/Table.module.css +534 -0
- package/dist/components/table/Table.types.cjs +2 -0
- package/dist/components/table/Table.types.js +1 -0
- package/dist/components/table/TanstackTable.cjs +172 -0
- package/dist/components/table/TanstackTable.js +147 -0
- package/dist/components/table/components/TableBody.cjs +50 -0
- package/dist/components/table/components/TableBody.js +44 -0
- package/dist/components/table/components/TableHeader.cjs +56 -0
- package/dist/components/table/components/TableHeader.js +50 -0
- package/dist/components/table/components/TableHeaderCell.cjs +69 -0
- package/dist/components/table/components/TableHeaderCell.js +63 -0
- package/dist/components/table/components/TableLoadingBody.cjs +35 -0
- package/dist/components/table/components/TableLoadingBody.js +29 -0
- package/dist/components/table/components/TablePagination.cjs +2 -0
- package/dist/components/table/components/TablePagination.js +1 -0
- package/dist/components/table/components/TableRow.cjs +151 -0
- package/dist/components/table/components/TableRow.js +145 -0
- package/dist/components/table/components/TableSelectionCell.cjs +41 -0
- package/dist/components/table/components/TableSelectionCell.js +35 -0
- package/dist/components/table/components/column-resizer/ColumnResizer.cjs +25 -0
- package/dist/components/table/components/column-resizer/ColumnResizer.js +19 -0
- package/dist/components/table/components/column-resizer/ColumnResizer.module.css +22 -0
- package/dist/components/table/components/empty-state/EmptyState.cjs +33 -0
- package/dist/components/table/components/empty-state/EmptyState.js +31 -0
- package/dist/components/table/components/empty-state/EmptyState.module.css +4 -0
- package/dist/components/table/components/table-settings/TableSettings.cjs +139 -0
- package/dist/components/table/components/table-settings/TableSettings.js +137 -0
- package/dist/components/table/hooks/useTableRowInteractions.cjs +46 -0
- package/dist/components/table/hooks/useTableRowInteractions.js +44 -0
- package/dist/components/table/table.classes.cjs +49 -0
- package/dist/components/table/table.classes.js +40 -0
- package/dist/components/table/table.utils.cjs +57 -0
- package/dist/components/table/table.utils.js +45 -0
- package/dist/components/table/tanstackTable.utils.cjs +166 -0
- package/dist/components/table/tanstackTable.utils.js +159 -0
- package/dist/components/tabs/Tabs.cjs +172 -0
- package/dist/components/tabs/Tabs.js +166 -0
- package/dist/components/tabs/Tabs.module.css +261 -0
- package/dist/components/theme-button/ThemeButton.cjs +78 -0
- package/dist/components/theme-button/ThemeButton.js +75 -0
- package/dist/components/toast/Toast.cjs +68 -0
- package/dist/components/toast/Toast.js +62 -0
- package/dist/components/toast/Toast.module.css +161 -0
- package/dist/components/toast/provider/ToastProvider.cjs +105 -0
- package/dist/components/toast/provider/ToastProvider.js +97 -0
- package/dist/components/user-display/UserDisplay.cjs +22 -0
- package/dist/components/user-display/UserDisplay.js +16 -0
- package/dist/components/user-display/UserDisplay.module.css +25 -0
- package/dist/constants/chart-colors.cjs +10 -0
- package/dist/constants/chart-colors.js +8 -0
- package/dist/constants/chart-colors.types.cjs +2 -0
- package/dist/constants/chart-colors.types.js +1 -0
- package/dist/constants/severity.cjs +30 -0
- package/dist/constants/severity.js +26 -0
- package/dist/constants/severity.types.cjs +2 -0
- package/dist/constants/severity.types.js +1 -0
- package/dist/constants/sizes.cjs +11 -0
- package/dist/constants/sizes.js +9 -0
- package/dist/hooks/useDeviceSize.cjs +35 -0
- package/dist/hooks/useDeviceSize.js +33 -0
- package/dist/hooks/useListNavigation.cjs +237 -0
- package/dist/hooks/useListNavigation.js +235 -0
- package/dist/hooks/usePagination.cjs +144 -0
- package/dist/hooks/usePagination.js +142 -0
- package/dist/hooks/useSorting.cjs +119 -0
- package/dist/hooks/useSorting.js +117 -0
- package/dist/hooks/useTableData.cjs +53 -0
- package/dist/hooks/useTableData.js +51 -0
- package/dist/hooks/useTableSelection.cjs +172 -0
- package/dist/hooks/useTableSelection.js +170 -0
- package/dist/hooks/useTableSettings.cjs +90 -0
- package/dist/hooks/useTableSettings.js +87 -0
- package/dist/hooks/useTheme.cjs +64 -0
- package/dist/hooks/useTheme.js +62 -0
- package/dist/hooks/useTimeDuration.cjs +72 -0
- package/dist/hooks/useTimeDuration.js +70 -0
- package/dist/hooks/useViewportFill.cjs +82 -0
- package/dist/hooks/useViewportFill.js +80 -0
- package/dist/index.cjs +161 -12908
- package/dist/index.css +67 -7357
- package/dist/index.d.ts +0 -58
- package/dist/index.js +24 -12809
- package/dist/styles/animation.cjs +9 -0
- package/dist/styles/animation.js +7 -0
- package/dist/styles/themes/dbc/colors.css +5 -0
- package/dist/styles/themes/types.cjs +2 -0
- package/dist/styles/themes/types.js +1 -0
- package/dist/tanstack.cjs +18 -2717
- package/dist/tanstack.js +3 -2701
- package/dist/types/a11y-props.types.cjs +2 -0
- package/dist/types/a11y-props.types.js +1 -0
- package/dist/types/assets.d.cjs +2 -0
- package/dist/types/assets.d.js +1 -0
- package/dist/types/css.d.cjs +2 -0
- package/dist/types/css.d.js +1 -0
- package/dist/types/sizes.types.cjs +2 -0
- package/dist/types/sizes.types.js +1 -0
- package/dist/utils/arrays/nested-filtering.cjs +40 -0
- package/dist/utils/arrays/nested-filtering.js +38 -0
- package/dist/utils/date/formatDate.cjs +35 -0
- package/dist/utils/date/formatDate.js +33 -0
- package/dist/utils/localStorage.utils.cjs +53 -0
- package/dist/utils/localStorage.utils.js +49 -0
- package/dist/utils/text/get-highlighted-segments.cjs +49 -0
- package/dist/utils/text/get-highlighted-segments.js +47 -0
- package/package.json +8 -3
- package/dist/tanstack.css +0 -1385
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useState, useEffect, useRef, useCallback, useMemo } from 'react';
|
|
3
|
+
|
|
4
|
+
function clamp(n, min, max) {
|
|
5
|
+
return Math.max(min, Math.min(max, n));
|
|
6
|
+
}
|
|
7
|
+
function normalizePaginationState(next) {
|
|
8
|
+
return {
|
|
9
|
+
skip: Math.max(0, next.skip),
|
|
10
|
+
take: Math.max(1, next.take)
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function safeParsePaginationState(raw) {
|
|
14
|
+
if (!raw) return null;
|
|
15
|
+
try {
|
|
16
|
+
const parsed = JSON.parse(raw);
|
|
17
|
+
if (typeof parsed !== "object" || parsed == null || typeof parsed.skip !== "number" || typeof parsed.take !== "number") {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return normalizePaginationState({
|
|
21
|
+
skip: parsed.skip,
|
|
22
|
+
take: parsed.take
|
|
23
|
+
});
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function usePagination({
|
|
29
|
+
data = [],
|
|
30
|
+
skip = 0,
|
|
31
|
+
take = 10,
|
|
32
|
+
state,
|
|
33
|
+
onStateChange,
|
|
34
|
+
resetOnDataChange = false,
|
|
35
|
+
storageKey
|
|
36
|
+
}) {
|
|
37
|
+
const isControlled = state != null;
|
|
38
|
+
const [hydrated, setHydrated] = useState(() => !storageKey || isControlled);
|
|
39
|
+
const [uncontrolled, setUncontrolled] = useState(() => {
|
|
40
|
+
var _a;
|
|
41
|
+
const initial = normalizePaginationState({
|
|
42
|
+
skip,
|
|
43
|
+
take
|
|
44
|
+
});
|
|
45
|
+
if (typeof window === "undefined" || !storageKey || isControlled) {
|
|
46
|
+
return initial;
|
|
47
|
+
}
|
|
48
|
+
return (_a = safeParsePaginationState(window.localStorage.getItem(storageKey))) != null ? _a : initial;
|
|
49
|
+
});
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (typeof window === "undefined") return;
|
|
52
|
+
if (isControlled || !storageKey) {
|
|
53
|
+
setHydrated(true);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const fallback = normalizePaginationState({ skip, take });
|
|
57
|
+
const stored = safeParsePaginationState(window.localStorage.getItem(storageKey));
|
|
58
|
+
setUncontrolled(stored != null ? stored : fallback);
|
|
59
|
+
setHydrated(true);
|
|
60
|
+
}, [isControlled, storageKey, skip, take]);
|
|
61
|
+
const didInitRef = useRef(false);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (isControlled) return;
|
|
64
|
+
if (storageKey) return;
|
|
65
|
+
if (!didInitRef.current) {
|
|
66
|
+
didInitRef.current = true;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
setUncontrolled((prev) => ({
|
|
70
|
+
skip: prev.skip,
|
|
71
|
+
take: Math.max(1, take)
|
|
72
|
+
}));
|
|
73
|
+
}, [isControlled, storageKey, take]);
|
|
74
|
+
const paginationState = isControlled ? state : uncontrolled;
|
|
75
|
+
const totalCount = data.length;
|
|
76
|
+
const safeTake = Math.max(1, paginationState.take);
|
|
77
|
+
const maxSkip = Math.max(
|
|
78
|
+
0,
|
|
79
|
+
totalCount === 0 ? 0 : Math.floor((totalCount - 1) / safeTake) * safeTake
|
|
80
|
+
);
|
|
81
|
+
const safeSkip = clamp(Math.max(0, paginationState.skip), 0, maxSkip);
|
|
82
|
+
const setPagination = useCallback(
|
|
83
|
+
(next) => {
|
|
84
|
+
const normalized = normalizePaginationState(next);
|
|
85
|
+
if (isControlled) {
|
|
86
|
+
onStateChange == null ? void 0 : onStateChange(normalized);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
setUncontrolled(normalized);
|
|
90
|
+
},
|
|
91
|
+
[isControlled, onStateChange]
|
|
92
|
+
);
|
|
93
|
+
const onPageChange = useCallback(
|
|
94
|
+
(pageEvent) => {
|
|
95
|
+
const nextTake = Math.max(1, pageEvent.take);
|
|
96
|
+
const nextSkip = Math.max(0, (pageEvent.page - 1) * nextTake);
|
|
97
|
+
setPagination({ skip: nextSkip, take: nextTake });
|
|
98
|
+
},
|
|
99
|
+
[setPagination]
|
|
100
|
+
);
|
|
101
|
+
const resetPage = useCallback(() => {
|
|
102
|
+
setPagination({ skip: 0, take: safeTake });
|
|
103
|
+
}, [setPagination, safeTake]);
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (!resetOnDataChange) return;
|
|
106
|
+
if (!hydrated) return;
|
|
107
|
+
resetPage();
|
|
108
|
+
}, [resetOnDataChange, data, hydrated]);
|
|
109
|
+
const paginatedData = useMemo(() => {
|
|
110
|
+
return data.slice(safeSkip, safeSkip + safeTake);
|
|
111
|
+
}, [data, safeSkip, safeTake]);
|
|
112
|
+
const page = useMemo(() => Math.floor(safeSkip / safeTake) + 1, [safeSkip, safeTake]);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (isControlled) return;
|
|
115
|
+
if (safeSkip !== paginationState.skip) {
|
|
116
|
+
setUncontrolled((prev) => ({ ...prev, skip: safeSkip }));
|
|
117
|
+
}
|
|
118
|
+
}, [isControlled, safeSkip, paginationState.skip]);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (typeof window === "undefined") return;
|
|
121
|
+
if (isControlled || !storageKey) return;
|
|
122
|
+
if (!hydrated) return;
|
|
123
|
+
window.localStorage.setItem(
|
|
124
|
+
storageKey,
|
|
125
|
+
JSON.stringify({
|
|
126
|
+
skip: paginationState.skip,
|
|
127
|
+
take: paginationState.take
|
|
128
|
+
})
|
|
129
|
+
);
|
|
130
|
+
}, [hydrated, isControlled, paginationState.skip, paginationState.take, storageKey]);
|
|
131
|
+
return {
|
|
132
|
+
paginatedData,
|
|
133
|
+
paginationState: { skip: safeSkip, take: safeTake },
|
|
134
|
+
onPageChange,
|
|
135
|
+
setPagination,
|
|
136
|
+
resetPage,
|
|
137
|
+
page,
|
|
138
|
+
totalCount
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { usePagination };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
function defaultCompare(a, b, dir, nulls) {
|
|
7
|
+
const aNull = a == null;
|
|
8
|
+
const bNull = b == null;
|
|
9
|
+
if (aNull || bNull) {
|
|
10
|
+
if (aNull && bNull) return 0;
|
|
11
|
+
const order = nulls === "first" ? -1 : 1;
|
|
12
|
+
return aNull ? order : -order;
|
|
13
|
+
}
|
|
14
|
+
if (a instanceof Date && b instanceof Date) {
|
|
15
|
+
const diff2 = a.getTime() - b.getTime();
|
|
16
|
+
return dir === "asc" ? diff2 : -diff2;
|
|
17
|
+
}
|
|
18
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
19
|
+
const diff2 = a - b;
|
|
20
|
+
return dir === "asc" ? diff2 : -diff2;
|
|
21
|
+
}
|
|
22
|
+
if (typeof a === "boolean" && typeof b === "boolean") {
|
|
23
|
+
const diff2 = Number(a) - Number(b);
|
|
24
|
+
return dir === "asc" ? diff2 : -diff2;
|
|
25
|
+
}
|
|
26
|
+
const aStr = String(a);
|
|
27
|
+
const bStr = String(b);
|
|
28
|
+
const diff = aStr.localeCompare(bStr, void 0, { numeric: true, sensitivity: "base" });
|
|
29
|
+
return dir === "asc" ? diff : -diff;
|
|
30
|
+
}
|
|
31
|
+
function stableSort(arr, cmp) {
|
|
32
|
+
return arr.map((item, idx) => ({ item, idx })).sort((x, y) => {
|
|
33
|
+
const c = cmp(x.item, y.item);
|
|
34
|
+
return c !== 0 ? c : x.idx - y.idx;
|
|
35
|
+
}).map((x) => x.item);
|
|
36
|
+
}
|
|
37
|
+
function useSorting({
|
|
38
|
+
data = [],
|
|
39
|
+
sortBy = null,
|
|
40
|
+
sortDirection = "asc",
|
|
41
|
+
state,
|
|
42
|
+
onStateChange,
|
|
43
|
+
columnComparators = {},
|
|
44
|
+
nulls = "last",
|
|
45
|
+
allowUnsort = true,
|
|
46
|
+
resetOnDataChange = false
|
|
47
|
+
}) {
|
|
48
|
+
const isControlled = state != null;
|
|
49
|
+
const [uncontrolled, setUncontrolled] = react.useState(() => ({
|
|
50
|
+
sortBy,
|
|
51
|
+
sortDirection
|
|
52
|
+
}));
|
|
53
|
+
const sortState = isControlled ? state : uncontrolled;
|
|
54
|
+
const setSort = react.useCallback(
|
|
55
|
+
(next) => {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
const normalized = {
|
|
58
|
+
sortBy: (_a = next.sortBy) != null ? _a : null,
|
|
59
|
+
sortDirection: (_b = next.sortDirection) != null ? _b : null
|
|
60
|
+
};
|
|
61
|
+
if (isControlled) onStateChange == null ? void 0 : onStateChange(normalized);
|
|
62
|
+
else setUncontrolled(normalized);
|
|
63
|
+
},
|
|
64
|
+
[isControlled, onStateChange]
|
|
65
|
+
);
|
|
66
|
+
const clearSort = react.useCallback(() => setSort({ sortBy: null, sortDirection: null }), [setSort]);
|
|
67
|
+
const onSortChange = react.useCallback(
|
|
68
|
+
(e) => {
|
|
69
|
+
var _a, _b;
|
|
70
|
+
const nextSortBy = (_a = e.sortBy) != null ? _a : null;
|
|
71
|
+
setSort(sortState);
|
|
72
|
+
const current = sortState;
|
|
73
|
+
if (nextSortBy == null) {
|
|
74
|
+
return clearSort();
|
|
75
|
+
}
|
|
76
|
+
if (current.sortBy === nextSortBy) {
|
|
77
|
+
const nextDir = current.sortDirection === "asc" ? "desc" : allowUnsort ? null : "asc";
|
|
78
|
+
return setSort({ sortBy: nextSortBy, sortDirection: nextDir });
|
|
79
|
+
}
|
|
80
|
+
return setSort({
|
|
81
|
+
sortBy: nextSortBy,
|
|
82
|
+
sortDirection: (_b = e.sortDirection) != null ? _b : "asc"
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
[allowUnsort, clearSort, setSort, sortState]
|
|
86
|
+
);
|
|
87
|
+
const didInitRef = react.useRef(false);
|
|
88
|
+
react.useEffect(() => {
|
|
89
|
+
if (!resetOnDataChange) return;
|
|
90
|
+
if (!didInitRef.current) {
|
|
91
|
+
didInitRef.current = true;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
clearSort();
|
|
95
|
+
}, [resetOnDataChange, data]);
|
|
96
|
+
const sortedData = react.useMemo(() => {
|
|
97
|
+
const { sortBy: sb, sortDirection: dir } = sortState;
|
|
98
|
+
if (!sb || !dir) return data;
|
|
99
|
+
const custom = columnComparators[sb];
|
|
100
|
+
const cmp = (a, b) => {
|
|
101
|
+
if (custom) return custom(a, b, dir);
|
|
102
|
+
const av = a[sb];
|
|
103
|
+
const bv = b[sb];
|
|
104
|
+
return defaultCompare(av, bv, dir, nulls);
|
|
105
|
+
};
|
|
106
|
+
return stableSort(data, cmp);
|
|
107
|
+
}, [data, sortState, columnComparators, nulls]);
|
|
108
|
+
const setSortRef = react.useRef(setSort);
|
|
109
|
+
setSortRef.current = setSort;
|
|
110
|
+
return {
|
|
111
|
+
sortedData,
|
|
112
|
+
sortState,
|
|
113
|
+
onSortChange,
|
|
114
|
+
setSort,
|
|
115
|
+
clearSort
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
exports.useSorting = useSorting;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useState, useCallback, useRef, useEffect, useMemo } from 'react';
|
|
3
|
+
|
|
4
|
+
function defaultCompare(a, b, dir, nulls) {
|
|
5
|
+
const aNull = a == null;
|
|
6
|
+
const bNull = b == null;
|
|
7
|
+
if (aNull || bNull) {
|
|
8
|
+
if (aNull && bNull) return 0;
|
|
9
|
+
const order = nulls === "first" ? -1 : 1;
|
|
10
|
+
return aNull ? order : -order;
|
|
11
|
+
}
|
|
12
|
+
if (a instanceof Date && b instanceof Date) {
|
|
13
|
+
const diff2 = a.getTime() - b.getTime();
|
|
14
|
+
return dir === "asc" ? diff2 : -diff2;
|
|
15
|
+
}
|
|
16
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
17
|
+
const diff2 = a - b;
|
|
18
|
+
return dir === "asc" ? diff2 : -diff2;
|
|
19
|
+
}
|
|
20
|
+
if (typeof a === "boolean" && typeof b === "boolean") {
|
|
21
|
+
const diff2 = Number(a) - Number(b);
|
|
22
|
+
return dir === "asc" ? diff2 : -diff2;
|
|
23
|
+
}
|
|
24
|
+
const aStr = String(a);
|
|
25
|
+
const bStr = String(b);
|
|
26
|
+
const diff = aStr.localeCompare(bStr, void 0, { numeric: true, sensitivity: "base" });
|
|
27
|
+
return dir === "asc" ? diff : -diff;
|
|
28
|
+
}
|
|
29
|
+
function stableSort(arr, cmp) {
|
|
30
|
+
return arr.map((item, idx) => ({ item, idx })).sort((x, y) => {
|
|
31
|
+
const c = cmp(x.item, y.item);
|
|
32
|
+
return c !== 0 ? c : x.idx - y.idx;
|
|
33
|
+
}).map((x) => x.item);
|
|
34
|
+
}
|
|
35
|
+
function useSorting({
|
|
36
|
+
data = [],
|
|
37
|
+
sortBy = null,
|
|
38
|
+
sortDirection = "asc",
|
|
39
|
+
state,
|
|
40
|
+
onStateChange,
|
|
41
|
+
columnComparators = {},
|
|
42
|
+
nulls = "last",
|
|
43
|
+
allowUnsort = true,
|
|
44
|
+
resetOnDataChange = false
|
|
45
|
+
}) {
|
|
46
|
+
const isControlled = state != null;
|
|
47
|
+
const [uncontrolled, setUncontrolled] = useState(() => ({
|
|
48
|
+
sortBy,
|
|
49
|
+
sortDirection
|
|
50
|
+
}));
|
|
51
|
+
const sortState = isControlled ? state : uncontrolled;
|
|
52
|
+
const setSort = useCallback(
|
|
53
|
+
(next) => {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
const normalized = {
|
|
56
|
+
sortBy: (_a = next.sortBy) != null ? _a : null,
|
|
57
|
+
sortDirection: (_b = next.sortDirection) != null ? _b : null
|
|
58
|
+
};
|
|
59
|
+
if (isControlled) onStateChange == null ? void 0 : onStateChange(normalized);
|
|
60
|
+
else setUncontrolled(normalized);
|
|
61
|
+
},
|
|
62
|
+
[isControlled, onStateChange]
|
|
63
|
+
);
|
|
64
|
+
const clearSort = useCallback(() => setSort({ sortBy: null, sortDirection: null }), [setSort]);
|
|
65
|
+
const onSortChange = useCallback(
|
|
66
|
+
(e) => {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
const nextSortBy = (_a = e.sortBy) != null ? _a : null;
|
|
69
|
+
setSort(sortState);
|
|
70
|
+
const current = sortState;
|
|
71
|
+
if (nextSortBy == null) {
|
|
72
|
+
return clearSort();
|
|
73
|
+
}
|
|
74
|
+
if (current.sortBy === nextSortBy) {
|
|
75
|
+
const nextDir = current.sortDirection === "asc" ? "desc" : allowUnsort ? null : "asc";
|
|
76
|
+
return setSort({ sortBy: nextSortBy, sortDirection: nextDir });
|
|
77
|
+
}
|
|
78
|
+
return setSort({
|
|
79
|
+
sortBy: nextSortBy,
|
|
80
|
+
sortDirection: (_b = e.sortDirection) != null ? _b : "asc"
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
[allowUnsort, clearSort, setSort, sortState]
|
|
84
|
+
);
|
|
85
|
+
const didInitRef = useRef(false);
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
if (!resetOnDataChange) return;
|
|
88
|
+
if (!didInitRef.current) {
|
|
89
|
+
didInitRef.current = true;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
clearSort();
|
|
93
|
+
}, [resetOnDataChange, data]);
|
|
94
|
+
const sortedData = useMemo(() => {
|
|
95
|
+
const { sortBy: sb, sortDirection: dir } = sortState;
|
|
96
|
+
if (!sb || !dir) return data;
|
|
97
|
+
const custom = columnComparators[sb];
|
|
98
|
+
const cmp = (a, b) => {
|
|
99
|
+
if (custom) return custom(a, b, dir);
|
|
100
|
+
const av = a[sb];
|
|
101
|
+
const bv = b[sb];
|
|
102
|
+
return defaultCompare(av, bv, dir, nulls);
|
|
103
|
+
};
|
|
104
|
+
return stableSort(data, cmp);
|
|
105
|
+
}, [data, sortState, columnComparators, nulls]);
|
|
106
|
+
const setSortRef = useRef(setSort);
|
|
107
|
+
setSortRef.current = setSort;
|
|
108
|
+
return {
|
|
109
|
+
sortedData,
|
|
110
|
+
sortState,
|
|
111
|
+
onSortChange,
|
|
112
|
+
setSort,
|
|
113
|
+
clearSort
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { useSorting };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var usePagination = require('./usePagination');
|
|
6
|
+
var useSorting = require('./useSorting');
|
|
7
|
+
|
|
8
|
+
function useTableData({
|
|
9
|
+
data = [],
|
|
10
|
+
pagination,
|
|
11
|
+
sorting,
|
|
12
|
+
resetPageOnSortChange = true
|
|
13
|
+
}) {
|
|
14
|
+
var _a, _b, _c, _d, _e, _f;
|
|
15
|
+
const s = useSorting.useSorting({
|
|
16
|
+
data,
|
|
17
|
+
sortBy: (_a = sorting == null ? void 0 : sorting.sortBy) != null ? _a : null,
|
|
18
|
+
sortDirection: (_b = sorting == null ? void 0 : sorting.direction) != null ? _b : null,
|
|
19
|
+
state: sorting == null ? void 0 : sorting.state,
|
|
20
|
+
onStateChange: sorting == null ? void 0 : sorting.onStateChange,
|
|
21
|
+
columnComparators: sorting == null ? void 0 : sorting.columnComparators,
|
|
22
|
+
nulls: (_c = sorting == null ? void 0 : sorting.nulls) != null ? _c : "last",
|
|
23
|
+
allowUnsort: (_d = sorting == null ? void 0 : sorting.allowUnsort) != null ? _d : true
|
|
24
|
+
});
|
|
25
|
+
const p = usePagination.usePagination({
|
|
26
|
+
data: s.sortedData,
|
|
27
|
+
skip: (_e = pagination == null ? void 0 : pagination.skip) != null ? _e : 0,
|
|
28
|
+
take: (_f = pagination == null ? void 0 : pagination.take) != null ? _f : 10,
|
|
29
|
+
state: pagination == null ? void 0 : pagination.state,
|
|
30
|
+
onStateChange: pagination == null ? void 0 : pagination.onStateChange
|
|
31
|
+
});
|
|
32
|
+
react.useEffect(() => {
|
|
33
|
+
if (!resetPageOnSortChange) return;
|
|
34
|
+
p.resetPage();
|
|
35
|
+
}, [resetPageOnSortChange, s.sortState.sortBy, s.sortState.sortDirection]);
|
|
36
|
+
const rows = react.useMemo(() => p.paginatedData, [p.paginatedData]);
|
|
37
|
+
return {
|
|
38
|
+
rows,
|
|
39
|
+
totalCount: s.sortedData.length,
|
|
40
|
+
pagination: {
|
|
41
|
+
state: p.paginationState,
|
|
42
|
+
onPageChange: p.onPageChange,
|
|
43
|
+
resetPage: p.resetPage
|
|
44
|
+
},
|
|
45
|
+
sorting: {
|
|
46
|
+
state: s.sortState,
|
|
47
|
+
onSortChange: s.onSortChange,
|
|
48
|
+
clearSort: s.clearSort
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.useTableData = useTableData;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useEffect, useMemo } from 'react';
|
|
3
|
+
import { usePagination } from './usePagination';
|
|
4
|
+
import { useSorting } from './useSorting';
|
|
5
|
+
|
|
6
|
+
function useTableData({
|
|
7
|
+
data = [],
|
|
8
|
+
pagination,
|
|
9
|
+
sorting,
|
|
10
|
+
resetPageOnSortChange = true
|
|
11
|
+
}) {
|
|
12
|
+
var _a, _b, _c, _d, _e, _f;
|
|
13
|
+
const s = useSorting({
|
|
14
|
+
data,
|
|
15
|
+
sortBy: (_a = sorting == null ? void 0 : sorting.sortBy) != null ? _a : null,
|
|
16
|
+
sortDirection: (_b = sorting == null ? void 0 : sorting.direction) != null ? _b : null,
|
|
17
|
+
state: sorting == null ? void 0 : sorting.state,
|
|
18
|
+
onStateChange: sorting == null ? void 0 : sorting.onStateChange,
|
|
19
|
+
columnComparators: sorting == null ? void 0 : sorting.columnComparators,
|
|
20
|
+
nulls: (_c = sorting == null ? void 0 : sorting.nulls) != null ? _c : "last",
|
|
21
|
+
allowUnsort: (_d = sorting == null ? void 0 : sorting.allowUnsort) != null ? _d : true
|
|
22
|
+
});
|
|
23
|
+
const p = usePagination({
|
|
24
|
+
data: s.sortedData,
|
|
25
|
+
skip: (_e = pagination == null ? void 0 : pagination.skip) != null ? _e : 0,
|
|
26
|
+
take: (_f = pagination == null ? void 0 : pagination.take) != null ? _f : 10,
|
|
27
|
+
state: pagination == null ? void 0 : pagination.state,
|
|
28
|
+
onStateChange: pagination == null ? void 0 : pagination.onStateChange
|
|
29
|
+
});
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!resetPageOnSortChange) return;
|
|
32
|
+
p.resetPage();
|
|
33
|
+
}, [resetPageOnSortChange, s.sortState.sortBy, s.sortState.sortDirection]);
|
|
34
|
+
const rows = useMemo(() => p.paginatedData, [p.paginatedData]);
|
|
35
|
+
return {
|
|
36
|
+
rows,
|
|
37
|
+
totalCount: s.sortedData.length,
|
|
38
|
+
pagination: {
|
|
39
|
+
state: p.paginationState,
|
|
40
|
+
onPageChange: p.onPageChange,
|
|
41
|
+
resetPage: p.resetPage
|
|
42
|
+
},
|
|
43
|
+
sorting: {
|
|
44
|
+
state: s.sortState,
|
|
45
|
+
onSortChange: s.onSortChange,
|
|
46
|
+
clearSort: s.clearSort
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { useTableData };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
const EMPTY_IDS = /* @__PURE__ */ new Set();
|
|
7
|
+
function safeParseIds(raw) {
|
|
8
|
+
if (!raw) return null;
|
|
9
|
+
try {
|
|
10
|
+
const parsed = JSON.parse(raw);
|
|
11
|
+
if (!Array.isArray(parsed)) return null;
|
|
12
|
+
return parsed.filter((v) => typeof v === "string" || typeof v === "number");
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function serializeIds(ids) {
|
|
18
|
+
return JSON.stringify(Array.from(ids));
|
|
19
|
+
}
|
|
20
|
+
function areSetsEqual(a, b) {
|
|
21
|
+
if (a.size !== b.size) return false;
|
|
22
|
+
for (const value of a) {
|
|
23
|
+
if (!b.has(value)) return false;
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
function useTableSelection({
|
|
28
|
+
storageKey,
|
|
29
|
+
items,
|
|
30
|
+
getId,
|
|
31
|
+
initialSelectedIds,
|
|
32
|
+
onSelectionChange,
|
|
33
|
+
selectionMode = "single",
|
|
34
|
+
pruneToItems = false,
|
|
35
|
+
storage = "session"
|
|
36
|
+
}) {
|
|
37
|
+
const resolvedInitialSelectedIds = initialSelectedIds != null ? initialSelectedIds : EMPTY_IDS;
|
|
38
|
+
const [selectedIds, setSelectedIds] = react.useState(resolvedInitialSelectedIds);
|
|
39
|
+
const [hydrated, setHydrated] = react.useState(false);
|
|
40
|
+
const lastWrittenRef = react.useRef(null);
|
|
41
|
+
const itemsById = react.useMemo(() => {
|
|
42
|
+
const map = /* @__PURE__ */ new Map();
|
|
43
|
+
for (const item of items) {
|
|
44
|
+
map.set(getId(item), item);
|
|
45
|
+
}
|
|
46
|
+
return map;
|
|
47
|
+
}, [items, getId]);
|
|
48
|
+
react.useEffect(() => {
|
|
49
|
+
if (typeof window === "undefined") return;
|
|
50
|
+
const storageApi = storage === "local" ? window.localStorage : window.sessionStorage;
|
|
51
|
+
if (!storageKey) {
|
|
52
|
+
setSelectedIds(
|
|
53
|
+
(prev) => areSetsEqual(prev, resolvedInitialSelectedIds) ? prev : new Set(resolvedInitialSelectedIds)
|
|
54
|
+
);
|
|
55
|
+
setHydrated(true);
|
|
56
|
+
lastWrittenRef.current = null;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const parsed = safeParseIds(storageApi.getItem(storageKey));
|
|
60
|
+
const next = new Set(parsed != null ? parsed : Array.from(resolvedInitialSelectedIds));
|
|
61
|
+
setSelectedIds((prev) => areSetsEqual(prev, next) ? prev : next);
|
|
62
|
+
lastWrittenRef.current = serializeIds(next);
|
|
63
|
+
setHydrated(true);
|
|
64
|
+
}, [storage, storageKey, resolvedInitialSelectedIds]);
|
|
65
|
+
react.useEffect(() => {
|
|
66
|
+
if (!pruneToItems) return;
|
|
67
|
+
const visibleIds = new Set(Array.from(itemsById.keys()));
|
|
68
|
+
setSelectedIds((prev) => {
|
|
69
|
+
if (prev.size === 0) return prev;
|
|
70
|
+
if (visibleIds.size === 0) return prev;
|
|
71
|
+
const next = /* @__PURE__ */ new Set();
|
|
72
|
+
for (const id of prev) {
|
|
73
|
+
if (visibleIds.has(id)) next.add(id);
|
|
74
|
+
}
|
|
75
|
+
return areSetsEqual(prev, next) ? prev : next;
|
|
76
|
+
});
|
|
77
|
+
}, [pruneToItems, itemsById]);
|
|
78
|
+
const selectedItemMap = react.useMemo(() => {
|
|
79
|
+
const map = /* @__PURE__ */ new Map();
|
|
80
|
+
for (const id of selectedIds) {
|
|
81
|
+
const item = itemsById.get(id);
|
|
82
|
+
if (item !== void 0) map.set(id, item);
|
|
83
|
+
}
|
|
84
|
+
return map;
|
|
85
|
+
}, [selectedIds, itemsById]);
|
|
86
|
+
const selectedItems = react.useMemo(() => Array.from(selectedItemMap.values()), [selectedItemMap]);
|
|
87
|
+
const allSelected = react.useMemo(() => {
|
|
88
|
+
if (items.length === 0) return false;
|
|
89
|
+
return items.every((item) => selectedIds.has(getId(item)));
|
|
90
|
+
}, [items, selectedIds, getId]);
|
|
91
|
+
const anySelected = react.useMemo(() => selectedIds.size > 0, [selectedIds]);
|
|
92
|
+
react.useEffect(() => {
|
|
93
|
+
if (!hydrated) return;
|
|
94
|
+
if (typeof window === "undefined") return;
|
|
95
|
+
if (storageKey) {
|
|
96
|
+
const storageApi = storage === "local" ? window.localStorage : window.sessionStorage;
|
|
97
|
+
const nextStr = serializeIds(selectedIds);
|
|
98
|
+
if (lastWrittenRef.current !== nextStr) {
|
|
99
|
+
storageApi.setItem(storageKey, nextStr);
|
|
100
|
+
lastWrittenRef.current = nextStr;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
onSelectionChange == null ? void 0 : onSelectionChange({ selectedIds, selectedItems });
|
|
104
|
+
}, [hydrated, onSelectionChange, selectedIds, selectedItems, storage, storageKey]);
|
|
105
|
+
const toggleId = react.useCallback(
|
|
106
|
+
(id, selected) => {
|
|
107
|
+
setSelectedIds((prev) => {
|
|
108
|
+
const next = new Set(prev);
|
|
109
|
+
const isSelected = next.has(id);
|
|
110
|
+
const shouldSelect = selected === void 0 ? !isSelected : selected;
|
|
111
|
+
if (selectionMode === "single") {
|
|
112
|
+
next.clear();
|
|
113
|
+
if (shouldSelect) next.add(id);
|
|
114
|
+
return areSetsEqual(prev, next) ? prev : next;
|
|
115
|
+
}
|
|
116
|
+
if (shouldSelect) next.add(id);
|
|
117
|
+
else next.delete(id);
|
|
118
|
+
return areSetsEqual(prev, next) ? prev : next;
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
[selectionMode]
|
|
122
|
+
);
|
|
123
|
+
const toggleItem = react.useCallback(
|
|
124
|
+
(item) => {
|
|
125
|
+
toggleId(getId(item));
|
|
126
|
+
},
|
|
127
|
+
[toggleId, getId]
|
|
128
|
+
);
|
|
129
|
+
const selectOnly = react.useCallback((id) => {
|
|
130
|
+
setSelectedIds((prev) => {
|
|
131
|
+
const next = /* @__PURE__ */ new Set([id]);
|
|
132
|
+
return areSetsEqual(prev, next) ? prev : next;
|
|
133
|
+
});
|
|
134
|
+
}, []);
|
|
135
|
+
const clearSelection = react.useCallback(() => {
|
|
136
|
+
setSelectedIds((prev) => prev.size === 0 ? prev : /* @__PURE__ */ new Set());
|
|
137
|
+
}, []);
|
|
138
|
+
const toggleAll = react.useCallback(
|
|
139
|
+
(selected) => {
|
|
140
|
+
if (!selected) {
|
|
141
|
+
clearSelection();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (selectionMode === "single") {
|
|
145
|
+
const first = items[0];
|
|
146
|
+
const next2 = first ? /* @__PURE__ */ new Set([getId(first)]) : /* @__PURE__ */ new Set();
|
|
147
|
+
setSelectedIds((prev) => areSetsEqual(prev, next2) ? prev : next2);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const next = /* @__PURE__ */ new Set();
|
|
151
|
+
for (const item of items) {
|
|
152
|
+
next.add(getId(item));
|
|
153
|
+
}
|
|
154
|
+
setSelectedIds((prev) => areSetsEqual(prev, next) ? prev : next);
|
|
155
|
+
},
|
|
156
|
+
[clearSelection, getId, items, selectionMode]
|
|
157
|
+
);
|
|
158
|
+
return {
|
|
159
|
+
selectedIds,
|
|
160
|
+
selectedItems,
|
|
161
|
+
selectedItemMap,
|
|
162
|
+
toggleItem,
|
|
163
|
+
toggleId,
|
|
164
|
+
selectOnly,
|
|
165
|
+
clearSelection,
|
|
166
|
+
allSelected,
|
|
167
|
+
anySelected,
|
|
168
|
+
toggleAll
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
exports.useTableSelection = useTableSelection;
|