agentic-ds-kit 0.1.0
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/AGENTS.md +10 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +21 -0
- package/README.md +38 -0
- package/contracts/accordion.json +38 -0
- package/contracts/alert.json +57 -0
- package/contracts/alertdialog.json +89 -0
- package/contracts/appheader.json +140 -0
- package/contracts/appnav.json +72 -0
- package/contracts/avatar.json +63 -0
- package/contracts/avatargroup.json +83 -0
- package/contracts/badge.json +92 -0
- package/contracts/barchart.json +55 -0
- package/contracts/breadcrumb.json +76 -0
- package/contracts/button.json +120 -0
- package/contracts/buttongroup.json +48 -0
- package/contracts/calendar.json +93 -0
- package/contracts/card.json +123 -0
- package/contracts/carousel.json +98 -0
- package/contracts/cell.json +203 -0
- package/contracts/chat.json +131 -0
- package/contracts/checkbox.json +101 -0
- package/contracts/collapsible.json +66 -0
- package/contracts/command.json +84 -0
- package/contracts/datatable.json +126 -0
- package/contracts/datepicker.json +102 -0
- package/contracts/drawer.json +93 -0
- package/contracts/dropdownmenu.json +190 -0
- package/contracts/empty.json +79 -0
- package/contracts/field.json +49 -0
- package/contracts/fieldset.json +29 -0
- package/contracts/headercell.json +107 -0
- package/contracts/index.json +478 -0
- package/contracts/input.json +119 -0
- package/contracts/inputotp.json +119 -0
- package/contracts/insightcard.json +130 -0
- package/contracts/linechart.json +51 -0
- package/contracts/loadinganimation.json +33 -0
- package/contracts/modal.json +100 -0
- package/contracts/modalcard.json +88 -0
- package/contracts/navigationmenu.json +65 -0
- package/contracts/numbertransition.json +33 -0
- package/contracts/pageheader.json +101 -0
- package/contracts/pagination.json +85 -0
- package/contracts/patterns/activity.json +30 -0
- package/contracts/patterns/assistant-workspace.json +16 -0
- package/contracts/patterns/dashboard.json +12 -0
- package/contracts/patterns/empty-first-run.json +15 -0
- package/contracts/patterns/inbox.json +32 -0
- package/contracts/patterns/invite-members.json +14 -0
- package/contracts/patterns/list-detail.json +13 -0
- package/contracts/patterns/settings-form.json +13 -0
- package/contracts/piechart.json +68 -0
- package/contracts/progress.json +124 -0
- package/contracts/progresssteps.json +91 -0
- package/contracts/radiogroup.json +132 -0
- package/contracts/scoreboard.json +46 -0
- package/contracts/scorecard.json +124 -0
- package/contracts/section.json +97 -0
- package/contracts/select.json +130 -0
- package/contracts/shimmertext.json +33 -0
- package/contracts/sidenav.json +133 -0
- package/contracts/slider.json +81 -0
- package/contracts/spinner.json +52 -0
- package/contracts/switch.json +94 -0
- package/contracts/table.json +92 -0
- package/contracts/tabs.json +90 -0
- package/contracts/textarea.json +54 -0
- package/contracts/thinkinganimation.json +32 -0
- package/contracts/timeline.json +49 -0
- package/contracts/toast.json +104 -0
- package/contracts/tooltip.json +66 -0
- package/lib/tokens.css +637 -0
- package/package.json +60 -0
- package/skills/prototype-from-kit/SKILL.md +29 -0
- package/src/index.ts +71 -0
- package/tokens/tokens.json +725 -0
- package/ui/Accordion/Accordion.module.css +68 -0
- package/ui/Accordion/Accordion.tsx +51 -0
- package/ui/Accordion/Accordion.types.ts +10 -0
- package/ui/Accordion/index.ts +2 -0
- package/ui/Alert/Alert.module.css +62 -0
- package/ui/Alert/Alert.tsx +27 -0
- package/ui/Alert/Alert.types.ts +7 -0
- package/ui/Alert/index.ts +2 -0
- package/ui/AlertDialog/AlertDialog.module.css +71 -0
- package/ui/AlertDialog/AlertDialog.tsx +109 -0
- package/ui/AlertDialog/AlertDialog.types.ts +14 -0
- package/ui/AlertDialog/index.ts +6 -0
- package/ui/AppHeader/AppHeader.module.css +120 -0
- package/ui/AppHeader/AppHeader.tsx +70 -0
- package/ui/AppHeader/AppHeader.types.ts +28 -0
- package/ui/AppHeader/index.ts +2 -0
- package/ui/AppNav/AppNav.module.css +168 -0
- package/ui/AppNav/AppNav.tsx +137 -0
- package/ui/AppNav/AppNav.types.ts +24 -0
- package/ui/AppNav/index.ts +2 -0
- package/ui/Avatar/Avatar.module.css +52 -0
- package/ui/Avatar/Avatar.tsx +62 -0
- package/ui/Avatar/Avatar.types.ts +8 -0
- package/ui/Avatar/index.ts +2 -0
- package/ui/AvatarGroup/AvatarGroup.module.css +49 -0
- package/ui/AvatarGroup/AvatarGroup.tsx +29 -0
- package/ui/AvatarGroup/AvatarGroup.types.ts +16 -0
- package/ui/AvatarGroup/index.ts +2 -0
- package/ui/Badge/Badge.module.css +92 -0
- package/ui/Badge/Badge.tsx +43 -0
- package/ui/Badge/Badge.types.ts +11 -0
- package/ui/Badge/index.ts +2 -0
- package/ui/BarChart/BarChart.module.css +56 -0
- package/ui/BarChart/BarChart.tsx +380 -0
- package/ui/BarChart/BarChart.types.ts +18 -0
- package/ui/BarChart/index.ts +2 -0
- package/ui/Breadcrumb/Breadcrumb.module.css +64 -0
- package/ui/Breadcrumb/Breadcrumb.tsx +89 -0
- package/ui/Breadcrumb/Breadcrumb.types.ts +14 -0
- package/ui/Breadcrumb/index.ts +2 -0
- package/ui/Button/Button.module.css +101 -0
- package/ui/Button/Button.tsx +49 -0
- package/ui/Button/Button.types.ts +18 -0
- package/ui/Button/index.ts +2 -0
- package/ui/Button/lucideName.tsx +188 -0
- package/ui/ButtonGroup/ButtonGroup.module.css +31 -0
- package/ui/ButtonGroup/ButtonGroup.tsx +12 -0
- package/ui/ButtonGroup/ButtonGroup.types.ts +6 -0
- package/ui/ButtonGroup/index.ts +2 -0
- package/ui/Calendar/Calendar.module.css +146 -0
- package/ui/Calendar/Calendar.tsx +296 -0
- package/ui/Calendar/Calendar.types.ts +11 -0
- package/ui/Calendar/index.ts +2 -0
- package/ui/Card/Card.module.css +95 -0
- package/ui/Card/Card.tsx +69 -0
- package/ui/Card/Card.types.ts +23 -0
- package/ui/Card/index.ts +2 -0
- package/ui/Carousel/Carousel.module.css +114 -0
- package/ui/Carousel/Carousel.tsx +152 -0
- package/ui/Carousel/Carousel.types.ts +17 -0
- package/ui/Carousel/index.ts +7 -0
- package/ui/Cell/Cell.module.css +133 -0
- package/ui/Cell/Cell.tsx +300 -0
- package/ui/Cell/Cell.types.ts +69 -0
- package/ui/Cell/index.ts +11 -0
- package/ui/Chat/Chat.module.css +149 -0
- package/ui/Chat/Chat.tsx +175 -0
- package/ui/Chat/Chat.types.ts +39 -0
- package/ui/Chat/index.ts +2 -0
- package/ui/Checkbox/Checkbox.module.css +135 -0
- package/ui/Checkbox/Checkbox.tsx +70 -0
- package/ui/Checkbox/Checkbox.types.ts +15 -0
- package/ui/Checkbox/index.ts +2 -0
- package/ui/Collapsible/Collapsible.module.css +68 -0
- package/ui/Collapsible/Collapsible.tsx +59 -0
- package/ui/Collapsible/Collapsible.types.ts +9 -0
- package/ui/Collapsible/index.ts +2 -0
- package/ui/Command/Command.module.css +138 -0
- package/ui/Command/Command.tsx +152 -0
- package/ui/Command/Command.types.ts +20 -0
- package/ui/Command/index.ts +2 -0
- package/ui/DataTable/DataTable.module.css +211 -0
- package/ui/DataTable/DataTable.tsx +429 -0
- package/ui/DataTable/DataTable.types.ts +35 -0
- package/ui/DataTable/index.ts +8 -0
- package/ui/DatePicker/DatePicker.module.css +44 -0
- package/ui/DatePicker/DatePicker.tsx +123 -0
- package/ui/DatePicker/DatePicker.types.ts +15 -0
- package/ui/DatePicker/index.ts +2 -0
- package/ui/Drawer/Drawer.module.css +204 -0
- package/ui/Drawer/Drawer.tsx +112 -0
- package/ui/Drawer/Drawer.types.ts +14 -0
- package/ui/Drawer/index.ts +2 -0
- package/ui/DropdownMenu/DropdownMenu.module.css +319 -0
- package/ui/DropdownMenu/DropdownMenu.tsx +580 -0
- package/ui/DropdownMenu/DropdownMenu.types.ts +48 -0
- package/ui/DropdownMenu/index.ts +8 -0
- package/ui/Empty/Empty.module.css +68 -0
- package/ui/Empty/Empty.tsx +35 -0
- package/ui/Empty/Empty.types.ts +10 -0
- package/ui/Empty/index.ts +2 -0
- package/ui/Field/Field.module.css +66 -0
- package/ui/Field/Field.tsx +46 -0
- package/ui/Field/Field.types.ts +12 -0
- package/ui/Field/index.ts +2 -0
- package/ui/FieldSet/FieldSet.module.css +30 -0
- package/ui/FieldSet/FieldSet.tsx +14 -0
- package/ui/FieldSet/FieldSet.types.ts +7 -0
- package/ui/FieldSet/index.ts +2 -0
- package/ui/HeaderCell/HeaderCell.module.css +85 -0
- package/ui/HeaderCell/HeaderCell.tsx +71 -0
- package/ui/HeaderCell/HeaderCell.types.ts +18 -0
- package/ui/HeaderCell/index.ts +2 -0
- package/ui/Input/Input.module.css +59 -0
- package/ui/Input/Input.tsx +65 -0
- package/ui/Input/Input.types.ts +22 -0
- package/ui/Input/index.ts +2 -0
- package/ui/InputOTP/InputOTP.module.css +139 -0
- package/ui/InputOTP/InputOTP.tsx +136 -0
- package/ui/InputOTP/InputOTP.types.ts +19 -0
- package/ui/InputOTP/index.ts +2 -0
- package/ui/InsightCard/InsightCard.module.css +166 -0
- package/ui/InsightCard/InsightCard.tsx +90 -0
- package/ui/InsightCard/InsightCard.types.ts +20 -0
- package/ui/InsightCard/index.ts +7 -0
- package/ui/LineChart/LineChart.module.css +93 -0
- package/ui/LineChart/LineChart.tsx +424 -0
- package/ui/LineChart/LineChart.types.ts +19 -0
- package/ui/LineChart/index.ts +2 -0
- package/ui/LoadingAnimation/LoadingAnimation.module.css +66 -0
- package/ui/LoadingAnimation/LoadingAnimation.tsx +41 -0
- package/ui/LoadingAnimation/LoadingAnimation.types.ts +9 -0
- package/ui/LoadingAnimation/index.ts +6 -0
- package/ui/Modal/Modal.module.css +48 -0
- package/ui/Modal/Modal.tsx +114 -0
- package/ui/Modal/Modal.types.ts +15 -0
- package/ui/Modal/index.ts +2 -0
- package/ui/ModalCard/ModalCard.module.css +137 -0
- package/ui/ModalCard/ModalCard.tsx +58 -0
- package/ui/ModalCard/ModalCard.types.ts +15 -0
- package/ui/ModalCard/index.ts +2 -0
- package/ui/NavigationMenu/NavigationMenu.module.css +31 -0
- package/ui/NavigationMenu/NavigationMenu.tsx +57 -0
- package/ui/NavigationMenu/NavigationMenu.types.ts +22 -0
- package/ui/NavigationMenu/index.ts +7 -0
- package/ui/NumberTransition/NumberTransition.module.css +110 -0
- package/ui/NumberTransition/NumberTransition.tsx +78 -0
- package/ui/NumberTransition/NumberTransition.types.ts +9 -0
- package/ui/NumberTransition/index.ts +2 -0
- package/ui/PageHeader/PageHeader.module.css +57 -0
- package/ui/PageHeader/PageHeader.tsx +39 -0
- package/ui/PageHeader/PageHeader.types.ts +12 -0
- package/ui/PageHeader/index.ts +2 -0
- package/ui/Pagination/Pagination.module.css +19 -0
- package/ui/Pagination/Pagination.tsx +147 -0
- package/ui/Pagination/Pagination.types.ts +12 -0
- package/ui/Pagination/index.ts +2 -0
- package/ui/PieChart/PieChart.module.css +116 -0
- package/ui/PieChart/PieChart.tsx +293 -0
- package/ui/PieChart/PieChart.types.ts +22 -0
- package/ui/PieChart/index.ts +2 -0
- package/ui/Progress/Progress.module.css +226 -0
- package/ui/Progress/Progress.tsx +167 -0
- package/ui/Progress/Progress.types.ts +15 -0
- package/ui/Progress/index.ts +8 -0
- package/ui/ProgressSteps/ProgressSteps.module.css +210 -0
- package/ui/ProgressSteps/ProgressSteps.tsx +105 -0
- package/ui/ProgressSteps/ProgressSteps.types.ts +19 -0
- package/ui/ProgressSteps/index.ts +8 -0
- package/ui/RadioGroup/RadioGroup.module.css +284 -0
- package/ui/RadioGroup/RadioGroup.tsx +116 -0
- package/ui/RadioGroup/RadioGroup.types.ts +30 -0
- package/ui/RadioGroup/index.ts +8 -0
- package/ui/Scoreboard/Scoreboard.module.css +53 -0
- package/ui/Scoreboard/Scoreboard.tsx +41 -0
- package/ui/Scoreboard/Scoreboard.types.ts +7 -0
- package/ui/Scoreboard/index.ts +2 -0
- package/ui/Scorecard/Scorecard.module.css +150 -0
- package/ui/Scorecard/Scorecard.tsx +92 -0
- package/ui/Scorecard/Scorecard.types.ts +18 -0
- package/ui/Scorecard/index.ts +2 -0
- package/ui/Section/Section.module.css +104 -0
- package/ui/Section/Section.tsx +75 -0
- package/ui/Section/Section.types.ts +15 -0
- package/ui/Section/index.ts +2 -0
- package/ui/Select/Select.module.css +7 -0
- package/ui/Select/Select.tsx +113 -0
- package/ui/Select/Select.types.ts +26 -0
- package/ui/Select/index.ts +2 -0
- package/ui/ShimmerText/ShimmerText.module.css +46 -0
- package/ui/ShimmerText/ShimmerText.tsx +48 -0
- package/ui/ShimmerText/ShimmerText.types.ts +12 -0
- package/ui/ShimmerText/index.ts +2 -0
- package/ui/SideNav/SideNav.module.css +347 -0
- package/ui/SideNav/SideNav.tsx +222 -0
- package/ui/SideNav/SideNav.types.ts +42 -0
- package/ui/SideNav/index.ts +8 -0
- package/ui/Slider/Slider.module.css +105 -0
- package/ui/Slider/Slider.tsx +187 -0
- package/ui/Slider/Slider.types.ts +11 -0
- package/ui/Slider/index.ts +2 -0
- package/ui/Spinner/Spinner.module.css +58 -0
- package/ui/Spinner/Spinner.tsx +19 -0
- package/ui/Spinner/Spinner.types.ts +6 -0
- package/ui/Spinner/index.ts +2 -0
- package/ui/Switch/Switch.module.css +189 -0
- package/ui/Switch/Switch.tsx +65 -0
- package/ui/Switch/Switch.types.ts +12 -0
- package/ui/Switch/index.ts +2 -0
- package/ui/Table/Table.module.css +132 -0
- package/ui/Table/Table.tsx +108 -0
- package/ui/Table/Table.types.ts +26 -0
- package/ui/Table/index.ts +8 -0
- package/ui/Tabs/Tabs.module.css +162 -0
- package/ui/Tabs/Tabs.tsx +150 -0
- package/ui/Tabs/Tabs.types.ts +21 -0
- package/ui/Tabs/index.ts +2 -0
- package/ui/Textarea/Textarea.module.css +74 -0
- package/ui/Textarea/Textarea.tsx +38 -0
- package/ui/Textarea/Textarea.types.ts +14 -0
- package/ui/Textarea/index.ts +2 -0
- package/ui/ThinkingAnimation/ThinkingAnimation.module.css +75 -0
- package/ui/ThinkingAnimation/ThinkingAnimation.tsx +31 -0
- package/ui/ThinkingAnimation/ThinkingAnimation.types.ts +8 -0
- package/ui/ThinkingAnimation/index.ts +2 -0
- package/ui/Timeline/Timeline.module.css +99 -0
- package/ui/Timeline/Timeline.tsx +47 -0
- package/ui/Timeline/Timeline.types.ts +16 -0
- package/ui/Timeline/index.ts +2 -0
- package/ui/Toast/Toast.module.css +133 -0
- package/ui/Toast/Toast.tsx +89 -0
- package/ui/Toast/Toast.types.ts +17 -0
- package/ui/Toast/index.ts +2 -0
- package/ui/Tooltip/Tooltip.module.css +111 -0
- package/ui/Tooltip/Tooltip.tsx +101 -0
- package/ui/Tooltip/index.ts +2 -0
- package/ui/patterns/ActivityPattern/ActivityFeed.tsx +77 -0
- package/ui/patterns/ActivityPattern/ActivityPattern.tsx +120 -0
- package/ui/patterns/ActivityPattern/ActivityTasks.tsx +86 -0
- package/ui/patterns/ActivityPattern/TaskDrawer.module.css +39 -0
- package/ui/patterns/ActivityPattern/TaskDrawer.tsx +186 -0
- package/ui/patterns/ActivityPattern/index.ts +2 -0
- package/ui/patterns/AssistantWorkspacePattern/AssistantWorkspacePattern.module.css +16 -0
- package/ui/patterns/AssistantWorkspacePattern/AssistantWorkspacePattern.tsx +159 -0
- package/ui/patterns/AssistantWorkspacePattern/index.ts +2 -0
- package/ui/patterns/DashboardPattern/DashboardPattern.tsx +102 -0
- package/ui/patterns/DashboardPattern/index.ts +1 -0
- package/ui/patterns/DashboardPattern/patterns.module.css +6 -0
- package/ui/patterns/EmptyFirstRunPattern/EmptyFirstRunPattern.tsx +125 -0
- package/ui/patterns/EmptyFirstRunPattern/index.ts +2 -0
- package/ui/patterns/InboxPattern/InboxPattern.module.css +40 -0
- package/ui/patterns/InboxPattern/InboxPattern.tsx +192 -0
- package/ui/patterns/InboxPattern/InboxReplyDrawer.tsx +97 -0
- package/ui/patterns/InboxPattern/inbox-data.ts +101 -0
- package/ui/patterns/InboxPattern/index.ts +2 -0
- package/ui/patterns/InviteMembersPattern/InviteMembersPattern.tsx +148 -0
- package/ui/patterns/InviteMembersPattern/index.ts +2 -0
- package/ui/patterns/ListDetailPattern/ListDetailPattern.module.css +34 -0
- package/ui/patterns/ListDetailPattern/ListDetailPattern.tsx +145 -0
- package/ui/patterns/ListDetailPattern/index.ts +2 -0
- package/ui/patterns/SettingsFormPattern/SettingsFormPattern.module.css +12 -0
- package/ui/patterns/SettingsFormPattern/SettingsFormPattern.tsx +89 -0
- package/ui/patterns/SettingsFormPattern/index.ts +3 -0
- package/ui/shared/ChartFrame.module.css +295 -0
- package/ui/shared/ChartFrame.tsx +87 -0
- package/ui/shared/chartMath.ts +228 -0
- package/ui/shared/controls.module.css +48 -0
- package/ui/shared/useChartSize.ts +22 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { Button } from "../Button";
|
|
5
|
+
import { Checkbox } from "../Checkbox";
|
|
6
|
+
import { Cell } from "../Cell";
|
|
7
|
+
import { HeaderCell } from "../HeaderCell";
|
|
8
|
+
import { DropdownMenu } from "../DropdownMenu";
|
|
9
|
+
import type { CellSize, CellValue } from "../Cell";
|
|
10
|
+
import { LucideByName } from "../Button/lucideName";
|
|
11
|
+
import type {
|
|
12
|
+
DataTableCell,
|
|
13
|
+
DataTableColumn,
|
|
14
|
+
DataTableFilter,
|
|
15
|
+
DataTableProps,
|
|
16
|
+
DataTableRow,
|
|
17
|
+
} from "./DataTable.types";
|
|
18
|
+
import styles from "./DataTable.module.css";
|
|
19
|
+
|
|
20
|
+
export type {
|
|
21
|
+
DataTableProps,
|
|
22
|
+
DataTableColumn,
|
|
23
|
+
DataTableFilter,
|
|
24
|
+
DataTableCell,
|
|
25
|
+
DataTableRow,
|
|
26
|
+
} from "./DataTable.types";
|
|
27
|
+
|
|
28
|
+
const DEFAULT_SEARCH = "Search...";
|
|
29
|
+
const DEFAULT_EMPTY = "No results.";
|
|
30
|
+
const EMPTY_FILTERS: DataTableFilter[] = [];
|
|
31
|
+
|
|
32
|
+
function isPrimitive(value: unknown): value is string | number {
|
|
33
|
+
return typeof value === "string" || typeof value === "number";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function stringifyCell(value: DataTableCell | undefined): string {
|
|
37
|
+
if (value === undefined) return "";
|
|
38
|
+
if (isPrimitive(value)) return String(value);
|
|
39
|
+
const cell = value as CellValue;
|
|
40
|
+
if (cell.label) return String(cell.label);
|
|
41
|
+
if (cell.name) return String(cell.name);
|
|
42
|
+
if (cell.value !== undefined && cell.value !== "") return String(cell.value);
|
|
43
|
+
if (cell.badges && cell.badges.length) return cell.badges.map((item) => item.label).join(" ");
|
|
44
|
+
if (cell.people && cell.people.length) return cell.people.map((item) => item.name).join(" ");
|
|
45
|
+
if (cell.options && cell.options.length) {
|
|
46
|
+
const selected = cell.value !== undefined ? String(cell.value) : "";
|
|
47
|
+
const match = cell.options.find((item) => item.value === selected);
|
|
48
|
+
if (match) return match.label;
|
|
49
|
+
return cell.options.map((item) => item.label).join(" ");
|
|
50
|
+
}
|
|
51
|
+
if (cell.actions && cell.actions.length) {
|
|
52
|
+
return cell.actions.map((item) => item.label || item.ariaLabel || "").join(" ");
|
|
53
|
+
}
|
|
54
|
+
return "";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function rowId(row: DataTableRow, index: number): string {
|
|
58
|
+
const id = row.id;
|
|
59
|
+
if (isPrimitive(id) && id !== "") return String(id);
|
|
60
|
+
return String(index);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function uniqueValues(rows: DataTableRow[], key: string): string[] {
|
|
64
|
+
const seen = new Set<string>();
|
|
65
|
+
const values: string[] = [];
|
|
66
|
+
for (const row of rows) {
|
|
67
|
+
if (row[key] === undefined) continue;
|
|
68
|
+
const value = stringifyCell(row[key]);
|
|
69
|
+
if (!seen.has(value)) {
|
|
70
|
+
seen.add(value);
|
|
71
|
+
values.push(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return values;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function lockedColumnKey(columns: DataTableColumn[]): string | undefined {
|
|
78
|
+
if (columns.some((column) => column.key === "name")) return "name";
|
|
79
|
+
return columns[0]?.key;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function rowLabel(row: DataTableRow, lockedKey: string | undefined, fallback: string): string {
|
|
83
|
+
if (lockedKey && row[lockedKey] !== undefined) {
|
|
84
|
+
const text = stringifyCell(row[lockedKey]);
|
|
85
|
+
if (text) return text;
|
|
86
|
+
}
|
|
87
|
+
return fallback;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function renderCell(value: DataTableCell | undefined, cellSize: CellSize) {
|
|
91
|
+
if (value === undefined) {
|
|
92
|
+
return <Cell type="text" size={cellSize} text={false} />;
|
|
93
|
+
}
|
|
94
|
+
if (isPrimitive(value)) {
|
|
95
|
+
return <Cell type="text" label={String(value)} size={cellSize} />;
|
|
96
|
+
}
|
|
97
|
+
return <Cell {...value} size={cellSize} />;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isActionMenuValue(value: DataTableCell | undefined): boolean {
|
|
101
|
+
return Boolean(value) && typeof value === "object" && value.type === "actionMenu";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isActionColumn(column: DataTableColumn, rows: DataTableRow[]): boolean {
|
|
105
|
+
return rows.some((row) => isActionMenuValue(row[column.key]));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function DataTable({
|
|
109
|
+
caption,
|
|
110
|
+
selectable = false,
|
|
111
|
+
toolbar = false,
|
|
112
|
+
searchPlaceholder = DEFAULT_SEARCH,
|
|
113
|
+
filters,
|
|
114
|
+
columnSettings = false,
|
|
115
|
+
cellSize = "md",
|
|
116
|
+
columns,
|
|
117
|
+
rows,
|
|
118
|
+
emptyMessage = DEFAULT_EMPTY,
|
|
119
|
+
onSelectionChange,
|
|
120
|
+
}: DataTableProps) {
|
|
121
|
+
const uid = useId();
|
|
122
|
+
const columnsWrapRef = useRef<HTMLDivElement>(null);
|
|
123
|
+
const [query, setQuery] = useState("");
|
|
124
|
+
const [filterValues, setFilterValues] = useState<Record<string, string>>({});
|
|
125
|
+
const [hidden, setHidden] = useState<Record<string, boolean>>({});
|
|
126
|
+
const [selected, setSelected] = useState<string[]>([]);
|
|
127
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
128
|
+
const [sortKey, setSortKey] = useState<string | null>(null);
|
|
129
|
+
const [sortDir, setSortDir] = useState<"asc" | "desc">("asc");
|
|
130
|
+
|
|
131
|
+
const filterDefs = filters ?? EMPTY_FILTERS;
|
|
132
|
+
const lockedKey = lockedColumnKey(columns);
|
|
133
|
+
|
|
134
|
+
const visibleColumns = useMemo(
|
|
135
|
+
() => columns.filter((column) => column.key === lockedKey || hidden[column.key] !== true),
|
|
136
|
+
[columns, hidden, lockedKey],
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const indexedRows = useMemo(
|
|
140
|
+
() => rows.map((row, index) => ({ row, id: rowId(row, index) })),
|
|
141
|
+
[rows],
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const facetValues = useMemo(() => {
|
|
145
|
+
const map: Record<string, string[]> = {};
|
|
146
|
+
for (const filter of filterDefs) {
|
|
147
|
+
map[filter.key] = uniqueValues(rows, filter.key);
|
|
148
|
+
}
|
|
149
|
+
return map;
|
|
150
|
+
}, [filterDefs, rows]);
|
|
151
|
+
|
|
152
|
+
const visibleRows = useMemo(() => {
|
|
153
|
+
const q = query.trim().toLowerCase();
|
|
154
|
+
const filtered = indexedRows.filter(({ row }) => {
|
|
155
|
+
for (const filter of filterDefs) {
|
|
156
|
+
const selectedValue = filterValues[filter.key];
|
|
157
|
+
if (selectedValue && stringifyCell(row[filter.key]) !== selectedValue) return false;
|
|
158
|
+
}
|
|
159
|
+
if (!q) return true;
|
|
160
|
+
return visibleColumns.some((column) => stringifyCell(row[column.key]).toLowerCase().includes(q));
|
|
161
|
+
});
|
|
162
|
+
if (!sortKey) return filtered;
|
|
163
|
+
const column = columns.find((item) => item.key === sortKey);
|
|
164
|
+
const numeric = column?.type === "number";
|
|
165
|
+
const dir = sortDir === "asc" ? 1 : -1;
|
|
166
|
+
return [...filtered].sort((a, b) => {
|
|
167
|
+
const av = stringifyCell(a.row[sortKey]);
|
|
168
|
+
const bv = stringifyCell(b.row[sortKey]);
|
|
169
|
+
if (numeric) {
|
|
170
|
+
const an = Number(av);
|
|
171
|
+
const bn = Number(bv);
|
|
172
|
+
const aOk = Number.isFinite(an);
|
|
173
|
+
const bOk = Number.isFinite(bn);
|
|
174
|
+
if (aOk && bOk && an !== bn) return (an - bn) * dir;
|
|
175
|
+
if (aOk !== bOk) return (aOk ? -1 : 1) * dir;
|
|
176
|
+
}
|
|
177
|
+
return av.localeCompare(bv, undefined, { numeric: true, sensitivity: "base" }) * dir;
|
|
178
|
+
});
|
|
179
|
+
}, [indexedRows, filterDefs, filterValues, query, visibleColumns, sortKey, sortDir, columns]);
|
|
180
|
+
|
|
181
|
+
const visibleIds = visibleRows.map((entry) => entry.id);
|
|
182
|
+
const selectedVisible = selected.filter((id) => visibleIds.includes(id));
|
|
183
|
+
const allVisibleSelected = visibleIds.length > 0 && selectedVisible.length === visibleIds.length;
|
|
184
|
+
const someVisibleSelected = selectedVisible.length > 0 && !allVisibleSelected;
|
|
185
|
+
const colSpan = visibleColumns.length + (selectable ? 1 : 0);
|
|
186
|
+
const empty = visibleRows.length === 0;
|
|
187
|
+
|
|
188
|
+
useLayoutEffect(() => {
|
|
189
|
+
if (!columnSettings || !toolbar) return;
|
|
190
|
+
const button = columnsWrapRef.current?.querySelector("button");
|
|
191
|
+
if (!button) return;
|
|
192
|
+
button.setAttribute("aria-expanded", menuOpen ? "true" : "false");
|
|
193
|
+
button.setAttribute("aria-haspopup", "true");
|
|
194
|
+
}, [columnSettings, toolbar, menuOpen]);
|
|
195
|
+
|
|
196
|
+
useLayoutEffect(() => {
|
|
197
|
+
if (!menuOpen) return;
|
|
198
|
+
function onPointerDown(event: MouseEvent) {
|
|
199
|
+
if (columnsWrapRef.current && !columnsWrapRef.current.contains(event.target as Node)) {
|
|
200
|
+
setMenuOpen(false);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function onKeyDown(event: KeyboardEvent) {
|
|
204
|
+
if (event.key === "Escape") setMenuOpen(false);
|
|
205
|
+
}
|
|
206
|
+
document.addEventListener("mousedown", onPointerDown);
|
|
207
|
+
document.addEventListener("keydown", onKeyDown);
|
|
208
|
+
return () => {
|
|
209
|
+
document.removeEventListener("mousedown", onPointerDown);
|
|
210
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
211
|
+
};
|
|
212
|
+
}, [menuOpen]);
|
|
213
|
+
|
|
214
|
+
function emitSelection(next: string[]) {
|
|
215
|
+
setSelected(next);
|
|
216
|
+
onSelectionChange?.(next);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function toggleAll(checked: boolean) {
|
|
220
|
+
if (checked) {
|
|
221
|
+
emitSelection(Array.from(new Set([...selected, ...visibleIds])));
|
|
222
|
+
} else {
|
|
223
|
+
const drop = new Set(visibleIds);
|
|
224
|
+
emitSelection(selected.filter((id) => !drop.has(id)));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function toggleRow(id: string, checked: boolean) {
|
|
229
|
+
emitSelection(checked ? [...selected, id] : selected.filter((entry) => entry !== id));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function toggleSort(key: string) {
|
|
233
|
+
if (sortKey !== key) {
|
|
234
|
+
setSortKey(key);
|
|
235
|
+
setSortDir("asc");
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (sortDir === "asc") {
|
|
239
|
+
setSortDir("desc");
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
setSortKey(null);
|
|
243
|
+
setSortDir("asc");
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function toggleColumn(key: string, checked: boolean) {
|
|
247
|
+
if (key === lockedKey) return;
|
|
248
|
+
setHidden((prev) => ({ ...prev, [key]: !checked }));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function columnClass(column: DataTableColumn): string {
|
|
252
|
+
if (column.type === "number") return ` ${styles.numeric}`;
|
|
253
|
+
if (isActionColumn(column, rows)) return ` ${styles.actionCell}`;
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return (
|
|
258
|
+
<div className={styles.root} data-size={cellSize}>
|
|
259
|
+
{toolbar ? (
|
|
260
|
+
<div className={styles.toolbar}>
|
|
261
|
+
<div className={styles.search}>
|
|
262
|
+
<LucideByName name="Search" size={16} className={styles.searchIcon} />
|
|
263
|
+
<input
|
|
264
|
+
type="search"
|
|
265
|
+
className={styles.searchInput}
|
|
266
|
+
placeholder={searchPlaceholder}
|
|
267
|
+
value={query}
|
|
268
|
+
autoComplete="off"
|
|
269
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
270
|
+
aria-label={searchPlaceholder}
|
|
271
|
+
/>
|
|
272
|
+
</div>
|
|
273
|
+
{filterDefs.map((filter) => {
|
|
274
|
+
const allId = `__all-${filter.key}`;
|
|
275
|
+
const allLabel = `All ${filter.label.toLowerCase()}`;
|
|
276
|
+
const selected = filterValues[filter.key] ?? "";
|
|
277
|
+
const items = [
|
|
278
|
+
{ id: allId, label: allLabel },
|
|
279
|
+
...(facetValues[filter.key] ?? [])
|
|
280
|
+
.filter((value) => value !== "")
|
|
281
|
+
.map((value) => ({ id: value, label: value })),
|
|
282
|
+
];
|
|
283
|
+
return (
|
|
284
|
+
<div key={filter.key} className={styles.filter}>
|
|
285
|
+
<DropdownMenu
|
|
286
|
+
trigger={selected || allLabel}
|
|
287
|
+
variant="secondary"
|
|
288
|
+
size="md"
|
|
289
|
+
iconEnd="ChevronDown"
|
|
290
|
+
ariaLabel={filter.label}
|
|
291
|
+
groups={[{ items }]}
|
|
292
|
+
onSelect={(id) =>
|
|
293
|
+
setFilterValues((prev) => ({
|
|
294
|
+
...prev,
|
|
295
|
+
[filter.key]: id === allId ? "" : id,
|
|
296
|
+
}))
|
|
297
|
+
}
|
|
298
|
+
/>
|
|
299
|
+
</div>
|
|
300
|
+
);
|
|
301
|
+
})}
|
|
302
|
+
{columnSettings ? (
|
|
303
|
+
<div ref={columnsWrapRef} className={styles.columns}>
|
|
304
|
+
<Button
|
|
305
|
+
variant="secondary"
|
|
306
|
+
size="md"
|
|
307
|
+
iconStart="Settings"
|
|
308
|
+
onClick={() => setMenuOpen((open) => !open)}
|
|
309
|
+
>
|
|
310
|
+
Columns
|
|
311
|
+
</Button>
|
|
312
|
+
{menuOpen ? (
|
|
313
|
+
<div className={styles.columnMenu} role="group" aria-label="Columns">
|
|
314
|
+
{columns.map((column) => {
|
|
315
|
+
const locked = column.key === lockedKey;
|
|
316
|
+
return (
|
|
317
|
+
<Checkbox
|
|
318
|
+
key={column.key}
|
|
319
|
+
id={`${uid}-col-${column.key}`}
|
|
320
|
+
size="sm"
|
|
321
|
+
label={column.header}
|
|
322
|
+
checked={locked || hidden[column.key] !== true}
|
|
323
|
+
disabled={locked}
|
|
324
|
+
onChange={(checked) => toggleColumn(column.key, checked)}
|
|
325
|
+
/>
|
|
326
|
+
);
|
|
327
|
+
})}
|
|
328
|
+
</div>
|
|
329
|
+
) : null}
|
|
330
|
+
</div>
|
|
331
|
+
) : null}
|
|
332
|
+
</div>
|
|
333
|
+
) : null}
|
|
334
|
+
|
|
335
|
+
{toolbar ? (
|
|
336
|
+
<p className={styles.count}>
|
|
337
|
+
Showing {visibleRows.length} of {rows.length}
|
|
338
|
+
</p>
|
|
339
|
+
) : null}
|
|
340
|
+
|
|
341
|
+
<div className={styles.scroller}>
|
|
342
|
+
<table className={styles.table}>
|
|
343
|
+
{caption ? <caption className={styles.caption}>{caption}</caption> : null}
|
|
344
|
+
<thead>
|
|
345
|
+
<tr>
|
|
346
|
+
{selectable ? (
|
|
347
|
+
<th scope="col" className={`${styles.th} ${styles.checkCell}`}>
|
|
348
|
+
<HeaderCell
|
|
349
|
+
size={cellSize}
|
|
350
|
+
checkbox
|
|
351
|
+
id={`${uid}-all`}
|
|
352
|
+
checked={allVisibleSelected}
|
|
353
|
+
indeterminate={someVisibleSelected}
|
|
354
|
+
onCheckedChange={toggleAll}
|
|
355
|
+
/>
|
|
356
|
+
</th>
|
|
357
|
+
) : null}
|
|
358
|
+
{visibleColumns.map((column) => (
|
|
359
|
+
<th
|
|
360
|
+
key={column.key}
|
|
361
|
+
scope="col"
|
|
362
|
+
className={`${styles.th}${columnClass(column)}`}
|
|
363
|
+
aria-sort={
|
|
364
|
+
column.sortable
|
|
365
|
+
? sortKey === column.key
|
|
366
|
+
? sortDir === "asc"
|
|
367
|
+
? "ascending"
|
|
368
|
+
: "descending"
|
|
369
|
+
: "none"
|
|
370
|
+
: undefined
|
|
371
|
+
}
|
|
372
|
+
>
|
|
373
|
+
<HeaderCell
|
|
374
|
+
label={column.header}
|
|
375
|
+
size={cellSize}
|
|
376
|
+
align={
|
|
377
|
+
column.type === "number"
|
|
378
|
+
? "end"
|
|
379
|
+
: isActionColumn(column, rows)
|
|
380
|
+
? "center"
|
|
381
|
+
: "start"
|
|
382
|
+
}
|
|
383
|
+
sortable={column.sortable}
|
|
384
|
+
sort={sortKey === column.key ? sortDir : undefined}
|
|
385
|
+
onSort={() => toggleSort(column.key)}
|
|
386
|
+
/>
|
|
387
|
+
</th>
|
|
388
|
+
))}
|
|
389
|
+
</tr>
|
|
390
|
+
</thead>
|
|
391
|
+
<tbody>
|
|
392
|
+
{empty ? (
|
|
393
|
+
<tr className={styles.tr}>
|
|
394
|
+
<td className={`${styles.td} ${styles.emptyCell}`} colSpan={Math.max(colSpan, 1)}>
|
|
395
|
+
{emptyMessage}
|
|
396
|
+
</td>
|
|
397
|
+
</tr>
|
|
398
|
+
) : (
|
|
399
|
+
visibleRows.map(({ row, id }) => {
|
|
400
|
+
const checked = selected.includes(id);
|
|
401
|
+
const name = rowLabel(row, lockedKey, id);
|
|
402
|
+
return (
|
|
403
|
+
<tr key={id} className={styles.tr} data-selected={checked || undefined}>
|
|
404
|
+
{selectable ? (
|
|
405
|
+
<td className={`${styles.td} ${styles.checkCell}`}>
|
|
406
|
+
<Checkbox
|
|
407
|
+
id={`${uid}-row-${id}`}
|
|
408
|
+
size="sm"
|
|
409
|
+
ariaLabel={`Select ${name}`}
|
|
410
|
+
checked={checked}
|
|
411
|
+
onChange={(next) => toggleRow(id, next)}
|
|
412
|
+
/>
|
|
413
|
+
</td>
|
|
414
|
+
) : null}
|
|
415
|
+
{visibleColumns.map((column) => (
|
|
416
|
+
<td key={column.key} className={`${styles.td}${columnClass(column)}`}>
|
|
417
|
+
{renderCell(row[column.key], cellSize)}
|
|
418
|
+
</td>
|
|
419
|
+
))}
|
|
420
|
+
</tr>
|
|
421
|
+
);
|
|
422
|
+
})
|
|
423
|
+
)}
|
|
424
|
+
</tbody>
|
|
425
|
+
</table>
|
|
426
|
+
</div>
|
|
427
|
+
</div>
|
|
428
|
+
);
|
|
429
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CellSize, CellValue } from "../Cell/Cell.types";
|
|
2
|
+
|
|
3
|
+
export type DataTableColumnType = "text" | "number";
|
|
4
|
+
|
|
5
|
+
export type DataTableSort = "asc" | "desc";
|
|
6
|
+
|
|
7
|
+
export interface DataTableColumn {
|
|
8
|
+
key: string;
|
|
9
|
+
header: string;
|
|
10
|
+
type?: DataTableColumnType;
|
|
11
|
+
sortable?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface DataTableFilter {
|
|
15
|
+
key: string;
|
|
16
|
+
label: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type DataTableCell = string | number | CellValue;
|
|
20
|
+
|
|
21
|
+
export type DataTableRow = Record<string, DataTableCell>;
|
|
22
|
+
|
|
23
|
+
export interface DataTableProps {
|
|
24
|
+
caption?: string;
|
|
25
|
+
selectable?: boolean;
|
|
26
|
+
toolbar?: boolean;
|
|
27
|
+
searchPlaceholder?: string;
|
|
28
|
+
filters?: DataTableFilter[];
|
|
29
|
+
columnSettings?: boolean;
|
|
30
|
+
cellSize?: CellSize;
|
|
31
|
+
columns: DataTableColumn[];
|
|
32
|
+
rows: DataTableRow[];
|
|
33
|
+
emptyMessage?: string;
|
|
34
|
+
onSelectionChange?: (ids: string[]) => void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
.root {
|
|
2
|
+
position: relative;
|
|
3
|
+
display: block;
|
|
4
|
+
width: 100%;
|
|
5
|
+
max-width: 20rem;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.trigger {
|
|
9
|
+
appearance: none;
|
|
10
|
+
font: inherit;
|
|
11
|
+
margin: 0;
|
|
12
|
+
display: inline-flex;
|
|
13
|
+
align-items: center;
|
|
14
|
+
gap: var(--space-2);
|
|
15
|
+
width: 100%;
|
|
16
|
+
text-align: start;
|
|
17
|
+
cursor: pointer;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.trigger:disabled {
|
|
21
|
+
cursor: not-allowed;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.icon {
|
|
25
|
+
flex-shrink: 0;
|
|
26
|
+
color: var(--icon-muted);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.value {
|
|
30
|
+
min-width: 0;
|
|
31
|
+
color: var(--text-primary);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.placeholder {
|
|
35
|
+
min-width: 0;
|
|
36
|
+
color: var(--text-muted);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.panel {
|
|
40
|
+
position: absolute;
|
|
41
|
+
z-index: 20;
|
|
42
|
+
top: calc(100% + var(--space-1));
|
|
43
|
+
left: 0;
|
|
44
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
4
|
+
import { Calendar } from "../Calendar";
|
|
5
|
+
import { LucideByName } from "../Button/lucideName";
|
|
6
|
+
import controlStyles from "../shared/controls.module.css";
|
|
7
|
+
import type { DatePickerProps } from "./DatePicker.types";
|
|
8
|
+
import styles from "./DatePicker.module.css";
|
|
9
|
+
|
|
10
|
+
export type { DatePickerProps, DatePickerSize } from "./DatePicker.types";
|
|
11
|
+
|
|
12
|
+
function parseISODate(iso?: string): Date | null {
|
|
13
|
+
if (!iso) return null;
|
|
14
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso);
|
|
15
|
+
if (!match) return null;
|
|
16
|
+
const year = Number(match[1]);
|
|
17
|
+
const month = Number(match[2]) - 1;
|
|
18
|
+
const day = Number(match[3]);
|
|
19
|
+
const date = new Date(year, month, day);
|
|
20
|
+
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return date;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function formatDisplay(iso?: string): string {
|
|
27
|
+
const date = parseISODate(iso);
|
|
28
|
+
if (!date) return "";
|
|
29
|
+
return date.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function DatePicker({
|
|
33
|
+
id,
|
|
34
|
+
value,
|
|
35
|
+
defaultValue,
|
|
36
|
+
onValueChange,
|
|
37
|
+
size = "md",
|
|
38
|
+
placeholder = "Pick a date",
|
|
39
|
+
disabled = false,
|
|
40
|
+
error = false,
|
|
41
|
+
minDate,
|
|
42
|
+
maxDate,
|
|
43
|
+
name,
|
|
44
|
+
}: DatePickerProps) {
|
|
45
|
+
const fallbackId = useId();
|
|
46
|
+
const triggerId = id || fallbackId;
|
|
47
|
+
const panelId = `${triggerId}-calendar`;
|
|
48
|
+
const isControlled = value !== undefined;
|
|
49
|
+
const [uncontrolled, setUncontrolled] = useState(defaultValue);
|
|
50
|
+
const selected = isControlled ? value : uncontrolled;
|
|
51
|
+
const [open, setOpen] = useState(false);
|
|
52
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
53
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
54
|
+
const display = formatDisplay(selected);
|
|
55
|
+
const iconPx = size === "sm" ? 14 : 16;
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!open) return undefined;
|
|
59
|
+
|
|
60
|
+
function onPointerDown(event: PointerEvent) {
|
|
61
|
+
if (!rootRef.current?.contains(event.target as Node)) {
|
|
62
|
+
setOpen(false);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function onKeyDown(event: KeyboardEvent) {
|
|
67
|
+
if (event.key === "Escape") {
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
setOpen(false);
|
|
70
|
+
triggerRef.current?.focus();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
document.addEventListener("pointerdown", onPointerDown);
|
|
75
|
+
document.addEventListener("keydown", onKeyDown);
|
|
76
|
+
return () => {
|
|
77
|
+
document.removeEventListener("pointerdown", onPointerDown);
|
|
78
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
79
|
+
};
|
|
80
|
+
}, [open]);
|
|
81
|
+
|
|
82
|
+
function commit(iso: string) {
|
|
83
|
+
if (!isControlled) setUncontrolled(iso);
|
|
84
|
+
onValueChange?.(iso);
|
|
85
|
+
setOpen(false);
|
|
86
|
+
triggerRef.current?.focus();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div ref={rootRef} className={styles.root}>
|
|
91
|
+
<button
|
|
92
|
+
ref={triggerRef}
|
|
93
|
+
id={triggerId}
|
|
94
|
+
type="button"
|
|
95
|
+
disabled={disabled}
|
|
96
|
+
aria-haspopup="dialog"
|
|
97
|
+
aria-expanded={open}
|
|
98
|
+
aria-controls={panelId}
|
|
99
|
+
aria-invalid={error || undefined}
|
|
100
|
+
className={`${controlStyles.control} ${size === "sm" ? controlStyles.controlSm : controlStyles.controlMd} ${error ? controlStyles.controlError : ""} ${styles.trigger}`}
|
|
101
|
+
onClick={() => {
|
|
102
|
+
if (!disabled) setOpen((current) => !current);
|
|
103
|
+
}}
|
|
104
|
+
>
|
|
105
|
+
<LucideByName name="CalendarDays" size={iconPx} className={styles.icon} />
|
|
106
|
+
<span className={display ? styles.value : styles.placeholder}>{display || placeholder}</span>
|
|
107
|
+
</button>
|
|
108
|
+
{name ? <input type="hidden" name={name} value={selected ?? ""} /> : null}
|
|
109
|
+
{open ? (
|
|
110
|
+
<div id={panelId} className={styles.panel} role="dialog" aria-label="Choose date">
|
|
111
|
+
<Calendar
|
|
112
|
+
size={size === "sm" ? "sm" : "md"}
|
|
113
|
+
value={selected}
|
|
114
|
+
onValueChange={commit}
|
|
115
|
+
disabled={disabled}
|
|
116
|
+
minDate={minDate}
|
|
117
|
+
maxDate={maxDate}
|
|
118
|
+
/>
|
|
119
|
+
</div>
|
|
120
|
+
) : null}
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type DatePickerSize = "sm" | "md";
|
|
2
|
+
|
|
3
|
+
export interface DatePickerProps {
|
|
4
|
+
id: string;
|
|
5
|
+
value?: string;
|
|
6
|
+
defaultValue?: string;
|
|
7
|
+
onValueChange?: (date: string) => void;
|
|
8
|
+
size?: DatePickerSize;
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
error?: boolean;
|
|
12
|
+
minDate?: string;
|
|
13
|
+
maxDate?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|