From 35e6aad35b5c6a5fa3c79dd052a7f9279afbf591 Mon Sep 17 00:00:00 2001 From: Adam Thompson Date: Tue, 14 Oct 2025 15:03:45 -0400 Subject: [PATCH 1/9] updates chevron button labels --- .../DatePickerMenuHeader.tsx | 89 +++++++++++++------ 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuHeader/DatePickerMenuHeader.tsx b/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuHeader/DatePickerMenuHeader.tsx index adccbffd94..2a8a4e78d1 100644 --- a/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuHeader/DatePickerMenuHeader.tsx +++ b/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuHeader/DatePickerMenuHeader.tsx @@ -1,9 +1,14 @@ import React, { forwardRef, MouseEventHandler } from 'react'; -import { isSameUTCMonth, setUTCMonth } from '@leafygreen-ui/date-utils'; +import { + getMonthName, + isSameUTCMonth, + setUTCMonth, +} from '@leafygreen-ui/date-utils'; import { SupportedLocales } from '@leafygreen-ui/date-utils'; -import Icon from '@leafygreen-ui/icon'; -import IconButton from '@leafygreen-ui/icon-button'; +import { Icon } from '@leafygreen-ui/icon'; +import { IconButton } from '@leafygreen-ui/icon-button'; +import { isDefined } from '@leafygreen-ui/lib'; import { useSharedDatePickerContext } from '../../../shared/context'; import { useDatePickerContext } from '../../DatePickerContext'; @@ -42,6 +47,12 @@ export const DatePickerMenuHeader = forwardRef< const isIsoFormat = locale === SupportedLocales.ISO_8601; + const formatMonth = (date: Date) => { + const monthName = getMonthName(date.getUTCMonth(), locale); + const year = date.getUTCFullYear().toString(); + return `${monthName.long} ${year}`; + }; + /** * If the month is not in range and is not the last valid month * e.g. @@ -63,6 +74,48 @@ export const DatePickerMenuHeader = forwardRef< return !isDateInRange && !isOnLastValidMonth; }; + /** + * Given a direction (left/right), computes the nearest valid adjacent month + * + * @example + * max: new Date(Date.UTC(2038, Month.January, 19)); + * current month date: new Date(Date.UTC(2038, Month.March, 19)); + * `left` chevron will change the month back to January 2038 + * + * @example + * min: new Date(Date.UTC(1970, Month.January, 1)); + * current month date: new Date(Date.UTC(1969, Month.November, 19)); + * "right" chevron will change the month back to January 1970 + */ + const getNewMonth = (dir: 'left' | 'right'): Date => { + if (isMonthInvalid(dir)) { + const closestValidDate = dir === 'left' ? max : min; + const newMonthIndex = closestValidDate.getUTCMonth(); + const newMonth = setUTCMonth(closestValidDate, newMonthIndex); + return newMonth; + } else { + const increment = dir === 'left' ? -1 : 1; + const newMonthIndex = month.getUTCMonth() + increment; + const newMonth = setUTCMonth(month, newMonthIndex); + return newMonth; + } + }; + + const getChevronButtonLabel = (dir: 'left' | 'right') => { + const dirLabel = dir === 'left' ? 'Previous' : 'Next'; + const isNewMonthInvalid = isMonthInvalid(dir); + const newMonth = getNewMonth(dir); + const newMonthString = formatMonth(newMonth); + return [ + dirLabel, + isNewMonthInvalid ? 'valid ' : undefined, + 'month', + `(${newMonthString})`, + ] + .filter(isDefined) + .join(' '); + }; + /** * Calls the `updateMonth` helper with the appropriate month when a Chevron is clicked */ @@ -71,35 +124,16 @@ export const DatePickerMenuHeader = forwardRef< e => { e.stopPropagation(); e.preventDefault(); - - // e.g. - // max: new Date(Date.UTC(2038, Month.January, 19)); - // current month date: new Date(Date.UTC(2038, Month.March, 19)); - // left chevron will change the month back to January 2038 - // e.g. - // min: new Date(Date.UTC(1970, Month.January, 1)); - // current month date: new Date(Date.UTC(1969, Month.November, 19)); - // right chevron will change the month back to January 1970 - if (isMonthInvalid(dir)) { - const closestValidDate = dir === 'left' ? max : min; - const newMonthIndex = closestValidDate.getUTCMonth(); - const newMonth = setUTCMonth(closestValidDate, newMonthIndex); - updateMonth(newMonth); - } else { - const increment = dir === 'left' ? -1 : 1; - const newMonthIndex = month.getUTCMonth() + increment; - const newMonth = setUTCMonth(month, newMonthIndex); - updateMonth(newMonth); - } + const newMonth = getNewMonth(dir); + updateMonth(newMonth); }; return (
@@ -120,7 +154,8 @@ export const DatePickerMenuHeader = forwardRef<
From 52382d3bfec9fa8d08db3a214273e5c3cce9eed8 Mon Sep 17 00:00:00 2001 From: Adam Thompson Date: Tue, 14 Oct 2025 15:04:23 -0400 Subject: [PATCH 2/9] update select labels --- .../DatePickerMenuSelectMonth.tsx | 14 +++++++++----- .../DatePickerMenuSelectYear.tsx | 8 +++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuSelect/DatePickerMenuSelectMonth.tsx b/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuSelect/DatePickerMenuSelectMonth.tsx index 4732c23b68..8dbd1b9c27 100644 --- a/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuSelect/DatePickerMenuSelectMonth.tsx +++ b/packages/date-picker/src/DatePicker/DatePickerMenu/DatePickerMenuSelect/DatePickerMenuSelectMonth.tsx @@ -1,6 +1,10 @@ import React, { useCallback } from 'react'; -import { getLocaleMonths, setUTCMonth } from '@leafygreen-ui/date-utils'; +import { + getLocaleMonths, + getMonthName, + setUTCMonth, +} from '@leafygreen-ui/date-utils'; import { cx } from '@leafygreen-ui/emotion'; import { Option, Select } from '@leafygreen-ui/select'; @@ -40,18 +44,18 @@ export const DatePickerMenuSelectMonth = ({ updateMonth(newMonth); }; + const monthString = getMonthName(month.getUTCMonth(), locale); + return (