Skip to content

Commit

Permalink
[@mantine/dates] fix: add locale and timezone to hidden input
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzo-Wada committed Sep 25, 2024
1 parent 5193ba7 commit 349c3f3
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import dayjs from 'dayjs';
import { DatePickerType, DatesRangeValue, DateValue } from '../../types';
import { shiftTimezone } from '../../utils';
import { useDatesContext } from '../DatesProvider';

export type HiddenDatesInputValue = DatesRangeValue | DateValue | DateValue[];

Expand All @@ -10,28 +13,36 @@ export interface HiddenDatesInputProps {
}

function formatValue(value: HiddenDatesInputValue, type: DatePickerType) {
const ctx = useDatesContext();
const locale = ctx.getLocale();
dayjs.locale(locale);
const formatDateWithTimezoneAndLocale = (date: Date) => {
const shiftedDate = shiftTimezone('add', date, ctx.getTimezone());
return dayjs(shiftedDate).format();
};

if (type === 'range' && Array.isArray(value)) {
const [startDate, endDate] = value;
if (!startDate) {
return '';
}

if (!endDate) {
return `${startDate.toISOString()} –`;
return `${formatDateWithTimezoneAndLocale(startDate)} –`;
}

return `${startDate.toISOString()}${endDate.toISOString()}`;
return `${formatDateWithTimezoneAndLocale(startDate)}${formatDateWithTimezoneAndLocale(endDate)}`;
}

if (type === 'multiple' && Array.isArray(value)) {
return value
.map((date) => date?.toISOString())
.map((date) => date && formatDateWithTimezoneAndLocale(date))
.filter(Boolean)
.join(', ');
}

if (!Array.isArray(value) && value) {
return value.toISOString();
return formatDateWithTimezoneAndLocale(value);
}

return '';
Expand Down

0 comments on commit 349c3f3

Please sign in to comment.