Skip to content

Commit fb35e8c

Browse files
committed
feat: added support for the Formatting of the Date
1 parent eed9d1e commit fb35e8c

File tree

9 files changed

+89
-10
lines changed

9 files changed

+89
-10
lines changed

apps/web/content/docs/components/datepicker.mdx

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ Use this example to show a simple datepicker component.
1919

2020
<Example name="datepicker.root" />
2121

22+
## Formatted Date
23+
24+
Use this `inputFormat` prop to set the format of the datepicker component to be displayed
25+
26+
**Note:** don't use `DD` instead use `dd` and also for year don't use `YYYY` instead use `yyyy`
27+
28+
<Example name="datepicker.format" />
29+
2230
## Localization
2331

2432
Use the `language` prop to set the language of the datepicker component.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { type CodeData } from '~/components/code-demo';
2+
import { Datepicker } from 'flowbite-react';
3+
4+
const code = `
5+
'use client';
6+
7+
import { Datepicker } from 'flowbite-react';
8+
9+
function Component() {
10+
return <Datepicker inputFormat='dd-MMM-yyyy' />;
11+
}
12+
`;
13+
14+
const codeRSC = `
15+
import { Datepicker } from 'flowbite-react';
16+
17+
function Component() {
18+
return <Datepicker inputFormat='dd-MMM-yyyy' />;
19+
}
20+
`;
21+
22+
function Component() {
23+
return <Datepicker inputFormat="dd-MMM-yyyy" />;
24+
}
25+
26+
export const format: CodeData = {
27+
type: 'single',
28+
code: [
29+
{
30+
fileName: 'client',
31+
language: 'tsx',
32+
code,
33+
},
34+
{
35+
fileName: 'server',
36+
language: 'tsx',
37+
code: codeRSC,
38+
},
39+
],
40+
githubSlug: 'datepicker/datepicker.format.tsx',
41+
component: <Component />,
42+
};

apps/web/examples/datepicker/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { autoHide } from "./datepicker.autoHide";
2+
export { format } from './datepicker.format';
23
export { inline } from "./datepicker.inline";
34
export { localization } from "./datepicker.localization";
45
export { range } from "./datepicker.range";

bun.lockb

-3.51 KB
Binary file not shown.

packages/ui/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"dev": "",
4747
"format": "prettier . --write",
4848
"format:check": "prettier .",
49-
"postinstall": "bun run build",
5049
"lint": "eslint .",
5150
"lint:fix": "eslint . --fix",
5251
"prepack": "clean-package",
@@ -76,6 +75,7 @@
7675
"@vitejs/plugin-react": "4.2.1",
7776
"@vitest/coverage-v8": "1.4.0",
7877
"clean-package": "2.2.0",
78+
"date-fns": "^3.6.0",
7979
"eslint-plugin-react": "7.34.1",
8080
"eslint-plugin-storybook": "0.8.0",
8181
"eslint-plugin-vitest": "0.3.26",

packages/ui/src/components/Datepicker/Datepicker.spec.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getFormattedDate } from "./helpers";
88

