@sanguo-dev/calendar-ui 0.0.2 → 0.0.3
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/README.md +194 -194
- package/package.json +61 -61
- package/src/calendar.ts +785 -785
- package/src/components/day.ts +79 -79
- package/src/components/grid.ts +181 -181
- package/src/components/header.ts +69 -69
- package/src/components/nav.ts +20 -20
- package/src/components/weekdays.ts +19 -19
- package/src/index.ts +26 -26
- package/src/locales/en-US.ts +28 -28
- package/src/locales/index.ts +19 -19
- package/src/locales/zh-CN.ts +13 -13
- package/src/style.css +388 -388
- package/src/types/calendar.ts +267 -267
- package/src/types/components.ts +69 -69
- package/src/types/index.ts +32 -32
- package/src/types/locale.ts +26 -26
- package/src/types/utils.ts +28 -28
- package/src/utils/date.ts +82 -82
- package/src/utils/days.ts +88 -88
- package/src/utils/dom.ts +11 -11
- package/src/utils/motion.ts +39 -39
package/src/components/nav.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import type { NavHandle, NavOptions } from "../types";
|
|
2
|
-
import { node } from "../utils/dom";
|
|
3
|
-
|
|
4
|
-
/** 头部左右切换按钮 */
|
|
5
|
-
export const createNav = function (options: NavOptions): NavHandle {
|
|
6
|
-
const el = node("button", "cui__nav", options.label);
|
|
7
|
-
el.type = "button";
|
|
8
|
-
el.setAttribute("aria-label", options.ariaLabel);
|
|
9
|
-
el.addEventListener("click", options.onClick);
|
|
10
|
-
|
|
11
|
-
return {
|
|
12
|
-
el,
|
|
13
|
-
setDisabled: (disabled: boolean) => {
|
|
14
|
-
el.disabled = disabled;
|
|
15
|
-
if (disabled) el.setAttribute("aria-disabled", "true");
|
|
16
|
-
else el.removeAttribute("aria-disabled");
|
|
17
|
-
},
|
|
18
|
-
destroy: () => el.removeEventListener("click", options.onClick),
|
|
19
|
-
};
|
|
20
|
-
};
|
|
1
|
+
import type { NavHandle, NavOptions } from "../types";
|
|
2
|
+
import { node } from "../utils/dom";
|
|
3
|
+
|
|
4
|
+
/** 头部左右切换按钮 */
|
|
5
|
+
export const createNav = function (options: NavOptions): NavHandle {
|
|
6
|
+
const el = node("button", "cui__nav", options.label);
|
|
7
|
+
el.type = "button";
|
|
8
|
+
el.setAttribute("aria-label", options.ariaLabel);
|
|
9
|
+
el.addEventListener("click", options.onClick);
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
el,
|
|
13
|
+
setDisabled: (disabled: boolean) => {
|
|
14
|
+
el.disabled = disabled;
|
|
15
|
+
if (disabled) el.setAttribute("aria-disabled", "true");
|
|
16
|
+
else el.removeAttribute("aria-disabled");
|
|
17
|
+
},
|
|
18
|
+
destroy: () => el.removeEventListener("click", options.onClick),
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import type { CalendarLocale, WeekStartsOn } from "../types";
|
|
2
|
-
import { node } from "../utils/dom";
|
|
3
|
-
|
|
4
|
-
/** 星期表头,按一周起始日轮转 */
|
|
5
|
-
export const createWeekdays = function (
|
|
6
|
-
weekStartsOn: WeekStartsOn,
|
|
7
|
-
locale: CalendarLocale,
|
|
8
|
-
): { el: HTMLElement } {
|
|
9
|
-
const el = node("div", "cui__weekdays");
|
|
10
|
-
|
|
11
|
-
for (let i = 0; i < 7; i++) {
|
|
12
|
-
const weekday = (weekStartsOn + i) % 7;
|
|
13
|
-
const cell = node("div", "cui__weekday", locale.weekdays[weekday]);
|
|
14
|
-
cell.title = locale.weekdaysLong[weekday] as string;
|
|
15
|
-
el.appendChild(cell);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return { el };
|
|
19
|
-
};
|
|
1
|
+
import type { CalendarLocale, WeekStartsOn } from "../types";
|
|
2
|
+
import { node } from "../utils/dom";
|
|
3
|
+
|
|
4
|
+
/** 星期表头,按一周起始日轮转 */
|
|
5
|
+
export const createWeekdays = function (
|
|
6
|
+
weekStartsOn: WeekStartsOn,
|
|
7
|
+
locale: CalendarLocale,
|
|
8
|
+
): { el: HTMLElement } {
|
|
9
|
+
const el = node("div", "cui__weekdays");
|
|
10
|
+
|
|
11
|
+
for (let i = 0; i < 7; i++) {
|
|
12
|
+
const weekday = (weekStartsOn + i) % 7;
|
|
13
|
+
const cell = node("div", "cui__weekday", locale.weekdays[weekday]);
|
|
14
|
+
cell.title = locale.weekdaysLong[weekday] as string;
|
|
15
|
+
el.appendChild(cell);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return { el };
|
|
19
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
export { createCalendarUI } from "./calendar";
|
|
2
|
-
export { enUS, locales, resolveLocale, zhCN } from "./locales";
|
|
3
|
-
export type {
|
|
4
|
-
CalendarAdapter,
|
|
5
|
-
CalendarAnimationOptions,
|
|
6
|
-
CalendarBadge,
|
|
7
|
-
CalendarDate,
|
|
8
|
-
CalendarDateInput,
|
|
9
|
-
CalendarDay,
|
|
10
|
-
CalendarInstance,
|
|
11
|
-
CalendarLocale,
|
|
12
|
-
CalendarLocaleName,
|
|
13
|
-
CalendarMode,
|
|
14
|
-
CalendarOptions,
|
|
15
|
-
CalendarRange,
|
|
16
|
-
CalendarTheme,
|
|
17
|
-
CollapseAnimationOptions,
|
|
18
|
-
DayDecoration,
|
|
19
|
-
HeaderRenderContext,
|
|
20
|
-
HeaderRenderer,
|
|
21
|
-
MonthInput,
|
|
22
|
-
SlideAnimationOptions,
|
|
23
|
-
SubtitleRenderer,
|
|
24
|
-
WeekStartsOn,
|
|
25
|
-
YearMonth,
|
|
26
|
-
} from "./types";
|
|
1
|
+
export { createCalendarUI } from "./calendar";
|
|
2
|
+
export { enUS, locales, resolveLocale, zhCN } from "./locales";
|
|
3
|
+
export type {
|
|
4
|
+
CalendarAdapter,
|
|
5
|
+
CalendarAnimationOptions,
|
|
6
|
+
CalendarBadge,
|
|
7
|
+
CalendarDate,
|
|
8
|
+
CalendarDateInput,
|
|
9
|
+
CalendarDay,
|
|
10
|
+
CalendarInstance,
|
|
11
|
+
CalendarLocale,
|
|
12
|
+
CalendarLocaleName,
|
|
13
|
+
CalendarMode,
|
|
14
|
+
CalendarOptions,
|
|
15
|
+
CalendarRange,
|
|
16
|
+
CalendarTheme,
|
|
17
|
+
CollapseAnimationOptions,
|
|
18
|
+
DayDecoration,
|
|
19
|
+
HeaderRenderContext,
|
|
20
|
+
HeaderRenderer,
|
|
21
|
+
MonthInput,
|
|
22
|
+
SlideAnimationOptions,
|
|
23
|
+
SubtitleRenderer,
|
|
24
|
+
WeekStartsOn,
|
|
25
|
+
YearMonth,
|
|
26
|
+
} from "./types";
|
package/src/locales/en-US.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import type { CalendarLocale } from "../types";
|
|
2
|
-
|
|
3
|
-
const MONTHS = [
|
|
4
|
-
"January",
|
|
5
|
-
"February",
|
|
6
|
-
"March",
|
|
7
|
-
"April",
|
|
8
|
-
"May",
|
|
9
|
-
"June",
|
|
10
|
-
"July",
|
|
11
|
-
"August",
|
|
12
|
-
"September",
|
|
13
|
-
"October",
|
|
14
|
-
"November",
|
|
15
|
-
"December",
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
/** 英文 */
|
|
19
|
-
export const enUS: CalendarLocale = {
|
|
20
|
-
name: "en-US",
|
|
21
|
-
weekdays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
22
|
-
weekdaysLong: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
|
23
|
-
monthTitle: (year, month) => `${MONTHS[month - 1] ?? month} ${year}`,
|
|
24
|
-
prevMonth: "Previous month",
|
|
25
|
-
nextMonth: "Next month",
|
|
26
|
-
collapse: "Collapse calendar",
|
|
27
|
-
expand: "Expand calendar",
|
|
28
|
-
};
|
|
1
|
+
import type { CalendarLocale } from "../types";
|
|
2
|
+
|
|
3
|
+
const MONTHS = [
|
|
4
|
+
"January",
|
|
5
|
+
"February",
|
|
6
|
+
"March",
|
|
7
|
+
"April",
|
|
8
|
+
"May",
|
|
9
|
+
"June",
|
|
10
|
+
"July",
|
|
11
|
+
"August",
|
|
12
|
+
"September",
|
|
13
|
+
"October",
|
|
14
|
+
"November",
|
|
15
|
+
"December",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
/** 英文 */
|
|
19
|
+
export const enUS: CalendarLocale = {
|
|
20
|
+
name: "en-US",
|
|
21
|
+
weekdays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
22
|
+
weekdaysLong: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
|
23
|
+
monthTitle: (year, month) => `${MONTHS[month - 1] ?? month} ${year}`,
|
|
24
|
+
prevMonth: "Previous month",
|
|
25
|
+
nextMonth: "Next month",
|
|
26
|
+
collapse: "Collapse calendar",
|
|
27
|
+
expand: "Expand calendar",
|
|
28
|
+
};
|
package/src/locales/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { enUS } from "./en-US";
|
|
2
|
-
import { zhCN } from "./zh-CN";
|
|
3
|
-
import type { CalendarLocale, CalendarLocaleName } from "../types";
|
|
4
|
-
|
|
5
|
-
/** 内置语言包,key 可直接传给 createCalendarUI 的 locale 选项 */
|
|
6
|
-
export const locales: Record<CalendarLocaleName, CalendarLocale> = {
|
|
7
|
-
"zh-CN": zhCN,
|
|
8
|
-
"en-US": enUS,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
/** 把 "zh-CN" 这类名称或自定义语言包,统一解析成语言包对象 */
|
|
12
|
-
export const resolveLocale = function (
|
|
13
|
-
locale: CalendarLocale | CalendarLocaleName = "zh-CN",
|
|
14
|
-
): CalendarLocale {
|
|
15
|
-
if (typeof locale !== "string") return locale;
|
|
16
|
-
return locales[locale];
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { zhCN, enUS };
|
|
1
|
+
import { enUS } from "./en-US";
|
|
2
|
+
import { zhCN } from "./zh-CN";
|
|
3
|
+
import type { CalendarLocale, CalendarLocaleName } from "../types";
|
|
4
|
+
|
|
5
|
+
/** 内置语言包,key 可直接传给 createCalendarUI 的 locale 选项 */
|
|
6
|
+
export const locales: Record<CalendarLocaleName, CalendarLocale> = {
|
|
7
|
+
"zh-CN": zhCN,
|
|
8
|
+
"en-US": enUS,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** 把 "zh-CN" 这类名称或自定义语言包,统一解析成语言包对象 */
|
|
12
|
+
export const resolveLocale = function (
|
|
13
|
+
locale: CalendarLocale | CalendarLocaleName = "zh-CN",
|
|
14
|
+
): CalendarLocale {
|
|
15
|
+
if (typeof locale !== "string") return locale;
|
|
16
|
+
return locales[locale];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { zhCN, enUS };
|
package/src/locales/zh-CN.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { CalendarLocale } from "../types";
|
|
2
|
-
|
|
3
|
-
/** 简体中文:默认语言 */
|
|
4
|
-
export const zhCN: CalendarLocale = {
|
|
5
|
-
name: "zh-CN",
|
|
6
|
-
weekdays: ["日", "一", "二", "三", "四", "五", "六"],
|
|
7
|
-
weekdaysLong: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
|
|
8
|
-
monthTitle: (year, month) => `${year} 年 ${month} 月`,
|
|
9
|
-
prevMonth: "上个月",
|
|
10
|
-
nextMonth: "下个月",
|
|
11
|
-
collapse: "收起日历",
|
|
12
|
-
expand: "展开日历",
|
|
13
|
-
};
|
|
1
|
+
import type { CalendarLocale } from "../types";
|
|
2
|
+
|
|
3
|
+
/** 简体中文:默认语言 */
|
|
4
|
+
export const zhCN: CalendarLocale = {
|
|
5
|
+
name: "zh-CN",
|
|
6
|
+
weekdays: ["日", "一", "二", "三", "四", "五", "六"],
|
|
7
|
+
weekdaysLong: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
|
|
8
|
+
monthTitle: (year, month) => `${year} 年 ${month} 月`,
|
|
9
|
+
prevMonth: "上个月",
|
|
10
|
+
nextMonth: "下个月",
|
|
11
|
+
collapse: "收起日历",
|
|
12
|
+
expand: "展开日历",
|
|
13
|
+
};
|