diff --git a/packages/calendar/src/components/calendar-types/travel-date-range-calendar/TravelDateRangeCalendar.tsx b/packages/calendar/src/components/calendar-types/travel-date-range-calendar/TravelDateRangeCalendar.tsx index c458536cd..3704324ed 100644 --- a/packages/calendar/src/components/calendar-types/travel-date-range-calendar/TravelDateRangeCalendar.tsx +++ b/packages/calendar/src/components/calendar-types/travel-date-range-calendar/TravelDateRangeCalendar.tsx @@ -18,6 +18,8 @@ export interface TravelDateRangeCalendarProps nextMonthButtonAriaLabel?: string; heading?: string; headingLevel?: HeadingVariant; + firstMonthInMonthPicker?: Date; + numMonthsInMonthPicker?: number; } export const TravelDateRangeCalendar: React.FC< @@ -33,6 +35,8 @@ export const TravelDateRangeCalendar: React.FC< nextMonthButtonAriaLabel = "Next month", heading, headingLevel, + numMonthsInMonthPicker = 12, + firstMonthInMonthPicker = new Date(), }) => { const inputProps = useTravelDateRangeInput( value, @@ -75,8 +79,8 @@ export const TravelDateRangeCalendar: React.FC< {visiblePanel === "month-picker" && ( { setVisibleMonth(v); diff --git a/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.stories.tsx b/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.stories.tsx index 44968634b..9315d6aac 100644 --- a/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.stories.tsx +++ b/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.stories.tsx @@ -3,9 +3,16 @@ import * as React from "react"; import { useState } from "react"; import { TravelDateRangeInput } from "./TravelDateRangeInput"; import { Column, Row, Spacing } from "@stenajs-webui/core"; -import { Banner, Label } from "@stenajs-webui/elements"; +import { + Banner, + Label, + PrimaryButton, + SecondaryButton, +} from "@stenajs-webui/elements"; import { TravelDateRangeInputValue } from "../../../features/travel-calendar/types"; import { parseLocalizedDateString } from "../../../features/localize-date-format/LocalizedDateParser"; +import { formatLocalizedDate } from "../../../features/localize-date-format/LocalizedDateFormatter"; +import { addWeeks } from "date-fns"; export default { title: "calendar/Input/TravelDateRangeInput", @@ -134,3 +141,69 @@ const LocaleDemo = ({ localeCode }: { localeCode: string }) => { ); }; + +export const WithValidationAndCloseButton = () => { + const [value, setValue] = useState( + undefined + ); + + return ( +
+ { + return ( + + + + + ); + }} + /> +
+ ); +}; + +export const WithPresets = () => { + const [value, setValue] = useState( + undefined + ); + + return ( +
+ { + return ( + + + setValue({ + startDate: formatLocalizedDate(new Date(), "sv"), + endDate: formatLocalizedDate(addWeeks(new Date(), 1), "sv"), + }) + } + /> + + setValue({ + startDate: formatLocalizedDate(new Date(), "sv"), + endDate: formatLocalizedDate(addWeeks(new Date(), 2), "sv"), + }) + } + /> + + ); + }} + /> +
+ ); +}; diff --git a/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.tsx b/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.tsx index bcace53ec..ebbac7cb0 100644 --- a/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.tsx +++ b/packages/calendar/src/components/input-types/travel-date-range-input/TravelDateRangeInput.tsx @@ -1,6 +1,7 @@ import * as React from "react"; import { KeyboardEventHandler, + ReactNode, useCallback, useLayoutEffect, useRef, @@ -8,7 +9,6 @@ import { } from "react"; import { Box, - Column, Heading, HeadingVariant, useOnClickOutside, @@ -24,6 +24,10 @@ import { TravelDateRangeInputValue } from "../../../features/travel-calendar/typ import styles from "./TravelDateRangeInput.module.css"; import cx from "classnames"; +export interface RenderBelowCalendarArgs { + hideCalendar: () => void; +} + export interface TravelDateRangeInputProps extends ValueAndOnValueChangeProps { localeCode?: string; @@ -34,8 +38,12 @@ export interface TravelDateRangeInputProps nextMonthButtonAriaLabel?: string; heading?: string; headingLevel?: HeadingVariant; + firstMonthInMonthPicker?: Date; + numMonthsInMonthPicker?: number; zIndex?: number; zIndexWhenClosed?: number; + onHideCalendar?: () => void; + renderBelowCalendar?: (args: RenderBelowCalendarArgs) => ReactNode; } export const TravelDateRangeInput: React.FC = ({ @@ -49,8 +57,12 @@ export const TravelDateRangeInput: React.FC = ({ nextMonthButtonAriaLabel = "Next month", heading, headingLevel, + numMonthsInMonthPicker = 12, + firstMonthInMonthPicker = new Date(), zIndex = 1000, zIndexWhenClosed, + onHideCalendar, + renderBelowCalendar, }) => { const [calendarOpen, setCalendarOpen] = useState(false); const [calendarInDom, setCalendarInDom] = useState(false); @@ -77,12 +89,14 @@ export const TravelDateRangeInput: React.FC = ({ setCalendarOpen(false); calendarOpenRef.current = false; + onHideCalendar?.(); + setTimeout(() => { if (!calendarOpenRef.current) { setCalendarInDom(false); } }, 120); - }, [calendarInDom]); + }, [calendarInDom, onHideCalendar]); const ref = useRef(null); const sizeSourceRef = useRef(null); @@ -152,8 +166,8 @@ export const TravelDateRangeInput: React.FC = ({ = ({ shadow={"popover"} borderRadius={"var(--swui-border-radius-large)"} > - - - {heading && ( - - {heading} - - )} - - + {heading && ( + + {heading} + + )} + + + + {visiblePanel === "calendar" && ( + + )} + + {visiblePanel === "month-picker" && ( + { + setVisibleMonth(v); + setVisiblePanel("calendar"); + monthPickerButtonRef.current?.focus(); + }} + onCancel={() => { + setVisiblePanel("calendar"); + monthPickerButtonRef.current?.focus(); + }} /> - - {visiblePanel === "calendar" && ( - - )} - - {visiblePanel === "month-picker" && ( - { - setVisibleMonth(v); - setVisiblePanel("calendar"); - monthPickerButtonRef.current?.focus(); - }} - onCancel={() => { - setVisiblePanel("calendar"); - monthPickerButtonRef.current?.focus(); - }} - /> - )} - + )} + {renderBelowCalendar?.({ hideCalendar })} diff --git a/packages/calendar/src/features/travel-calendar/components/TravelDateCell.tsx b/packages/calendar/src/features/travel-calendar/components/TravelDateCell.tsx index 36f36a140..802a1179d 100644 --- a/packages/calendar/src/features/travel-calendar/components/TravelDateCell.tsx +++ b/packages/calendar/src/features/travel-calendar/components/TravelDateCell.tsx @@ -168,7 +168,7 @@ const getTabIndex = ( /** * If date has been selected that date should be tabIndex = 0. * If no date has been selected, today's date should be tabIndex = 0. - * All else should be 1. + * All else should be -1. */ if ( selectedStartDate && selectedStartDateIsVisible