Skip to content

Commit

Permalink
fix(Calendar): always render dates with current locale (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Aug 23, 2024
1 parent b97fc99 commit 3767684
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/components/CalendarView/CalendarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ArrowToggle, Button} from '@gravity-ui/uikit';

import {block} from '../../utils/cn';
import type {AccessibilityProps, DomProps, FocusEvents, StyleProps} from '../types';
import {formatDateTime} from '../utils/dates';

import type {RangeCalendarState} from './hooks/types';
import {useCalendarCellProps} from './hooks/useCalendarCellProps';
Expand Down Expand Up @@ -165,9 +166,9 @@ function Weekdays({state}: WeekdaysProps) {
key={date.day()}
className={b('weekday', {weekend: state.isWeekend(date)})}
role="columnheader"
aria-label={date.format('dddd')}
aria-label={formatDateTime(date, 'dddd', state.timeZone)}
>
{date.format('dd')}
{formatDateTime(date, 'dd', state.timeZone)}
</div>
);
})}
Expand All @@ -188,7 +189,7 @@ function CalendarGridCells({state}: CalendarGridProps) {
<div key={rowIndex} className={b('grid-row')} role="row">
{state.mode === 'quarters' ? (
<span role="rowheader" className={b('grid-rowgroup-header')}>
{days[rowIndex * columnsInRow].format('YYYY')}
{formatDateTime(days[rowIndex * columnsInRow], 'YYYY', state.timeZone)}
</span>
) : null}
{days
Expand Down
41 changes: 24 additions & 17 deletions src/components/CalendarView/hooks/useCalendarCellProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React from 'react';

import type {DateTime} from '@gravity-ui/date-utils';

import type {CalendarLayout, CalendarState, RangeCalendarState} from './types';
import {formatDateTime} from '../../utils/dates';

import type {CalendarState, RangeCalendarState} from './types';

export function useCalendarCellProps(date: DateTime, state: CalendarState | RangeCalendarState) {
const ref = React.useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -30,7 +32,7 @@ export function useCalendarCellProps(date: DateTime, state: CalendarState | Rang
const isCurrent = state.isCurrent(date);
const isWeekend = state.isWeekend(date);

const label = getDateLabel(date, state.mode);
const label = getDateLabel(date, state);

const cellProps: React.HTMLAttributes<unknown> = {
role: 'gridcell',
Expand Down Expand Up @@ -64,13 +66,13 @@ export function useCalendarCellProps(date: DateTime, state: CalendarState | Rang
},
};

let formattedDate = date.format('D');
let formattedDate = formatDateTime(date, 'D', state.timeZone);
if (state.mode === 'months') {
formattedDate = date.format('MMM');
formattedDate = formatDateTime(date, 'MMM', state.timeZone);
} else if (state.mode === 'quarters') {
formattedDate = date.format('[Q]Q');
formattedDate = formatDateTime(date, '[Q]Q', state.timeZone);
} else if (state.mode === 'years') {
formattedDate = date.format('YYYY');
formattedDate = formatDateTime(date, 'YYYY', state.timeZone);
}

return {
Expand All @@ -89,16 +91,21 @@ export function useCalendarCellProps(date: DateTime, state: CalendarState | Rang
};
}

function getDateLabel(date: DateTime, mode: CalendarLayout) {
let label = '';
if (mode === 'days') {
label = `${date.format('dddd')}, ${date.format('LL')}`;
} else if (mode === 'months') {
label = `${date.format('MMMM YYYY')}`;
} else if (mode === 'quarters') {
label = `${date.format('[Q]Q YYYY')}`;
} else if (mode === 'years') {
label = `${date.format('YYYY')}`;
function getDateLabel(date: DateTime, state: CalendarState | RangeCalendarState) {
switch (state.mode) {
case 'days': {
return `${formatDateTime(date, 'dddd', state.timeZone)}, ${formatDateTime(date, 'LL', state.timeZone)}`;
}
case 'months': {
return `${formatDateTime(date, 'MMMM YYYY', state.timeZone)}`;
}
case 'quarters': {
return `${formatDateTime(date, '[Q]Q YYYY', state.timeZone)}`;
}
case 'years': {
return `${formatDateTime(date, 'YYYY', state.timeZone)}`;
}
default:
return '';
}
return label;
}
8 changes: 7 additions & 1 deletion src/components/CalendarView/hooks/useCalendarGridProps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {useFocusWithin} from '@gravity-ui/uikit';

import {formatDateTime} from '../../utils/dates';

import type {CalendarState, RangeCalendarState} from './types';

export function useCalendarGridProps(state: CalendarState | RangeCalendarState) {
Expand All @@ -14,7 +16,11 @@ export function useCalendarGridProps(state: CalendarState | RangeCalendarState)
'aria-label':
state.mode === 'years' || state.mode === 'quarters'
? `${state.startDate.year()}${state.endDate.year()}`
: state.focusedDate.format(state.mode === 'days' ? 'MMMM YYYY' : 'YYYY'),
: formatDateTime(
state.focusedDate,
state.mode === 'days' ? 'MMMM YYYY' : 'YYYY',
state.timeZone,
),
'aria-disabled': state.disabled ? 'true' : undefined,
'aria-readonly': state.readOnly ? 'true' : undefined,
...focusWithinProps,
Expand Down
7 changes: 6 additions & 1 deletion src/components/CalendarView/hooks/useCalendarProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useFocusWithin} from '@gravity-ui/uikit';
import type {ButtonProps} from '@gravity-ui/uikit';

import type {CalendarProps} from '../../Calendar/Calendar';
import {formatDateTime} from '../../utils/dates';
import {i18n} from '../i18n';

import type {CalendarLayout, CalendarState, RangeCalendarState} from './types';
Expand All @@ -15,7 +16,11 @@ export function useCalendarProps(props: CalendarProps, state: CalendarState | Ra
const title =
state.mode === 'years' || state.mode === 'quarters'
? `${state.startDate.year()}${state.endDate.year()}`
: state.focusedDate.format(state.mode === 'days' ? 'MMMM YYYY' : 'YYYY');
: formatDateTime(
state.focusedDate,
state.mode === 'days' ? 'MMMM YYYY' : 'YYYY',
state.timeZone,
);

const {focusWithinProps} = useFocusWithin({
onFocusWithin: props.onFocus,
Expand Down
4 changes: 4 additions & 0 deletions src/components/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ export function mergeDateTime(date: DateTime, time: DateTime) {
.set('minutes', time.minute())
.set('seconds', time.second());
}

export function formatDateTime(date: DateTime, format: string, timezone: string) {
return dateTime({input: date, timeZone: timezone}).format(format);
}

0 comments on commit 3767684

Please sign in to comment.