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 349c3f3 commit 988566c
Showing 1 changed file with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dayjs from 'dayjs';
import { DatePickerType, DatesRangeValue, DateValue } from '../../types';
import { shiftTimezone } from '../../utils';
import { useDatesContext } from '../DatesProvider';
Expand All @@ -14,11 +13,8 @@ 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();
const formatDateWithTimezone = (date: Date) => {
return shiftTimezone('remove', date, ctx.getTimezone()).toISOString();
};

if (type === 'range' && Array.isArray(value)) {
Expand All @@ -28,21 +24,21 @@ function formatValue(value: HiddenDatesInputValue, type: DatePickerType) {
}

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

return `${formatDateWithTimezoneAndLocale(startDate)}${formatDateWithTimezoneAndLocale(endDate)}`;
return `${formatDateWithTimezone(startDate)}${formatDateWithTimezone(endDate)}`;
}

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

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

return '';
Expand Down

0 comments on commit 988566c

Please sign in to comment.