Skip to content

Commit d8ce7bc

Browse files
authored
feat: export locales from react-day-picker/locale (#2474)
* Add export of locales from react-day-picker/locale * Update docs, tests and imports
1 parent 77a6abd commit d8ce7bc

9 files changed

Lines changed: 32 additions & 22 deletions

File tree

examples/ItalianLabels.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from "react";
22

33
import { format } from "date-fns";
4-
import { it } from "date-fns/locale";
54
import { DayPicker } from "react-day-picker";
5+
import { it } from "react-day-picker/locale";
66

77
export function ItalianLabels() {
88
return (

examples/NumberingSystem.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { render, screen } from "@/test/render";
44

55
import { NumberingSystem } from "./NumberingSystem";
66

7-
const today = new Date(2021, 10, 25);
7+
const today = new Date(2024, 8, 19);
88

99
beforeAll(() => jest.setSystemTime(today));
1010
afterAll(() => jest.useRealTimers());
@@ -14,8 +14,8 @@ beforeEach(() => {
1414
});
1515

1616
test("should localize the days", () => {
17-
expect(screen.getByText("أحد")).toBeInTheDocument();
17+
expect(screen.getByText("أربعاء")).toBeInTheDocument();
1818
});
1919
test("should localize the week numbers", () => {
20-
expect(screen.getByText("٤٥")).toBeInTheDocument();
20+
expect(screen.getByText("١٤")).toBeInTheDocument();
2121
});

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@
7474
"default": "./dist/cjs/jalali.js"
7575
}
7676
},
77+
"./locale": {
78+
"require": {
79+
"types": "./dist/cjs/locale.d.ts",
80+
"default": "./dist/cjs/locale.js"
81+
},
82+
"import": {
83+
"types": "./dist/esm/locale.d.ts",
84+
"default": "./dist/esm/locale.js"
85+
},
86+
"default": {
87+
"types": "./dist/cjs/locale.d.ts",
88+
"default": "./dist/cjs/locale.js"
89+
}
90+
},
7791
"./style.css": {
7892
"require": "./src/style.css",
7993
"import": "./src/style.css",

src/locale.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "date-fns/locale";

src/types/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export interface PropsBase {
357357
* localize the calendar.
358358
*
359359
* @example
360-
* import { es } from "date-fns/locale";
360+
* import { es } from "react-day-picker/locale";
361361
* <DayPicker locale={es} />
362362
*
363363
* @defaultValue enUS - The English locale default of `date-fns`.

website/docs/docs/localization.mdx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@ Learn how to set the locale, changing the time zone, and adjust other calendar o
1212
| --------- | ------------------------------------------ | ----------------------------------- |
1313
| `locale` | [`Locale`](https://date-fns.org/docs/I18n) | Set the locale. Default is `en-US`. |
1414

15-
### 1. Install date-fns
15+
DayPicker export the [date-fns](https://date-fns.org) locales for easier calendar localization. To set a different locale, import it from `react-day-picker/locale`.
1616

17-
DayPicker uses the [date-fns](https://date-fns.org) library for calendar localization. To set a different locale, install `date-fns`:
18-
19-
```bash npm2yarn
20-
npm install date-fns
17+
```bash ts
18+
// import the locale object
19+
import { es } from "react-day-picker/locale";
2120
```
2221
23-
### 2. Set a Different Locale
24-
25-
By default, the locale is set to English (US). To localize the calendar, pass a [`Locale`](https://date-fns.org/docs/I18n) object to the `locale` prop.
22+
By default, the locale is set to English (US). To localize the calendar, pass the imported [`Locale`](https://date-fns.org/docs/I18n) object to the `locale` prop.
2623
2724
For example, to localize the calendar in Spanish, import the `es` locale object from `date-fns` and pass it to the component.
2825
2926
```tsx
30-
import { es } from "date-fns/locale";
27+
import { es } from "react-day-picker/locale";
3128

3229
<DayPicker locale={es} />; // Set the locale to Spanish
3330
```
@@ -106,8 +103,6 @@ export function TimeZone() {
106103
107104
## Calendar Options
108105
109-
## Locale props
110-
111106
| Prop Name | Type | Description |
112107
| ----------------------- | --------------------------------------------- | ------------------------------------------------------------------ |
113108
| `weekStartsOn` | `1` \| `2` \| `3` \| `4` \| `5` \| `6` \| `7` | Display the days falling into the other months. |
@@ -182,8 +177,8 @@ You can set the right-to-left direction and change the locale as needed.
182177
```tsx title="./JalaliCalendar.jsx"
183178
import React from "react";
184179
185-
import { faIR } from "date-fns/locale";
186180
import { DayPicker } from "react-day-picker/jalali";
181+
import { faIR } from "react-day-picker/locale";
187182
188183
export function Jalali() {
189184
return <DayPicker mode="single" locale={faIR} dir="rtl" />;

website/docs/docs/translation.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The `labels` prop allows you to customize the [ARIA labels](./accessibility.mdx)
3030

3131
```tsx
3232
import { format } from "date-fns";
33-
import { it } from "date-fns/locale";
33+
import { it } from "react-day-picker/locale";
3434

3535
function ItalianLabels() {
3636
return (
@@ -89,7 +89,7 @@ The following labels are optional and should work out of the box in most languag
8989
To set the text direction to right-to-left, use the `dir` prop with the value `rtl`.
9090

9191
```tsx
92-
import { arSA } from "date-fns/locale";
92+
import { arSA } from "react-day-picker/locale";
9393

9494
<DayPicker locale={arSA} dir="rtl" />;
9595
```
@@ -130,8 +130,8 @@ For example, to switch to Hindu-Arabic numerals, use the native [`Date.toLocaleS
130130

131131
```tsx
132132
import { format } from "date-fns/format";
133-
import { arSA } from "date-fns/locale";
134133
import { DayPicker, Formatters } from "react-day-picker";
134+
import { arSA } from "react-day-picker/locale";
135135

136136
const NU_LOCALE = "ar-u-nu-arab";
137137

website/examples-v8/NumberingSystem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22

33
import { format } from "date-fns";
4-
import { arSA } from "date-fns/locale";
4+
import { arSA } from "react-day-picker/locale";
55

66
import {
77
DateFormatter,

website/src/pages/playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from "react";
22

33
import Layout from "@theme/Layout";
4-
import * as locales from "date-fns/locale";
54
import {
65
DateRange,
76
DayPicker,
87
DayPickerProps,
98
isDateRange
109
} from "react-day-picker";
10+
import * as locales from "react-day-picker/locale";
1111

1212
import { BrowserWindow } from "../components/BrowserWindow";
1313
import { HighlightWithTheme } from "../components/HighlightWithTheme";

0 commit comments

Comments
 (0)