@fluxwp/ui 0.0.0-stage → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/package.json +44 -4
- package/src/Avatar.tsx +12 -0
- package/src/Check.tsx +66 -0
- package/src/DatePicker.tsx +620 -0
- package/src/Empty.tsx +16 -0
- package/src/Icon.tsx +76 -0
- package/src/MenuIcon.tsx +36 -0
- package/src/Modal.tsx +19 -0
- package/src/index.ts +10 -0
- package/src/menu-icons/flx-admin.svg +13 -0
- package/src/menu-icons/flx-ai.svg +15 -0
- package/src/menu-icons/flx-analytics.svg +14 -0
- package/src/menu-icons/flx-app.svg +17 -0
- package/src/menu-icons/flx-backup.svg +13 -0
- package/src/menu-icons/flx-certificates.svg +13 -0
- package/src/menu-icons/flx-chat.svg +13 -0
- package/src/menu-icons/flx-donate.svg +13 -0
- package/src/menu-icons/flx-forms.svg +13 -0
- package/src/menu-icons/flx-lang.svg +13 -0
- package/src/menu-icons/flx-mail.svg +13 -0
- package/src/menu-icons/flx-qr-menu.svg +21 -0
- package/src/menu-icons/flx-roles.svg +17 -0
- package/src/menu-icons/flx-seo.svg +13 -0
- package/src/menu-icons/flx-setup.svg +2 -0
- package/src/menu-icons/flx-social.svg +19 -0
- package/src/menu-icons/flx-speed.svg +12 -0
- package/src/menu-icons/flx-studio.svg +16 -0
- package/src/menu-icons/flx-tasks.svg +13 -0
- package/src/menu-icons/flx-tv-board.svg +14 -0
- package/src/menu-icons/flx-tv-remote.svg +18 -0
- package/src/overlay.ts +15 -0
- package/src/sanitize.ts +91 -0
- package/src/styles.css +396 -0
- package/types/Avatar.d.ts +6 -0
- package/types/Check.d.ts +30 -0
- package/types/DatePicker.d.ts +65 -0
- package/types/Empty.d.ts +7 -0
- package/types/Icon.d.ts +5 -0
- package/types/MenuIcon.d.ts +6 -0
- package/types/Modal.d.ts +7 -0
- package/types/index.d.ts +9 -0
- package/types/overlay.d.ts +6 -0
- package/types/sanitize.d.ts +6 -0
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type KeyboardEvent as RKeyboardEvent } from 'react';
|
|
2
|
+
import { useOverlay } from './overlay';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Ortak tarih seçici. Görünüm ve sınıflar flx-brand `date/` ile aynı (`.flx-dp`, `.flx-dt`); CSS'i styles.css içe aktarır.
|
|
6
|
+
* Değer biçimi `YYYY-MM-DD`, `time` ile `YYYY-MM-DDTHH:MM`; boş = `''`.
|
|
7
|
+
* Dil: `locale` (yoksa `<html lang>`), hafta başı TR'de Pazartesi; metinler TR / EN gömülü.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type DateQuick = { label: string; value: string; hint?: string };
|
|
11
|
+
|
|
12
|
+
type Common = {
|
|
13
|
+
/** Saat satırı (24 saat). */
|
|
14
|
+
time?: boolean;
|
|
15
|
+
/** Soldaki hızlı seçim (varsayılan açık: Bugün, Yarın, Bu cuma, Gelecek hafta, 2 hafta sonra); false ya da kendi listen. */
|
|
16
|
+
quick?: boolean | DateQuick[];
|
|
17
|
+
min?: string;
|
|
18
|
+
max?: string;
|
|
19
|
+
/** Temizle düğmesi yok. */
|
|
20
|
+
required?: boolean;
|
|
21
|
+
/** "tr-TR", "en"… */
|
|
22
|
+
locale?: string;
|
|
23
|
+
/** 0 = Pazar, 1 = Pazartesi. Varsayılan dilden. */
|
|
24
|
+
weekStart?: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const STR = {
|
|
28
|
+
tr: { pick: 'Tarih seçin', today: 'Bugün', tomorrow: 'Yarın', nextWeek: 'Gelecek hafta', in2Weeks: '2 hafta sonra', thisFriday: 'Bu cuma', noDate: 'Tarih yok', clear: 'Temizle', cancel: 'Vazgeç', apply: 'Uygula', month: 'Ay', time: 'Saat', prev: 'Önceki ay', next: 'Sonraki ay', prevYear: 'Önceki yıl', nextYear: 'Sonraki yıl', pickMonth: 'Ay seçin', pickYear: 'Yıl seçin' },
|
|
29
|
+
en: { pick: 'Select date', today: 'Today', tomorrow: 'Tomorrow', nextWeek: 'Next week', in2Weeks: 'In 2 weeks', thisFriday: 'This Friday', noDate: 'No date', clear: 'Clear', cancel: 'Cancel', apply: 'Apply', month: 'Month', time: 'Time', prev: 'Previous month', next: 'Next month', prevYear: 'Previous year', nextYear: 'Next year', pickMonth: 'Choose month', pickYear: 'Choose year' },
|
|
30
|
+
};
|
|
31
|
+
type Strings = (typeof STR)['en'];
|
|
32
|
+
|
|
33
|
+
function docLocale(): string {
|
|
34
|
+
return (typeof document !== 'undefined' && document.documentElement.lang) || (typeof navigator !== 'undefined' ? navigator.language : 'en') || 'en';
|
|
35
|
+
}
|
|
36
|
+
function strings(locale: string): Strings {
|
|
37
|
+
return /^tr\b/i.test(locale) ? STR.tr : STR.en;
|
|
38
|
+
}
|
|
39
|
+
function fmt(locale: string, o: Intl.DateTimeFormatOptions) {
|
|
40
|
+
try {
|
|
41
|
+
return new Intl.DateTimeFormat(locale, o);
|
|
42
|
+
} catch {
|
|
43
|
+
return new Intl.DateTimeFormat('en', o);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const pad = (n: number) => (n < 10 ? '0' : '') + n;
|
|
47
|
+
const ymd = (x: Date) => `${x.getFullYear()}-${pad(x.getMonth() + 1)}-${pad(x.getDate())}`;
|
|
48
|
+
export function parseDate(s?: string | null): Date | null {
|
|
49
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(s ?? '');
|
|
50
|
+
if (!m) return null;
|
|
51
|
+
const x = new Date(+m[1]!, +m[2]! - 1, +m[3]!, 12);
|
|
52
|
+
return isNaN(x.getTime()) || x.getDate() !== +m[3]! ? null : x;
|
|
53
|
+
}
|
|
54
|
+
const timeOf = (s?: string | null) => /T(\d{2}:\d{2})/.exec(s ?? '')?.[1] ?? '';
|
|
55
|
+
const today = () => {
|
|
56
|
+
const n = new Date();
|
|
57
|
+
return new Date(n.getFullYear(), n.getMonth(), n.getDate(), 12);
|
|
58
|
+
};
|
|
59
|
+
const addDays = (x: Date, n: number) => new Date(x.getFullYear(), x.getMonth(), x.getDate() + n, 12);
|
|
60
|
+
const addMonths = (x: Date, n: number) => {
|
|
61
|
+
const y = x.getFullYear(),
|
|
62
|
+
m = x.getMonth() + n,
|
|
63
|
+
last = new Date(y, m + 1, 0).getDate();
|
|
64
|
+
return new Date(y, m, Math.min(x.getDate(), last), 12);
|
|
65
|
+
};
|
|
66
|
+
const cap = (s: string, l: string) => (s ? s.charAt(0).toLocaleUpperCase(l) + s.slice(1) : s);
|
|
67
|
+
function cleanTime(v: string): string {
|
|
68
|
+
const m = /^(\d{1,2})(?::?(\d{2}))?$/.exec(v.trim());
|
|
69
|
+
if (!m) return '';
|
|
70
|
+
const h = +m[1]!,
|
|
71
|
+
mi = m[2] ? +m[2] : 0;
|
|
72
|
+
return h > 23 || mi > 59 ? '' : `${pad(h)}:${pad(mi)}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** "2026-11-20T14:30" → "20 Kasım 2026 · 14:30" (style short: "20 Kas"). */
|
|
76
|
+
export function formatDate(v: string, locale = docLocale(), style: 'long' | 'short' = 'long'): string {
|
|
77
|
+
const x = parseDate(v);
|
|
78
|
+
if (!x) return '';
|
|
79
|
+
const tm = timeOf(v);
|
|
80
|
+
return fmt(locale, style === 'short' ? { day: 'numeric', month: 'short' } : { day: 'numeric', month: 'long', year: 'numeric' }).format(x) + (tm ? ' · ' + tm : '');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function defaultQuick(s: Strings, locale: string): DateQuick[] {
|
|
84
|
+
const td = today();
|
|
85
|
+
const add = (8 - td.getDay()) % 7 || 7;
|
|
86
|
+
const fri = (5 - td.getDay() + 7) % 7;
|
|
87
|
+
const wd = fmt(locale, { weekday: 'short' });
|
|
88
|
+
const sh = fmt(locale, { day: 'numeric', month: 'short' });
|
|
89
|
+
const w = (x: Date) => cap(wd.format(x), locale).replace('.', '');
|
|
90
|
+
const list: DateQuick[] = [
|
|
91
|
+
{ label: s.today, value: ymd(td), hint: w(td) },
|
|
92
|
+
{ label: s.tomorrow, value: ymd(addDays(td, 1)), hint: w(addDays(td, 1)) },
|
|
93
|
+
];
|
|
94
|
+
if (fri > 1) list.push({ label: s.thisFriday, value: ymd(addDays(td, fri)), hint: sh.format(addDays(td, fri)) });
|
|
95
|
+
list.push({ label: s.nextWeek, value: ymd(addDays(td, add)), hint: w(addDays(td, add)) });
|
|
96
|
+
list.push({ label: s.in2Weeks, value: ymd(addDays(td, 14)), hint: sh.format(addDays(td, 14)) });
|
|
97
|
+
return list;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Yazılan tarih: sıra ve ayraç dilden (TR "20.11.2026", ABD "11/20/2026"). */
|
|
101
|
+
function pattern(locale: string) {
|
|
102
|
+
const order: ('day' | 'month' | 'year')[] = [];
|
|
103
|
+
let sep = '.';
|
|
104
|
+
try {
|
|
105
|
+
for (const p of fmt(locale, { day: '2-digit', month: '2-digit', year: 'numeric' }).formatToParts(new Date(2026, 10, 20))) {
|
|
106
|
+
if (p.type === 'day' || p.type === 'month' || p.type === 'year') order.push(p.type);
|
|
107
|
+
else if (p.type === 'literal' && p.value.trim()) sep = p.value.trim().charAt(0);
|
|
108
|
+
}
|
|
109
|
+
} catch {
|
|
110
|
+
/* varsayılan */
|
|
111
|
+
}
|
|
112
|
+
const o = order.length === 3 ? order : (['day', 'month', 'year'] as const).slice();
|
|
113
|
+
const tr = /^tr\b/i.test(locale);
|
|
114
|
+
const ph = o.map((k) => (k === 'year' ? 'YYYY' : k === 'day' ? (tr ? 'GG' : 'DD') : tr ? 'AA' : 'MM')).join(sep);
|
|
115
|
+
const toText = (v: string) => {
|
|
116
|
+
const x = parseDate(v);
|
|
117
|
+
if (!x) return '';
|
|
118
|
+
const m = { day: pad(x.getDate()), month: pad(x.getMonth() + 1), year: String(x.getFullYear()) };
|
|
119
|
+
return o.map((k) => m[k]).join(sep);
|
|
120
|
+
};
|
|
121
|
+
const fromText = (str: string) => {
|
|
122
|
+
const p = str.trim().split(/[^\d]+/).filter(Boolean);
|
|
123
|
+
if (p.length !== 3) return '';
|
|
124
|
+
const m = { day: 0, month: 0, year: 0 };
|
|
125
|
+
o.forEach((k, i) => (m[k] = +p[i]!));
|
|
126
|
+
if (m.year < 100) m.year += 2000;
|
|
127
|
+
const v = `${m.year}-${pad(m.month)}-${pad(m.day)}`;
|
|
128
|
+
return parseDate(v) ? v : '';
|
|
129
|
+
};
|
|
130
|
+
return { ph, toText, fromText };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function themeOf(el: Element | null): 'dark' | 'light' {
|
|
134
|
+
const a = el?.closest('[data-theme]');
|
|
135
|
+
const v = a?.getAttribute('data-theme');
|
|
136
|
+
if (v === 'dark' || v === 'light') return v;
|
|
137
|
+
const h = document.documentElement.classList;
|
|
138
|
+
return h.contains('flxa-dark') || h.contains('flxfw-dark') || h.contains('flxg-dark') || h.contains('dark') ? 'dark' : 'light';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const Ico = ({ d, s = 18 }: { d: string; s?: number }) => (
|
|
142
|
+
<svg viewBox="0 0 24 24" width={s} height={s} fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" dangerouslySetInnerHTML={{ __html: d }} />
|
|
143
|
+
);
|
|
144
|
+
const I_L = '<path d="m15 6-6 6 6 6"/>';
|
|
145
|
+
const I_R = '<path d="m9 6 6 6-6 6"/>';
|
|
146
|
+
const I_DOWN = '<path d="m6 9 6 6 6-6"/>';
|
|
147
|
+
const I_CLK = '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/>';
|
|
148
|
+
const I_CAL = '<rect x="3" y="4.5" width="18" height="16.5" rx="3"/><path d="M3 9.5h18M8 2.5v4M16 2.5v4"/>';
|
|
149
|
+
|
|
150
|
+
type CalendarProps = Common & {
|
|
151
|
+
value: string;
|
|
152
|
+
onPick: (v: string) => void;
|
|
153
|
+
onCancel?: () => void;
|
|
154
|
+
/** Başka bir açılır kutunun içinde: çerçeve ve gölge yok. */
|
|
155
|
+
bare?: boolean;
|
|
156
|
+
/** Telefon görünümü (alttan açılan sayfa). */
|
|
157
|
+
sheet?: boolean;
|
|
158
|
+
style?: React.CSSProperties;
|
|
159
|
+
theme?: 'dark' | 'light';
|
|
160
|
+
label?: string;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Takvim paneli (konumlama yok). Görünüm B (Ahmet 2026-11-17): solda hızlı seçim, sağda takvim (ay / yıl menüsü),
|
|
165
|
+
* altta yazılabilir tarih (+ saat), Vazgeç ve Uygula. Güne tıklamak seçer, Uygula (Enter, çift tık) onaylar; hızlı seçim hemen onaylar.
|
|
166
|
+
* Tasks gibi kendi açılır kutusu olan ekranlar doğrudan kullanır (`bare`).
|
|
167
|
+
*/
|
|
168
|
+
export function DateCalendar({ value, onPick, onCancel, time, quick = true, min, max, required, locale = docLocale(), weekStart, bare, sheet, style, theme, label }: CalendarProps) {
|
|
169
|
+
const s = strings(locale);
|
|
170
|
+
const ws = weekStart ?? (/^tr\b/i.test(locale) ? 1 : 0);
|
|
171
|
+
const F = useMemo(
|
|
172
|
+
() => ({
|
|
173
|
+
month: fmt(locale, { month: 'long' }),
|
|
174
|
+
wd: fmt(locale, { weekday: 'short' }),
|
|
175
|
+
full: fmt(locale, { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }),
|
|
176
|
+
}),
|
|
177
|
+
[locale],
|
|
178
|
+
);
|
|
179
|
+
const P = useMemo(() => pattern(locale), [locale]);
|
|
180
|
+
const minD = min ? min.slice(0, 10) : '';
|
|
181
|
+
const maxD = max ? max.slice(0, 10) : '';
|
|
182
|
+
const allowed = (d: string) => !((minD && d < minD) || (maxD && d > maxD));
|
|
183
|
+
const init = (() => {
|
|
184
|
+
const v = parseDate(value);
|
|
185
|
+
if (v) return v;
|
|
186
|
+
let b = today();
|
|
187
|
+
const mn = parseDate(minD), mx = parseDate(maxD);
|
|
188
|
+
if (mn && b < mn) b = mn;
|
|
189
|
+
if (mx && b > mx) b = mx;
|
|
190
|
+
return b;
|
|
191
|
+
})();
|
|
192
|
+
const [day, setDay] = useState(parseDate(value) ? value.slice(0, 10) : '');
|
|
193
|
+
const [typed, setTyped] = useState(P.toText(value));
|
|
194
|
+
const [tm, setTm] = useState(timeOf(value));
|
|
195
|
+
const [view, setView] = useState(new Date(init.getFullYear(), init.getMonth(), 1, 12));
|
|
196
|
+
const [focus, setFocus] = useState(init);
|
|
197
|
+
const [mode, setMode] = useState<'days' | 'months' | 'years'>('days');
|
|
198
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
199
|
+
const wantFocus = useRef(true);
|
|
200
|
+
const [th, setTh] = useState<'dark' | 'light'>(theme ?? 'light');
|
|
201
|
+
useLayoutEffect(() => {
|
|
202
|
+
if (!theme) setTh(themeOf(ref.current?.parentElement ?? null));
|
|
203
|
+
}, [theme]);
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
if (!wantFocus.current) return;
|
|
206
|
+
wantFocus.current = false;
|
|
207
|
+
ref.current?.querySelector<HTMLElement>('.flx-dp-day[tabindex="0"], .flx-dp-cell[tabindex="0"]')?.focus({ preventScroll: true });
|
|
208
|
+
});
|
|
209
|
+
const q = !quick ? [] : Array.isArray(quick) ? quick : defaultQuick(s, locale);
|
|
210
|
+
const commit = (d: string) => {
|
|
211
|
+
if (d && !allowed(d)) return;
|
|
212
|
+
const t = time ? cleanTime(tm) : '';
|
|
213
|
+
const out = d ? d + (t ? 'T' + t : '') : '';
|
|
214
|
+
if (out === value) onCancel?.();
|
|
215
|
+
else onPick(out);
|
|
216
|
+
};
|
|
217
|
+
const apply = () => commit(P.fromText(typed) || day || (time && cleanTime(tm) ? ymd(today()) : ''));
|
|
218
|
+
const go = (fn: () => void) => {
|
|
219
|
+
wantFocus.current = true;
|
|
220
|
+
fn();
|
|
221
|
+
};
|
|
222
|
+
const select = (d: string) => {
|
|
223
|
+
const x = parseDate(d)!;
|
|
224
|
+
setDay(d);
|
|
225
|
+
setTyped(P.toText(d));
|
|
226
|
+
setFocus(x);
|
|
227
|
+
if (x.getMonth() !== view.getMonth() || x.getFullYear() !== view.getFullYear()) setView(new Date(x.getFullYear(), x.getMonth(), 1, 12));
|
|
228
|
+
};
|
|
229
|
+
const clampFocus = (v: Date) => new Date(v.getFullYear(), v.getMonth(), Math.min(focus.getDate(), new Date(v.getFullYear(), v.getMonth() + 1, 0).getDate()), 12);
|
|
230
|
+
|
|
231
|
+
const onKey = (e: RKeyboardEvent<HTMLDivElement>) => {
|
|
232
|
+
const tg = e.target as HTMLElement;
|
|
233
|
+
if (e.key === 'Escape') {
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
e.stopPropagation();
|
|
236
|
+
if (mode !== 'days') go(() => setMode('days'));
|
|
237
|
+
else onCancel?.();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (e.key === 'Enter' && (tg.tagName === 'INPUT' || tg.classList.contains('flx-dp-day'))) {
|
|
241
|
+
e.preventDefault();
|
|
242
|
+
if (tg.classList.contains('flx-dp-day')) commit(tg.dataset.day ?? '');
|
|
243
|
+
else apply();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (tg.classList.contains('flx-dp-cell')) {
|
|
247
|
+
const cells = Array.from(ref.current!.querySelectorAll<HTMLElement>('.flx-dp-cell'));
|
|
248
|
+
const ix = cells.indexOf(tg);
|
|
249
|
+
const mv = ({ ArrowLeft: -1, ArrowRight: 1, ArrowUp: -3, ArrowDown: 3 } as Record<string, number>)[e.key];
|
|
250
|
+
if (mv && cells[ix + mv]) {
|
|
251
|
+
e.preventDefault();
|
|
252
|
+
tg.tabIndex = -1;
|
|
253
|
+
cells[ix + mv]!.tabIndex = 0;
|
|
254
|
+
cells[ix + mv]!.focus();
|
|
255
|
+
}
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (!tg.classList.contains('flx-dp-day')) return;
|
|
259
|
+
const step = ({ ArrowLeft: -1, ArrowRight: 1, ArrowUp: -7, ArrowDown: 7 } as Record<string, number>)[e.key];
|
|
260
|
+
let nd: Date | null = null;
|
|
261
|
+
if (step) nd = addDays(focus, step);
|
|
262
|
+
else if (e.key === 'PageUp') nd = addMonths(focus, e.shiftKey ? -12 : -1);
|
|
263
|
+
else if (e.key === 'PageDown') nd = addMonths(focus, e.shiftKey ? 12 : 1);
|
|
264
|
+
else if (e.key === 'Home') nd = addDays(focus, -((focus.getDay() - ws + 7) % 7));
|
|
265
|
+
else if (e.key === 'End') nd = addDays(focus, 6 - ((focus.getDay() - ws + 7) % 7));
|
|
266
|
+
if (!nd) return;
|
|
267
|
+
e.preventDefault();
|
|
268
|
+
const n = nd;
|
|
269
|
+
go(() => {
|
|
270
|
+
setFocus(n);
|
|
271
|
+
if (n.getMonth() !== view.getMonth() || n.getFullYear() !== view.getFullYear()) setView(new Date(n.getFullYear(), n.getMonth(), 1, 12));
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const td = ymd(today());
|
|
276
|
+
const fd = ymd(focus);
|
|
277
|
+
let body: React.ReactNode;
|
|
278
|
+
if (mode === 'days') {
|
|
279
|
+
const first = new Date(view.getFullYear(), view.getMonth(), 1, 12);
|
|
280
|
+
const start = addDays(first, -((first.getDay() - ws + 7) % 7));
|
|
281
|
+
const mn = parseDate(minD), mx = parseDate(maxD);
|
|
282
|
+
body = (
|
|
283
|
+
<>
|
|
284
|
+
<div className="flx-dp-head">
|
|
285
|
+
<span className="flx-dp-sels">
|
|
286
|
+
<button type="button" className="flx-dp-sel" aria-label={s.pickMonth} onClick={() => go(() => setMode('months'))}>
|
|
287
|
+
{cap(F.month.format(view), locale)}
|
|
288
|
+
<Ico d={I_DOWN} s={14} />
|
|
289
|
+
</button>
|
|
290
|
+
<button type="button" className="flx-dp-sel" aria-label={s.pickYear} onClick={() => go(() => setMode('years'))}>
|
|
291
|
+
{view.getFullYear()}
|
|
292
|
+
<Ico d={I_DOWN} s={14} />
|
|
293
|
+
</button>
|
|
294
|
+
</span>
|
|
295
|
+
<button type="button" className="flx-dp-nav" aria-label={s.prev} disabled={!!(mn && new Date(view.getFullYear(), view.getMonth(), 0, 12) < mn)} onClick={() => { const v = addMonths(view, -1); setView(v); setFocus(clampFocus(v)); }}>
|
|
296
|
+
<Ico d={I_L} />
|
|
297
|
+
</button>
|
|
298
|
+
<button type="button" className="flx-dp-nav" aria-label={s.next} disabled={!!(mx && new Date(view.getFullYear(), view.getMonth() + 1, 1, 12) > mx)} onClick={() => { const v = addMonths(view, 1); setView(v); setFocus(clampFocus(v)); }}>
|
|
299
|
+
<Ico d={I_R} />
|
|
300
|
+
</button>
|
|
301
|
+
</div>
|
|
302
|
+
<div className="flx-dp-week" aria-hidden="true">
|
|
303
|
+
{Array.from({ length: 7 }, (_, i) => (
|
|
304
|
+
<span key={i}>{cap(F.wd.format(addDays(new Date(2024, 0, 1, 12), ((ws + 6) % 7) + i)), locale).replace('.', '')}</span>
|
|
305
|
+
))}
|
|
306
|
+
</div>
|
|
307
|
+
<div className="flx-dp-grid" role="grid">
|
|
308
|
+
{Array.from({ length: 6 }, (_, r) => (
|
|
309
|
+
<div key={r} className="flx-dp-row" role="row">
|
|
310
|
+
{Array.from({ length: 7 }, (_, c) => {
|
|
311
|
+
const x = addDays(start, r * 7 + c);
|
|
312
|
+
const d = ymd(x);
|
|
313
|
+
const cls = ['flx-dp-day', x.getMonth() !== view.getMonth() ? 'is-out' : '', d === td ? 'is-today' : '', d === day ? 'is-sel' : ''].filter(Boolean).join(' ');
|
|
314
|
+
return (
|
|
315
|
+
<button
|
|
316
|
+
key={d}
|
|
317
|
+
type="button"
|
|
318
|
+
role="gridcell"
|
|
319
|
+
className={cls}
|
|
320
|
+
data-day={d}
|
|
321
|
+
tabIndex={d === fd ? 0 : -1}
|
|
322
|
+
aria-label={F.full.format(x)}
|
|
323
|
+
aria-selected={d === day || undefined}
|
|
324
|
+
disabled={!allowed(d)}
|
|
325
|
+
onClick={(e) => (e.detail > 1 && d === day ? commit(d) : go(() => select(d)))}
|
|
326
|
+
>
|
|
327
|
+
{x.getDate()}
|
|
328
|
+
</button>
|
|
329
|
+
);
|
|
330
|
+
})}
|
|
331
|
+
</div>
|
|
332
|
+
))}
|
|
333
|
+
</div>
|
|
334
|
+
</>
|
|
335
|
+
);
|
|
336
|
+
} else if (mode === 'months') {
|
|
337
|
+
body = (
|
|
338
|
+
<>
|
|
339
|
+
<div className="flx-dp-head">
|
|
340
|
+
<span className="flx-dp-sels">
|
|
341
|
+
<button type="button" className="flx-dp-sel is-on" onClick={() => go(() => setMode('days'))}>
|
|
342
|
+
{s.month}
|
|
343
|
+
</button>
|
|
344
|
+
<button type="button" className="flx-dp-sel" aria-label={s.pickYear} onClick={() => go(() => setMode('years'))}>
|
|
345
|
+
{view.getFullYear()}
|
|
346
|
+
<Ico d={I_DOWN} s={14} />
|
|
347
|
+
</button>
|
|
348
|
+
</span>
|
|
349
|
+
<button type="button" className="flx-dp-nav" aria-label={s.prevYear} onClick={() => setView(new Date(view.getFullYear() - 1, view.getMonth(), 1, 12))}>
|
|
350
|
+
<Ico d={I_L} />
|
|
351
|
+
</button>
|
|
352
|
+
<button type="button" className="flx-dp-nav" aria-label={s.nextYear} onClick={() => setView(new Date(view.getFullYear() + 1, view.getMonth(), 1, 12))}>
|
|
353
|
+
<Ico d={I_R} />
|
|
354
|
+
</button>
|
|
355
|
+
</div>
|
|
356
|
+
<div className="flx-dp-cells">
|
|
357
|
+
{Array.from({ length: 12 }, (_, i) => {
|
|
358
|
+
const mx = new Date(view.getFullYear(), i, 1, 12);
|
|
359
|
+
const sel = !!day && day.slice(0, 7) === ymd(mx).slice(0, 7);
|
|
360
|
+
return (
|
|
361
|
+
<button
|
|
362
|
+
key={i}
|
|
363
|
+
type="button"
|
|
364
|
+
className={'flx-dp-cell' + (sel ? ' is-sel' : '') + (i === view.getMonth() ? ' is-focus' : '')}
|
|
365
|
+
tabIndex={i === view.getMonth() ? 0 : -1}
|
|
366
|
+
onClick={() => go(() => { setView(mx); setFocus(clampFocus(mx)); setMode('days'); })}
|
|
367
|
+
>
|
|
368
|
+
{cap(F.month.format(mx), locale)}
|
|
369
|
+
</button>
|
|
370
|
+
);
|
|
371
|
+
})}
|
|
372
|
+
</div>
|
|
373
|
+
</>
|
|
374
|
+
);
|
|
375
|
+
} else {
|
|
376
|
+
const y0 = view.getFullYear() - (view.getFullYear() % 12);
|
|
377
|
+
body = (
|
|
378
|
+
<>
|
|
379
|
+
<div className="flx-dp-head">
|
|
380
|
+
<span className="flx-dp-sels">
|
|
381
|
+
<span className="flx-dp-range">
|
|
382
|
+
{y0} – {y0 + 11}
|
|
383
|
+
</span>
|
|
384
|
+
</span>
|
|
385
|
+
<button type="button" className="flx-dp-nav" aria-label={s.prev} onClick={() => setView(new Date(view.getFullYear() - 12, view.getMonth(), 1, 12))}>
|
|
386
|
+
<Ico d={I_L} />
|
|
387
|
+
</button>
|
|
388
|
+
<button type="button" className="flx-dp-nav" aria-label={s.next} onClick={() => setView(new Date(view.getFullYear() + 12, view.getMonth(), 1, 12))}>
|
|
389
|
+
<Ico d={I_R} />
|
|
390
|
+
</button>
|
|
391
|
+
</div>
|
|
392
|
+
<div className="flx-dp-cells">
|
|
393
|
+
{Array.from({ length: 12 }, (_, i) => {
|
|
394
|
+
const yy = y0 + i;
|
|
395
|
+
return (
|
|
396
|
+
<button
|
|
397
|
+
key={yy}
|
|
398
|
+
type="button"
|
|
399
|
+
className={'flx-dp-cell' + (day && +day.slice(0, 4) === yy ? ' is-sel' : '') + (yy === view.getFullYear() ? ' is-focus' : '')}
|
|
400
|
+
tabIndex={yy === view.getFullYear() ? 0 : -1}
|
|
401
|
+
onClick={() => go(() => { setView(new Date(yy, view.getMonth(), 1, 12)); setMode('months'); })}
|
|
402
|
+
>
|
|
403
|
+
{yy}
|
|
404
|
+
</button>
|
|
405
|
+
);
|
|
406
|
+
})}
|
|
407
|
+
</div>
|
|
408
|
+
</>
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
const quickShown = q.filter((o) => allowed(o.value));
|
|
412
|
+
return (
|
|
413
|
+
<div
|
|
414
|
+
ref={ref}
|
|
415
|
+
className={'flx-dp' + (bare ? ' is-bare' : '') + (sheet ? ' is-sheet' : '') + (q.length ? ' has-side' : '')}
|
|
416
|
+
role="dialog"
|
|
417
|
+
aria-label={label ?? s.pick}
|
|
418
|
+
data-theme={theme ?? th}
|
|
419
|
+
style={style}
|
|
420
|
+
onKeyDown={onKey}
|
|
421
|
+
>
|
|
422
|
+
{q.length > 0 && (
|
|
423
|
+
<div className="flx-dp-side">
|
|
424
|
+
{quickShown.map((o) => (
|
|
425
|
+
<button key={o.label} type="button" className={'flx-dp-q' + (o.value === day ? ' is-on' : '')} onClick={() => commit(o.value)}>
|
|
426
|
+
<span>{o.label}</span>
|
|
427
|
+
{o.hint && <small>{o.hint}</small>}
|
|
428
|
+
</button>
|
|
429
|
+
))}
|
|
430
|
+
{!required && (
|
|
431
|
+
<>
|
|
432
|
+
<hr />
|
|
433
|
+
<button type="button" className="flx-dp-q is-none" onClick={() => commit('')}>
|
|
434
|
+
<span>{s.noDate}</span>
|
|
435
|
+
</button>
|
|
436
|
+
</>
|
|
437
|
+
)}
|
|
438
|
+
</div>
|
|
439
|
+
)}
|
|
440
|
+
<div className="flx-dp-main">
|
|
441
|
+
{body}
|
|
442
|
+
<div className="flx-dp-foot">
|
|
443
|
+
<label className="flx-dp-in flx-dp-in--date">
|
|
444
|
+
<Ico d={I_CAL} s={16} />
|
|
445
|
+
<input
|
|
446
|
+
type="text"
|
|
447
|
+
inputMode="numeric"
|
|
448
|
+
autoComplete="off"
|
|
449
|
+
maxLength={10}
|
|
450
|
+
placeholder={P.ph}
|
|
451
|
+
aria-label={s.pick}
|
|
452
|
+
value={typed}
|
|
453
|
+
onChange={(e) => {
|
|
454
|
+
setTyped(e.target.value);
|
|
455
|
+
const d = P.fromText(e.target.value);
|
|
456
|
+
if (d && allowed(d)) select(d);
|
|
457
|
+
}}
|
|
458
|
+
onBlur={() => {
|
|
459
|
+
if (!P.fromText(typed)) setTyped(P.toText(day));
|
|
460
|
+
}}
|
|
461
|
+
/>
|
|
462
|
+
</label>
|
|
463
|
+
{time && (
|
|
464
|
+
<label className="flx-dp-in flx-dp-in--time">
|
|
465
|
+
<Ico d={I_CLK} s={16} />
|
|
466
|
+
<input
|
|
467
|
+
type="text"
|
|
468
|
+
inputMode="numeric"
|
|
469
|
+
autoComplete="off"
|
|
470
|
+
maxLength={5}
|
|
471
|
+
placeholder="--:--"
|
|
472
|
+
aria-label={s.time}
|
|
473
|
+
value={tm}
|
|
474
|
+
onChange={(e) => {
|
|
475
|
+
const raw = e.target.value.replace(/\D/g, '').slice(0, 4);
|
|
476
|
+
setTm(raw.length > 2 ? raw.slice(0, 2) + ':' + raw.slice(2) : raw);
|
|
477
|
+
}}
|
|
478
|
+
onBlur={() => {
|
|
479
|
+
const c = cleanTime(tm);
|
|
480
|
+
if (c || !tm) setTm(c);
|
|
481
|
+
}}
|
|
482
|
+
/>
|
|
483
|
+
</label>
|
|
484
|
+
)}
|
|
485
|
+
</div>
|
|
486
|
+
<div className="flx-dp-acts">
|
|
487
|
+
{!q.length && !required && (!!value || !!day) && (
|
|
488
|
+
<button type="button" className="flx-dp-act" onClick={() => commit('')}>
|
|
489
|
+
{s.clear}
|
|
490
|
+
</button>
|
|
491
|
+
)}
|
|
492
|
+
<span className="flx-dp-sp" />
|
|
493
|
+
<button type="button" className="flx-dp-act" onClick={() => onCancel?.()}>
|
|
494
|
+
{s.cancel}
|
|
495
|
+
</button>
|
|
496
|
+
<button type="button" className="flx-dp-act is-main" onClick={apply}>
|
|
497
|
+
{s.apply}
|
|
498
|
+
</button>
|
|
499
|
+
</div>
|
|
500
|
+
</div>
|
|
501
|
+
</div>
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
type PickerProps = Common & {
|
|
506
|
+
value: string;
|
|
507
|
+
onChange: (v: string) => void;
|
|
508
|
+
placeholder?: string;
|
|
509
|
+
disabled?: boolean;
|
|
510
|
+
id?: string;
|
|
511
|
+
name?: string;
|
|
512
|
+
'aria-label'?: string;
|
|
513
|
+
className?: string;
|
|
514
|
+
/** Takvim ikonu (varsayılan açık). */
|
|
515
|
+
icon?: boolean;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Tarih alanı: değeri sitenin dilinde gösteren düğme + takvim. Tarayıcının `<input type="date">`'i yerine.
|
|
520
|
+
* <DatePicker value={start} onChange={setStart} />
|
|
521
|
+
* <DatePicker value={at} onChange={setAt} time /> // tarih + saat
|
|
522
|
+
* <DatePicker value={birth} onChange={setBirth} quick={false} /> // hızlı seçim yok
|
|
523
|
+
*/
|
|
524
|
+
export function DatePicker({ value, onChange, placeholder, disabled, id, name, className, icon = true, locale = docLocale(), ...rest }: PickerProps) {
|
|
525
|
+
const [open, setOpen] = useState(false);
|
|
526
|
+
const [pos, setPos] = useState<{ left: number; top: number } | null>(null);
|
|
527
|
+
const [sheet, setSheet] = useState(false);
|
|
528
|
+
const btn = useRef<HTMLButtonElement>(null);
|
|
529
|
+
const wrap = useRef<HTMLSpanElement>(null);
|
|
530
|
+
const s = strings(locale);
|
|
531
|
+
const close = useCallback((refocus: boolean) => {
|
|
532
|
+
setOpen(false);
|
|
533
|
+
setPos(null);
|
|
534
|
+
if (refocus) btn.current?.focus({ preventScroll: true });
|
|
535
|
+
}, []);
|
|
536
|
+
useEffect(() => (open ? useOverlay.getState().pushOverlay() : undefined), [open]);
|
|
537
|
+
const place = useCallback(() => {
|
|
538
|
+
const b = btn.current;
|
|
539
|
+
const p = wrap.current?.querySelector<HTMLElement>('.flx-dp');
|
|
540
|
+
if (!b || !p) return;
|
|
541
|
+
const isSheet = window.innerWidth <= 600;
|
|
542
|
+
setSheet(isSheet);
|
|
543
|
+
if (isSheet) return setPos({ left: 0, top: 0 });
|
|
544
|
+
const r = b.getBoundingClientRect();
|
|
545
|
+
const vw = window.innerWidth, vh = window.innerHeight, pw = p.offsetWidth, ph = p.offsetHeight;
|
|
546
|
+
let top = r.bottom + 6;
|
|
547
|
+
if (top + ph > vh - 8 && r.top - ph - 6 > 8) top = r.top - ph - 6;
|
|
548
|
+
if (top + ph > vh - 8) top = Math.max(8, vh - ph - 8);
|
|
549
|
+
setPos({ left: Math.min(Math.max(8, r.left), vw - pw - 8), top });
|
|
550
|
+
}, []);
|
|
551
|
+
useLayoutEffect(() => {
|
|
552
|
+
if (open) place();
|
|
553
|
+
}, [open, place]);
|
|
554
|
+
useEffect(() => {
|
|
555
|
+
if (!open) return;
|
|
556
|
+
const down = (e: MouseEvent) => {
|
|
557
|
+
if (!wrap.current?.contains(e.target as Node)) close(false);
|
|
558
|
+
};
|
|
559
|
+
const re = () => place();
|
|
560
|
+
document.addEventListener('mousedown', down, true);
|
|
561
|
+
window.addEventListener('resize', re);
|
|
562
|
+
window.addEventListener('scroll', re, true);
|
|
563
|
+
return () => {
|
|
564
|
+
document.removeEventListener('mousedown', down, true);
|
|
565
|
+
window.removeEventListener('resize', re);
|
|
566
|
+
window.removeEventListener('scroll', re, true);
|
|
567
|
+
};
|
|
568
|
+
}, [open, close, place]);
|
|
569
|
+
const text = value ? formatDate(value, locale) : placeholder ?? s.pick;
|
|
570
|
+
return (
|
|
571
|
+
<span ref={wrap} className="flx-dt-wrap">
|
|
572
|
+
<button
|
|
573
|
+
ref={btn}
|
|
574
|
+
type="button"
|
|
575
|
+
id={id}
|
|
576
|
+
name={name}
|
|
577
|
+
className={['flx-dt', !value ? 'is-empty' : '', open ? 'is-open' : '', className ?? ''].filter(Boolean).join(' ')}
|
|
578
|
+
disabled={disabled}
|
|
579
|
+
aria-haspopup="dialog"
|
|
580
|
+
aria-expanded={open}
|
|
581
|
+
aria-label={rest['aria-label'] ? `${rest['aria-label']}: ${text}` : undefined}
|
|
582
|
+
onClick={() => (open ? close(true) : setOpen(true))}
|
|
583
|
+
onKeyDown={(e) => {
|
|
584
|
+
if (e.key === 'ArrowDown') {
|
|
585
|
+
e.preventDefault();
|
|
586
|
+
setOpen(true);
|
|
587
|
+
} else if ((e.key === 'Backspace' || e.key === 'Delete') && !rest.required && value) {
|
|
588
|
+
e.preventDefault();
|
|
589
|
+
onChange('');
|
|
590
|
+
}
|
|
591
|
+
}}
|
|
592
|
+
>
|
|
593
|
+
{icon && (
|
|
594
|
+
<span className="flx-dt__ico">
|
|
595
|
+
<Ico d={I_CAL} />
|
|
596
|
+
</span>
|
|
597
|
+
)}
|
|
598
|
+
<span className="flx-dt__val">{text}</span>
|
|
599
|
+
</button>
|
|
600
|
+
{open && (
|
|
601
|
+
<>
|
|
602
|
+
{sheet && <div className="flx-dp-shade" data-theme={themeOf(btn.current)} onMouseDown={() => close(false)} />}
|
|
603
|
+
<DateCalendar
|
|
604
|
+
{...rest}
|
|
605
|
+
locale={locale}
|
|
606
|
+
value={value}
|
|
607
|
+
sheet={sheet}
|
|
608
|
+
label={rest['aria-label']}
|
|
609
|
+
style={sheet ? undefined : pos ? { left: pos.left, top: pos.top } : { visibility: 'hidden', left: 0, top: 0 }}
|
|
610
|
+
onPick={(v) => {
|
|
611
|
+
close(true);
|
|
612
|
+
if (v !== value) onChange(v);
|
|
613
|
+
}}
|
|
614
|
+
onCancel={() => close(true)}
|
|
615
|
+
/>
|
|
616
|
+
</>
|
|
617
|
+
)}
|
|
618
|
+
</span>
|
|
619
|
+
);
|
|
620
|
+
}
|
package/src/Empty.tsx
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { Loader2 } from 'lucide-react';
|
|
3
|
+
|
|
4
|
+
export const Loading = () => (
|
|
5
|
+
<div className="loading"><Loader2 className="spin" size={20} /></div>
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
export function Empty({ icon, title, body }: { icon?: ReactNode; title: string; body?: string }) {
|
|
9
|
+
return (
|
|
10
|
+
<div className="empty-mini">
|
|
11
|
+
{icon}
|
|
12
|
+
<strong>{title}</strong>
|
|
13
|
+
{body && <span className="muted small">{body}</span>}
|
|
14
|
+
</div>
|
|
15
|
+
);
|
|
16
|
+
}
|