import { flip, FloatingPortal, offset, size, useFloating, } from "@floating-ui/react-dom-interactions"; import { Listbox } from "@headlessui/react"; import clsx from "clsx"; import { addMinutes, format, isSameDay, setHours, setMinutes } from "date-fns"; import * as React from "react"; import { stopPropagation } from "utils/stop-propagation"; import { usePreferences } from "@/components/preferences/use-preferences"; import ChevronDown from "../../../icons/chevron-down.svg"; import { styleMenuItem } from "../../../menu-styles"; export interface TimePickerProps { value: Date; startFrom?: Date; className?: string; onChange?: (value: Date) => void; } const TimePicker: React.VoidFunctionComponent = ({ value, onChange, className, startFrom = setMinutes(setHours(value, 0), 0), }) => { const { locale } = usePreferences(); const { reference, floating, x, y, strategy, refs } = useFloating({ strategy: "fixed", middleware: [ offset(5), flip(), size({ apply: ({ reference }) => { if (refs.floating.current) { Object.assign(refs.floating.current.style, { width: `${reference.width}px`, }); } }, }), ], }); const options: React.ReactNode[] = []; for (let i = 0; i < 96; i++) { const optionValue = addMinutes(startFrom, i * 15); if (!isSameDay(value, optionValue)) { // we only support event that start and end on the same day for now // because react-big-calendar does not support events that span days break; } options.push( {format(optionValue, "p", { locale })} , ); } return ( { onChange?.(new Date(newValue)); }} > {(open) => ( <>
{format(value, "p", { locale })}
{open ? ( {options} ) : null} )}
); }; export default TimePicker;