Skip to content

Commit 4279c39

Browse files
DateRangePicker allows arbitrary dates; scrollable date list
1 parent 0fa7f6a commit 4279c39

File tree

3 files changed

+231
-104
lines changed

3 files changed

+231
-104
lines changed

src/components/DatePicker/Common.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IconButton } from "../IconButton/IconButton";
88
import { Text } from "../Typography/Text/Text";
99

1010
const locale = "en-US";
11-
const selectedDateFormatter = new Intl.DateTimeFormat(locale, {
11+
export const selectedDateFormatter = new Intl.DateTimeFormat(locale, {
1212
day: "2-digit",
1313
month: "short",
1414
year: "numeric",
Lines changed: 141 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
import { Args } from "@storybook/react";
2-
import { DateRangePicker } from "./DateRangePicker";
1+
import { Args, Meta, StoryObj } from "@storybook/react";
2+
import { DateRange, DateRangePicker } from "./DateRangePicker";
3+
import dayjs from "dayjs";
34

4-
const defaultStory = {
5+
const getMonthsByNumber = (numberOfMonths: number): Array<DateRange> => {
6+
const now = dayjs();
7+
8+
if (numberOfMonths < 0) {
9+
const lastSixMonths: Array<DateRange> = [];
10+
for (let i = 0; i < Math.abs(numberOfMonths); i++) {
11+
const date = now.subtract(i, "month");
12+
lastSixMonths.push({
13+
startDate: date.startOf("month").toDate(),
14+
endDate: i === 0 ? now.toDate() : date.endOf("month").toDate(),
15+
});
16+
}
17+
18+
return lastSixMonths.reverse();
19+
}
20+
21+
const nextSixMonths: Array<DateRange> = [];
22+
for (let i = 0; i < numberOfMonths; i++) {
23+
const date = now.add(i, "month");
24+
nextSixMonths.push({
25+
startDate: date.startOf("month").toDate(),
26+
endDate: i === 0 ? now.toDate() : date.endOf("month").toDate(),
27+
});
28+
}
29+
30+
return nextSixMonths;
31+
};
32+
33+
const meta: Meta<typeof DateRangePicker> = {
34+
component: DateRangePicker,
535
args: {
636
onSelectDateRange: (startDate: Date, endDate: Date) => {
737
console.log("Date range selected: ", startDate, endDate);
@@ -20,21 +50,25 @@ const defaultStory = {
2050
placeholder: {
2151
control: "text",
2252
},
23-
predefinedDatesCount: {
24-
control: "number",
25-
default: 0,
26-
},
2753
onSelectDateRange: {
2854
control: "object",
2955
},
3056
},
31-
component: DateRangePicker,
57+
tags: ["autodocs"],
58+
title: "Display/DateRangePicker",
59+
};
60+
61+
export default meta;
62+
63+
type Story = StoryObj<typeof meta>;
64+
65+
export const Default: Story = {
66+
args: {
67+
predefinedDatesList: [],
68+
},
3269
render: (args: Args) => {
3370
const endDate = args.endDate ? new Date(args.endDate) : undefined;
3471
const startDate = args.startDate ? new Date(args.startDate) : undefined;
35-
const predefinedDatesCount = args.predefinedDatesCount
36-
? args.predefinedDatesCount
37-
: undefined;
3872

3973
return (
4074
<DateRangePicker
@@ -44,17 +78,107 @@ const defaultStory = {
4478
futureDatesDisabled={args.futureDatesDisabled}
4579
onSelectDateRange={args.onSelectDateRange}
4680
placeholder={args.placeholder}
47-
predefinedDatesCount={predefinedDatesCount}
4881
startDate={startDate}
4982
/>
5083
);
5184
},
52-
title: "Display/DateRangePicker",
53-
tags: ["autodocs"],
5485
};
5586

56-
export default defaultStory;
87+
export const PredefinedDatesLastSixMonths: Story = {
88+
render: (args: Args) => {
89+
const endDate = args.endDate ? new Date(args.endDate) : undefined;
90+
const startDate = args.startDate ? new Date(args.startDate) : undefined;
91+
const predefinedDatesList = getMonthsByNumber(-6);
92+
93+
return (
94+
<DateRangePicker
95+
key="default"
96+
endDate={endDate}
97+
disabled={args.disabled}
98+
futureDatesDisabled={args.futureDatesDisabled}
99+
onSelectDateRange={args.onSelectDateRange}
100+
placeholder={args.placeholder}
101+
predefinedDatesList={predefinedDatesList}
102+
startDate={startDate}
103+
/>
104+
);
105+
},
106+
};
107+
108+
export const PredefinedDatesNextSixMonths: Story = {
109+
render: (args: Args) => {
110+
const endDate = args.endDate ? new Date(args.endDate) : undefined;
111+
const startDate = args.startDate ? new Date(args.startDate) : undefined;
112+
const predefinedDatesList = getMonthsByNumber(6);
113+
114+
return (
115+
<DateRangePicker
116+
key="default"
117+
endDate={endDate}
118+
disabled={args.disabled}
119+
futureDatesDisabled={args.futureDatesDisabled}
120+
onSelectDateRange={args.onSelectDateRange}
121+
placeholder={args.placeholder}
122+
predefinedDatesList={predefinedDatesList}
123+
startDate={startDate}
124+
/>
125+
);
126+
},
127+
};
128+
129+
export const PredefinedDatesArbitraryDates: Story = {
130+
render: (args: Args) => {
131+
const endDate = args.endDate ? new Date(args.endDate) : undefined;
132+
const startDate = args.startDate ? new Date(args.startDate) : undefined;
133+
const predefinedDatesList = [
134+
{ startDate: new Date("04/14/2025"), endDate: new Date("05/14/2025") },
135+
{ startDate: new Date("05/14/2025"), endDate: new Date("06/14/2025") },
136+
{ startDate: new Date("06/14/2025"), endDate: new Date("07/14/2025") },
137+
];
138+
139+
return (
140+
<DateRangePicker
141+
key="default"
142+
endDate={endDate}
143+
disabled={args.disabled}
144+
futureDatesDisabled={args.futureDatesDisabled}
145+
onSelectDateRange={args.onSelectDateRange}
146+
placeholder={args.placeholder}
147+
predefinedDatesList={predefinedDatesList}
148+
startDate={startDate}
149+
/>
150+
);
151+
},
152+
};
153+
154+
export const PredefinedDatesScrollable: Story = {
155+
render: (args: Args) => {
156+
const endDate = args.endDate ? new Date(args.endDate) : undefined;
157+
const startDate = args.startDate ? new Date(args.startDate) : undefined;
158+
const predefinedDatesList = [
159+
{ startDate: new Date("09/14/2024"), endDate: new Date("10/14/2024") },
160+
{ startDate: new Date("10/14/2024"), endDate: new Date("11/14/2024") },
161+
{ startDate: new Date("11/14/2024"), endDate: new Date("12/14/2024") },
162+
{ startDate: new Date("12/14/2024"), endDate: new Date("01/14/2025") },
163+
{ startDate: new Date("01/14/2025"), endDate: new Date("02/14/2025") },
164+
{ startDate: new Date("02/14/2025"), endDate: new Date("03/14/2025") },
165+
{ startDate: new Date("03/14/2025"), endDate: new Date("04/14/2025") },
166+
{ startDate: new Date("04/14/2025"), endDate: new Date("05/14/2025") },
167+
{ startDate: new Date("05/14/2025"), endDate: new Date("06/14/2025") },
168+
{ startDate: new Date("06/14/2025"), endDate: new Date("07/14/2025") },
169+
];
57170

58-
export const Playground = {
59-
...defaultStory,
171+
return (
172+
<DateRangePicker
173+
key="default"
174+
endDate={endDate}
175+
disabled={args.disabled}
176+
futureDatesDisabled={args.futureDatesDisabled}
177+
onSelectDateRange={args.onSelectDateRange}
178+
placeholder={args.placeholder}
179+
predefinedDatesList={predefinedDatesList}
180+
startDate={startDate}
181+
/>
182+
);
183+
},
60184
};

0 commit comments

Comments
 (0)