diff options
Diffstat (limited to 'src/components/input/MonthSelect.tsx')
| -rw-r--r-- | src/components/input/MonthSelect.tsx | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/components/input/MonthSelect.tsx b/src/components/input/MonthSelect.tsx new file mode 100644 index 0000000..241634e --- /dev/null +++ b/src/components/input/MonthSelect.tsx @@ -0,0 +1,47 @@ +import { ListItem, Row, Select } from '@umami/react-zen'; +import { useLocale } from '@/components/hooks'; +import { formatDate } from '@/lib/date'; + +export function MonthSelect({ date = new Date(), onChange }) { + const { locale } = useLocale(); + const month = date.getMonth(); + const year = date.getFullYear(); + const currentYear = new Date().getFullYear(); + + const months = [...Array(12)].map((_, i) => i); + const years = [...Array(10)].map((_, i) => currentYear - i); + + const handleMonthChange = (month: number) => { + const d = new Date(date); + d.setMonth(month); + onChange?.(d); + }; + const handleYearChange = (year: number) => { + const d = new Date(date); + d.setFullYear(year); + onChange?.(d); + }; + + return ( + <Row gap> + <Select value={month} onChange={handleMonthChange}> + {months.map(m => { + return ( + <ListItem id={m} key={m}> + {formatDate(new Date(year, m, 1), 'MMMM', locale)} + </ListItem> + ); + })} + </Select> + <Select value={year} onChange={handleYearChange}> + {years.map(y => { + return ( + <ListItem id={y} key={y}> + {y} + </ListItem> + ); + })} + </Select> + </Row> + ); +} |