@inf-monkeys-tech/monkeys-design 2.0.314 → 2.0.315
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/base/index.js +97 -97
- package/dist/components/base/index.js.map +1 -1
- package/dist/components/base/index.mjs +11 -11
- package/dist/components/base/index.mjs.map +1 -1
- package/dist/index.js +197 -197
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +15 -15
- package/dist/index.mjs.map +1 -1
- package/dist/sources/{f0c1e9b5564c6baf7dc0b6da8601f0fac26048196d51dfdc63fccba1bd906604.tsx → 62c4f443e78e36113663e18e92061e57fda4846dfa34079ccfe61a6d341a25c0.tsx} +191 -33
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from 'react';
|
|
2
|
-
import { CalendarDays } from 'lucide-react';
|
|
2
|
+
import { CalendarDays, ChevronLeft, ChevronRight } from 'lucide-react';
|
|
3
3
|
import { enUS, zhCN } from 'date-fns/locale';
|
|
4
|
+
import type { CaptionProps, DateRange } from 'react-day-picker';
|
|
4
5
|
|
|
5
6
|
import { useMonkeysLocale } from '../../provider/context';
|
|
6
7
|
import { Calendar } from '../primitives/Calendar';
|
|
@@ -34,6 +35,160 @@ const calendarDate = (value: string | undefined): Date | undefined => {
|
|
|
34
35
|
return Number.isFinite(date.getTime()) && dateValue(date) === value ? date : undefined;
|
|
35
36
|
};
|
|
36
37
|
|
|
38
|
+
function HiddenCalendarCaption({ id, displayMonth }: CaptionProps) {
|
|
39
|
+
const { locale } = useMonkeysLocale();
|
|
40
|
+
return (
|
|
41
|
+
<span id={id} className="sr-only">
|
|
42
|
+
{new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'long' }).format(displayMonth)}
|
|
43
|
+
</span>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Each panel owns navigation only; browsing months and years never changes the selected range. */
|
|
48
|
+
function RangeCalendarPanel({
|
|
49
|
+
initialMonth,
|
|
50
|
+
selected,
|
|
51
|
+
minimum,
|
|
52
|
+
maximum,
|
|
53
|
+
allowed,
|
|
54
|
+
onSelect,
|
|
55
|
+
previousLabel,
|
|
56
|
+
nextLabel,
|
|
57
|
+
}: {
|
|
58
|
+
initialMonth: Date;
|
|
59
|
+
selected: DateRange;
|
|
60
|
+
minimum?: Date;
|
|
61
|
+
maximum?: Date;
|
|
62
|
+
allowed: (date: Date) => boolean;
|
|
63
|
+
onSelect: (date: Date) => void;
|
|
64
|
+
previousLabel: string;
|
|
65
|
+
nextLabel: string;
|
|
66
|
+
}) {
|
|
67
|
+
const { locale } = useMonkeysLocale();
|
|
68
|
+
const zh = locale.toLowerCase().startsWith('zh');
|
|
69
|
+
const [month, setMonth] = useState(() => new Date(initialMonth.getFullYear(), initialMonth.getMonth(), 1, 12));
|
|
70
|
+
const [view, setView] = useState<'days' | 'months' | 'years'>('days');
|
|
71
|
+
const [yearPage, setYearPage] = useState(() => Math.floor(initialMonth.getFullYear() / 12) * 12);
|
|
72
|
+
const yearButton = useRef<HTMLButtonElement>(null);
|
|
73
|
+
const year = month.getFullYear();
|
|
74
|
+
const monthLabel = new Intl.DateTimeFormat(locale, { month: 'long' }).format(month);
|
|
75
|
+
const label = new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'long' }).format(month);
|
|
76
|
+
const periodAllowed = (from: Date, to: Date) => (!minimum || to >= minimum) && (!maximum || from <= maximum);
|
|
77
|
+
const monthAllowed = (y: number, m: number) => periodAllowed(new Date(y, m, 1, 12), new Date(y, m + 1, 0, 12));
|
|
78
|
+
const yearAllowed = (y: number) => periodAllowed(new Date(y, 0, 1, 12), new Date(y, 11, 31, 12));
|
|
79
|
+
const canNavigate = (offset: number) =>
|
|
80
|
+
view === 'days'
|
|
81
|
+
? monthAllowed(year, month.getMonth() + offset)
|
|
82
|
+
: view === 'months'
|
|
83
|
+
? yearAllowed(year + offset)
|
|
84
|
+
: periodAllowed(new Date(yearPage + offset * 12, 0, 1, 12), new Date(yearPage + offset * 12 + 11, 11, 31, 12));
|
|
85
|
+
const navigate = (offset: number) => {
|
|
86
|
+
if (view === 'years') setYearPage(yearPage + offset * 12);
|
|
87
|
+
else setMonth(new Date(year + (view === 'months' ? offset : 0), month.getMonth() + (view === 'days' ? offset : 0), 1, 12));
|
|
88
|
+
};
|
|
89
|
+
const selectPeriod = (y: number, m: number) => {
|
|
90
|
+
const next = new Date(y, m, 1, 12);
|
|
91
|
+
// A bounded year may contain only some months. Navigate to the first permitted month.
|
|
92
|
+
setMonth(
|
|
93
|
+
minimum && next < new Date(minimum.getFullYear(), minimum.getMonth(), 1, 12)
|
|
94
|
+
? new Date(minimum.getFullYear(), minimum.getMonth(), 1, 12)
|
|
95
|
+
: maximum && next > maximum
|
|
96
|
+
? new Date(maximum.getFullYear(), maximum.getMonth(), 1, 12)
|
|
97
|
+
: next,
|
|
98
|
+
);
|
|
99
|
+
setView('days');
|
|
100
|
+
yearButton.current?.focus();
|
|
101
|
+
};
|
|
102
|
+
const navLabel = (offset: number) =>
|
|
103
|
+
view === 'days'
|
|
104
|
+
? offset < 0
|
|
105
|
+
? previousLabel
|
|
106
|
+
: nextLabel
|
|
107
|
+
: zh
|
|
108
|
+
? view === 'years'
|
|
109
|
+
? offset < 0
|
|
110
|
+
? '上一组年份'
|
|
111
|
+
: '下一组年份'
|
|
112
|
+
: offset < 0
|
|
113
|
+
? '上一年'
|
|
114
|
+
: '下一年'
|
|
115
|
+
: view === 'years'
|
|
116
|
+
? offset < 0
|
|
117
|
+
? 'Previous years'
|
|
118
|
+
: 'Next years'
|
|
119
|
+
: offset < 0
|
|
120
|
+
? 'Previous year'
|
|
121
|
+
: 'Next year';
|
|
122
|
+
return (
|
|
123
|
+
<section aria-label={label} style={{ width: '17.25rem', flexShrink: 0, padding: '.75rem' }}>
|
|
124
|
+
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: '2rem', marginBottom: '.5rem' }}>
|
|
125
|
+
<BaseButton iconOnly ariaLabel={navLabel(-1)} disabled={!canNavigate(-1)} onClick={() => navigate(-1)} icon={<ChevronLeft className="size-4" />} />
|
|
126
|
+
<div className="flex items-center" style={{ gap: '.25rem' }}>
|
|
127
|
+
<BaseButton
|
|
128
|
+
ref={yearButton}
|
|
129
|
+
ariaLabel={zh ? '选择年份' : 'Choose year'}
|
|
130
|
+
active={view === 'years'}
|
|
131
|
+
onClick={() => {
|
|
132
|
+
setYearPage(Math.floor(year / 12) * 12);
|
|
133
|
+
setView(view === 'years' ? 'days' : 'years');
|
|
134
|
+
}}
|
|
135
|
+
>
|
|
136
|
+
{view === 'years' ? `${yearPage} – ${yearPage + 11}` : year}
|
|
137
|
+
</BaseButton>
|
|
138
|
+
{view !== 'years' && (
|
|
139
|
+
<BaseButton ariaLabel={zh ? '选择月份' : 'Choose month'} active={view === 'months'} onClick={() => setView(view === 'months' ? 'days' : 'months')}>
|
|
140
|
+
{monthLabel}
|
|
141
|
+
</BaseButton>
|
|
142
|
+
)}
|
|
143
|
+
</div>
|
|
144
|
+
<BaseButton iconOnly ariaLabel={navLabel(1)} disabled={!canNavigate(1)} onClick={() => navigate(1)} icon={<ChevronRight className="size-4" />} />
|
|
145
|
+
</div>
|
|
146
|
+
<div style={{ position: 'relative' }}>
|
|
147
|
+
<div style={{ visibility: view === 'days' ? 'visible' : 'hidden' }} aria-hidden={view !== 'days' || undefined}>
|
|
148
|
+
<Calendar
|
|
149
|
+
mode="range"
|
|
150
|
+
month={month}
|
|
151
|
+
onMonthChange={setMonth}
|
|
152
|
+
selected={selected}
|
|
153
|
+
fixedWeeks
|
|
154
|
+
showOutsideDays
|
|
155
|
+
locale={zh ? zhCN : enUS}
|
|
156
|
+
disabled={date => !allowed(date)}
|
|
157
|
+
onSelect={(_range, day) => onSelect(day)}
|
|
158
|
+
className="p-0"
|
|
159
|
+
classNames={{ caption: 'sr-only', month: '', table: 'w-full border-collapse' }}
|
|
160
|
+
components={{ Caption: HiddenCalendarCaption }}
|
|
161
|
+
/>
|
|
162
|
+
</div>
|
|
163
|
+
{view !== 'days' && (
|
|
164
|
+
<div
|
|
165
|
+
role="group"
|
|
166
|
+
aria-label={zh ? (view === 'years' ? '年份' : '月份') : view === 'years' ? 'Years' : 'Months'}
|
|
167
|
+
style={{ position: 'absolute', inset: 0, display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gridTemplateRows: 'repeat(4, 1fr)', gap: '.5rem' }}
|
|
168
|
+
>
|
|
169
|
+
{Array.from({ length: 12 }, (_, index) => {
|
|
170
|
+
const optionYear = view === 'years' ? yearPage + index : year;
|
|
171
|
+
const optionMonth = view === 'months' ? index : month.getMonth();
|
|
172
|
+
const text = view === 'years' ? String(optionYear) : new Intl.DateTimeFormat(locale, { month: 'short' }).format(new Date(year, index, 1));
|
|
173
|
+
return (
|
|
174
|
+
<BaseButton
|
|
175
|
+
key={index}
|
|
176
|
+
disabled={view === 'years' ? !yearAllowed(optionYear) : !monthAllowed(year, index)}
|
|
177
|
+
active={view === 'years' ? year === optionYear : month.getMonth() === index}
|
|
178
|
+
className="h-full w-full"
|
|
179
|
+
onClick={() => selectPeriod(optionYear, optionMonth)}
|
|
180
|
+
>
|
|
181
|
+
{text}
|
|
182
|
+
</BaseButton>
|
|
183
|
+
);
|
|
184
|
+
})}
|
|
185
|
+
</div>
|
|
186
|
+
)}
|
|
187
|
+
</div>
|
|
188
|
+
</section>
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
37
192
|
/** JSON date values compose the shared calendar without browser-specific locale UI. */
|
|
38
193
|
export function BaseDatePicker({
|
|
39
194
|
value = '',
|
|
@@ -158,8 +313,10 @@ export function BaseDatePicker({
|
|
|
158
313
|
)}
|
|
159
314
|
<PopoverContent
|
|
160
315
|
align="start"
|
|
316
|
+
side={mode === 'range' ? 'top' : 'bottom'}
|
|
161
317
|
className="w-auto p-0"
|
|
162
|
-
|
|
318
|
+
collisionPadding={8}
|
|
319
|
+
style={{ maxWidth: 'calc(100vw - 2rem)', maxHeight: 'var(--radix-popover-content-available-height)', overflow: 'auto' }}
|
|
163
320
|
aria-label={ariaLabel}
|
|
164
321
|
onInteractOutside={event => {
|
|
165
322
|
if (rangeAnchor.current?.contains(event.target as Node)) event.preventDefault();
|
|
@@ -174,40 +331,41 @@ export function BaseDatePicker({
|
|
|
174
331
|
}
|
|
175
332
|
>
|
|
176
333
|
{mode === 'range' ? (
|
|
177
|
-
<
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}}
|
|
207
|
-
|
|
334
|
+
<div key={activeBoundary} style={{ display: 'flex' }}>
|
|
335
|
+
{[0, 1].map(index => {
|
|
336
|
+
const initial = (activeBoundary === 'end' ? draft.to ?? draft.from : draft.from ?? draft.to) ?? today;
|
|
337
|
+
return (
|
|
338
|
+
<RangeCalendarPanel
|
|
339
|
+
key={index}
|
|
340
|
+
initialMonth={new Date(initial.getFullYear(), initial.getMonth() + index, 1, 12)}
|
|
341
|
+
selected={draft}
|
|
342
|
+
minimum={activeBoundary === 'end' && draft.from && (!minimum || draft.from > minimum) ? draft.from : minimum}
|
|
343
|
+
maximum={activeBoundary === 'start' && draft.to && (!maximum || draft.to < maximum) ? draft.to : maximum}
|
|
344
|
+
allowed={rangeAllowed}
|
|
345
|
+
previousLabel={previousMonthLabel}
|
|
346
|
+
nextLabel={nextMonthLabel}
|
|
347
|
+
onSelect={day => {
|
|
348
|
+
if (!rangeAllowed(day)) return;
|
|
349
|
+
if (activeBoundary === 'start') {
|
|
350
|
+
if (draft.to) commitRange(day, draft.to);
|
|
351
|
+
else {
|
|
352
|
+
setDraft({ from: day, to: undefined });
|
|
353
|
+
setActiveBoundary('end');
|
|
354
|
+
}
|
|
355
|
+
} else if (draft.from) commitRange(draft.from, day);
|
|
356
|
+
else {
|
|
357
|
+
setDraft({ from: undefined, to: day });
|
|
358
|
+
setActiveBoundary('start');
|
|
359
|
+
}
|
|
360
|
+
}}
|
|
361
|
+
/>
|
|
362
|
+
);
|
|
363
|
+
})}
|
|
364
|
+
</div>
|
|
208
365
|
) : (
|
|
209
366
|
<Calendar
|
|
210
367
|
mode="single"
|
|
368
|
+
fixedWeeks
|
|
211
369
|
locale={locale.toLowerCase().startsWith('zh') ? zhCN : enUS}
|
|
212
370
|
selected={selected}
|
|
213
371
|
defaultMonth={selected ?? today}
|