Skip to content

[Feature] Add locale support by adding another type of config #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { DatepickerConfigs, OnDateSelected } from './utils/commonTypes';
export type { CalendarPanelProps } from './components/calendarPanel';
export * from './utils/calanderUtils';
export * from './utils/calendarUtils';
export { CalendarPanel } from './components/calendarPanel';
export * from './single';
export * from './range';
50 changes: 44 additions & 6 deletions src/range.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { Props as DayzedHookProps } from 'dayzed';
import { Month_Names_Short, Weekday_Names_Short } from './utils/calanderUtils';
import { Month_Names_Short, Weekday_Names_Short } from './utils/calendarUtils';
import {
Flex,
Input,
Expand All @@ -14,13 +14,17 @@ import {
import { CalendarPanel } from './components/calendarPanel';
import {
CalendarConfigs,
CalendarLocaleConfigs,
DatepickerConfigs,
DatepickerLocaleConfigs,
DatepickerProps,
OnDateSelected,
PropsConfigs,
} from './utils/commonTypes';
import { format } from 'date-fns';
import FocusLock from 'react-focus-lock';
import { daysForLocale, monthsForLocale } from './utils/calendarUtils';
import * as dateFnsLocales from 'date-fns/locale';

interface RangeCalendarPanelProps {
dayzedHookProps: DayzedHookProps;
Expand Down Expand Up @@ -82,7 +86,7 @@ export const RangeCalendarPanel: React.FC<RangeCalendarPanelProps> = ({

export interface RangeDatepickerProps extends DatepickerProps {
selectedDates: Date[];
configs?: DatepickerConfigs;
configs?: DatepickerConfigs | DatepickerLocaleConfigs;
disabled?: boolean;
defaultIsOpen?: boolean;
closeOnSelect?: boolean;
Expand All @@ -99,6 +103,13 @@ const DefaultConfigs: CalendarConfigs = {
firstDayOfWeek: 0,
};

const DefaultLocaleConfigs: CalendarLocaleConfigs = {
locale: 'en-US',
month: 'short',
day: 'short',
firstDayOfWeek: 0,
};

export const RangeDatepicker: React.FC<RangeDatepickerProps> = ({
configs,
propsConfigs = {},
Expand All @@ -116,10 +127,37 @@ export const RangeDatepicker: React.FC<RangeDatepickerProps> = ({
const [offset, setOffset] = useState(0);
const { onOpen, onClose, isOpen } = useDisclosure({ defaultIsOpen });

const calendarConfigs: CalendarConfigs = {
...DefaultConfigs,
...configs,
};
let calendarConfigs: CalendarConfigs;

if (configs !== undefined && 'locale' in configs) {
let calendarLocaleConfigs = {
...DefaultLocaleConfigs,
...configs,
};

let currentDateFns =
Object.values(dateFnsLocales)
.filter((dateFns) => dateFns.code === calendarLocaleConfigs.locale)
.pop() || dateFnsLocales.enUS;

calendarConfigs = {
dateFormat: currentDateFns.formatLong?.date({ width: 'short' }),
dayNames: daysForLocale(
calendarLocaleConfigs.locale,
calendarLocaleConfigs.day
),
monthNames: monthsForLocale(
calendarLocaleConfigs.locale,
calendarLocaleConfigs.month
),
firstDayOfWeek: calendarLocaleConfigs.firstDayOfWeek,
};
} else {
calendarConfigs = {
...DefaultConfigs,
...configs,
};
}

const onPopoverClose = () => {
onClose();
Expand Down
50 changes: 44 additions & 6 deletions src/single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import {
} from '@chakra-ui/react';
import { format } from 'date-fns';
import FocusLock from 'react-focus-lock';
import { Month_Names_Short, Weekday_Names_Short } from './utils/calanderUtils';
import { Month_Names_Short, Weekday_Names_Short } from './utils/calendarUtils';
import { CalendarPanel } from './components/calendarPanel';
import {
CalendarConfigs,
DatepickerConfigs,
DatepickerProps,
CalendarLocaleConfigs,
DatepickerLocaleConfigs,
OnDateSelected,
} from './utils/commonTypes';
import { daysForLocale, monthsForLocale } from './utils/calendarUtils';
import * as dateFnsLocales from 'date-fns/locale';

export interface SingleDatepickerProps extends DatepickerProps {
date?: Date;
onDateChange: (date: Date) => void;
configs?: DatepickerConfigs;
configs?: DatepickerConfigs | DatepickerLocaleConfigs;
disabled?: boolean;
/**
* disabledDates: `Uses startOfDay as comparison`
Expand All @@ -42,6 +46,13 @@ const DefaultConfigs: CalendarConfigs = {
firstDayOfWeek: 0,
};

const DefaultLocaleConfigs: CalendarLocaleConfigs = {
locale: 'en-US',
month: 'short',
day: 'short',
firstDayOfWeek: 0,
};

export const SingleDatepicker: React.FC<SingleDatepickerProps> = ({
configs,
propsConfigs,
Expand All @@ -66,10 +77,37 @@ export const SingleDatepicker: React.FC<SingleDatepickerProps> = ({

const { onOpen, onClose, isOpen } = useDisclosure({ defaultIsOpen });

const calendarConfigs: CalendarConfigs = {
...DefaultConfigs,
...configs,
};
let calendarConfigs: CalendarConfigs;

if (configs !== undefined && 'locale' in configs) {
let calendarLocaleConfigs = {
...DefaultLocaleConfigs,
...configs,
};

let currentDateFns =
Object.values(dateFnsLocales)
.filter((dateFns) => dateFns.code === calendarLocaleConfigs.locale)
.pop() || dateFnsLocales.enUS;

calendarConfigs = {
dateFormat: currentDateFns.formatLong?.date({ width: 'short' }),
dayNames: daysForLocale(
calendarLocaleConfigs.locale,
calendarLocaleConfigs.day
),
monthNames: monthsForLocale(
calendarLocaleConfigs.locale,
calendarLocaleConfigs.month
),
firstDayOfWeek: calendarLocaleConfigs.firstDayOfWeek,
};
} else {
calendarConfigs = {
...DefaultConfigs,
...configs,
};
}

const onPopoverClose = () => {
onClose();
Expand Down
39 changes: 0 additions & 39 deletions src/utils/calanderUtils.ts

This file was deleted.

61 changes: 61 additions & 0 deletions src/utils/calendarUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export const Month_Names_Full = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];

export const Month_Names_Short = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];

export const Weekday_Names_Short = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
];

export function daysForLocale(
localeName: string = 'en-US',
weekday: 'short' | 'long' = 'short'
) {
const { format } = new Intl.DateTimeFormat(localeName, { weekday });
return [...Array(7).keys()].map((day) =>
format(new Date(Date.UTC(2021, 5, day - 1)))
);
}

export function monthsForLocale(
localeName: string = 'en-US',
monthFormat: 'short' | 'long' = 'short'
) {
const { format } = new Intl.DateTimeFormat(localeName, {
month: monthFormat,
});
return [...Array(12).keys()].map((m) =>
format(new Date(Date.UTC(2021, m % 12)))
);
}
14 changes: 14 additions & 0 deletions src/utils/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ export interface CalendarConfigs {
dayNames: string[];
firstDayOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6;
}

export interface DatepickerLocaleConfigs {
locale?: string;
month?: 'long' | 'short';
day?: 'long' | 'short';
firstDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
}

export interface CalendarLocaleConfigs {
locale: string;
month: 'long' | 'short';
day: 'long' | 'short';
firstDayOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6;
}