99
describe("Components / Datepicker", () => {
1010
it("should display today's date by default", () => {
11-
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date());
11+
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date(), 'dd-MMM-yyy');
1212

1313
render(<Datepicker />);
1414

@@ -28,7 +28,7 @@ describe("Components / Datepicker", () => {
2828
});
2929

3030
it("should reset to today's date when Clear button is clicked", async () => {
31-
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date());
31+
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date(), 'dd-MMM-yyy');
3232
const todaysDayOfMonth = new Date().getDate();
3333
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
3434

@@ -43,7 +43,7 @@ describe("Components / Datepicker", () => {
4343
});
4444

4545
it("should use today's date when Today button is clicked", async () => {
46-
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date());
46+
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date(), 'dd-MMM-yyy');
4747
const todaysDayOfMonth = new Date().getDate();
4848
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
4949

@@ -90,7 +90,7 @@ describe("Components / Datepicker", () => {
9090
});
9191

9292
it("should clear the value when ref.current.clear is called", async () => {
93-
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date());
93+
const todaysDateInDefaultLanguage = getFormattedDate("en", new Date(), 'dd-MMM-yyyy');
9494
const todaysDayOfMonth = new Date().getDate();
9595
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
9696

@@ -105,4 +105,12 @@ describe("Components / Datepicker", () => {
105105

106106
expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument();
107107
});
108+
109+
it("should display today's date in dd-MMM-yyyy format", () => {
110+
const todaysDateInDefaultLanguage = getFormattedDate('en-US', new Date(), 'dd-MMM-yyyy');
111+
112+
render(<Datepicker inputFormat="dd-MMM-yyyy" />);
113+
114+
expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument();
115+
});
108116
});

packages/ui/src/components/Datepicker/Datepicker.stories.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,18 @@ Default.args = {
6060
weekStart: WeekStart.Sunday,
6161
theme: {},
6262
};
63+
64+
export const FormattedDate = Template.bind({})
65+
FormattedDate.args = {
66+
open: false,
67+
autoHide: true,
68+
showClearButton: true,
69+
showTodayButton: true,
70+
defaultDate: new Date(),
71+
minDate: undefined,
72+
maxDate: undefined,
73+
language: "en",
74+
weekStart: WeekStart.Sunday,
75+
theme: {},
76+
inputFormat: 'dd MMM yyyy'
77+
};

packages/ui/src/components/Datepicker/Datepicker.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export interface DatepickerProps extends Omit<TextInputProps, "theme"> {
9797
weekStart?: WeekStart;
9898
theme?: DeepPartial<FlowbiteDatepickerTheme>;
9999
onSelectedDateChanged?: (date: Date) => void;
100+
inputFormat?: string;
100101
}
101102

102103
const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps> = (
@@ -117,6 +118,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
117118
className,
118119
theme: customTheme = {},
119120
onSelectedDateChanged,
121+
inputFormat = 'dd-MMM-yyyy',
120122
...props
121123
},
122124
ref,
@@ -201,10 +203,10 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
201203
case Views.Years:
202204
return `${startOfYearPeriod(viewDate, 10)} - ${startOfYearPeriod(viewDate, 10) + 9}`;
203205
case Views.Months:
204-
return getFormattedDate(language, viewDate, { year: "numeric" });
206+
return getFormattedDate(language, viewDate, inputFormat, { year: "numeric" });
205207
case Views.Days:
206208
default:
207-
return getFormattedDate(language, viewDate, { month: "long", year: "numeric" });
209+
return getFormattedDate(language, viewDate, inputFormat, { month: "long", year: "numeric" });
208210
}
209211
};
210212

@@ -272,7 +274,7 @@ const DatepickerRender: ForwardRefRenderFunction<DatepickerRef, DatepickerProps>
272274
}
273275
setIsOpen(true);
274276
}}
275-
value={selectedDate && getFormattedDate(language, selectedDate)}
277+
value={selectedDate && getFormattedDate(language, selectedDate, inputFormat, {})}
276278
readOnly
277279
{...props}
278280
/>

packages/ui/src/components/Datepicker/helpers.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { format as DateFNSFormat } from 'date-fns';
2+
13
export enum Views {
24
Days = 0,
35
Months = 1,
@@ -99,7 +101,7 @@ export const addYears = (date: Date, amount: number): Date => {
99101
return newDate;
100102
};
101103

102-
export const getFormattedDate = (language: string, date: Date, options?: Intl.DateTimeFormatOptions): string => {
104+
export const getFormattedDate = (language: string, date: Date, dateFormat: string, options?: Intl.DateTimeFormatOptions,): string => {
103105
let defaultOptions: Intl.DateTimeFormatOptions = {
104106
day: "numeric",
105107
month: "long",
@@ -110,7 +112,8 @@ export const getFormattedDate = (language: string, date: Date, options?: Intl.Da
110112
defaultOptions = options;
111113
}
112114

113-
return new Intl.DateTimeFormat(language, defaultOptions).format(date);
115+
return DateFNSFormat(new Intl.DateTimeFormat(language, defaultOptions).format(date), dateFormat);
116+
114117
};
115118

116119
export const startOfYearPeriod = (date: Date, years: number): number => {

0 commit comments

Comments
 (0)