@stackshift-ui/calendar 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +373 -0
- package/README.md +24 -0
- package/dist/calendar.d.ts +14 -0
- package/dist/calendar.js +2 -0
- package/dist/calendar.mjs +2 -0
- package/dist/chunk-3EYN2AKU.mjs +1 -0
- package/dist/chunk-QUOK23A4.mjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.mjs +2 -0
- package/dist/setupTests.d.ts +2 -0
- package/dist/setupTests.js +51 -0
- package/dist/setupTests.mjs +51 -0
- package/package.json +66 -0
- package/src/calendar.test.tsx +64 -0
- package/src/calendar.tsx +200 -0
- package/src/index.ts +4 -0
- package/src/setupTests.ts +4 -0
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stackshift-ui/calendar",
|
|
3
|
+
"description": "A date field component that allows users to enter and edit date.",
|
|
4
|
+
"version": "1.0.0-beta.1",
|
|
5
|
+
"private": false,
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/**",
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"author": "WebriQ <info@webriq.com>",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@testing-library/react": "^16.0.1",
|
|
17
|
+
"@testing-library/user-event": "^14.5.2",
|
|
18
|
+
"@testing-library/jest-dom": "^6.5.0",
|
|
19
|
+
"@types/node": "^22.7.0",
|
|
20
|
+
"@types/react": "^18.3.9",
|
|
21
|
+
"@types/react-dom": "^18.3.0",
|
|
22
|
+
"@vitejs/plugin-react": "^4.3.1",
|
|
23
|
+
"@vitest/coverage-v8": "^2.1.1",
|
|
24
|
+
"esbuild-plugin-rdi": "^0.0.0",
|
|
25
|
+
"esbuild-plugin-react18": "^0.2.5",
|
|
26
|
+
"esbuild-plugin-react18-css": "^0.0.4",
|
|
27
|
+
"jsdom": "^25.0.1",
|
|
28
|
+
"react": "^18.3.1",
|
|
29
|
+
"react-dom": "^18.3.1",
|
|
30
|
+
"tsup": "^8.3.0",
|
|
31
|
+
"typescript": "^5.6.2",
|
|
32
|
+
"vite-tsconfig-paths": "^5.0.1",
|
|
33
|
+
"vitest": "^2.1.1",
|
|
34
|
+
"@stackshift-ui/eslint-config": "6.0.10",
|
|
35
|
+
"@stackshift-ui/typescript-config": "6.0.10"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"classnames": "^2.5.1",
|
|
39
|
+
"date-fns": "^4.1.0",
|
|
40
|
+
"lucide-react": "^0.468.0",
|
|
41
|
+
"react-day-picker": "^9.7.0",
|
|
42
|
+
"@stackshift-ui/scripts": "6.1.0-beta.0",
|
|
43
|
+
"@stackshift-ui/system": "6.1.0-beta.0",
|
|
44
|
+
"@stackshift-ui/button": "6.1.0-beta.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@stackshift-ui/system": ">=6.1.0-beta.0",
|
|
48
|
+
"@types/react": "16.8 - 19",
|
|
49
|
+
"next": "10 - 14",
|
|
50
|
+
"react": "16.8 - 19",
|
|
51
|
+
"react-dom": "16.8 - 19"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"next": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsup && tsc -p tsconfig-build.json",
|
|
60
|
+
"clean": "rm -rf dist",
|
|
61
|
+
"dev": "tsup --watch && tsc -p tsconfig-build.json -w",
|
|
62
|
+
"typecheck": "tsc --noEmit",
|
|
63
|
+
"lint": "eslint src/",
|
|
64
|
+
"test": "vitest run --coverage"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
4
|
+
import { Calendar } from "./calendar";
|
|
5
|
+
|
|
6
|
+
describe.concurrent("Calendar", () => {
|
|
7
|
+
afterEach(cleanup);
|
|
8
|
+
|
|
9
|
+
test("Common: Calendar - test if renders without errors", () => {
|
|
10
|
+
const { unmount } = render(<Calendar />);
|
|
11
|
+
expect(screen.getByRole("grid")).toBeInTheDocument();
|
|
12
|
+
unmount();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("Common: Calendar - test if calls onSelect when date is clicked", async () => {
|
|
16
|
+
const user = userEvent.setup();
|
|
17
|
+
const onSelect = vi.fn();
|
|
18
|
+
// @ts-ignore-error
|
|
19
|
+
const { unmount } = render(<Calendar onSelect={onSelect} />);
|
|
20
|
+
|
|
21
|
+
const dateButtons = screen.getAllByRole("button");
|
|
22
|
+
// @ts-ignore-error
|
|
23
|
+
const dateButton = dateButtons.find(button => button.textContent === "15" && !button.disabled);
|
|
24
|
+
|
|
25
|
+
if (dateButton) {
|
|
26
|
+
await user.click(dateButton);
|
|
27
|
+
expect(onSelect).toHaveBeenCalled();
|
|
28
|
+
}
|
|
29
|
+
unmount();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("Common: Calendar - test if renders today's date", () => {
|
|
33
|
+
const { unmount } = render(<Calendar />);
|
|
34
|
+
const today = new Date();
|
|
35
|
+
const todayDate = today.getDate().toString();
|
|
36
|
+
|
|
37
|
+
const dateButtons = screen.getAllByRole("button");
|
|
38
|
+
const todayButton = dateButtons.find(
|
|
39
|
+
button =>
|
|
40
|
+
button.textContent === todayDate &&
|
|
41
|
+
button.getAttribute("data-day")?.includes(today.toLocaleDateString()),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
if (todayButton) {
|
|
45
|
+
expect(todayButton).toBeInTheDocument();
|
|
46
|
+
}
|
|
47
|
+
unmount();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("Common: Calendar - test if renders without outside days", () => {
|
|
51
|
+
const { unmount } = render(<Calendar showOutsideDays={false} />);
|
|
52
|
+
const days = screen.queryAllByRole("gridcell");
|
|
53
|
+
const outsideDays = days.filter(day => day.getAttribute("data-outside") === "true");
|
|
54
|
+
expect(outsideDays.length).toBeGreaterThan(0);
|
|
55
|
+
unmount();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("Common: Calendar - test if renders with custom className", ({ expect }) => {
|
|
59
|
+
const clx = "calendar-class";
|
|
60
|
+
const { unmount } = render(<Calendar data-testid="calendar" className={clx} />);
|
|
61
|
+
expect(screen.getByTestId("calendar").classList).toContain(clx);
|
|
62
|
+
unmount();
|
|
63
|
+
});
|
|
64
|
+
});
|
package/src/calendar.tsx
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { DayButton, DayPicker, getDefaultClassNames } from "react-day-picker";
|
|
6
|
+
|
|
7
|
+
import { Button, buttonVariants } from "@stackshift-ui/button";
|
|
8
|
+
import { cn, DefaultComponent, useStackShiftUIComponents } from "@stackshift-ui/system";
|
|
9
|
+
|
|
10
|
+
const displayName = "Calendar";
|
|
11
|
+
const displayNameDayButton = "CalendarDayButton";
|
|
12
|
+
|
|
13
|
+
function Calendar({
|
|
14
|
+
className,
|
|
15
|
+
classNames,
|
|
16
|
+
showOutsideDays = true,
|
|
17
|
+
captionLayout = "label",
|
|
18
|
+
buttonVariant = "ghost",
|
|
19
|
+
formatters,
|
|
20
|
+
components,
|
|
21
|
+
...props
|
|
22
|
+
}: React.ComponentProps<typeof DayPicker> & {
|
|
23
|
+
buttonVariant?: React.ComponentProps<typeof Button>["variant"];
|
|
24
|
+
}) {
|
|
25
|
+
const { [displayName]: Component = DefaultComponent } = useStackShiftUIComponents();
|
|
26
|
+
const defaultClassNames = getDefaultClassNames();
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<Component
|
|
30
|
+
as={DayPicker}
|
|
31
|
+
showOutsideDays={showOutsideDays}
|
|
32
|
+
className={cn(
|
|
33
|
+
"bg-background group/calendar p-3 [--cell-size:--spacing(8)] [[data-slot=card-content]_&]:bg-transparent [[data-slot=popover-content]_&]:bg-transparent",
|
|
34
|
+
String.raw`rtl:**:[.rdp-button\_next>svg]:rotate-180`,
|
|
35
|
+
String.raw`rtl:**:[.rdp-button\_previous>svg]:rotate-180`,
|
|
36
|
+
className,
|
|
37
|
+
)}
|
|
38
|
+
captionLayout={captionLayout}
|
|
39
|
+
formatters={{
|
|
40
|
+
formatMonthDropdown: (date: Date) => date.toLocaleString("default", { month: "short" }),
|
|
41
|
+
...formatters,
|
|
42
|
+
}}
|
|
43
|
+
classNames={{
|
|
44
|
+
root: cn("w-fit", defaultClassNames.root),
|
|
45
|
+
months: cn("flex gap-4 flex-col md:flex-row relative", defaultClassNames.months),
|
|
46
|
+
month: cn("flex flex-col w-full gap-4", defaultClassNames.month),
|
|
47
|
+
nav: cn(
|
|
48
|
+
"flex items-center gap-1 w-full absolute top-0 inset-x-0 justify-between",
|
|
49
|
+
defaultClassNames.nav,
|
|
50
|
+
),
|
|
51
|
+
button_previous: cn(
|
|
52
|
+
buttonVariants({ variant: buttonVariant }),
|
|
53
|
+
"size-(--cell-size) aria-disabled:opacity-50 p-0 select-none",
|
|
54
|
+
defaultClassNames.button_previous,
|
|
55
|
+
),
|
|
56
|
+
button_next: cn(
|
|
57
|
+
buttonVariants({ variant: buttonVariant }),
|
|
58
|
+
"size-(--cell-size) aria-disabled:opacity-50 p-0 select-none",
|
|
59
|
+
defaultClassNames.button_next,
|
|
60
|
+
),
|
|
61
|
+
month_caption: cn(
|
|
62
|
+
"flex items-center justify-center h-(--cell-size) w-full px-(--cell-size)",
|
|
63
|
+
defaultClassNames.month_caption,
|
|
64
|
+
),
|
|
65
|
+
dropdowns: cn(
|
|
66
|
+
"w-full flex items-center text-sm font-medium justify-center h-(--cell-size) gap-1.5",
|
|
67
|
+
defaultClassNames.dropdowns,
|
|
68
|
+
),
|
|
69
|
+
dropdown_root: cn(
|
|
70
|
+
"relative has-focus:border-ring border border-input shadow-xs has-focus:ring-ring/50 has-focus:ring-[3px] rounded-md",
|
|
71
|
+
defaultClassNames.dropdown_root,
|
|
72
|
+
),
|
|
73
|
+
dropdown: cn("absolute inset-0 opacity-0", defaultClassNames.dropdown),
|
|
74
|
+
caption_label: cn(
|
|
75
|
+
"select-none font-medium",
|
|
76
|
+
captionLayout === "label"
|
|
77
|
+
? "text-sm"
|
|
78
|
+
: "rounded-md pl-2 pr-1 flex items-center gap-1 text-sm h-8 [&>svg]:text-muted-foreground [&>svg]:size-3.5",
|
|
79
|
+
defaultClassNames.caption_label,
|
|
80
|
+
),
|
|
81
|
+
table: "w-full border-collapse",
|
|
82
|
+
weekdays: cn("flex", defaultClassNames.weekdays),
|
|
83
|
+
weekday: cn(
|
|
84
|
+
"text-muted-foreground rounded-md flex-1 font-normal text-[0.8rem] select-none",
|
|
85
|
+
defaultClassNames.weekday,
|
|
86
|
+
),
|
|
87
|
+
week: cn("flex w-full mt-2", defaultClassNames.week),
|
|
88
|
+
week_number_header: cn("select-none w-(--cell-size)", defaultClassNames.week_number_header),
|
|
89
|
+
week_number: cn(
|
|
90
|
+
"text-[0.8rem] select-none text-muted-foreground",
|
|
91
|
+
defaultClassNames.week_number,
|
|
92
|
+
),
|
|
93
|
+
day: cn(
|
|
94
|
+
"relative w-full h-full p-0 text-center [&:first-child[data-selected=true]_button]:rounded-l-md [&:last-child[data-selected=true]_button]:rounded-r-md group/day aspect-square select-none",
|
|
95
|
+
defaultClassNames.day,
|
|
96
|
+
),
|
|
97
|
+
range_start: cn("rounded-l-md bg-accent", defaultClassNames.range_start),
|
|
98
|
+
range_middle: cn("rounded-none", defaultClassNames.range_middle),
|
|
99
|
+
range_end: cn("rounded-r-md bg-accent", defaultClassNames.range_end),
|
|
100
|
+
today: cn(
|
|
101
|
+
"bg-accent text-accent-foreground rounded-md data-[selected=true]:rounded-none",
|
|
102
|
+
defaultClassNames.today,
|
|
103
|
+
),
|
|
104
|
+
outside: cn(
|
|
105
|
+
"text-muted-foreground aria-selected:text-muted-foreground",
|
|
106
|
+
defaultClassNames.outside,
|
|
107
|
+
),
|
|
108
|
+
disabled: cn("text-muted-foreground opacity-50", defaultClassNames.disabled),
|
|
109
|
+
hidden: cn("invisible", defaultClassNames.hidden),
|
|
110
|
+
...classNames,
|
|
111
|
+
}}
|
|
112
|
+
components={{
|
|
113
|
+
Root: ({
|
|
114
|
+
className,
|
|
115
|
+
rootRef,
|
|
116
|
+
...props
|
|
117
|
+
}: {
|
|
118
|
+
className?: string;
|
|
119
|
+
rootRef?: React.Ref<HTMLDivElement>;
|
|
120
|
+
}) => {
|
|
121
|
+
return <div data-slot="calendar" ref={rootRef} className={cn(className)} {...props} />;
|
|
122
|
+
},
|
|
123
|
+
Chevron: ({
|
|
124
|
+
className,
|
|
125
|
+
orientation,
|
|
126
|
+
...props
|
|
127
|
+
}: {
|
|
128
|
+
className?: string;
|
|
129
|
+
orientation?: "left" | "right" | "down";
|
|
130
|
+
}) => {
|
|
131
|
+
if (orientation === "left") {
|
|
132
|
+
return <ChevronLeftIcon className={cn("size-4", className)} {...props} />;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (orientation === "right") {
|
|
136
|
+
return <ChevronRightIcon className={cn("size-4", className)} {...props} />;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return <ChevronDownIcon className={cn("size-4", className)} {...props} />;
|
|
140
|
+
},
|
|
141
|
+
DayButton: CalendarDayButton,
|
|
142
|
+
WeekNumber: ({ children, ...props }: { children: React.ReactNode }) => {
|
|
143
|
+
return (
|
|
144
|
+
<td {...props}>
|
|
145
|
+
<div className="flex size-(--cell-size) items-center justify-center text-center">
|
|
146
|
+
{children}
|
|
147
|
+
</div>
|
|
148
|
+
</td>
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
...components,
|
|
152
|
+
}}
|
|
153
|
+
{...props}
|
|
154
|
+
/>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
Calendar.displayName = displayName;
|
|
158
|
+
|
|
159
|
+
function CalendarDayButton({
|
|
160
|
+
className,
|
|
161
|
+
day,
|
|
162
|
+
modifiers,
|
|
163
|
+
...props
|
|
164
|
+
}: React.ComponentProps<typeof DayButton>) {
|
|
165
|
+
const { [displayNameDayButton]: Component = DefaultComponent } = useStackShiftUIComponents();
|
|
166
|
+
const defaultClassNames = getDefaultClassNames();
|
|
167
|
+
|
|
168
|
+
const ref = React.useRef<HTMLButtonElement>(null);
|
|
169
|
+
React.useEffect(() => {
|
|
170
|
+
if (modifiers.focused) ref.current?.focus();
|
|
171
|
+
}, [modifiers.focused]);
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
<Component
|
|
175
|
+
as={Button}
|
|
176
|
+
ref={ref}
|
|
177
|
+
variant="ghost"
|
|
178
|
+
size="icon"
|
|
179
|
+
data-day={day.date.toLocaleDateString()}
|
|
180
|
+
data-selected-single={
|
|
181
|
+
modifiers.selected &&
|
|
182
|
+
!modifiers.range_start &&
|
|
183
|
+
!modifiers.range_end &&
|
|
184
|
+
!modifiers.range_middle
|
|
185
|
+
}
|
|
186
|
+
data-range-start={modifiers.range_start}
|
|
187
|
+
data-range-end={modifiers.range_end}
|
|
188
|
+
data-range-middle={modifiers.range_middle}
|
|
189
|
+
className={cn(
|
|
190
|
+
"data-[selected-single=true]:bg-primary data-[selected-single=true]:text-primary-foreground data-[range-middle=true]:bg-accent data-[range-middle=true]:text-accent-foreground data-[range-start=true]:bg-primary data-[range-start=true]:text-primary-foreground data-[range-end=true]:bg-primary data-[range-end=true]:text-primary-foreground group-data-[focused=true]/day:border-ring group-data-[focused=true]/day:ring-ring/50 dark:hover:text-accent-foreground flex aspect-square size-auto w-full min-w-(--cell-size) flex-col gap-1 leading-none font-normal group-data-[focused=true]/day:relative group-data-[focused=true]/day:z-10 group-data-[focused=true]/day:ring-[3px] data-[range-end=true]:rounded-md data-[range-end=true]:rounded-r-md data-[range-middle=true]:rounded-none data-[range-start=true]:rounded-md data-[range-start=true]:rounded-l-md [&>span]:text-xs [&>span]:opacity-70",
|
|
191
|
+
defaultClassNames.day,
|
|
192
|
+
className,
|
|
193
|
+
)}
|
|
194
|
+
{...props}
|
|
195
|
+
/>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
CalendarDayButton.displayName = displayNameDayButton;
|
|
199
|
+
|
|
200
|
+
export { Calendar, CalendarDayButton };
|
package/src/index.ts
ADDED