sendou.ink/app/components/elements/DatePicker.tsx
Kalle c3944070b9
Some checks are pending
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Refactor popover to use React Aria components (#2069)
* Initial

* Progress

* Progress

* SQ work

* Migrate rest

* Clean up

* Rename style file
2025-02-02 12:03:39 +02:00

84 lines
2.0 KiB
TypeScript

import clsx from "clsx";
import {
Button,
Calendar,
CalendarCell,
CalendarGrid,
DateInput,
type DatePickerProps,
DateSegment,
type DateValue,
Dialog,
Group,
Heading,
Popover,
DatePicker as ReactAriaDatePicker,
} from "react-aria-components";
import {
type FormFieldSize,
formFieldSizeToClassName,
} from "../form/form-utils";
import { ArrowLeftIcon } from "../icons/ArrowLeft";
import { ArrowRightIcon } from "../icons/ArrowRight";
import { CalendarIcon } from "../icons/Calendar";
import { SendouFieldError } from "./FieldError";
import { SendouFieldMessage } from "./FieldMessage";
import { SendouLabel } from "./Label";
interface SendouDatePickerProps<T extends DateValue>
extends DatePickerProps<T> {
label: string;
bottomText?: string;
errorText?: string;
size?: FormFieldSize;
}
export function SendouDatePicker<T extends DateValue>({
label,
errorText,
bottomText,
size,
isRequired,
...rest
}: SendouDatePickerProps<T>) {
return (
<ReactAriaDatePicker {...rest} validationBehavior="aria">
<SendouLabel required={isRequired}>{label}</SendouLabel>
<Group
className={clsx("react-aria-Group", formFieldSizeToClassName(size))}
>
<DateInput>{(segment) => <DateSegment segment={segment} />}</DateInput>
<Button data-testid="open-calendar-button">
<CalendarIcon />
</Button>
</Group>
{errorText && <SendouFieldError>{errorText}</SendouFieldError>}
{bottomText && !errorText ? (
<SendouFieldMessage>{bottomText}</SendouFieldMessage>
) : null}
<Popover>
<Dialog>
<Calendar>
<header>
<Button slot="previous">
<ArrowLeftIcon />
</Button>
<Heading />
<Button slot="next">
<ArrowRightIcon />
</Button>
</header>
<CalendarGrid>
{(date) => {
return (
<CalendarCell date={date} data-testid="choose-date-button" />
);
}}
</CalendarGrid>
</Calendar>
</Dialog>
</Popover>
</ReactAriaDatePicker>
);
}