From ece2227814ae1272082e909577a8f289193a911a Mon Sep 17 00:00:00 2001 From: Benjamin Leonard Date: Fri, 27 Jan 2023 15:57:24 +0000 Subject: [PATCH 01/13] Roughing in date picker --- app/components/SystemMetric.tsx | 2 +- .../instances/instance/tabs/MetricsTab.tsx | 2 +- .../lib/date-time-range-picker/Calendar.tsx | 44 + .../date-time-range-picker/CalendarCell.tsx | 103 + .../date-time-range-picker/CalendarGrid.tsx | 46 + .../lib/date-time-range-picker/DateField.tsx | 108 + .../lib/date-time-range-picker/DatePicker.tsx | 63 + .../DateRangePicker.stories.tsx | 32 + .../DateRangePicker.tsx | 89 + libs/ui/lib/date-time-range-picker/Dialog.tsx | 19 + .../ui/lib/date-time-range-picker/Popover.tsx | 40 + .../date-time-range-picker/RangeCalendar.tsx | 69 + package-lock.json | 2786 +++++++++++++++++ package.json | 2 + 14 files changed, 3403 insertions(+), 2 deletions(-) create mode 100644 libs/ui/lib/date-time-range-picker/Calendar.tsx create mode 100644 libs/ui/lib/date-time-range-picker/CalendarCell.tsx create mode 100644 libs/ui/lib/date-time-range-picker/CalendarGrid.tsx create mode 100644 libs/ui/lib/date-time-range-picker/DateField.tsx create mode 100644 libs/ui/lib/date-time-range-picker/DatePicker.tsx create mode 100644 libs/ui/lib/date-time-range-picker/DateRangePicker.stories.tsx create mode 100644 libs/ui/lib/date-time-range-picker/DateRangePicker.tsx create mode 100644 libs/ui/lib/date-time-range-picker/Dialog.tsx create mode 100644 libs/ui/lib/date-time-range-picker/Popover.tsx create mode 100644 libs/ui/lib/date-time-range-picker/RangeCalendar.tsx diff --git a/app/components/SystemMetric.tsx b/app/components/SystemMetric.tsx index e8bde6bc39..ef09f48474 100644 --- a/app/components/SystemMetric.tsx +++ b/app/components/SystemMetric.tsx @@ -62,7 +62,7 @@ export function SystemMetric({ return (
-

+

{title} {isLoading && }

{/* TODO: proper skeleton for empty chart */} diff --git a/app/pages/project/instances/instance/tabs/MetricsTab.tsx b/app/pages/project/instances/instance/tabs/MetricsTab.tsx index a7ff79ba8a..676d2d3855 100644 --- a/app/pages/project/instances/instance/tabs/MetricsTab.tsx +++ b/app/pages/project/instances/instance/tabs/MetricsTab.tsx @@ -50,7 +50,7 @@ function DiskMetric({ return (
-

+

{title} {unit &&
{unit}
} {isLoading && }

diff --git a/libs/ui/lib/date-time-range-picker/Calendar.tsx b/libs/ui/lib/date-time-range-picker/Calendar.tsx new file mode 100644 index 0000000000..3da67c042e --- /dev/null +++ b/libs/ui/lib/date-time-range-picker/Calendar.tsx @@ -0,0 +1,44 @@ +import type { DateValue } from '@internationalized/date' +import { createCalendar } from '@internationalized/date' +import type { CalendarProps } from 'react-aria' +import { useCalendar, useLocale } from 'react-aria' +import { useCalendarState } from 'react-stately' + +import { DirectionLeftIcon, DirectionRightIcon } from '../icons' +import { CalendarGrid } from './CalendarGrid' +import { CalendarButton } from './RangeCalendar' + +export function Calendar(props: CalendarProps) { + const { locale } = useLocale() + const state = useCalendarState({ + ...props, + locale, + createCalendar, + }) + + const { calendarProps, prevButtonProps, nextButtonProps, title } = useCalendar( + props, + state + ) + + return ( +
+
+ + + +

{title}

+ + + +
+ +
+ ) +} diff --git a/libs/ui/lib/date-time-range-picker/CalendarCell.tsx b/libs/ui/lib/date-time-range-picker/CalendarCell.tsx new file mode 100644 index 0000000000..e02f922834 --- /dev/null +++ b/libs/ui/lib/date-time-range-picker/CalendarCell.tsx @@ -0,0 +1,103 @@ +import type { CalendarDate } from '@internationalized/date' +import { getDayOfWeek, isSameDay } from '@internationalized/date' +import cn from 'classnames' +import { useRef } from 'react' +import { mergeProps, useCalendarCell, useFocusRing, useLocale } from 'react-aria' +import type { CalendarState, RangeCalendarState } from 'react-stately' + +export interface CalendarCellProps { + state: CalendarState | RangeCalendarState + date: CalendarDate +} + +export function CalendarCell({ state, date }: CalendarCellProps) { + const ref = useRef(null) + const { + cellProps, + buttonProps, + isSelected, + isOutsideVisibleRange, + isDisabled, + formattedDate, + isInvalid, + } = useCalendarCell({ date }, state, ref) + + // The start and end date of the selected range will have + // an emphasized appearance. + const isSelectionStart = (state as RangeCalendarState).highlightedRange + ? isSameDay(date, (state as RangeCalendarState).highlightedRange.start) + : isSelected + const isSelectionEnd = (state as RangeCalendarState).highlightedRange + ? isSameDay(date, (state as RangeCalendarState).highlightedRange.end) + : isSelected + + // We add rounded corners on the left for the first day of the month, + // the first day of each week, and the start date of the selection. + // We add rounded corners on the right for the last day of the month, + // the last day of each week, and the end date of the selection. + const { locale } = useLocale() + const dayOfWeek = getDayOfWeek(date, locale) + const isRoundedLeft = + isSelected && (isSelectionStart || dayOfWeek === 0 || date.day === 1) + const isRoundedRight = + isSelected && + (isSelectionEnd || dayOfWeek === 6 || date.day === date.calendar.getDaysInMonth(date)) + + const { focusProps } = useFocusRing() + + return ( + +