@foundrykit/components 2.1.1 → 2.1.3
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/.turbo/turbo-build.log +14 -14
- package/.turbo/turbo-type-check.log +1 -1
- package/CHANGELOG.md +16 -0
- package/README.md +49 -145
- package/dist/index.d.ts +270 -60
- package/dist/index.js +2137 -1447
- package/dist/index.js.map +1 -1
- package/package.json +16 -9
- package/src/components/advanced-search-bar/index.tsx +121 -122
- package/src/components/calendar/config.ts +1 -1
- package/src/components/calendar/index.tsx +37 -25
- package/src/components/carousel/config.ts +28 -0
- package/src/components/carousel/index.tsx +258 -0
- package/src/components/chart/config.ts +28 -0
- package/src/components/chart/index.tsx +365 -0
- package/src/components/combobox/config.ts +28 -0
- package/src/components/combobox/index.tsx +101 -0
- package/src/components/command/config.ts +1 -1
- package/src/components/command/index.tsx +23 -12
- package/src/components/data-table/config.ts +28 -0
- package/src/components/data-table/index.tsx +124 -0
- package/src/components/date-picker/config.ts +1 -1
- package/src/components/date-picker/index.tsx +119 -51
- package/src/components/form/index.tsx +29 -11
- package/src/components/input-otp/config.ts +28 -0
- package/src/components/input-otp/index.tsx +92 -0
- package/src/components/pagination/config.ts +1 -1
- package/src/components/pagination/index.tsx +16 -8
- package/src/components/search-bar/config.ts +1 -1
- package/src/components/search-bar/index.tsx +9 -3
- package/src/components/sidebar/index.tsx +22 -25
- package/src/components/theme-toggle/index.tsx +2 -3
- package/src/index.ts +66 -37
- package/eslint.config.js +0 -4
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
Popover,
|
|
6
|
+
PopoverContent,
|
|
7
|
+
PopoverTrigger,
|
|
8
|
+
} from '@foundrykit/primitives';
|
|
9
|
+
import { cn } from '@foundrykit/utils';
|
|
10
|
+
import { CheckIcon, ChevronsUpDownIcon } from 'lucide-react';
|
|
11
|
+
import * as React from 'react';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
Command,
|
|
15
|
+
CommandEmpty,
|
|
16
|
+
CommandGroup,
|
|
17
|
+
CommandInput,
|
|
18
|
+
CommandItem,
|
|
19
|
+
CommandList,
|
|
20
|
+
} from '../command/index.js';
|
|
21
|
+
|
|
22
|
+
type ComboboxOption = {
|
|
23
|
+
value: string;
|
|
24
|
+
label: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Searchable single-select combobox composed from `Popover` and `Command`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* <Combobox
|
|
33
|
+
* options={[{ value: 'next', label: 'Next.js' }]}
|
|
34
|
+
* value={value}
|
|
35
|
+
* onValueChange={setValue}
|
|
36
|
+
* placeholder='Select framework...'
|
|
37
|
+
* />
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function Combobox({
|
|
41
|
+
options,
|
|
42
|
+
value,
|
|
43
|
+
onValueChange,
|
|
44
|
+
placeholder = 'Select an option...',
|
|
45
|
+
}: {
|
|
46
|
+
options: ComboboxOption[];
|
|
47
|
+
value?: string;
|
|
48
|
+
onValueChange?: (value: string) => void;
|
|
49
|
+
placeholder?: string;
|
|
50
|
+
}) {
|
|
51
|
+
const [open, setOpen] = React.useState(false);
|
|
52
|
+
|
|
53
|
+
const selected = options.find(option => option.value === value);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
57
|
+
<PopoverTrigger asChild>
|
|
58
|
+
<Button
|
|
59
|
+
data-slot='combobox-trigger'
|
|
60
|
+
variant='outline'
|
|
61
|
+
role='combobox'
|
|
62
|
+
aria-expanded={open}
|
|
63
|
+
className='w-[200px] justify-between'
|
|
64
|
+
>
|
|
65
|
+
{selected ? selected.label : placeholder}
|
|
66
|
+
<ChevronsUpDownIcon className='ml-2 size-4 shrink-0 opacity-50' />
|
|
67
|
+
</Button>
|
|
68
|
+
</PopoverTrigger>
|
|
69
|
+
<PopoverContent data-slot='combobox-content' className='w-[200px] p-0'>
|
|
70
|
+
<Command>
|
|
71
|
+
<CommandInput placeholder={placeholder} className='h-9' />
|
|
72
|
+
<CommandList>
|
|
73
|
+
<CommandEmpty>No option found.</CommandEmpty>
|
|
74
|
+
<CommandGroup>
|
|
75
|
+
{options.map(option => (
|
|
76
|
+
<CommandItem
|
|
77
|
+
key={option.value}
|
|
78
|
+
value={option.value}
|
|
79
|
+
onSelect={currentValue => {
|
|
80
|
+
onValueChange?.(currentValue === value ? '' : currentValue);
|
|
81
|
+
setOpen(false);
|
|
82
|
+
}}
|
|
83
|
+
>
|
|
84
|
+
{option.label}
|
|
85
|
+
<CheckIcon
|
|
86
|
+
className={cn(
|
|
87
|
+
'ml-auto size-4',
|
|
88
|
+
value === option.value ? 'opacity-100' : 'opacity-0'
|
|
89
|
+
)}
|
|
90
|
+
/>
|
|
91
|
+
</CommandItem>
|
|
92
|
+
))}
|
|
93
|
+
</CommandGroup>
|
|
94
|
+
</CommandList>
|
|
95
|
+
</Command>
|
|
96
|
+
</PopoverContent>
|
|
97
|
+
</Popover>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { Combobox };
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import * as React from 'react';
|
|
4
|
-
import { Command as CommandPrimitive } from 'cmdk';
|
|
5
|
-
import { SearchIcon } from 'lucide-react';
|
|
6
|
-
|
|
7
|
-
import { cn } from '@foundrykit/utils';
|
|
8
3
|
import {
|
|
9
4
|
Dialog,
|
|
10
5
|
DialogContent,
|
|
@@ -12,7 +7,12 @@ import {
|
|
|
12
7
|
DialogHeader,
|
|
13
8
|
DialogTitle,
|
|
14
9
|
} from '@foundrykit/primitives';
|
|
10
|
+
import { cn } from '@foundrykit/utils';
|
|
11
|
+
import { Command as CommandPrimitive } from 'cmdk';
|
|
12
|
+
import { SearchIcon } from 'lucide-react';
|
|
13
|
+
import type * as React from 'react';
|
|
15
14
|
|
|
15
|
+
/** Styled `cmdk` command menu — the root container for a command palette. */
|
|
16
16
|
function Command({
|
|
17
17
|
className,
|
|
18
18
|
...props
|
|
@@ -29,6 +29,10 @@ function Command({
|
|
|
29
29
|
);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Command menu rendered inside a modal dialog. `title` and `description` are
|
|
34
|
+
* visually hidden but read by assistive technology.
|
|
35
|
+
*/
|
|
32
36
|
function CommandDialog({
|
|
33
37
|
title = 'Command Palette',
|
|
34
38
|
description = 'Search for a command to run...',
|
|
@@ -52,7 +56,7 @@ function CommandDialog({
|
|
|
52
56
|
className={cn('overflow-hidden p-0', className)}
|
|
53
57
|
showCloseButton={showCloseButton}
|
|
54
58
|
>
|
|
55
|
-
<Command className='[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-
|
|
59
|
+
<Command className='[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5'>
|
|
56
60
|
{children}
|
|
57
61
|
</Command>
|
|
58
62
|
</DialogContent>
|
|
@@ -60,6 +64,7 @@ function CommandDialog({
|
|
|
60
64
|
);
|
|
61
65
|
}
|
|
62
66
|
|
|
67
|
+
/** Search input for a command menu, with a leading search icon. */
|
|
63
68
|
function CommandInput({
|
|
64
69
|
className,
|
|
65
70
|
...props
|
|
@@ -73,7 +78,7 @@ function CommandInput({
|
|
|
73
78
|
<CommandPrimitive.Input
|
|
74
79
|
data-slot='command-input'
|
|
75
80
|
className={cn(
|
|
76
|
-
'placeholder:text-muted-foreground
|
|
81
|
+
'placeholder:text-muted-foreground flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-hidden disabled:cursor-not-allowed disabled:opacity-50',
|
|
77
82
|
className
|
|
78
83
|
)}
|
|
79
84
|
{...props}
|
|
@@ -82,6 +87,7 @@ function CommandInput({
|
|
|
82
87
|
);
|
|
83
88
|
}
|
|
84
89
|
|
|
90
|
+
/** Scrollable list containing command groups and items. */
|
|
85
91
|
function CommandList({
|
|
86
92
|
className,
|
|
87
93
|
...props
|
|
@@ -90,7 +96,7 @@ function CommandList({
|
|
|
90
96
|
<CommandPrimitive.List
|
|
91
97
|
data-slot='command-list'
|
|
92
98
|
className={cn(
|
|
93
|
-
'max-h-[300px] scroll-py-1 overflow-
|
|
99
|
+
'max-h-[300px] scroll-py-1 overflow-x-hidden overflow-y-auto',
|
|
94
100
|
className
|
|
95
101
|
)}
|
|
96
102
|
{...props}
|
|
@@ -98,6 +104,7 @@ function CommandList({
|
|
|
98
104
|
);
|
|
99
105
|
}
|
|
100
106
|
|
|
107
|
+
/** Message shown when no command items match the current query. */
|
|
101
108
|
function CommandEmpty({
|
|
102
109
|
...props
|
|
103
110
|
}: React.ComponentProps<typeof CommandPrimitive.Empty>) {
|
|
@@ -110,6 +117,7 @@ function CommandEmpty({
|
|
|
110
117
|
);
|
|
111
118
|
}
|
|
112
119
|
|
|
120
|
+
/** Labelled group of related command items. */
|
|
113
121
|
function CommandGroup({
|
|
114
122
|
className,
|
|
115
123
|
...props
|
|
@@ -126,6 +134,7 @@ function CommandGroup({
|
|
|
126
134
|
);
|
|
127
135
|
}
|
|
128
136
|
|
|
137
|
+
/** Horizontal divider between command groups or items. */
|
|
129
138
|
function CommandSeparator({
|
|
130
139
|
className,
|
|
131
140
|
...props
|
|
@@ -139,6 +148,7 @@ function CommandSeparator({
|
|
|
139
148
|
);
|
|
140
149
|
}
|
|
141
150
|
|
|
151
|
+
/** Selectable command item within a {@link CommandList} or {@link CommandGroup}. */
|
|
142
152
|
function CommandItem({
|
|
143
153
|
className,
|
|
144
154
|
...props
|
|
@@ -147,7 +157,7 @@ function CommandItem({
|
|
|
147
157
|
<CommandPrimitive.Item
|
|
148
158
|
data-slot='command-item'
|
|
149
159
|
className={cn(
|
|
150
|
-
"data-[selected=true]:bg-accent data-[selected=true]:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground
|
|
160
|
+
"data-[selected=true]:bg-accent data-[selected=true]:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
151
161
|
className
|
|
152
162
|
)}
|
|
153
163
|
{...props}
|
|
@@ -155,6 +165,7 @@ function CommandItem({
|
|
|
155
165
|
);
|
|
156
166
|
}
|
|
157
167
|
|
|
168
|
+
/** Right-aligned keyboard-shortcut hint shown inside a command item. */
|
|
158
169
|
function CommandShortcut({
|
|
159
170
|
className,
|
|
160
171
|
...props
|
|
@@ -174,11 +185,11 @@ function CommandShortcut({
|
|
|
174
185
|
export {
|
|
175
186
|
Command,
|
|
176
187
|
CommandDialog,
|
|
177
|
-
CommandInput,
|
|
178
|
-
CommandList,
|
|
179
188
|
CommandEmpty,
|
|
180
189
|
CommandGroup,
|
|
190
|
+
CommandInput,
|
|
181
191
|
CommandItem,
|
|
182
|
-
|
|
192
|
+
CommandList,
|
|
183
193
|
CommandSeparator,
|
|
194
|
+
CommandShortcut,
|
|
184
195
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ComponentConfig } from '@foundrykit/types';
|
|
2
|
+
|
|
3
|
+
export const DataTableConfig: ComponentConfig = {
|
|
4
|
+
name: 'Data Table',
|
|
5
|
+
description:
|
|
6
|
+
'A generic, paginated, and sortable table powered by TanStack Table and the Table primitives.',
|
|
7
|
+
category: 'components',
|
|
8
|
+
tags: ['table', 'data', 'tanstack', 'pagination', 'sorting'],
|
|
9
|
+
preview: {
|
|
10
|
+
type: 'static',
|
|
11
|
+
html: '<div class="w-full overflow-hidden rounded-md border border-gray-300 bg-white"><div class="flex border-b border-gray-300 bg-gray-100 px-3 py-2 text-xs font-medium text-gray-600"><span class="flex-1">Name</span><span class="flex-1">Status</span></div><div class="flex px-3 py-2 text-xs text-gray-600"><span class="flex-1">Row 1</span><span class="flex-1">Active</span></div></div>',
|
|
12
|
+
},
|
|
13
|
+
variants: [
|
|
14
|
+
{
|
|
15
|
+
name: 'Default',
|
|
16
|
+
description: 'Default data table with pagination',
|
|
17
|
+
code: '<DataTable columns={columns} data={data} />',
|
|
18
|
+
preview: {
|
|
19
|
+
type: 'component',
|
|
20
|
+
component: 'DataTable',
|
|
21
|
+
props: {},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
dependencies: ['utils', 'primitives'],
|
|
26
|
+
packageDependencies: ['@tanstack/react-table'],
|
|
27
|
+
files: ['components/data-table/index.tsx'],
|
|
28
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
Table,
|
|
6
|
+
TableBody,
|
|
7
|
+
TableCell,
|
|
8
|
+
TableHead,
|
|
9
|
+
TableHeader,
|
|
10
|
+
TableRow,
|
|
11
|
+
} from '@foundrykit/primitives';
|
|
12
|
+
import {
|
|
13
|
+
type ColumnDef,
|
|
14
|
+
flexRender,
|
|
15
|
+
getCoreRowModel,
|
|
16
|
+
getPaginationRowModel,
|
|
17
|
+
getSortedRowModel,
|
|
18
|
+
type SortingState,
|
|
19
|
+
useReactTable,
|
|
20
|
+
} from '@tanstack/react-table';
|
|
21
|
+
import * as React from 'react';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generic, paginated, sortable data table driven by TanStack Table column defs.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* <DataTable columns={columns} data={data} />
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function DataTable<TData, TValue>({
|
|
32
|
+
columns,
|
|
33
|
+
data,
|
|
34
|
+
}: {
|
|
35
|
+
columns: ColumnDef<TData, TValue>[];
|
|
36
|
+
data: TData[];
|
|
37
|
+
}) {
|
|
38
|
+
const [sorting, setSorting] = React.useState<SortingState>([]);
|
|
39
|
+
|
|
40
|
+
const table = useReactTable({
|
|
41
|
+
data,
|
|
42
|
+
columns,
|
|
43
|
+
getCoreRowModel: getCoreRowModel(),
|
|
44
|
+
getPaginationRowModel: getPaginationRowModel(),
|
|
45
|
+
getSortedRowModel: getSortedRowModel(),
|
|
46
|
+
onSortingChange: setSorting,
|
|
47
|
+
state: {
|
|
48
|
+
sorting,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<div data-slot='data-table' className='space-y-4'>
|
|
54
|
+
<div className='rounded-md border'>
|
|
55
|
+
<Table>
|
|
56
|
+
<TableHeader>
|
|
57
|
+
{table.getHeaderGroups().map(headerGroup => (
|
|
58
|
+
<TableRow key={headerGroup.id}>
|
|
59
|
+
{headerGroup.headers.map(header => (
|
|
60
|
+
<TableHead key={header.id}>
|
|
61
|
+
{header.isPlaceholder
|
|
62
|
+
? null
|
|
63
|
+
: flexRender(
|
|
64
|
+
header.column.columnDef.header,
|
|
65
|
+
header.getContext()
|
|
66
|
+
)}
|
|
67
|
+
</TableHead>
|
|
68
|
+
))}
|
|
69
|
+
</TableRow>
|
|
70
|
+
))}
|
|
71
|
+
</TableHeader>
|
|
72
|
+
<TableBody>
|
|
73
|
+
{table.getRowModel().rows?.length ? (
|
|
74
|
+
table.getRowModel().rows.map(row => (
|
|
75
|
+
<TableRow
|
|
76
|
+
key={row.id}
|
|
77
|
+
data-state={row.getIsSelected() && 'selected'}
|
|
78
|
+
>
|
|
79
|
+
{row.getVisibleCells().map(cell => (
|
|
80
|
+
<TableCell key={cell.id}>
|
|
81
|
+
{flexRender(
|
|
82
|
+
cell.column.columnDef.cell,
|
|
83
|
+
cell.getContext()
|
|
84
|
+
)}
|
|
85
|
+
</TableCell>
|
|
86
|
+
))}
|
|
87
|
+
</TableRow>
|
|
88
|
+
))
|
|
89
|
+
) : (
|
|
90
|
+
<TableRow>
|
|
91
|
+
<TableCell
|
|
92
|
+
colSpan={columns.length}
|
|
93
|
+
className='h-24 text-center'
|
|
94
|
+
>
|
|
95
|
+
No results.
|
|
96
|
+
</TableCell>
|
|
97
|
+
</TableRow>
|
|
98
|
+
)}
|
|
99
|
+
</TableBody>
|
|
100
|
+
</Table>
|
|
101
|
+
</div>
|
|
102
|
+
<div className='flex items-center justify-end gap-2'>
|
|
103
|
+
<Button
|
|
104
|
+
variant='outline'
|
|
105
|
+
size='sm'
|
|
106
|
+
onClick={() => table.previousPage()}
|
|
107
|
+
disabled={!table.getCanPreviousPage()}
|
|
108
|
+
>
|
|
109
|
+
Previous
|
|
110
|
+
</Button>
|
|
111
|
+
<Button
|
|
112
|
+
variant='outline'
|
|
113
|
+
size='sm'
|
|
114
|
+
onClick={() => table.nextPage()}
|
|
115
|
+
disabled={!table.getCanNextPage()}
|
|
116
|
+
>
|
|
117
|
+
Next
|
|
118
|
+
</Button>
|
|
119
|
+
</div>
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { DataTable };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import { Calendar as CalendarIcon } from 'lucide-react';
|
|
3
|
-
import { format } from 'date-fns';
|
|
1
|
+
'use client';
|
|
4
2
|
|
|
5
|
-
import { Button } from '@foundrykit/primitives';
|
|
6
|
-
import { Input } from '@foundrykit/primitives';
|
|
3
|
+
import { Button, Input } from '@foundrykit/primitives';
|
|
7
4
|
import { cn } from '@foundrykit/utils';
|
|
5
|
+
import { format } from 'date-fns';
|
|
6
|
+
import { Calendar as CalendarIcon } from 'lucide-react';
|
|
7
|
+
import * as React from 'react';
|
|
8
8
|
|
|
9
9
|
export interface DatePickerProps {
|
|
10
10
|
value?: Date;
|
|
@@ -14,6 +14,13 @@ export interface DatePickerProps {
|
|
|
14
14
|
disabled?: boolean;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
const WEEK_DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Date picker pairing a native date input with a pop-up month grid. Controlled
|
|
21
|
+
* via `value`/`onValueChange`; the calendar closes on selection, outside click,
|
|
22
|
+
* or Escape and returns focus to the toggle button.
|
|
23
|
+
*/
|
|
17
24
|
const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>(
|
|
18
25
|
(
|
|
19
26
|
{ value, onValueChange, placeholder = 'Pick a date', className, disabled },
|
|
@@ -24,10 +31,48 @@ const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>(
|
|
|
24
31
|
value
|
|
25
32
|
);
|
|
26
33
|
|
|
34
|
+
// Keep internal state in sync with the controlled `value` prop so a parent
|
|
35
|
+
// that changes `value` after mount is respected (previously ignored).
|
|
36
|
+
React.useEffect(() => {
|
|
37
|
+
setSelectedDate(value);
|
|
38
|
+
}, [value]);
|
|
39
|
+
|
|
40
|
+
const containerRef = React.useRef<HTMLDivElement>(null);
|
|
41
|
+
const toggleRef = React.useRef<HTMLButtonElement>(null);
|
|
42
|
+
// Merge the forwarded ref with our internal container ref.
|
|
43
|
+
React.useImperativeHandle(
|
|
44
|
+
ref,
|
|
45
|
+
() => containerRef.current as HTMLDivElement
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Dismiss on outside click and on Escape; restore focus to the toggle when
|
|
49
|
+
// closing via keyboard.
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
if (!open) return;
|
|
52
|
+
|
|
53
|
+
const onPointerDown = (e: MouseEvent) => {
|
|
54
|
+
if (!containerRef.current?.contains(e.target as Node)) setOpen(false);
|
|
55
|
+
};
|
|
56
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
57
|
+
if (e.key === 'Escape') {
|
|
58
|
+
setOpen(false);
|
|
59
|
+
toggleRef.current?.focus();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
document.addEventListener('mousedown', onPointerDown);
|
|
64
|
+
document.addEventListener('keydown', onKeyDown);
|
|
65
|
+
return () => {
|
|
66
|
+
document.removeEventListener('mousedown', onPointerDown);
|
|
67
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
68
|
+
};
|
|
69
|
+
}, [open]);
|
|
70
|
+
|
|
27
71
|
const handleDateSelect = (date: Date) => {
|
|
28
72
|
setSelectedDate(date);
|
|
29
73
|
onValueChange?.(date);
|
|
30
74
|
setOpen(false);
|
|
75
|
+
toggleRef.current?.focus();
|
|
31
76
|
};
|
|
32
77
|
|
|
33
78
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
@@ -44,40 +89,29 @@ const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>(
|
|
|
44
89
|
}
|
|
45
90
|
};
|
|
46
91
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
: today.getMonth();
|
|
52
|
-
const currentYear = selectedDate
|
|
53
|
-
? selectedDate.getFullYear()
|
|
54
|
-
: today.getFullYear();
|
|
92
|
+
const viewDate = selectedDate ?? new Date();
|
|
93
|
+
const currentMonth = viewDate.getMonth();
|
|
94
|
+
const currentYear = viewDate.getFullYear();
|
|
95
|
+
const monthLabel = format(viewDate, 'MMMM yyyy');
|
|
55
96
|
|
|
97
|
+
const calendarDays = React.useMemo(() => {
|
|
56
98
|
const firstDay = new Date(currentYear, currentMonth, 1);
|
|
57
|
-
const
|
|
58
|
-
const daysInMonth = lastDay.getDate();
|
|
99
|
+
const daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
|
|
59
100
|
const startingDayOfWeek = firstDay.getDay();
|
|
60
101
|
|
|
61
|
-
const days = [];
|
|
62
|
-
|
|
63
|
-
// Add empty cells for days before the first day of the month
|
|
64
|
-
for (let i = 0; i < startingDayOfWeek; i++) {
|
|
65
|
-
days.push(null);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Add days of the month
|
|
102
|
+
const days: (Date | null)[] = [];
|
|
103
|
+
for (let i = 0; i < startingDayOfWeek; i++) days.push(null);
|
|
69
104
|
for (let day = 1; day <= daysInMonth; day++) {
|
|
70
105
|
days.push(new Date(currentYear, currentMonth, day));
|
|
71
106
|
}
|
|
72
|
-
|
|
73
107
|
return days;
|
|
74
|
-
};
|
|
108
|
+
}, [currentMonth, currentYear]);
|
|
75
109
|
|
|
76
|
-
const
|
|
77
|
-
const
|
|
110
|
+
const today = new Date();
|
|
111
|
+
const dialogId = React.useId();
|
|
78
112
|
|
|
79
113
|
return (
|
|
80
|
-
<div className={cn('relative', className)} ref={
|
|
114
|
+
<div className={cn('relative', className)} ref={containerRef}>
|
|
81
115
|
<div className='flex gap-2'>
|
|
82
116
|
<Input
|
|
83
117
|
type='date'
|
|
@@ -88,44 +122,78 @@ const DatePicker = React.forwardRef<HTMLDivElement, DatePickerProps>(
|
|
|
88
122
|
className='flex-1'
|
|
89
123
|
/>
|
|
90
124
|
<Button
|
|
125
|
+
ref={toggleRef}
|
|
126
|
+
type='button'
|
|
91
127
|
variant='outline'
|
|
92
128
|
onClick={() => setOpen(!open)}
|
|
93
129
|
disabled={disabled}
|
|
94
130
|
className='px-3'
|
|
131
|
+
aria-label={
|
|
132
|
+
selectedDate
|
|
133
|
+
? `Change date, currently ${format(selectedDate, 'PPP')}`
|
|
134
|
+
: 'Choose date'
|
|
135
|
+
}
|
|
136
|
+
aria-haspopup='dialog'
|
|
137
|
+
aria-expanded={open}
|
|
138
|
+
aria-controls={open ? dialogId : undefined}
|
|
95
139
|
>
|
|
96
140
|
<CalendarIcon className='h-4 w-4' />
|
|
97
141
|
</Button>
|
|
98
142
|
</div>
|
|
99
143
|
|
|
100
144
|
{open && (
|
|
101
|
-
<div
|
|
102
|
-
|
|
103
|
-
|
|
145
|
+
<div
|
|
146
|
+
id={dialogId}
|
|
147
|
+
role='dialog'
|
|
148
|
+
aria-label={`Choose date, ${monthLabel}`}
|
|
149
|
+
className='bg-popover absolute top-full z-50 mt-1 w-80 rounded-md border p-3 shadow-md'
|
|
150
|
+
>
|
|
151
|
+
<div
|
|
152
|
+
aria-hidden='true'
|
|
153
|
+
className='text-foreground mb-2 text-center text-sm font-semibold'
|
|
154
|
+
>
|
|
155
|
+
{monthLabel}
|
|
156
|
+
</div>
|
|
157
|
+
<div
|
|
158
|
+
className='mb-2 grid grid-cols-7 gap-1 text-center text-sm font-medium'
|
|
159
|
+
aria-hidden='true'
|
|
160
|
+
>
|
|
161
|
+
{WEEK_DAYS.map(day => (
|
|
104
162
|
<div key={day} className='p-1'>
|
|
105
163
|
{day}
|
|
106
164
|
</div>
|
|
107
165
|
))}
|
|
108
166
|
</div>
|
|
109
|
-
<div className='grid grid-cols-7 gap-1'>
|
|
110
|
-
{calendarDays.map((day, index) =>
|
|
111
|
-
|
|
112
|
-
key={index}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
167
|
+
<div role='grid' className='grid grid-cols-7 gap-1'>
|
|
168
|
+
{calendarDays.map((day, index) => {
|
|
169
|
+
if (!day) {
|
|
170
|
+
return <div key={`empty-${index}`} role='presentation' />;
|
|
171
|
+
}
|
|
172
|
+
const isSelected =
|
|
173
|
+
selectedDate &&
|
|
174
|
+
day.toDateString() === selectedDate.toDateString();
|
|
175
|
+
const isToday = day.toDateString() === today.toDateString();
|
|
176
|
+
return (
|
|
177
|
+
<button
|
|
178
|
+
key={day.toDateString()}
|
|
179
|
+
type='button'
|
|
180
|
+
aria-label={format(day, 'PPPP')}
|
|
181
|
+
aria-pressed={isSelected}
|
|
182
|
+
aria-current={isToday ? 'date' : undefined}
|
|
183
|
+
className={cn(
|
|
184
|
+
'hover:bg-accent hover:text-accent-foreground rounded p-2 text-sm',
|
|
185
|
+
isSelected
|
|
186
|
+
? 'bg-primary text-primary-foreground'
|
|
187
|
+
: isToday
|
|
188
|
+
? 'bg-muted'
|
|
189
|
+
: 'text-muted-foreground'
|
|
190
|
+
)}
|
|
191
|
+
onClick={() => handleDateSelect(day)}
|
|
192
|
+
>
|
|
193
|
+
{day.getDate()}
|
|
194
|
+
</button>
|
|
195
|
+
);
|
|
196
|
+
})}
|
|
129
197
|
</div>
|
|
130
198
|
</div>
|
|
131
199
|
)}
|