@appdirect/design-system 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/dist/Buttons/ActionIcon.tsx +37 -0
- package/dist/Buttons/Button.module.css +5 -0
- package/dist/Buttons/Button.tsx +124 -0
- package/dist/Buttons/CloseButton.tsx +83 -0
- package/dist/Buttons/index.ts +20 -0
- package/dist/Combobox/AutocompleteClearable.tsx +128 -0
- package/dist/Combobox/Combobox.tsx +37 -0
- package/dist/Combobox/Multiselect.tsx +158 -0
- package/dist/Combobox/SearchableSelect.tsx +131 -0
- package/dist/Combobox/Select.tsx +154 -0
- package/dist/Combobox/index.ts +20 -0
- package/dist/ComplexComponents/DashboardWidget/DashboardWidget.tsx +313 -0
- package/dist/ComplexComponents/DashboardWidget/index.ts +2 -0
- package/dist/ComplexComponents/DataTable/CustomBottomToolbar/index.tsx +177 -0
- package/dist/ComplexComponents/DataTable/CustomBottomToolbar/utils.ts +35 -0
- package/dist/ComplexComponents/DataTable/CustomToolbarAlertBannerContent/index.tsx +74 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/DateRangeSelectorFilter/index.tsx +358 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterChip.tsx +48 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/DateRangeFilter.tsx +26 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/FilterRenderer.tsx +71 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/FilterSystem.tsx +158 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/MultiSelectFilter.tsx +79 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/SingleSelectFilter.tsx +65 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/index.ts +70 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/FilterSystem/types.ts +140 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/ResultCount/index.tsx +76 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/SearchFilter.tsx +73 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/SelectFilter.tsx +50 -0
- package/dist/ComplexComponents/DataTable/CustomTopToolbar/index.tsx +788 -0
- package/dist/ComplexComponents/DataTable/DataTable.tsx +582 -0
- package/dist/ComplexComponents/DataTable/ErrorOverlay/index.tsx +29 -0
- package/dist/ComplexComponents/DataTable/SortableHeaderCell/index.tsx +57 -0
- package/dist/ComplexComponents/DataTable/index.ts +2 -0
- package/dist/ComplexComponents/DataTable/translations/en.ts +115 -0
- package/dist/ComplexComponents/DataTable/translations/index.ts +23 -0
- package/dist/ComplexComponents/KeyInsight/KeyInsights.tsx +221 -0
- package/dist/ComplexComponents/KeyInsight/index.ts +2 -0
- package/dist/ComplexComponents/NameValue/NameValue.tsx +175 -0
- package/dist/ComplexComponents/NameValue/NameValueItem.tsx +100 -0
- package/dist/ComplexComponents/NameValue/index.ts +4 -0
- package/dist/ComplexComponents/PageContentHeader/DescriptionBlock.tsx +65 -0
- package/dist/ComplexComponents/PageContentHeader/PageContentHeader.tsx +383 -0
- package/dist/ComplexComponents/PageContentHeader/index.ts +4 -0
- package/dist/ComplexComponents/Utilities/CopyButton.tsx +82 -0
- package/dist/ComplexComponents/Utilities/index.ts +2 -0
- package/dist/DataDisplay/Alert.tsx +130 -0
- package/dist/DataDisplay/Avatar.tsx +216 -0
- package/dist/DataDisplay/Badge.tsx +119 -0
- package/dist/DataDisplay/Card.module.css +26 -0
- package/dist/DataDisplay/Card.tsx +163 -0
- package/dist/DataDisplay/Chip.tsx +66 -0
- package/dist/DataDisplay/Indicator.tsx +122 -0
- package/dist/DataDisplay/List.tsx +90 -0
- package/dist/DataDisplay/Pill.tsx +125 -0
- package/dist/DataDisplay/Progress.tsx +77 -0
- package/dist/DataDisplay/Table.tsx +57 -0
- package/dist/DataDisplay/ThemeIcon.tsx +67 -0
- package/dist/DataDisplay/index.ts +41 -0
- package/dist/Inputs/Checkbox.tsx +94 -0
- package/dist/Inputs/ColorInput.tsx +124 -0
- package/dist/Inputs/DropZone.tsx +78 -0
- package/dist/Inputs/FileInput.tsx +120 -0
- package/dist/Inputs/NumberInput.tsx +125 -0
- package/dist/Inputs/Radio.tsx +86 -0
- package/dist/Inputs/RadioGroup.tsx +90 -0
- package/dist/Inputs/SegmentedControl.tsx +64 -0
- package/dist/Inputs/Slider.tsx +69 -0
- package/dist/Inputs/Switch.tsx +67 -0
- package/dist/Inputs/TextArea.tsx +126 -0
- package/dist/Inputs/TextInput.tsx +167 -0
- package/dist/Inputs/index.ts +51 -0
- package/dist/Layout/Box.tsx +82 -0
- package/dist/Layout/Center.tsx +17 -0
- package/dist/Layout/Container.tsx +17 -0
- package/dist/Layout/Flex.tsx +18 -0
- package/dist/Layout/Grid.tsx +31 -0
- package/dist/Layout/Group.tsx +54 -0
- package/dist/Layout/Inline.tsx +86 -0
- package/dist/Layout/SimpleGrid.tsx +50 -0
- package/dist/Layout/Stack.tsx +86 -0
- package/dist/Layout/index.ts +29 -0
- package/dist/Misc/Divider.tsx +27 -0
- package/dist/Misc/Paper.tsx +47 -0
- package/dist/Misc/index.ts +15 -0
- package/dist/Navigation/Breadcrumb.tsx +138 -0
- package/dist/Navigation/NavLink.tsx +37 -0
- package/dist/Navigation/Stepper.tsx +17 -0
- package/dist/Navigation/Tabs.tsx +29 -0
- package/dist/Navigation/index.ts +17 -0
- package/dist/Overlays/Drawer.tsx +123 -0
- package/dist/Overlays/Menu.tsx +192 -0
- package/dist/Overlays/Modal.tsx +159 -0
- package/dist/Overlays/Popover.tsx +319 -0
- package/dist/Overlays/Tooltip.tsx +131 -0
- package/dist/Overlays/index.ts +19 -0
- package/dist/Shell/AppShellLayout.tsx +56 -0
- package/dist/Shell/HeaderBar.tsx +26 -0
- package/dist/Shell/SidebarNav.tsx +47 -0
- package/dist/Shell/SidebarNavLink.tsx +50 -0
- package/dist/Shell/SingleColumnLayout.tsx +22 -0
- package/dist/Shell/TertiaryColumnLayout.tsx +32 -0
- package/dist/Shell/index.ts +17 -0
- package/dist/Typography/Code.tsx +27 -0
- package/dist/Typography/Kbd.tsx +87 -0
- package/dist/Typography/Text.tsx +27 -0
- package/dist/Typography/Title.tsx +34 -0
- package/dist/Typography/index.ts +21 -0
- package/dist/bin/ds-audit.js +521 -0
- package/dist/config.ts +226 -0
- package/dist/contracts/Alert.json +80 -0
- package/dist/contracts/Badge.json +78 -0
- package/dist/contracts/Box.json +52 -0
- package/dist/contracts/Button.json +111 -0
- package/dist/contracts/DataTable.json +68 -0
- package/dist/contracts/Grid.json +35 -0
- package/dist/contracts/Group.json +44 -0
- package/dist/contracts/Inline.json +73 -0
- package/dist/contracts/PageContentHeader.json +107 -0
- package/dist/contracts/Stack.json +62 -0
- package/dist/contracts/ds-component-contract.v1.json +23 -0
- package/dist/contracts/index.json +55 -0
- package/dist/css/foundations.css +703 -0
- package/dist/css/mantine.css +1777 -0
- package/dist/hooks/useTableHandlers.ts +179 -0
- package/dist/hooks/useTableRenderFunctions.tsx +283 -0
- package/dist/hooks/useTableState.ts +81 -0
- package/dist/index.ts +210 -0
- package/dist/types.ts +202 -0
- package/package.json +56 -4
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import React, { ReactElement, useMemo } from "react";
|
|
2
|
+
import { Select, rem, ActionIcon, Text } from '@mantine/core';
|
|
3
|
+
import { Inline } from '../../..';
|
|
4
|
+
import {
|
|
5
|
+
MRT_TableInstance as MRTTableInstance,
|
|
6
|
+
MRT_RowData,
|
|
7
|
+
} from "mantine-react-table-open";
|
|
8
|
+
import { translate } from '../translations';
|
|
9
|
+
import { RiArrowLeftSLine, RiArrowRightSLine } from '@remixicon/react';
|
|
10
|
+
|
|
11
|
+
// Constants
|
|
12
|
+
const PAGE_SIZE_OPTIONS = [
|
|
13
|
+
{ value: "5", label: "5" },
|
|
14
|
+
{ value: "10", label: "10" },
|
|
15
|
+
{ value: "25", label: "25" },
|
|
16
|
+
{ value: "50", label: "50" },
|
|
17
|
+
{ value: "100", label: "100" },
|
|
18
|
+
] as const;
|
|
19
|
+
|
|
20
|
+
const TOOLBAR_STYLES = {
|
|
21
|
+
container: {
|
|
22
|
+
borderTop: "1px solid var(--ad-color-border-subtle)",
|
|
23
|
+
padding: `${rem(5)} ${rem(12)}`,
|
|
24
|
+
backgroundColor: "var(--ad-color-bg-subtle)",
|
|
25
|
+
},
|
|
26
|
+
select: {
|
|
27
|
+
width: "80px",
|
|
28
|
+
},
|
|
29
|
+
selectInput: {
|
|
30
|
+
backgroundColor: "transparent",
|
|
31
|
+
border: "none",
|
|
32
|
+
color: "var(--ad-color-text-default)",
|
|
33
|
+
fontSize: "var(--mantine-font-size-xs)",
|
|
34
|
+
},
|
|
35
|
+
} as const;
|
|
36
|
+
|
|
37
|
+
interface PageInfo {
|
|
38
|
+
hasNextPage: boolean;
|
|
39
|
+
hasPreviousPage: boolean;
|
|
40
|
+
startCursor?: string;
|
|
41
|
+
endCursor?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface CustomBottomToolbarProps<T extends MRT_RowData> {
|
|
45
|
+
table: MRTTableInstance<T>;
|
|
46
|
+
totalCount: number;
|
|
47
|
+
pageInfo?: PageInfo;
|
|
48
|
+
numberOfSelectedRows?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Generate the appropriate selected rows text based on count
|
|
53
|
+
*/
|
|
54
|
+
const useSelectedRowsText = (numberOfSelectedRows: number): string | null => {
|
|
55
|
+
return useMemo(() => {
|
|
56
|
+
if (numberOfSelectedRows === 0) return null;
|
|
57
|
+
|
|
58
|
+
return numberOfSelectedRows === 1
|
|
59
|
+
? translate("table.selected.row")
|
|
60
|
+
: translate("table.selected.rows", { count: numberOfSelectedRows });
|
|
61
|
+
}, [numberOfSelectedRows]);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Calculate pagination information
|
|
66
|
+
*/
|
|
67
|
+
const usePaginationInfo = (pageIndex: number, totalCount: number, pageSize: number) => {
|
|
68
|
+
return useMemo(() => ({
|
|
69
|
+
totalPages: Math.ceil(totalCount / pageSize),
|
|
70
|
+
currentPage: pageIndex + 1,
|
|
71
|
+
}), [pageIndex, totalCount, pageSize]);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const CustomBottomToolbar = <T extends MRT_RowData>({
|
|
75
|
+
table,
|
|
76
|
+
totalCount,
|
|
77
|
+
pageInfo,
|
|
78
|
+
numberOfSelectedRows = 0,
|
|
79
|
+
}: CustomBottomToolbarProps<T>): ReactElement => {
|
|
80
|
+
const { getState, setPageIndex, setPageSize } = table;
|
|
81
|
+
const { pagination } = getState();
|
|
82
|
+
const { pageIndex, pageSize } = pagination;
|
|
83
|
+
|
|
84
|
+
const selectedRowsText = useSelectedRowsText(numberOfSelectedRows);
|
|
85
|
+
const { totalPages, currentPage } = usePaginationInfo(pageIndex, totalCount, pageSize);
|
|
86
|
+
|
|
87
|
+
const handlePageSizeChange = (value: string | null) => {
|
|
88
|
+
if (value) {
|
|
89
|
+
setPageSize(Number(value));
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handlePreviousPage = () => {
|
|
94
|
+
if (pageIndex > 0) {
|
|
95
|
+
setPageIndex(pageIndex - 1);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const handleNextPage = () => {
|
|
100
|
+
if (pageIndex < totalPages - 1) {
|
|
101
|
+
setPageIndex(pageIndex + 1);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const canGoPrevious = pageIndex > 0;
|
|
106
|
+
const canGoNext = pageIndex < totalPages - 1;
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<Inline
|
|
110
|
+
justify="space-between"
|
|
111
|
+
style={TOOLBAR_STYLES.container}
|
|
112
|
+
>
|
|
113
|
+
{/* Left section - Rows per page selector */}
|
|
114
|
+
<Inline gap="xs">
|
|
115
|
+
<Text size="sm">
|
|
116
|
+
{translate("table.rows.per.page")}
|
|
117
|
+
</Text>
|
|
118
|
+
<Select
|
|
119
|
+
size="xs"
|
|
120
|
+
value={String(pageSize)}
|
|
121
|
+
onChange={handlePageSizeChange}
|
|
122
|
+
data={PAGE_SIZE_OPTIONS}
|
|
123
|
+
style={TOOLBAR_STYLES.select}
|
|
124
|
+
styles={{
|
|
125
|
+
input: TOOLBAR_STYLES.selectInput,
|
|
126
|
+
}}
|
|
127
|
+
aria-label={translate("table.rows.per.page")}
|
|
128
|
+
/>
|
|
129
|
+
</Inline>
|
|
130
|
+
|
|
131
|
+
{/* Middle section - Selected rows indicator */}
|
|
132
|
+
|
|
133
|
+
<Inline gap="xs">
|
|
134
|
+
{selectedRowsText && (
|
|
135
|
+
<Text size="sm">
|
|
136
|
+
{selectedRowsText}
|
|
137
|
+
</Text>
|
|
138
|
+
)}
|
|
139
|
+
</Inline>
|
|
140
|
+
|
|
141
|
+
{/* Right section - Page navigation */}
|
|
142
|
+
<Inline gap="xs">
|
|
143
|
+
<ActionIcon
|
|
144
|
+
variant="subtle"
|
|
145
|
+
color="gray"
|
|
146
|
+
size="sm"
|
|
147
|
+
onClick={handlePreviousPage}
|
|
148
|
+
disabled={!canGoPrevious}
|
|
149
|
+
aria-label="Previous page"
|
|
150
|
+
>
|
|
151
|
+
<RiArrowLeftSLine size={16} />
|
|
152
|
+
</ActionIcon>
|
|
153
|
+
|
|
154
|
+
<Text size="sm">
|
|
155
|
+
{translate("table.pagination.page", {
|
|
156
|
+
current: currentPage,
|
|
157
|
+
total: totalPages,
|
|
158
|
+
})}
|
|
159
|
+
</Text>
|
|
160
|
+
|
|
161
|
+
<ActionIcon
|
|
162
|
+
variant="subtle"
|
|
163
|
+
color="gray"
|
|
164
|
+
size="sm"
|
|
165
|
+
onClick={handleNextPage}
|
|
166
|
+
disabled={!canGoNext}
|
|
167
|
+
aria-label="Next page"
|
|
168
|
+
>
|
|
169
|
+
<RiArrowRightSLine size={16} />
|
|
170
|
+
</ActionIcon>
|
|
171
|
+
</Inline>
|
|
172
|
+
|
|
173
|
+
</Inline>
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export default CustomBottomToolbar;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { translate } from "../translations";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_PAGE_SIZE = 5;
|
|
4
|
+
|
|
5
|
+
export const getFormattedPageSize = (newPageSize: string | null): number =>
|
|
6
|
+
newPageSize ? parseInt(newPageSize, 10) : DEFAULT_PAGE_SIZE;
|
|
7
|
+
|
|
8
|
+
export const getFormattedPageCount = (
|
|
9
|
+
pageIndex: number,
|
|
10
|
+
pageSize: number,
|
|
11
|
+
rowCount: number
|
|
12
|
+
): string => {
|
|
13
|
+
const firstRowIndex = rowCount ? pageIndex * pageSize + 1 : 0;
|
|
14
|
+
const clampedFirstRowIndex = Math.min(firstRowIndex, rowCount);
|
|
15
|
+
const lastRowIndex = Math.min(clampedFirstRowIndex + pageSize - 1, rowCount);
|
|
16
|
+
return translate("table.row.count", {
|
|
17
|
+
start: clampedFirstRowIndex,
|
|
18
|
+
end: lastRowIndex,
|
|
19
|
+
total: rowCount,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getNumOfRowsPerPageOptions = (
|
|
24
|
+
totalNumOfRows: number,
|
|
25
|
+
increment = DEFAULT_PAGE_SIZE
|
|
26
|
+
): string[] => {
|
|
27
|
+
if (!totalNumOfRows) {
|
|
28
|
+
return [increment.toString()];
|
|
29
|
+
}
|
|
30
|
+
const options: string[] = [];
|
|
31
|
+
for (let i = increment; i - increment < totalNumOfRows; i += increment) {
|
|
32
|
+
options.push(i.toString());
|
|
33
|
+
}
|
|
34
|
+
return options;
|
|
35
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import React, { ReactElement } from "react";
|
|
2
|
+
import { Text, ActionIcon } from '@mantine/core';
|
|
3
|
+
import { Inline } from '../../..';
|
|
4
|
+
import { Button } from '../../..';
|
|
5
|
+
import { IconX } from '@tabler/icons-react';
|
|
6
|
+
import { RiFlashlightFill } from '@remixicon/react';
|
|
7
|
+
import { translate } from '../translations';
|
|
8
|
+
|
|
9
|
+
interface CustomToolbarAlertBannerContentProps {
|
|
10
|
+
numberOfSelectedRows: number;
|
|
11
|
+
onClearSelection: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const CustomToolbarAlertBannerContent = ({
|
|
15
|
+
numberOfSelectedRows,
|
|
16
|
+
onClearSelection,
|
|
17
|
+
}: CustomToolbarAlertBannerContentProps): ReactElement => {
|
|
18
|
+
|
|
19
|
+
if (numberOfSelectedRows === 0) {
|
|
20
|
+
return <></>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const selectedRowsText = numberOfSelectedRows === 1
|
|
24
|
+
? translate("table.selected.row")
|
|
25
|
+
: translate("table.selected.rows", { count: numberOfSelectedRows });
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<Inline
|
|
30
|
+
gap="sm"
|
|
31
|
+
align="center"
|
|
32
|
+
style={{
|
|
33
|
+
background: 'var(--ad-color-brand-primary)',
|
|
34
|
+
color: 'var(--ad-white)',
|
|
35
|
+
borderRadius: 0,
|
|
36
|
+
minHeight: 'var(--mantine-spacing-xl)',
|
|
37
|
+
}}
|
|
38
|
+
>
|
|
39
|
+
<ActionIcon
|
|
40
|
+
variant="transparent"
|
|
41
|
+
color="white"
|
|
42
|
+
onClick={onClearSelection}
|
|
43
|
+
aria-label="Clear selection"
|
|
44
|
+
|
|
45
|
+
size="sm"
|
|
46
|
+
>
|
|
47
|
+
<IconX size={16} />
|
|
48
|
+
</ActionIcon>
|
|
49
|
+
<Button
|
|
50
|
+
variant="outline"
|
|
51
|
+
color="gray"
|
|
52
|
+
size="xs"
|
|
53
|
+
leftSection={<RiFlashlightFill size={16} />}
|
|
54
|
+
style={{ borderColor: 'white', color: 'white' }}
|
|
55
|
+
>
|
|
56
|
+
Button one
|
|
57
|
+
</Button>
|
|
58
|
+
<Button
|
|
59
|
+
variant="outline"
|
|
60
|
+
color="gray"
|
|
61
|
+
size="xs"
|
|
62
|
+
leftSection={<RiFlashlightFill size={16} />}
|
|
63
|
+
style={{ borderColor: 'white', color: 'white' }}
|
|
64
|
+
>
|
|
65
|
+
Button two
|
|
66
|
+
</Button>
|
|
67
|
+
<Text size="sm" c="white" fw={500}>
|
|
68
|
+
{selectedRowsText}
|
|
69
|
+
</Text>
|
|
70
|
+
</Inline>
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default CustomToolbarAlertBannerContent;
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import React, { useMemo, useState, ReactElement } from "react";
|
|
2
|
+
import {
|
|
3
|
+
CloseButton,
|
|
4
|
+
Combobox,
|
|
5
|
+
InputBase,
|
|
6
|
+
InputPlaceholder,
|
|
7
|
+
Popover,
|
|
8
|
+
useCombobox,
|
|
9
|
+
} from "@mantine/core";
|
|
10
|
+
import { DatePicker, DatesRangeValue, type DateValue } from "@mantine/dates";
|
|
11
|
+
import { translate } from '../../translations';
|
|
12
|
+
|
|
13
|
+
/** Local-calendar YYYY-MM-DD for Mantine DatePicker string values */
|
|
14
|
+
function toDateStringValue(d: Date): string {
|
|
15
|
+
const y = d.getFullYear();
|
|
16
|
+
const m = String(d.getMonth() + 1).padStart(2, "0");
|
|
17
|
+
const day = String(d.getDate()).padStart(2, "0");
|
|
18
|
+
return `${y}-${m}-${day}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function formatDateValueLocale(v: DateValue | null, locale: string): string {
|
|
22
|
+
if (v == null) return "";
|
|
23
|
+
const d = typeof v === "string" ? new Date(`${v}T12:00:00`) : v;
|
|
24
|
+
return d.toLocaleDateString(locale);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function toPickerDate(v: DateValue | null | undefined): Date {
|
|
28
|
+
if (v == null) return new Date();
|
|
29
|
+
return typeof v === "string" ? new Date(`${v}T12:00:00`) : v;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Props interface for the DateRangeSelector component
|
|
34
|
+
*/
|
|
35
|
+
interface DateRangeSelectorProps {
|
|
36
|
+
/** Label text displayed above the selector */
|
|
37
|
+
label: string;
|
|
38
|
+
/** Locale string for date formatting (e.g., 'en-US', 'fr-FR') */
|
|
39
|
+
locale: string;
|
|
40
|
+
/** Callback function called when date range selection changes */
|
|
41
|
+
onChange: (value: DatesRangeValue | null) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Enum defining available preset date range options
|
|
46
|
+
*/
|
|
47
|
+
enum Preset {
|
|
48
|
+
WEEK = "week",
|
|
49
|
+
MONTH = "month",
|
|
50
|
+
QUARTER = "quarter",
|
|
51
|
+
CUSTOM = "custom",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Internal state interface for managing date filter selections
|
|
56
|
+
*/
|
|
57
|
+
interface DateFilterState {
|
|
58
|
+
/** Previously selected preset (used for rollback on cancel) */
|
|
59
|
+
previousPreset: string | null;
|
|
60
|
+
/** Currently selected preset option */
|
|
61
|
+
preset: string | null;
|
|
62
|
+
/** Selected custom date range (Mantine 9 uses date strings or Date) */
|
|
63
|
+
customDateRange: DatesRangeValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Preset option interface for dropdown display
|
|
68
|
+
*/
|
|
69
|
+
interface PresetOption {
|
|
70
|
+
/** Display label for the preset */
|
|
71
|
+
label: string;
|
|
72
|
+
/** Internal value for the preset */
|
|
73
|
+
value: Preset;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns available preset options with translated labels
|
|
78
|
+
* @returns Array of preset options for the dropdown
|
|
79
|
+
*/
|
|
80
|
+
const getPresets = (): PresetOption[] => [
|
|
81
|
+
{
|
|
82
|
+
label: translate("table.filters.date.range.week"),
|
|
83
|
+
value: Preset.WEEK,
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
label: translate("table.filters.date.range.month"),
|
|
87
|
+
value: Preset.MONTH,
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
label: translate("table.filters.date.range.quarter"),
|
|
91
|
+
value: Preset.QUARTER,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
label: translate("table.filters.date.range.custom"),
|
|
95
|
+
value: Preset.CUSTOM,
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Finds and returns the display label for a given preset value
|
|
101
|
+
* @param value - The preset value to look up
|
|
102
|
+
* @returns The translated label or empty string if not found
|
|
103
|
+
*/
|
|
104
|
+
const getPresetLabel = (value: string): string => {
|
|
105
|
+
return getPresets().find(item => item.value === value)?.label || "";
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Calculates date range for preset options (last N days/months/quarters)
|
|
110
|
+
* @param preset - The preset type to calculate range for
|
|
111
|
+
* @returns Tuple of [startDate, endDate] or null for invalid preset
|
|
112
|
+
*/
|
|
113
|
+
const getDateRangeFromPreset = (preset: string): DatesRangeValue | null => {
|
|
114
|
+
const now = new Date();
|
|
115
|
+
const start = new Date();
|
|
116
|
+
|
|
117
|
+
switch (preset) {
|
|
118
|
+
case Preset.WEEK:
|
|
119
|
+
start.setDate(now.getDate() - 7);
|
|
120
|
+
break;
|
|
121
|
+
case Preset.MONTH:
|
|
122
|
+
start.setMonth(now.getMonth() - 1);
|
|
123
|
+
break;
|
|
124
|
+
case Preset.QUARTER:
|
|
125
|
+
start.setMonth(now.getMonth() - 3);
|
|
126
|
+
break;
|
|
127
|
+
default:
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return [toDateStringValue(start), toDateStringValue(now)];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* DateRangeSelector - A sophisticated date range filter component with preset options
|
|
136
|
+
* and custom date selection via floating calendar dialog.
|
|
137
|
+
*
|
|
138
|
+
* Features:
|
|
139
|
+
* - Preset options: Last week, month, quarter
|
|
140
|
+
* - Custom date range selection with floating calendar
|
|
141
|
+
* - Proper state management with rollback capability
|
|
142
|
+
* - Localized date display and formatting
|
|
143
|
+
* - Clear functionality with visual feedback
|
|
144
|
+
*/
|
|
145
|
+
const DateRangeSelector = ({ label, locale, onChange }: DateRangeSelectorProps): ReactElement => {
|
|
146
|
+
// Main state management for date filter selections
|
|
147
|
+
const [dateFilterState, setDateFilterState] = useState<DateFilterState>({
|
|
148
|
+
previousPreset: null,
|
|
149
|
+
preset: null,
|
|
150
|
+
customDateRange: [null, null],
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Destructure state for easier access
|
|
154
|
+
const { preset, customDateRange } = dateFilterState;
|
|
155
|
+
|
|
156
|
+
// Controls visibility of the floating date picker
|
|
157
|
+
const [isDatePickerOpened, setIsDatePickerOpened] = useState(false);
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Checks if a complete custom date range has been selected
|
|
161
|
+
* @param value - The preset value to check
|
|
162
|
+
* @returns True if custom preset with both start and end dates selected
|
|
163
|
+
*/
|
|
164
|
+
const isDateRangeSelected = (value: string | null): boolean => {
|
|
165
|
+
return value === Preset.CUSTOM && customDateRange[0] !== null && customDateRange[1] !== null;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Determines what value to display in the input field
|
|
170
|
+
* Handles different states: empty, preset selected, custom dates, partial custom selection
|
|
171
|
+
*/
|
|
172
|
+
const getValueDisplayed = useMemo(() => {
|
|
173
|
+
// Show formatted date range for completed custom selection
|
|
174
|
+
if (isDateRangeSelected(preset)) {
|
|
175
|
+
return `${formatDateValueLocale(customDateRange[0], locale)} - ${formatDateValueLocale(
|
|
176
|
+
customDateRange[1],
|
|
177
|
+
locale
|
|
178
|
+
)}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Show helper text for partial custom selection
|
|
182
|
+
if (preset === Preset.CUSTOM) {
|
|
183
|
+
return translate("table.filters.date.range.select");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Show preset label for selected presets
|
|
187
|
+
if (preset) {
|
|
188
|
+
return getPresetLabel(preset);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Show placeholder for empty state
|
|
192
|
+
return <InputPlaceholder>{translate("table.filters.show.all")}</InputPlaceholder>;
|
|
193
|
+
}, [preset, customDateRange, locale]);
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Combobox configuration for dropdown behavior
|
|
197
|
+
* Handles dropdown close events and option selection
|
|
198
|
+
*/
|
|
199
|
+
const combobox = useCombobox({
|
|
200
|
+
onDropdownClose: () => {
|
|
201
|
+
// Reset selected option when dropdown closes
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Renders preset options for the dropdown menu
|
|
207
|
+
*/
|
|
208
|
+
const options = getPresets().map(item => (
|
|
209
|
+
<Combobox.Option value={item.value} key={item.value}>
|
|
210
|
+
{item.label}
|
|
211
|
+
</Combobox.Option>
|
|
212
|
+
));
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Handles clearing the current selection
|
|
216
|
+
* Resets all state and notifies parent component
|
|
217
|
+
*/
|
|
218
|
+
const handleClearClick = (): void => {
|
|
219
|
+
setDateFilterState(prev => ({
|
|
220
|
+
previousPreset: prev.preset,
|
|
221
|
+
preset: null,
|
|
222
|
+
customDateRange: [null, null],
|
|
223
|
+
}));
|
|
224
|
+
setIsDatePickerOpened(false);
|
|
225
|
+
onChange(null);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Handles selection of preset options from dropdown
|
|
230
|
+
* @param value - The selected preset value
|
|
231
|
+
*/
|
|
232
|
+
const handlePresetChange = (value: string): void => {
|
|
233
|
+
combobox.closeDropdown();
|
|
234
|
+
|
|
235
|
+
// Update state with new selection
|
|
236
|
+
setDateFilterState(prev => ({
|
|
237
|
+
...prev,
|
|
238
|
+
previousPreset: prev.preset,
|
|
239
|
+
preset: value,
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
if (value === Preset.CUSTOM) {
|
|
243
|
+
// Open floating date picker for custom selection
|
|
244
|
+
setIsDatePickerOpened(true);
|
|
245
|
+
} else {
|
|
246
|
+
// Apply preset date range immediately and close any open picker
|
|
247
|
+
setIsDatePickerOpened(false);
|
|
248
|
+
onChange(getDateRangeFromPreset(value));
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Handles clicks on the main input button
|
|
254
|
+
* Toggles dropdown or date picker based on current state
|
|
255
|
+
*/
|
|
256
|
+
const handleInputBaseClick = (): void => {
|
|
257
|
+
if (!isDatePickerOpened) {
|
|
258
|
+
// Open dropdown for preset selection
|
|
259
|
+
combobox.toggleDropdown();
|
|
260
|
+
} else {
|
|
261
|
+
// Close date picker
|
|
262
|
+
setIsDatePickerOpened(false);
|
|
263
|
+
|
|
264
|
+
// Revert to previous preset if no complete date range was selected
|
|
265
|
+
if (customDateRange[0] === null && customDateRange[1] === null) {
|
|
266
|
+
setDateFilterState(prev => ({
|
|
267
|
+
...prev,
|
|
268
|
+
previousPreset: prev.preset,
|
|
269
|
+
preset: prev.previousPreset,
|
|
270
|
+
}));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Handles date range changes from the floating date picker
|
|
277
|
+
* @param value - The selected date range [startDate, endDate]
|
|
278
|
+
*/
|
|
279
|
+
const handleDatePickerChange = (value: DatesRangeValue): void => {
|
|
280
|
+
// Update state and notify parent immediately for real-time filtering
|
|
281
|
+
setDateFilterState(prev => ({ ...prev, customDateRange: value }));
|
|
282
|
+
onChange(value);
|
|
283
|
+
|
|
284
|
+
// Auto-close the picker when both dates are selected (complete range)
|
|
285
|
+
if (value[0] !== null && value[1] !== null) {
|
|
286
|
+
setIsDatePickerOpened(false);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
return (
|
|
291
|
+
<Popover opened={isDatePickerOpened} position="bottom-start">
|
|
292
|
+
<Popover.Target>
|
|
293
|
+
{/* Main container with click-outside detection */}
|
|
294
|
+
<div role="menuitem">
|
|
295
|
+
<Combobox
|
|
296
|
+
size="xs"
|
|
297
|
+
store={combobox}
|
|
298
|
+
withinPortal={false}
|
|
299
|
+
onOptionSubmit={handlePresetChange}
|
|
300
|
+
>
|
|
301
|
+
<Combobox.Target>
|
|
302
|
+
<InputBase
|
|
303
|
+
component="button"
|
|
304
|
+
type="button"
|
|
305
|
+
size="xs"
|
|
306
|
+
pointer
|
|
307
|
+
rightSection={
|
|
308
|
+
preset ? (
|
|
309
|
+
/* Clear button - shown when something is selected */
|
|
310
|
+
<CloseButton
|
|
311
|
+
size="xs"
|
|
312
|
+
variant="transparent"
|
|
313
|
+
onMouseDown={(event): void => event.preventDefault()}
|
|
314
|
+
onClick={handleClearClick}
|
|
315
|
+
aria-label="Clear date range"
|
|
316
|
+
/>
|
|
317
|
+
) : (
|
|
318
|
+
/* Dropdown chevron - shown when nothing is selected */
|
|
319
|
+
<Combobox.Chevron size="xs" />
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
onClick={handleInputBaseClick}
|
|
323
|
+
rightSectionPointerEvents={preset === null ? "none" : "all"}
|
|
324
|
+
label={label}
|
|
325
|
+
styles={{
|
|
326
|
+
label: {
|
|
327
|
+
fontWeight: 600,
|
|
328
|
+
},
|
|
329
|
+
}}
|
|
330
|
+
>
|
|
331
|
+
{getValueDisplayed}
|
|
332
|
+
</InputBase>
|
|
333
|
+
</Combobox.Target>
|
|
334
|
+
|
|
335
|
+
{/* Dropdown with preset options */}
|
|
336
|
+
<Combobox.Dropdown>
|
|
337
|
+
<Combobox.Options>{options}</Combobox.Options>
|
|
338
|
+
</Combobox.Dropdown>
|
|
339
|
+
</Combobox>
|
|
340
|
+
</div>
|
|
341
|
+
</Popover.Target>
|
|
342
|
+
|
|
343
|
+
{/* Floating date picker for custom date selection */}
|
|
344
|
+
<Popover.Dropdown>
|
|
345
|
+
<DatePicker
|
|
346
|
+
id="date-picker"
|
|
347
|
+
data-testid="date-picker"
|
|
348
|
+
type="range"
|
|
349
|
+
value={customDateRange}
|
|
350
|
+
onChange={handleDatePickerChange}
|
|
351
|
+
defaultDate={toPickerDate(customDateRange[0] ?? customDateRange[1])}
|
|
352
|
+
/>
|
|
353
|
+
</Popover.Dropdown>
|
|
354
|
+
</Popover>
|
|
355
|
+
);
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
export default DateRangeSelector;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Chip } from "@mantine/core";
|
|
3
|
+
import { RiCloseLine } from '@remixicon/react';
|
|
4
|
+
|
|
5
|
+
interface FilterChipProps {
|
|
6
|
+
label: string;
|
|
7
|
+
onClick: () => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* FilterChip component for displaying active filters with remove functionality
|
|
12
|
+
* Shows as an outlined chip with a close icon for easy removal
|
|
13
|
+
*/
|
|
14
|
+
const FilterChip = ({ label, onClick }: FilterChipProps): JSX.Element => {
|
|
15
|
+
return (
|
|
16
|
+
<Chip
|
|
17
|
+
color="gray"
|
|
18
|
+
size="xs"
|
|
19
|
+
variant="outline"
|
|
20
|
+
checked={true}
|
|
21
|
+
onChange={onClick}
|
|
22
|
+
icon={<RiCloseLine size={12} />}
|
|
23
|
+
styles={{
|
|
24
|
+
root: {
|
|
25
|
+
cursor: 'pointer',
|
|
26
|
+
'&[dataChecked="true"]': {
|
|
27
|
+
backgroundColor: 'transparent',
|
|
28
|
+
border: '1px solid var(--ad-color-border-default)',
|
|
29
|
+
color: 'var(--ad-color-text-dimmed)',
|
|
30
|
+
},
|
|
31
|
+
'&:hover': {
|
|
32
|
+
backgroundColor: 'var(--ad-color-bg-subtle)',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
label: {
|
|
36
|
+
cursor: 'pointer',
|
|
37
|
+
},
|
|
38
|
+
iconWrapper: {
|
|
39
|
+
color: 'var(--ad-color-text-dimmed)',
|
|
40
|
+
},
|
|
41
|
+
}}
|
|
42
|
+
>
|
|
43
|
+
{label}
|
|
44
|
+
</Chip>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default FilterChip;
|