143 lines
4.6 KiB
TypeScript
143 lines
4.6 KiB
TypeScript
import { useState } from 'react';
|
|
import { Calendar, ChevronLeft, ChevronRight } from 'lucide-react';
|
|
import { Button } from '../components/ui';
|
|
|
|
export const MONTH_NAMES = [
|
|
'Styczeń',
|
|
'Luty',
|
|
'Marzec',
|
|
'Kwiecień',
|
|
'Maj',
|
|
'Czerwiec',
|
|
'Lipiec',
|
|
'Sierpień',
|
|
'Wrzesień',
|
|
'Październik',
|
|
'Listopad',
|
|
'Grudzień',
|
|
];
|
|
|
|
/** Krótkie nazwy do siatki miesięcy w panelu selektora. */
|
|
export const MONTH_SHORT = ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'];
|
|
|
|
/** `2026-08` → `Sierpień 2026`. */
|
|
export function formatMonth(month: string): string {
|
|
const [y, m] = month.split('-').map(Number);
|
|
return `${MONTH_NAMES[m - 1]} ${y}`;
|
|
}
|
|
|
|
function shiftMonth(month: string, delta: number): string {
|
|
const [y, m] = month.split('-').map(Number);
|
|
const d = new Date(y, m - 1 + delta, 1);
|
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`;
|
|
}
|
|
|
|
interface MonthPickerProps {
|
|
/** `YYYY-MM` albo `''` (brak filtra). */
|
|
value: string;
|
|
onChange: (month: string) => void;
|
|
/** Lata pokazywane w panelu (nowe → stare). */
|
|
years: string[];
|
|
}
|
|
|
|
/**
|
|
* Selektor miesiąca (YYYY-MM) — widget top menu.
|
|
* ◀/▶ przeklikują do poprzedniego/następnego miesiąca, panel: lata + siatka 12 miesięcy.
|
|
*/
|
|
export function MonthPicker({ value, onChange, years }: MonthPickerProps) {
|
|
const [open, setOpen] = useState(false);
|
|
const [panelYear, setPanelYear] = useState<number | null>(null);
|
|
const now = new Date();
|
|
const base = value || `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
|
|
const prevMonth = shiftMonth(base, -1);
|
|
const nextMonth = shiftMonth(base, 1);
|
|
|
|
return (
|
|
<div className="relative">
|
|
<div className="flex items-center gap-1">
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => onChange(prevMonth)}
|
|
aria-label="Poprzedni miesiąc"
|
|
>
|
|
<ChevronLeft className="w-4 h-4" />
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="secondary"
|
|
className="whitespace-nowrap h-9"
|
|
onClick={() => {
|
|
if (open) setOpen(false);
|
|
else {
|
|
setPanelYear(Number(base.slice(0, 4)));
|
|
setOpen(true);
|
|
}
|
|
}}
|
|
aria-haspopup="true"
|
|
aria-expanded={open}
|
|
>
|
|
<Calendar className="w-4 h-4" />
|
|
{value ? formatMonth(value) : 'Miesiąc'}
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => onChange(nextMonth)}
|
|
aria-label="Następny miesiąc"
|
|
>
|
|
<ChevronRight className="w-4 h-4" />
|
|
</Button>
|
|
</div>
|
|
{open && (
|
|
<>
|
|
<div className="fixed inset-0 z-40" onClick={() => setOpen(false)} />
|
|
<div className="absolute right-0 top-full z-50 mt-2 w-72 bg-card border border-line rounded-2xl p-3 shadow-lg">
|
|
{/* filtr po roku */}
|
|
<div className="flex flex-wrap gap-1.5 mb-3">
|
|
{years.map((y) => (
|
|
<button
|
|
key={y}
|
|
type="button"
|
|
onClick={() => setPanelYear(Number(y))}
|
|
className={`px-2.5 py-1.5 rounded-lg text-[0.85rem] font-semibold cursor-pointer ${
|
|
panelYear === Number(y)
|
|
? 'bg-accent/15 text-accent-text'
|
|
: 'text-dim hover:text-ink hover:bg-line/30'
|
|
}`}
|
|
>
|
|
{y}
|
|
</button>
|
|
))}
|
|
</div>
|
|
{/* siatka 12 miesięcy wybranego roku */}
|
|
<div className="grid grid-cols-4 gap-1.5">
|
|
{Array.from({ length: 12 }, (_, i) => {
|
|
const m = `${panelYear ?? base.slice(0, 4)}-${String(i + 1).padStart(2, '0')}`;
|
|
const selected = m === value;
|
|
return (
|
|
<button
|
|
key={m}
|
|
type="button"
|
|
title={selected ? 'Odznacz (wszystkie miesiące)' : undefined}
|
|
onClick={() => {
|
|
// ponowne kliknięcie zaznaczonego miesiąca odznacza filtr
|
|
onChange(selected ? '' : m);
|
|
setOpen(false);
|
|
}}
|
|
className={`px-1 py-2 rounded-lg text-[0.85rem] font-semibold cursor-pointer ${
|
|
selected ? 'bg-accent/15 text-accent-text' : 'text-ink hover:bg-line/30'
|
|
}`}
|
|
>
|
|
{MONTH_SHORT[i]}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|