Skip to content

Commit

Permalink
feat: persian calendar (#2645)
Browse files Browse the repository at this point in the history
* feat: persian calendar

* Update docs

* Update playground

* Playground updates

* Update playground
  • Loading branch information
gpbl authored Dec 25, 2024
1 parent 39b3cb1 commit 37907fc
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 115 deletions.
7 changes: 0 additions & 7 deletions examples/Jalali.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions examples/Jalali.test.tsx → examples/Persian.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import React from "react";
import { grid } from "@/test/elements";
import { render } from "@/test/render";

import { Jalali } from "./Jalali";
import { Persian } from "./Persian";

const today = new Date(2024, 11, 22);

beforeAll(() => jest.setSystemTime(today));
afterAll(() => jest.useRealTimers());

test("should render دی 1403", () => {
render(<Jalali />);
render(<Persian />);
expect(grid("دی 1403")).toBeInTheDocument();
});
7 changes: 7 additions & 0 deletions examples/Persian.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

import { DayPicker } from "react-day-picker/persian";

export function Persian() {
return <DayPicker mode="single" />;
}
18 changes: 18 additions & 0 deletions examples/PersianEn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";

import { DayPicker, getDateLib, enUS } from "react-day-picker/persian";

export function PersianEn() {
const [selected, setSelected] = React.useState(new Date());
const dateLib = getDateLib({ locale: enUS });
return (
<DayPicker
locale={enUS}
mode="single"
selected={selected}
required
onSelect={setSelected}
footer={`Selected: ${dateLib.format(selected, "d MMM yyyy")}`}
/>
);
}
16 changes: 16 additions & 0 deletions examples/PersianFormatted.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";

import { grid } from "@/test/elements";
import { render } from "@/test/render";

import { PersianFormatted } from "./PersianFormatted";

const today = new Date(2024, 11, 22);

beforeAll(() => jest.setSystemTime(today));
afterAll(() => jest.useRealTimers());

test("should render دی 1403", () => {
render(<PersianFormatted />);
expect(grid("دی 1403")).toBeInTheDocument();
});
17 changes: 17 additions & 0 deletions examples/PersianFormatted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";

import { DayPicker, getDateLib } from "react-day-picker/persian";

export function PersianFormatted() {
const [selected, setSelected] = React.useState(new Date());
const dateLib = getDateLib();
return (
<DayPicker
mode="single"
selected={selected}
required
onSelect={setSelected}
footer={`Selected: ${dateLib.format(selected, "yyyy/MM/dd")}`}
/>
);
}
4 changes: 3 additions & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export * from "./InputRange";
export * from "./InputTime";
export * from "./ItalianLabels";
export * from "./ItalianLabels";
export * from "./Jalali";
export * from "./Keyboard";
export * from "./ModifiersClassnames";
export * from "./ModifiersCustom";
Expand All @@ -46,6 +45,9 @@ export * from "./MultipleMonthsPaged";
export * from "./NumberingSystem";
export * from "./OutsideDays";
export * from "./PastDatesDisabled";
export * from "./Persian";
export * from "./PersianFormatted";
export * from "./PersianEn";
export * from "./Range";
export * from "./RangeExcludeDisabled";
export * from "./RangeLong";
Expand Down
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const config: Config.InitialOptions = {
roots: ["<rootDir>/examples"],
moduleNameMapper: {
"@/test/(.*)": ["<rootDir>/test/$1"],
"react-day-picker/jalali": ["<rootDir>/src/jalali.tsx"],
"react-day-picker/persian": ["<rootDir>/src/persian.tsx"],
"react-day-picker": ["<rootDir>/src/index.ts"],
"^(\\.\\.?\\/.+)\\.jsx?$": "$1" // see https://github.com/kulshekhar/ts-jest/issues/1057
}
Expand All @@ -42,7 +42,7 @@ const config: Config.InitialOptions = {
roots: ["<rootDir>/examples"],
moduleNameMapper: {
"@/test/(.*)": ["<rootDir>/test/$1"],
"react-day-picker/jalali": ["<rootDir>/dist/cjs/jalali.js"],
"react-day-picker/persian": ["<rootDir>/dist/cjs/persian.js"],
"react-day-picker": ["<rootDir>/dist/cjs/index.js"],
"../src": ["<rootDir>/dist/cjs"], // allow using same @/test/elements in both env
"^(\\.\\.?\\/.+)\\.jsx?$": "$1" // see https://github.com/kulshekhar/ts-jest/issues/1057
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
"default": "./dist/cjs/jalali.js"
}
},
"./persian": {
"import": {
"types": "./dist/esm/persian.d.ts",
"default": "./dist/esm/persian.js"
},
"require": {
"types": "./dist/cjs/persian.d.ts",
"default": "./dist/cjs/persian.js"
}
},
"./locale": {
"import": {
"types": "./dist/esm/locale.d.ts",
Expand Down
1 change: 1 addition & 0 deletions persian.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/cjs/persian.d.ts";
4 changes: 4 additions & 0 deletions persian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable no-undef */
const persian = require("./dist/cjs/persian.js");
module.exports = persian;
41 changes: 2 additions & 39 deletions src/jalali.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,2 @@
import React from "react";

import * as jalaliDateLib from "date-fns-jalali";
import { faIR } from "date-fns-jalali/locale";

import { DayPicker as DayPickerComponent } from "./index.js";
import type { DayPickerProps } from "./types/props.js";

export function DayPicker(
props: DayPickerProps & {
/**
* The locale to use in the calendar.
*
* @default `faIR` from `date-fns-jalali`
*/
locale?: DayPickerProps["locale"];
/**
* The direction of the text in the calendar.
*
* @default `rtl`
*/
dir?: DayPickerProps["dir"];
/**
* The date library to use in the calendar.
*
* @default `jalaliDateLib` from `date-fns-jalali`
*/
dateLib?: DayPickerProps["dateLib"];
}
) {
return (
<DayPickerComponent
{...props}
locale={props.locale ?? faIR}
dir={props.dir ?? "rtl"}
dateLib={props.dateLib ?? jalaliDateLib}
/>
);
}
/** @deprecated Import from `react-day-picker/persian` instead. */
export * from "./persian.js";
61 changes: 61 additions & 0 deletions src/persian.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from "react";

import * as dateFnsJalali from "date-fns-jalali";
import { Locale } from "date-fns-jalali";
import * as locales from "date-fns-jalali/locale";

import {
DateLib,
DateLibOptions,
DayPicker as DayPickerComponent
} from "./index.js";
import type { DayPickerProps } from "./types/props.js";

export const faIR = locales.faIR;
export const enUS = locales.enUS;

/** Render the Persian Calendar */
export function DayPicker(
props: DayPickerProps & {
/**
* The locale to use in the calendar.
*
* @default `faIR`
*/
locale?: Locale;
/**
* The direction of the text in the calendar.
*
* @default `rtl`
*/
dir?: DayPickerProps["dir"];
/**
* The date library to use in the calendar.
*
* @default `jalaliDateLib` from `date-fns-jalali`
*/
dateLib?: DayPickerProps["dateLib"];
}
) {
const dateLib = getDateLib({
locale: props.locale,
weekStartsOn: props.broadcastCalendar ? 1 : props.weekStartsOn,
firstWeekContainsDate: props.firstWeekContainsDate,
useAdditionalWeekYearTokens: props.useAdditionalWeekYearTokens,
useAdditionalDayOfYearTokens: props.useAdditionalDayOfYearTokens,
timeZone: props.timeZone
});
return (
<DayPickerComponent
{...props}
locale={props.locale ?? faIR}
dir={props.dir ?? "rtl"}
dateLib={dateLib}
/>
);
}

/** Returns the date library used in the calendar. */
export const getDateLib = (options?: DateLibOptions) => {
return new DateLib(options, dateFnsJalali);
};
Loading

0 comments on commit 37907fc

Please sign in to comment.