@umituz/web-dashboard 8.4.0 → 8.6.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/package.json +4 -2
- package/src/domains/calendar/calendar.css +342 -0
- package/src/domains/calendar/components/Calendar.test.tsx +74 -0
- package/src/domains/calendar/components/Calendar.tsx +189 -0
- package/src/domains/calendar/components/CalendarMonth.test.tsx +59 -0
- package/src/domains/calendar/components/CalendarMonth.tsx +109 -0
- package/src/domains/calendar/components/CalendarTimeGrid.test.tsx +72 -0
- package/src/domains/calendar/components/CalendarTimeGrid.tsx +143 -0
- package/src/domains/calendar/components/index.ts +6 -0
- package/src/domains/calendar/index.ts +2 -1
- package/src/domains/calendar/types/index.ts +2 -0
- package/src/domains/calendar/types/view.types.ts +46 -0
- package/src/domains/calendar/utils/index.ts +1 -0
- package/src/domains/calendar/utils/layout.test.ts +144 -0
- package/src/domains/calendar/utils/layout.ts +210 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-dashboard",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.6.0",
|
|
4
4
|
"description": "Installable app skeleton for the fleet — layouts, analytics (dependency-free SVG charts), calendar, onboarding flow permissions/module registry; udb-* stylesheet on design-system tokens",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -17,9 +17,11 @@
|
|
|
17
17
|
"./analytics/types": "./src/domains/analytics/types/index.ts",
|
|
18
18
|
"./analytics/utils": "./src/domains/analytics/utils/index.ts",
|
|
19
19
|
"./calendar": "./src/domains/calendar/index.ts",
|
|
20
|
+
"./calendar/calendar.css": "./src/domains/calendar/calendar.css",
|
|
21
|
+
"./calendar/components": "./src/domains/calendar/components/index.ts",
|
|
20
22
|
"./calendar/hooks": "./src/domains/calendar/hooks/index.ts",
|
|
21
23
|
"./calendar/services": "./src/domains/calendar/services/index.ts",
|
|
22
|
-
"./calendar/types": "./src/domains/calendar/types/
|
|
24
|
+
"./calendar/types": "./src/domains/calendar/types/index.ts",
|
|
23
25
|
"./calendar/utils": "./src/domains/calendar/utils/index.ts",
|
|
24
26
|
"./charts": "./src/domains/charts/index.ts",
|
|
25
27
|
"./chrome.css": "./src/styles/chrome.css",
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
.udb-cal {
|
|
2
|
+
border: 1px solid var(--color-border, #e2e8f0);
|
|
3
|
+
border-radius: 12px;
|
|
4
|
+
background: var(--color-card, #ffffff);
|
|
5
|
+
color: var(--color-foreground, #0f172a);
|
|
6
|
+
overflow: hidden;
|
|
7
|
+
font-size: var(--udb-font-size-sm, 12px);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.udb-cal-toolbar {
|
|
11
|
+
display: flex;
|
|
12
|
+
flex-wrap: wrap;
|
|
13
|
+
align-items: center;
|
|
14
|
+
justify-content: space-between;
|
|
15
|
+
gap: var(--udb-space-3, 12px);
|
|
16
|
+
padding: var(--udb-space-3, 12px);
|
|
17
|
+
border-bottom: 1px solid var(--color-border, #e2e8f0);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.udb-cal-toolbar-nav {
|
|
21
|
+
display: flex;
|
|
22
|
+
align-items: center;
|
|
23
|
+
gap: var(--udb-space-2, 8px);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.udb-cal-title {
|
|
27
|
+
min-width: 160px;
|
|
28
|
+
margin-left: var(--udb-space-2, 8px);
|
|
29
|
+
font-size: var(--udb-font-size-md, 14px);
|
|
30
|
+
font-weight: 600;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.udb-cal-toolbar-side {
|
|
34
|
+
display: flex;
|
|
35
|
+
flex-wrap: wrap;
|
|
36
|
+
align-items: center;
|
|
37
|
+
gap: var(--udb-space-3, 12px);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.udb-cal-extra {
|
|
41
|
+
display: flex;
|
|
42
|
+
flex-wrap: wrap;
|
|
43
|
+
align-items: center;
|
|
44
|
+
gap: var(--udb-space-2, 8px);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.udb-cal-legend {
|
|
48
|
+
display: flex;
|
|
49
|
+
flex-wrap: wrap;
|
|
50
|
+
gap: var(--udb-space-3, 12px);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.udb-cal-legend-entry {
|
|
54
|
+
display: inline-flex;
|
|
55
|
+
align-items: center;
|
|
56
|
+
gap: 6px;
|
|
57
|
+
font-size: var(--udb-font-size-xs, 11px);
|
|
58
|
+
font-weight: 500;
|
|
59
|
+
color: var(--color-muted-foreground, #64748b);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.udb-cal-legend-dot {
|
|
63
|
+
width: 8px;
|
|
64
|
+
height: 8px;
|
|
65
|
+
border-radius: 3px;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.udb-cal-dot--primary,
|
|
69
|
+
.udb-cal-chip--primary,
|
|
70
|
+
.udb-cal-event--primary {
|
|
71
|
+
background: color-mix(in srgb, var(--color-primary, #2563eb) 12%, transparent);
|
|
72
|
+
color: var(--color-primary, #2563eb);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.udb-cal-dot--success,
|
|
76
|
+
.udb-cal-chip--success,
|
|
77
|
+
.udb-cal-event--success {
|
|
78
|
+
background: color-mix(in srgb, var(--color-success, #16a34a) 12%, transparent);
|
|
79
|
+
color: var(--color-success, #16a34a);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.udb-cal-dot--warning,
|
|
83
|
+
.udb-cal-chip--warning,
|
|
84
|
+
.udb-cal-event--warning {
|
|
85
|
+
background: color-mix(in srgb, var(--color-warning, #d97706) 16%, transparent);
|
|
86
|
+
color: var(--color-warning, #d97706);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.udb-cal-dot--danger,
|
|
90
|
+
.udb-cal-chip--danger,
|
|
91
|
+
.udb-cal-event--danger {
|
|
92
|
+
background: color-mix(in srgb, var(--color-destructive, #dc2626) 12%, transparent);
|
|
93
|
+
color: var(--color-destructive, #dc2626);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.udb-cal-dot--info,
|
|
97
|
+
.udb-cal-chip--info,
|
|
98
|
+
.udb-cal-event--info {
|
|
99
|
+
background: color-mix(in srgb, var(--color-info, #0891b2) 12%, transparent);
|
|
100
|
+
color: var(--color-info, #0891b2);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.udb-cal-dot--accent,
|
|
104
|
+
.udb-cal-chip--accent,
|
|
105
|
+
.udb-cal-event--accent {
|
|
106
|
+
background: color-mix(in srgb, var(--color-accent, #7c3aed) 12%, transparent);
|
|
107
|
+
color: var(--color-accent, #7c3aed);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.udb-cal-dot--neutral {
|
|
111
|
+
background: var(--color-secondary-foreground, #334155);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.udb-cal-chip--neutral,
|
|
115
|
+
.udb-cal-event--neutral {
|
|
116
|
+
background: var(--color-secondary, #f1f5f9);
|
|
117
|
+
color: var(--color-secondary-foreground, #334155);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.udb-cal-month-head {
|
|
121
|
+
display: grid;
|
|
122
|
+
grid-template-columns: repeat(7, minmax(0, 1fr));
|
|
123
|
+
border-bottom: 1px solid var(--color-border, #e2e8f0);
|
|
124
|
+
background: color-mix(in srgb, var(--color-muted, #f8fafc) 60%, transparent);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
.udb-cal-month-weekday {
|
|
128
|
+
padding: 6px 0;
|
|
129
|
+
text-align: center;
|
|
130
|
+
font-size: var(--udb-font-size-xs, 11px);
|
|
131
|
+
font-weight: 600;
|
|
132
|
+
text-transform: uppercase;
|
|
133
|
+
letter-spacing: 0.04em;
|
|
134
|
+
color: var(--color-muted-foreground, #64748b);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
.udb-cal-month-body {
|
|
138
|
+
display: grid;
|
|
139
|
+
grid-template-columns: repeat(7, minmax(0, 1fr));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.udb-cal-month-cell {
|
|
143
|
+
min-height: 96px;
|
|
144
|
+
padding: 6px;
|
|
145
|
+
border-right: 1px solid var(--color-border, #e2e8f0);
|
|
146
|
+
border-bottom: 1px solid var(--color-border, #e2e8f0);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.udb-cal-month-cell:nth-child(7n) {
|
|
150
|
+
border-right: none;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.udb-cal-month-cell.is-outside {
|
|
154
|
+
background: color-mix(in srgb, var(--color-muted, #f8fafc) 40%, transparent);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.udb-cal-month-cell.is-outside .udb-cal-month-daynum {
|
|
158
|
+
color: var(--color-muted-foreground, #94a3b8);
|
|
159
|
+
opacity: 0.6;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.udb-cal-month-daynum {
|
|
163
|
+
display: inline-grid;
|
|
164
|
+
place-items: center;
|
|
165
|
+
min-width: 22px;
|
|
166
|
+
height: 22px;
|
|
167
|
+
padding: 0;
|
|
168
|
+
border: none;
|
|
169
|
+
border-radius: 9999px;
|
|
170
|
+
background: transparent;
|
|
171
|
+
font-size: var(--udb-font-size-xs, 11px);
|
|
172
|
+
font-weight: 500;
|
|
173
|
+
color: var(--color-foreground, #0f172a);
|
|
174
|
+
cursor: pointer;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.udb-cal-month-daynum.is-today {
|
|
178
|
+
background: var(--color-primary, #2563eb);
|
|
179
|
+
color: var(--color-primary-foreground, #ffffff);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.udb-cal-month-chips {
|
|
183
|
+
display: flex;
|
|
184
|
+
flex-direction: column;
|
|
185
|
+
gap: 3px;
|
|
186
|
+
margin-top: 4px;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
.udb-cal-chip {
|
|
190
|
+
display: flex;
|
|
191
|
+
align-items: center;
|
|
192
|
+
gap: 4px;
|
|
193
|
+
width: 100%;
|
|
194
|
+
padding: 2px 5px;
|
|
195
|
+
border: none;
|
|
196
|
+
border-radius: 5px;
|
|
197
|
+
font-size: var(--udb-font-size-xs, 11px);
|
|
198
|
+
font-weight: 500;
|
|
199
|
+
text-align: left;
|
|
200
|
+
cursor: pointer;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.udb-cal-chip:focus-visible,
|
|
204
|
+
.udb-cal-event:focus-visible,
|
|
205
|
+
.udb-cal-chip-more:focus-visible,
|
|
206
|
+
.udb-cal-month-daynum:focus-visible {
|
|
207
|
+
outline: 2px solid var(--color-ring, #2563eb);
|
|
208
|
+
outline-offset: 1px;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
.udb-cal-chip.is-muted,
|
|
212
|
+
.udb-cal-event.is-muted {
|
|
213
|
+
opacity: 0.45;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.udb-cal-chip-time {
|
|
217
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
218
|
+
font-size: 10px;
|
|
219
|
+
opacity: 0.8;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
.udb-cal-chip-title {
|
|
223
|
+
overflow: hidden;
|
|
224
|
+
text-overflow: ellipsis;
|
|
225
|
+
white-space: nowrap;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.udb-cal-chip-more {
|
|
229
|
+
padding: 1px 5px;
|
|
230
|
+
border: none;
|
|
231
|
+
border-radius: 5px;
|
|
232
|
+
background: transparent;
|
|
233
|
+
font-size: 10px;
|
|
234
|
+
font-weight: 600;
|
|
235
|
+
color: var(--color-muted-foreground, #64748b);
|
|
236
|
+
text-align: left;
|
|
237
|
+
cursor: pointer;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
.udb-cal-grid {
|
|
241
|
+
display: grid;
|
|
242
|
+
grid-template-columns: 3.5rem repeat(var(--udb-cal-days, 1), minmax(0, 1fr));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.udb-cal-gutter {
|
|
246
|
+
position: relative;
|
|
247
|
+
border-right: 1px solid var(--color-border, #e2e8f0);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
.udb-cal-hour-label {
|
|
251
|
+
position: absolute;
|
|
252
|
+
right: 6px;
|
|
253
|
+
transform: translateY(-50%);
|
|
254
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
255
|
+
font-size: 10px;
|
|
256
|
+
color: var(--color-muted-foreground, #94a3b8);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.udb-cal-day + .udb-cal-day {
|
|
260
|
+
border-left: 1px solid var(--color-border, #e2e8f0);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
.udb-cal-day-head {
|
|
264
|
+
display: flex;
|
|
265
|
+
align-items: baseline;
|
|
266
|
+
justify-content: center;
|
|
267
|
+
gap: 6px;
|
|
268
|
+
padding: 8px 4px;
|
|
269
|
+
border-bottom: 1px solid var(--color-border, #e2e8f0);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
.udb-cal-day-head.is-today .udb-cal-daynum {
|
|
273
|
+
display: inline-grid;
|
|
274
|
+
place-items: center;
|
|
275
|
+
min-width: 20px;
|
|
276
|
+
height: 20px;
|
|
277
|
+
border-radius: 9999px;
|
|
278
|
+
background: var(--color-primary, #2563eb);
|
|
279
|
+
color: var(--color-primary-foreground, #ffffff);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
.udb-cal-weekday {
|
|
283
|
+
font-size: var(--udb-font-size-xs, 11px);
|
|
284
|
+
font-weight: 600;
|
|
285
|
+
text-transform: uppercase;
|
|
286
|
+
color: var(--color-muted-foreground, #64748b);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.udb-cal-daynum {
|
|
290
|
+
font-size: var(--udb-font-size-sm, 12px);
|
|
291
|
+
font-weight: 500;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
.udb-cal-lane {
|
|
295
|
+
position: relative;
|
|
296
|
+
height: 36rem;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
.udb-cal-hour-line {
|
|
300
|
+
position: absolute;
|
|
301
|
+
left: 0;
|
|
302
|
+
right: 0;
|
|
303
|
+
border-top: 1px solid var(--color-border, #eef2f7);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
.udb-cal-event {
|
|
307
|
+
position: absolute;
|
|
308
|
+
display: flex;
|
|
309
|
+
flex-direction: column;
|
|
310
|
+
gap: 1px;
|
|
311
|
+
padding: 3px 6px;
|
|
312
|
+
border: none;
|
|
313
|
+
border-left: 3px solid currentColor;
|
|
314
|
+
border-radius: 5px;
|
|
315
|
+
overflow: hidden;
|
|
316
|
+
text-align: left;
|
|
317
|
+
font-size: var(--udb-font-size-xs, 11px);
|
|
318
|
+
font-weight: 500;
|
|
319
|
+
cursor: pointer;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
.udb-cal-event-time {
|
|
323
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
324
|
+
font-size: 10px;
|
|
325
|
+
opacity: 0.8;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
.udb-cal-event-title {
|
|
329
|
+
overflow: hidden;
|
|
330
|
+
text-overflow: ellipsis;
|
|
331
|
+
white-space: nowrap;
|
|
332
|
+
font-weight: 600;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
.udb-cal-empty {
|
|
336
|
+
position: absolute;
|
|
337
|
+
inset: 0;
|
|
338
|
+
display: grid;
|
|
339
|
+
place-items: center;
|
|
340
|
+
margin: 0;
|
|
341
|
+
color: var(--color-muted-foreground, #94a3b8);
|
|
342
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import { Calendar } from './Calendar';
|
|
4
|
+
import type { CalendarItem } from '../types/view.types';
|
|
5
|
+
|
|
6
|
+
const item = (id: string, dayKey: string, startMin?: number, endMin?: number): CalendarItem => ({
|
|
7
|
+
id,
|
|
8
|
+
dayKey,
|
|
9
|
+
startMin,
|
|
10
|
+
endMin,
|
|
11
|
+
title: id,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const mount = (props: Partial<Parameters<typeof Calendar>[0]> = {}) =>
|
|
15
|
+
render(
|
|
16
|
+
<Calendar
|
|
17
|
+
items={[item('Kickoff', '2026-03-15', 600, 660)]}
|
|
18
|
+
cursor="2026-03-10"
|
|
19
|
+
{...props}
|
|
20
|
+
/>,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
describe('Calendar', () => {
|
|
24
|
+
it('renders the month grid with a localized title by default', () => {
|
|
25
|
+
const { container } = mount();
|
|
26
|
+
expect(screen.getByText('March 2026')).toBeTruthy();
|
|
27
|
+
expect(container.querySelectorAll('[role="gridcell"]')).toHaveLength(42);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('switches to a seven-lane week grid and a single day grid', () => {
|
|
31
|
+
mount();
|
|
32
|
+
fireEvent.click(screen.getByText('Week'));
|
|
33
|
+
expect(screen.getByRole('region', { name: '2026-03-15' })).toBeTruthy();
|
|
34
|
+
fireEvent.click(screen.getByText('Day'));
|
|
35
|
+
expect(screen.getByText('Tuesday, March 10, 2026')).toBeTruthy();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('drills from a month cell into the day view', () => {
|
|
39
|
+
const onSelectDay = vi.fn();
|
|
40
|
+
render(
|
|
41
|
+
<Calendar
|
|
42
|
+
items={[item('Kickoff', '2026-03-15', 600, 660)]}
|
|
43
|
+
initialCursor="2026-03-10"
|
|
44
|
+
onSelectDay={onSelectDay}
|
|
45
|
+
/>,
|
|
46
|
+
);
|
|
47
|
+
fireEvent.click(screen.getByRole('button', { name: '2026-03-15' }));
|
|
48
|
+
expect(onSelectDay).toHaveBeenCalledWith('2026-03-15');
|
|
49
|
+
expect(screen.getByText('Sunday, March 15, 2026')).toBeTruthy();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('respects a controlled view', () => {
|
|
53
|
+
mount({ view: 'day' });
|
|
54
|
+
expect(screen.getByText('Tuesday, March 10, 2026')).toBeTruthy();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('reports view changes', () => {
|
|
58
|
+
const onViewChange = vi.fn();
|
|
59
|
+
mount({ onViewChange });
|
|
60
|
+
fireEvent.click(screen.getByText('Week'));
|
|
61
|
+
expect(onViewChange).toHaveBeenCalledWith('week');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('renders the legend', () => {
|
|
65
|
+
mount({ legend: [{ tone: 'success', label: 'Confirmed' }] });
|
|
66
|
+
expect(screen.getByText('Confirmed')).toBeTruthy();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('renders toolbar extras inside the toolbar', () => {
|
|
70
|
+
mount({ toolbarExtra: <span className="calendar-count">4 appointments</span> });
|
|
71
|
+
const extra = screen.getByText('4 appointments').closest('.udb-cal-extra');
|
|
72
|
+
expect(extra).toBeTruthy();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { useMemo, useState } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
|
4
|
+
import { Button, IconButton } from '@umituz/web-design-system/atoms';
|
|
5
|
+
import { Segmented } from '@umituz/web-design-system/molecules';
|
|
6
|
+
import {
|
|
7
|
+
DEFAULT_CALENDAR_LABELS,
|
|
8
|
+
type CalendarItem,
|
|
9
|
+
type CalendarLabels,
|
|
10
|
+
type CalendarLegendEntry,
|
|
11
|
+
type CalendarScreenView,
|
|
12
|
+
} from '../types/view.types';
|
|
13
|
+
import {
|
|
14
|
+
DEFAULT_DAY_END_MIN,
|
|
15
|
+
DEFAULT_DAY_START_MIN,
|
|
16
|
+
DEFAULT_MAX_PER_DAY,
|
|
17
|
+
addDaysKey,
|
|
18
|
+
addMonthsKey,
|
|
19
|
+
formatDayTitle,
|
|
20
|
+
formatMonthTitle,
|
|
21
|
+
formatWeekTitle,
|
|
22
|
+
todayKey,
|
|
23
|
+
weekDays,
|
|
24
|
+
} from '../utils/layout';
|
|
25
|
+
import { CalendarMonth } from './CalendarMonth';
|
|
26
|
+
import { CalendarTimeGrid } from './CalendarTimeGrid';
|
|
27
|
+
|
|
28
|
+
export interface CalendarProps {
|
|
29
|
+
items: readonly CalendarItem[];
|
|
30
|
+
view?: CalendarScreenView;
|
|
31
|
+
onViewChange?: (view: CalendarScreenView) => void;
|
|
32
|
+
cursor?: string;
|
|
33
|
+
initialCursor?: string;
|
|
34
|
+
onCursorChange?: (dayKey: string) => void;
|
|
35
|
+
views?: readonly CalendarScreenView[];
|
|
36
|
+
weekStartsOn?: 0 | 1;
|
|
37
|
+
dayStartMin?: number;
|
|
38
|
+
dayEndMin?: number;
|
|
39
|
+
maxPerDay?: number;
|
|
40
|
+
locale?: string;
|
|
41
|
+
labels?: Partial<CalendarLabels>;
|
|
42
|
+
legend?: readonly CalendarLegendEntry[];
|
|
43
|
+
toolbarExtra?: ReactNode;
|
|
44
|
+
onSelectItem?: (item: CalendarItem) => void;
|
|
45
|
+
onSelectDay?: (dayKey: string) => void;
|
|
46
|
+
onSlotClick?: (dayKey: string, minutes: number) => void;
|
|
47
|
+
className?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const Calendar = ({
|
|
51
|
+
items,
|
|
52
|
+
view,
|
|
53
|
+
onViewChange,
|
|
54
|
+
cursor,
|
|
55
|
+
initialCursor,
|
|
56
|
+
onCursorChange,
|
|
57
|
+
views = ['month', 'week', 'day'],
|
|
58
|
+
weekStartsOn = 1,
|
|
59
|
+
dayStartMin = DEFAULT_DAY_START_MIN,
|
|
60
|
+
dayEndMin = DEFAULT_DAY_END_MIN,
|
|
61
|
+
maxPerDay = DEFAULT_MAX_PER_DAY,
|
|
62
|
+
locale = 'en-US',
|
|
63
|
+
labels,
|
|
64
|
+
legend,
|
|
65
|
+
toolbarExtra,
|
|
66
|
+
onSelectItem,
|
|
67
|
+
onSelectDay,
|
|
68
|
+
onSlotClick,
|
|
69
|
+
className,
|
|
70
|
+
}: CalendarProps) => {
|
|
71
|
+
const text = { ...DEFAULT_CALENDAR_LABELS, ...labels };
|
|
72
|
+
const [innerView, setInnerView] = useState<CalendarScreenView>(views[0] ?? 'month');
|
|
73
|
+
const [innerCursor, setInnerCursor] = useState(() => cursor ?? initialCursor ?? todayKey());
|
|
74
|
+
const activeView = view ?? innerView;
|
|
75
|
+
const activeCursor = cursor ?? innerCursor;
|
|
76
|
+
|
|
77
|
+
const applyView = (next: CalendarScreenView) => {
|
|
78
|
+
setInnerView(next);
|
|
79
|
+
onViewChange?.(next);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const applyCursor = (next: string) => {
|
|
83
|
+
setInnerCursor(next);
|
|
84
|
+
onCursorChange?.(next);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const shift = (direction: number) => {
|
|
88
|
+
if (activeView === 'month') applyCursor(addMonthsKey(activeCursor, direction));
|
|
89
|
+
else if (activeView === 'week') applyCursor(addDaysKey(activeCursor, direction * 7));
|
|
90
|
+
else applyCursor(addDaysKey(activeCursor, direction));
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const title = useMemo(() => {
|
|
94
|
+
if (activeView === 'day') return formatDayTitle(activeCursor, locale);
|
|
95
|
+
if (activeView === 'week') return formatWeekTitle(activeCursor, locale);
|
|
96
|
+
return formatMonthTitle(activeCursor, locale);
|
|
97
|
+
}, [activeView, activeCursor, locale]);
|
|
98
|
+
|
|
99
|
+
const handleSelectDay = (dayKey: string) => {
|
|
100
|
+
applyCursor(dayKey);
|
|
101
|
+
if (activeView !== 'day') applyView('day');
|
|
102
|
+
onSelectDay?.(dayKey);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<div className={className ? `udb-cal ${className}` : 'udb-cal'}>
|
|
107
|
+
<div className="udb-cal-toolbar">
|
|
108
|
+
<div className="udb-cal-toolbar-nav">
|
|
109
|
+
<IconButton
|
|
110
|
+
icon={<ChevronLeft size={16} />}
|
|
111
|
+
label={text.previous}
|
|
112
|
+
size="sm"
|
|
113
|
+
variant="outline"
|
|
114
|
+
onClick={() => shift(-1)}
|
|
115
|
+
/>
|
|
116
|
+
<IconButton
|
|
117
|
+
icon={<ChevronRight size={16} />}
|
|
118
|
+
label={text.next}
|
|
119
|
+
size="sm"
|
|
120
|
+
variant="outline"
|
|
121
|
+
onClick={() => shift(1)}
|
|
122
|
+
/>
|
|
123
|
+
<span className="udb-cal-title">{title}</span>
|
|
124
|
+
<Button variant="ghost" size="sm" onClick={() => applyCursor(todayKey())}>
|
|
125
|
+
{text.today}
|
|
126
|
+
</Button>
|
|
127
|
+
</div>
|
|
128
|
+
<div className="udb-cal-toolbar-side">
|
|
129
|
+
{toolbarExtra && <div className="udb-cal-extra">{toolbarExtra}</div>}
|
|
130
|
+
{legend && legend.length > 0 && (
|
|
131
|
+
<div className="udb-cal-legend">
|
|
132
|
+
{legend.map((entry) => (
|
|
133
|
+
<span className="udb-cal-legend-entry" key={entry.tone + entry.label}>
|
|
134
|
+
<span className={`udb-cal-legend-dot udb-cal-dot--${entry.tone}`} />
|
|
135
|
+
{entry.label}
|
|
136
|
+
</span>
|
|
137
|
+
))}
|
|
138
|
+
</div>
|
|
139
|
+
)}
|
|
140
|
+
<Segmented
|
|
141
|
+
options={views.map((option) => ({ value: option, label: text[option] }))}
|
|
142
|
+
value={activeView}
|
|
143
|
+
onChange={(next) => applyView(next)}
|
|
144
|
+
aria-label={text.month}
|
|
145
|
+
/>
|
|
146
|
+
</div>
|
|
147
|
+
</div>
|
|
148
|
+
|
|
149
|
+
{activeView === 'month' && (
|
|
150
|
+
<CalendarMonth
|
|
151
|
+
items={items}
|
|
152
|
+
anchorKey={activeCursor}
|
|
153
|
+
weekStartsOn={weekStartsOn}
|
|
154
|
+
locale={locale}
|
|
155
|
+
maxPerDay={maxPerDay}
|
|
156
|
+
labels={labels}
|
|
157
|
+
onSelectDay={handleSelectDay}
|
|
158
|
+
onSelectItem={onSelectItem}
|
|
159
|
+
/>
|
|
160
|
+
)}
|
|
161
|
+
|
|
162
|
+
{activeView === 'week' && (
|
|
163
|
+
<CalendarTimeGrid
|
|
164
|
+
items={items}
|
|
165
|
+
days={weekDays(activeCursor, weekStartsOn)}
|
|
166
|
+
dayStartMin={dayStartMin}
|
|
167
|
+
dayEndMin={dayEndMin}
|
|
168
|
+
locale={locale}
|
|
169
|
+
labels={labels}
|
|
170
|
+
onSelectItem={onSelectItem}
|
|
171
|
+
onSlotClick={onSlotClick}
|
|
172
|
+
/>
|
|
173
|
+
)}
|
|
174
|
+
|
|
175
|
+
{activeView === 'day' && (
|
|
176
|
+
<CalendarTimeGrid
|
|
177
|
+
items={items}
|
|
178
|
+
days={[activeCursor]}
|
|
179
|
+
dayStartMin={dayStartMin}
|
|
180
|
+
dayEndMin={dayEndMin}
|
|
181
|
+
locale={locale}
|
|
182
|
+
labels={labels}
|
|
183
|
+
onSelectItem={onSelectItem}
|
|
184
|
+
onSlotClick={onSlotClick}
|
|
185
|
+
/>
|
|
186
|
+
)}
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import { CalendarMonth } from './CalendarMonth';
|
|
4
|
+
import type { CalendarItem } from '../types/view.types';
|
|
5
|
+
|
|
6
|
+
const item = (id: string, dayKey: string, startMin?: number): CalendarItem => ({
|
|
7
|
+
id,
|
|
8
|
+
dayKey,
|
|
9
|
+
startMin,
|
|
10
|
+
title: id,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const mount = (props: Partial<Parameters<typeof CalendarMonth>[0]> = {}) =>
|
|
14
|
+
render(<CalendarMonth items={[]} anchorKey="2026-03-10" {...props} />);
|
|
15
|
+
|
|
16
|
+
describe('CalendarMonth', () => {
|
|
17
|
+
it('renders a six-by-seven matrix', () => {
|
|
18
|
+
const { container } = mount();
|
|
19
|
+
expect(container.querySelectorAll('[role="gridcell"]')).toHaveLength(42);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('places items on their day and reports selections', () => {
|
|
23
|
+
const onSelectItem = vi.fn();
|
|
24
|
+
mount({ items: [item('Kickoff', '2026-03-15', 600)], onSelectItem });
|
|
25
|
+
fireEvent.click(screen.getByText('Kickoff'));
|
|
26
|
+
expect(onSelectItem).toHaveBeenCalledTimes(1);
|
|
27
|
+
expect(onSelectItem.mock.calls[0]![0].id).toBe('Kickoff');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('caps chips per cell and overflows through the more label', () => {
|
|
31
|
+
const onSelectDay = vi.fn();
|
|
32
|
+
const items = [
|
|
33
|
+
item('one', '2026-03-15', 540),
|
|
34
|
+
item('two', '2026-03-15', 600),
|
|
35
|
+
item('three', '2026-03-15', 660),
|
|
36
|
+
item('four', '2026-03-15', 720),
|
|
37
|
+
];
|
|
38
|
+
mount({ items, maxPerDay: 2, onSelectDay });
|
|
39
|
+
expect(screen.getByText('one')).toBeTruthy();
|
|
40
|
+
expect(screen.getByText('two')).toBeTruthy();
|
|
41
|
+
expect(screen.queryByText('three')).toBeNull();
|
|
42
|
+
expect(screen.getByText('+2 more')).toBeTruthy();
|
|
43
|
+
fireEvent.click(screen.getByText('+2 more'));
|
|
44
|
+
expect(onSelectDay).toHaveBeenCalledWith('2026-03-15');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('supports label overrides for the overflow chip', () => {
|
|
48
|
+
const items = [item('one', '2026-03-15'), item('two', '2026-03-15'), item('three', '2026-03-15')];
|
|
49
|
+
mount({ items, maxPerDay: 1, labels: { more: '+{count} tane' } });
|
|
50
|
+
expect(screen.getByText('+2 tane')).toBeTruthy();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('fires day selection from the day number button', () => {
|
|
54
|
+
const onSelectDay = vi.fn();
|
|
55
|
+
mount({ onSelectDay });
|
|
56
|
+
fireEvent.click(screen.getByRole('button', { name: '2026-03-15' }));
|
|
57
|
+
expect(onSelectDay).toHaveBeenCalledWith('2026-03-15');
|
|
58
|
+
});
|
|
59
|
+
});
|