[portal][ui] allow customization of MonthYearPicker

pull/368/head
Yangshun Tay 2 years ago
parent ee9e2ba257
commit 3fe24cff3a

@ -1,3 +1,4 @@
import { useId } from 'react';
import { Select } from '@tih/ui'; import { Select } from '@tih/ui';
export type Month = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; export type Month = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
@ -8,8 +9,11 @@ export type MonthYear = Readonly<{
}>; }>;
type Props = Readonly<{ type Props = Readonly<{
errorMessage?: string;
monthLabel?: string;
onChange: (value: MonthYear) => void; onChange: (value: MonthYear) => void;
value: MonthYear; value: MonthYear;
yearLabel?: string;
}>; }>;
const MONTH_OPTIONS = [ const MONTH_OPTIONS = [
@ -72,11 +76,22 @@ const YEAR_OPTIONS = Array.from({ length: NUM_YEARS }, (_, i) => {
}; };
}); });
export default function MonthYearPicker({ value, onChange }: Props) { export default function MonthYearPicker({
errorMessage,
monthLabel = 'Month',
value,
onChange,
yearLabel = 'Year',
}: Props) {
const hasError = errorMessage != null;
const errorId = useId();
return ( return (
<div className="flex space-x-4"> <div
aria-describedby={hasError ? errorId : undefined}
className="flex space-x-4">
<Select <Select
label="Month" label={monthLabel}
options={MONTH_OPTIONS} options={MONTH_OPTIONS}
value={value.month} value={value.month}
onChange={(newMonth) => onChange={(newMonth) =>
@ -84,13 +99,18 @@ export default function MonthYearPicker({ value, onChange }: Props) {
} }
/> />
<Select <Select
label="Year" label={yearLabel}
options={YEAR_OPTIONS} options={YEAR_OPTIONS}
value={value.year} value={value.year}
onChange={(newYear) => onChange={(newYear) =>
onChange({ month: value.month, year: Number(newYear) }) onChange({ month: value.month, year: Number(newYear) })
} }
/> />
{errorMessage && (
<p className="text-danger-600 mt-2 text-sm" id={errorId}>
{errorMessage}
</p>
)}
</div> </div>
); );
} }

Loading…
Cancel
Save