Skip to content
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

feat: allow partial locales, export defaultLocale #2348

Merged
merged 5 commits into from
Aug 4, 2024
Merged
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
17 changes: 17 additions & 0 deletions src/DayPicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";

import { addMonths, startOfDay, startOfMonth } from "date-fns";
import { defaultLocale } from "react-day-picker";

import {
activeElement,
Expand Down Expand Up @@ -158,3 +159,19 @@ describe("when the `startMonth` is changed programmatically", () => {
expect(grid(labelGrid(newStartMonth))).toBeInTheDocument();
});
});

test("extends the default locale", () => {
render(
<DayPicker
month={new Date(2024, 0)}
locale={{
localize: {
...defaultLocale.localize,
month: () => "bar"
}
}}
/>
);
// Check if the custom month name is rendered
expect(grid("bar 2024")).toBeInTheDocument();
});
37 changes: 20 additions & 17 deletions src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getWeekdays } from "./helpers/getWeekdays.js";
import { getYearOptions } from "./helpers/getYearOptions.js";
import * as defaultLabels from "./labels/index.js";
import type { FormatOptions, LabelOptions } from "./lib/dateLib.js";
import { enUS } from "./lib/locales.js";
import type {
DayPickerProps,
Modifiers,
Expand All @@ -43,27 +44,29 @@ import { isDateRange } from "./utils/typeguards.js";
* @see https://daypicker.dev
*/
export function DayPicker(props: DayPickerProps) {
const { components, formatters, labels, dateLib, classNames } = useMemo(
() => ({
dateLib: getDateLib(props.dateLib),
components: getComponents(props.components),
formatters: getFormatters(props.formatters),
labels: { ...defaultLabels, ...props.labels },
classNames: { ...getDefaultClassNames(), ...props.classNames }
}),
[
props.classNames,
props.components,
props.dateLib,
props.formatters,
props.labels
]
);
const { components, formatters, labels, dateLib, locale, classNames } =
useMemo(
() => ({
dateLib: getDateLib(props.dateLib),
components: getComponents(props.components),
formatters: getFormatters(props.formatters),
labels: { ...defaultLabels, ...props.labels },
locale: { ...enUS, ...props.locale },
classNames: { ...getDefaultClassNames(), ...props.classNames }
}),
[
props.classNames,
props.components,
props.dateLib,
props.formatters,
props.labels,
props.locale
]
);

const {
captionLayout,
firstWeekContainsDate,
locale,
mode,
onDayBlur,
onDayClick,
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export * from "./utils/index.js";
export * from "./UI.js";

export * from "./useDayPicker.js";

export { enUS as defaultLocale } from "./lib/locales.js";
18 changes: 13 additions & 5 deletions src/types/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export type DayPickerProps = PropsBase &
);

/**
* Props used for customization of the calendar, localization, and event
* handling.
* Props for customizing the calendar, handling localization, and managing
* events. These exclude the selection mode props.
*
* @group DayPicker
* @see https://daypicker.dev/api/interfaces/PropsBase
*/
export interface PropsBase {
/**
Expand Down Expand Up @@ -335,12 +338,17 @@ export interface PropsBase {
/** Add the language tag to the container element. */
lang?: HTMLDivElement["lang"];
/**
* The date-fns locale object used to localize dates.
* The locale object used to localize dates. Pass a locale from `date-fns` to
* localize the calendar.
*
* @example
* import { es } from "date-fns/locale";
* <DayPicker locale={es} />
*
* @defaultValue en-US
* @defaultValue enUS - The English locale default of `date-fns`.
* @see https://daypicker.dev/docs/localization
*/
locale?: Locale | undefined;
locale?: Partial<Locale> | undefined;
/**
* The index of the first day of the week (0 - Sunday). Overrides the locale's
* one.
Expand Down
2 changes: 1 addition & 1 deletion src/useDayPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { DayPickerProps } from "./types/props.js";
import type { SelectedValue, SelectHandler } from "./types/selection.js";
import { Modifiers } from "./types/shared.js";

// Create a context with a default value
/** @private */
export const dayPickerContext = createContext<
DayPickerContext<DayPickerProps> | undefined
>(undefined);
Expand Down
21 changes: 21 additions & 0 deletions website/docs/docs/localization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ import { es } from "date-fns/locale";
<Examples.Spanish />
</BrowserWindow>

:::tip Customizing the Locale

You can customize the locale by passing an object with the desired translations. Use the `defaultLocale` object for the default translations.

```tsx
import { DayPicker, defaultLocale } from "react-day-picker";

<DayPicker
locale={{
localize: {
...defaultLocale.localize,
day: (day) => "custom-localized-day"
}
}}
month={new Date(2023, 0, 1)}
mode="single"
/>;
```

:::

## First Date of the Week

Use the `weekStartsOn` prop to specify the starting day of the week.
Expand Down