@lichta/react 2.0.0
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 +21 -0
- package/README.md +83 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +210 -0
- package/dist/index.mjs +173 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zeforc Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# @lichta/react
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@lichta/react)
|
|
4
|
+
|
|
5
|
+
Component `Calendar` (lịch tháng có ngày âm lịch) cho React, đóng gói trên [`@lichta/core`](https://www.npmjs.com/package/@lichta/core).
|
|
6
|
+
|
|
7
|
+
## Cài đặt
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @lichta/react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@lichta/core` được cài kèm tự động (dependency thường), nhưng bạn cần cài `react`/`react-dom` (peer dependency, `>=18.0.0`) nếu dự án chưa có.
|
|
14
|
+
|
|
15
|
+
## Sử dụng
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { Calendar } from '@lichta/react';
|
|
19
|
+
import '@lichta/core/styles/calendar-base.css';
|
|
20
|
+
import '@lichta/core/styles/calendar-glass.css'; // theme "glass" (tùy chọn)
|
|
21
|
+
|
|
22
|
+
export default function App() {
|
|
23
|
+
return (
|
|
24
|
+
<Calendar
|
|
25
|
+
theme="glass"
|
|
26
|
+
locale="vi"
|
|
27
|
+
onSelect={(date, lunar) => console.log(date, lunar)}
|
|
28
|
+
/>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
> ⚠️ `Calendar` chỉ export class CSS (`lich-ta-calendar`, ...) — bạn **phải tự import** file CSS ở trên (hoặc CSS tương đương của riêng bạn), nếu không lịch sẽ hiển thị không có style.
|
|
34
|
+
|
|
35
|
+
### Custom render cho mỗi ô ngày
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
<Calendar
|
|
39
|
+
renderDay={(cell) => (
|
|
40
|
+
<div>
|
|
41
|
+
<span>{cell.solar.getDate()}</span>
|
|
42
|
+
{cell.isCurrentMonth && <span>{cell.lunar.day}</span>}
|
|
43
|
+
</div>
|
|
44
|
+
)}
|
|
45
|
+
/>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`cell` có kiểu `DayCellData`: `{ solar: Date, lunar: LunarDate, isToday: boolean, isSelected: boolean, isCurrentMonth: boolean }`.
|
|
49
|
+
|
|
50
|
+
## Props
|
|
51
|
+
|
|
52
|
+
| Prop | Kiểu | Mặc định | Mô tả |
|
|
53
|
+
|---|---|---|---|
|
|
54
|
+
| `month` | `number` | Tháng hiện tại | Tháng hiển thị ban đầu (1–12) |
|
|
55
|
+
| `year` | `number` | Năm hiện tại | Năm hiển thị ban đầu |
|
|
56
|
+
| `showLunar` | `boolean` | `true` | Hiện ngày âm lịch dưới ngày dương |
|
|
57
|
+
| `locale` | `'vi' \| 'en' \| 'ja' \| 'ko'` | `'vi'` | Ngôn ngữ nhãn thứ trong tuần |
|
|
58
|
+
| `theme` | `'classic' \| 'glass'` | `'classic'` | Giao diện lịch |
|
|
59
|
+
| `className` | `string` | `''` | Class CSS bổ sung cho container |
|
|
60
|
+
| `onSelect` | `(date: Date, lunar: LunarDate) => void` | — | Callback khi chọn ngày |
|
|
61
|
+
| `renderDay` | `(cell: DayCellData) => React.ReactNode` | — | Custom render cho mỗi ô ngày |
|
|
62
|
+
| `children` | `React.ReactNode` | — | Nội dung footer tùy chỉnh |
|
|
63
|
+
|
|
64
|
+
> Lưu ý: `month`/`year` hiện chỉ dùng làm giá trị khởi tạo state nội bộ (uncontrolled) — thay đổi prop sau khi mount chưa tự động cập nhật lại lịch đang hiển thị.
|
|
65
|
+
|
|
66
|
+
## Theming
|
|
67
|
+
|
|
68
|
+
```css
|
|
69
|
+
:root {
|
|
70
|
+
--lichta-primary: #d4a373;
|
|
71
|
+
--lichta-bg: #fffcf7;
|
|
72
|
+
--lichta-text: #2c1810;
|
|
73
|
+
--lichta-today-bg: #d4a373;
|
|
74
|
+
--lichta-selected-bg: #a0522d;
|
|
75
|
+
--lichta-lunar-text: #b08968;
|
|
76
|
+
--lichta-radius: 8px;
|
|
77
|
+
--lichta-font: 'Inter', sans-serif;
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
[MIT](./LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { LunarDate, Locale } from '@lichta/core';
|
|
3
|
+
|
|
4
|
+
interface CalendarProps {
|
|
5
|
+
month?: number;
|
|
6
|
+
year?: number;
|
|
7
|
+
onSelect?: (date: Date, lunar: LunarDate) => void;
|
|
8
|
+
showLunar?: boolean;
|
|
9
|
+
locale?: Locale;
|
|
10
|
+
theme?: 'classic' | 'glass';
|
|
11
|
+
className?: string;
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
renderDay?: (cell: DayCellData) => React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
interface DayCellData {
|
|
16
|
+
solar: Date;
|
|
17
|
+
lunar: LunarDate;
|
|
18
|
+
isToday: boolean;
|
|
19
|
+
isSelected: boolean;
|
|
20
|
+
isCurrentMonth: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare const Calendar: React.FC<CalendarProps>;
|
|
23
|
+
|
|
24
|
+
export { Calendar, type CalendarProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { LunarDate, Locale } from '@lichta/core';
|
|
3
|
+
|
|
4
|
+
interface CalendarProps {
|
|
5
|
+
month?: number;
|
|
6
|
+
year?: number;
|
|
7
|
+
onSelect?: (date: Date, lunar: LunarDate) => void;
|
|
8
|
+
showLunar?: boolean;
|
|
9
|
+
locale?: Locale;
|
|
10
|
+
theme?: 'classic' | 'glass';
|
|
11
|
+
className?: string;
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
renderDay?: (cell: DayCellData) => React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
interface DayCellData {
|
|
16
|
+
solar: Date;
|
|
17
|
+
lunar: LunarDate;
|
|
18
|
+
isToday: boolean;
|
|
19
|
+
isSelected: boolean;
|
|
20
|
+
isCurrentMonth: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare const Calendar: React.FC<CalendarProps>;
|
|
23
|
+
|
|
24
|
+
export { Calendar, type CalendarProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Calendar: () => Calendar
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/Calendar.tsx
|
|
38
|
+
var import_react = __toESM(require("react"));
|
|
39
|
+
var import_core = require("@lichta/core");
|
|
40
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
41
|
+
var monthNames = [
|
|
42
|
+
"Th\xE1ng 1",
|
|
43
|
+
"Th\xE1ng 2",
|
|
44
|
+
"Th\xE1ng 3",
|
|
45
|
+
"Th\xE1ng 4",
|
|
46
|
+
"Th\xE1ng 5",
|
|
47
|
+
"Th\xE1ng 6",
|
|
48
|
+
"Th\xE1ng 7",
|
|
49
|
+
"Th\xE1ng 8",
|
|
50
|
+
"Th\xE1ng 9",
|
|
51
|
+
"Th\xE1ng 10",
|
|
52
|
+
"Th\xE1ng 11",
|
|
53
|
+
"Th\xE1ng 12"
|
|
54
|
+
];
|
|
55
|
+
var weekDayLabelsByLocale = {
|
|
56
|
+
vi: ["CN", "T2", "T3", "T4", "T5", "T6", "T7"],
|
|
57
|
+
en: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
58
|
+
ja: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
59
|
+
ko: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
|
|
60
|
+
};
|
|
61
|
+
var Calendar = ({
|
|
62
|
+
month = (/* @__PURE__ */ new Date()).getMonth() + 1,
|
|
63
|
+
year = (/* @__PURE__ */ new Date()).getFullYear(),
|
|
64
|
+
onSelect,
|
|
65
|
+
showLunar = true,
|
|
66
|
+
locale = "vi",
|
|
67
|
+
theme = "classic",
|
|
68
|
+
className = "",
|
|
69
|
+
children,
|
|
70
|
+
renderDay
|
|
71
|
+
}) => {
|
|
72
|
+
const [currentMonth, setCurrentMonth] = (0, import_react.useState)(month);
|
|
73
|
+
const [currentYear, setCurrentYear] = (0, import_react.useState)(year);
|
|
74
|
+
const [selectedDate, setSelectedDate] = (0, import_react.useState)(null);
|
|
75
|
+
const yearInfo = (0, import_react.useMemo)(() => (0, import_core.getYearDetails)(currentYear), [currentYear]);
|
|
76
|
+
const weekDayLabels = weekDayLabelsByLocale[locale];
|
|
77
|
+
const calendarGrid = (0, import_react.useMemo)(() => {
|
|
78
|
+
const grid = [];
|
|
79
|
+
const firstDay = new Date(currentYear, currentMonth - 1, 1);
|
|
80
|
+
const lastDay = new Date(currentYear, currentMonth, 0);
|
|
81
|
+
const startingDayOfWeek = firstDay.getDay();
|
|
82
|
+
const daysInMonth = lastDay.getDate();
|
|
83
|
+
const today = /* @__PURE__ */ new Date();
|
|
84
|
+
const todayStr = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
|
|
85
|
+
let selStr = "";
|
|
86
|
+
if (selectedDate) {
|
|
87
|
+
selStr = `${selectedDate.getFullYear()}-${selectedDate.getMonth() + 1}-${selectedDate.getDate()}`;
|
|
88
|
+
}
|
|
89
|
+
const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0).getDate();
|
|
90
|
+
for (let i = startingDayOfWeek - 1; i >= 0; i--) {
|
|
91
|
+
const d = prevMonthLastDay - i;
|
|
92
|
+
const m = currentMonth - 1 < 1 ? 12 : currentMonth - 1;
|
|
93
|
+
const y = currentMonth - 1 < 1 ? currentYear - 1 : currentYear;
|
|
94
|
+
const solar = new Date(y, m - 1, d);
|
|
95
|
+
const lunar = import_core.LichTa.toLunar(d, m, y);
|
|
96
|
+
const dateStr = `${y}-${m}-${d}`;
|
|
97
|
+
grid.push({
|
|
98
|
+
solar,
|
|
99
|
+
lunar,
|
|
100
|
+
isToday: dateStr === todayStr,
|
|
101
|
+
isSelected: dateStr === selStr,
|
|
102
|
+
isCurrentMonth: false
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
for (let d = 1; d <= daysInMonth; d++) {
|
|
106
|
+
const solar = new Date(currentYear, currentMonth - 1, d);
|
|
107
|
+
const lunar = import_core.LichTa.toLunar(d, currentMonth, currentYear);
|
|
108
|
+
const dateStr = `${currentYear}-${currentMonth}-${d}`;
|
|
109
|
+
grid.push({
|
|
110
|
+
solar,
|
|
111
|
+
lunar,
|
|
112
|
+
isToday: dateStr === todayStr,
|
|
113
|
+
isSelected: dateStr === selStr,
|
|
114
|
+
isCurrentMonth: true
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const remaining = 42 - grid.length;
|
|
118
|
+
for (let d = 1; d <= remaining; d++) {
|
|
119
|
+
const m = currentMonth + 1 > 12 ? 1 : currentMonth + 1;
|
|
120
|
+
const y = currentMonth + 1 > 12 ? currentYear + 1 : currentYear;
|
|
121
|
+
const solar = new Date(y, m - 1, d);
|
|
122
|
+
const lunar = import_core.LichTa.toLunar(d, m, y);
|
|
123
|
+
const dateStr = `${y}-${m}-${d}`;
|
|
124
|
+
grid.push({
|
|
125
|
+
solar,
|
|
126
|
+
lunar,
|
|
127
|
+
isToday: dateStr === todayStr,
|
|
128
|
+
isSelected: dateStr === selStr,
|
|
129
|
+
isCurrentMonth: false
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return grid;
|
|
133
|
+
}, [currentMonth, currentYear, selectedDate]);
|
|
134
|
+
const prevMonth = () => {
|
|
135
|
+
if (currentMonth === 1) {
|
|
136
|
+
setCurrentMonth(12);
|
|
137
|
+
setCurrentYear((y) => y - 1);
|
|
138
|
+
} else {
|
|
139
|
+
setCurrentMonth((m) => m - 1);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const nextMonth = () => {
|
|
143
|
+
if (currentMonth === 12) {
|
|
144
|
+
setCurrentMonth(1);
|
|
145
|
+
setCurrentYear((y) => y + 1);
|
|
146
|
+
} else {
|
|
147
|
+
setCurrentMonth((m) => m + 1);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const handleDayClick = (cell) => {
|
|
151
|
+
setSelectedDate(cell.solar);
|
|
152
|
+
if (onSelect) {
|
|
153
|
+
onSelect(cell.solar, cell.lunar);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const handleKeydown = (event, cell) => {
|
|
157
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
handleDayClick(cell);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: `lich-ta-calendar lichta-theme-${theme} ${className}`, children: [
|
|
163
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "lich-ta-calendar-header", children: [
|
|
164
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "lich-ta-calendar-nav", onClick: prevMonth, "aria-label": "Th\xE1ng tr\u01B0\u1EDBc", children: "\u25C0" }),
|
|
165
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "lich-ta-calendar-title", children: [
|
|
166
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "lich-ta-calendar-month-year", children: [
|
|
167
|
+
monthNames[currentMonth - 1],
|
|
168
|
+
", ",
|
|
169
|
+
currentYear
|
|
170
|
+
] }),
|
|
171
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "lich-ta-calendar-canchi", children: [
|
|
172
|
+
yearInfo.can,
|
|
173
|
+
" ",
|
|
174
|
+
yearInfo.chi
|
|
175
|
+
] })
|
|
176
|
+
] }),
|
|
177
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "lich-ta-calendar-nav", onClick: nextMonth, "aria-label": "Th\xE1ng sau", children: "\u25B6" })
|
|
178
|
+
] }),
|
|
179
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "lich-ta-calendar-weekdays", children: weekDayLabels.map((label) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "lich-ta-calendar-weekday", children: label }, label)) }),
|
|
180
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "lich-ta-calendar-grid", children: calendarGrid.map((cell, idx) => {
|
|
181
|
+
if (renderDay) {
|
|
182
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.default.Fragment, { children: renderDay(cell) }, idx);
|
|
183
|
+
}
|
|
184
|
+
let classes = "lich-ta-calendar-day";
|
|
185
|
+
if (cell.isToday) classes += " is-today";
|
|
186
|
+
if (cell.isSelected) classes += " is-selected";
|
|
187
|
+
if (!cell.isCurrentMonth) classes += " is-other-month";
|
|
188
|
+
if (cell.lunar.day === 1) classes += " is-first-lunar";
|
|
189
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
190
|
+
"button",
|
|
191
|
+
{
|
|
192
|
+
className: classes,
|
|
193
|
+
onClick: () => handleDayClick(cell),
|
|
194
|
+
onKeyDown: (e) => handleKeydown(e, cell),
|
|
195
|
+
tabIndex: cell.isCurrentMonth ? 0 : -1,
|
|
196
|
+
children: [
|
|
197
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "solar-day", children: cell.solar.getDate() }),
|
|
198
|
+
showLunar && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "lunar-day", children: cell.lunar.day === 1 ? `${cell.lunar.day}/${cell.lunar.month}${cell.lunar.isLeap ? "*" : ""}` : cell.lunar.day })
|
|
199
|
+
]
|
|
200
|
+
},
|
|
201
|
+
idx
|
|
202
|
+
);
|
|
203
|
+
}) }),
|
|
204
|
+
children && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "lich-ta-calendar-footer", children })
|
|
205
|
+
] });
|
|
206
|
+
};
|
|
207
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
208
|
+
0 && (module.exports = {
|
|
209
|
+
Calendar
|
|
210
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// src/Calendar.tsx
|
|
2
|
+
import React, { useState, useMemo } from "react";
|
|
3
|
+
import { LichTa, getYearDetails } from "@lichta/core";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
var monthNames = [
|
|
6
|
+
"Th\xE1ng 1",
|
|
7
|
+
"Th\xE1ng 2",
|
|
8
|
+
"Th\xE1ng 3",
|
|
9
|
+
"Th\xE1ng 4",
|
|
10
|
+
"Th\xE1ng 5",
|
|
11
|
+
"Th\xE1ng 6",
|
|
12
|
+
"Th\xE1ng 7",
|
|
13
|
+
"Th\xE1ng 8",
|
|
14
|
+
"Th\xE1ng 9",
|
|
15
|
+
"Th\xE1ng 10",
|
|
16
|
+
"Th\xE1ng 11",
|
|
17
|
+
"Th\xE1ng 12"
|
|
18
|
+
];
|
|
19
|
+
var weekDayLabelsByLocale = {
|
|
20
|
+
vi: ["CN", "T2", "T3", "T4", "T5", "T6", "T7"],
|
|
21
|
+
en: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
22
|
+
ja: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
|
23
|
+
ko: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
|
|
24
|
+
};
|
|
25
|
+
var Calendar = ({
|
|
26
|
+
month = (/* @__PURE__ */ new Date()).getMonth() + 1,
|
|
27
|
+
year = (/* @__PURE__ */ new Date()).getFullYear(),
|
|
28
|
+
onSelect,
|
|
29
|
+
showLunar = true,
|
|
30
|
+
locale = "vi",
|
|
31
|
+
theme = "classic",
|
|
32
|
+
className = "",
|
|
33
|
+
children,
|
|
34
|
+
renderDay
|
|
35
|
+
}) => {
|
|
36
|
+
const [currentMonth, setCurrentMonth] = useState(month);
|
|
37
|
+
const [currentYear, setCurrentYear] = useState(year);
|
|
38
|
+
const [selectedDate, setSelectedDate] = useState(null);
|
|
39
|
+
const yearInfo = useMemo(() => getYearDetails(currentYear), [currentYear]);
|
|
40
|
+
const weekDayLabels = weekDayLabelsByLocale[locale];
|
|
41
|
+
const calendarGrid = useMemo(() => {
|
|
42
|
+
const grid = [];
|
|
43
|
+
const firstDay = new Date(currentYear, currentMonth - 1, 1);
|
|
44
|
+
const lastDay = new Date(currentYear, currentMonth, 0);
|
|
45
|
+
const startingDayOfWeek = firstDay.getDay();
|
|
46
|
+
const daysInMonth = lastDay.getDate();
|
|
47
|
+
const today = /* @__PURE__ */ new Date();
|
|
48
|
+
const todayStr = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
|
|
49
|
+
let selStr = "";
|
|
50
|
+
if (selectedDate) {
|
|
51
|
+
selStr = `${selectedDate.getFullYear()}-${selectedDate.getMonth() + 1}-${selectedDate.getDate()}`;
|
|
52
|
+
}
|
|
53
|
+
const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0).getDate();
|
|
54
|
+
for (let i = startingDayOfWeek - 1; i >= 0; i--) {
|
|
55
|
+
const d = prevMonthLastDay - i;
|
|
56
|
+
const m = currentMonth - 1 < 1 ? 12 : currentMonth - 1;
|
|
57
|
+
const y = currentMonth - 1 < 1 ? currentYear - 1 : currentYear;
|
|
58
|
+
const solar = new Date(y, m - 1, d);
|
|
59
|
+
const lunar = LichTa.toLunar(d, m, y);
|
|
60
|
+
const dateStr = `${y}-${m}-${d}`;
|
|
61
|
+
grid.push({
|
|
62
|
+
solar,
|
|
63
|
+
lunar,
|
|
64
|
+
isToday: dateStr === todayStr,
|
|
65
|
+
isSelected: dateStr === selStr,
|
|
66
|
+
isCurrentMonth: false
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
for (let d = 1; d <= daysInMonth; d++) {
|
|
70
|
+
const solar = new Date(currentYear, currentMonth - 1, d);
|
|
71
|
+
const lunar = LichTa.toLunar(d, currentMonth, currentYear);
|
|
72
|
+
const dateStr = `${currentYear}-${currentMonth}-${d}`;
|
|
73
|
+
grid.push({
|
|
74
|
+
solar,
|
|
75
|
+
lunar,
|
|
76
|
+
isToday: dateStr === todayStr,
|
|
77
|
+
isSelected: dateStr === selStr,
|
|
78
|
+
isCurrentMonth: true
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const remaining = 42 - grid.length;
|
|
82
|
+
for (let d = 1; d <= remaining; d++) {
|
|
83
|
+
const m = currentMonth + 1 > 12 ? 1 : currentMonth + 1;
|
|
84
|
+
const y = currentMonth + 1 > 12 ? currentYear + 1 : currentYear;
|
|
85
|
+
const solar = new Date(y, m - 1, d);
|
|
86
|
+
const lunar = LichTa.toLunar(d, m, y);
|
|
87
|
+
const dateStr = `${y}-${m}-${d}`;
|
|
88
|
+
grid.push({
|
|
89
|
+
solar,
|
|
90
|
+
lunar,
|
|
91
|
+
isToday: dateStr === todayStr,
|
|
92
|
+
isSelected: dateStr === selStr,
|
|
93
|
+
isCurrentMonth: false
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return grid;
|
|
97
|
+
}, [currentMonth, currentYear, selectedDate]);
|
|
98
|
+
const prevMonth = () => {
|
|
99
|
+
if (currentMonth === 1) {
|
|
100
|
+
setCurrentMonth(12);
|
|
101
|
+
setCurrentYear((y) => y - 1);
|
|
102
|
+
} else {
|
|
103
|
+
setCurrentMonth((m) => m - 1);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const nextMonth = () => {
|
|
107
|
+
if (currentMonth === 12) {
|
|
108
|
+
setCurrentMonth(1);
|
|
109
|
+
setCurrentYear((y) => y + 1);
|
|
110
|
+
} else {
|
|
111
|
+
setCurrentMonth((m) => m + 1);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const handleDayClick = (cell) => {
|
|
115
|
+
setSelectedDate(cell.solar);
|
|
116
|
+
if (onSelect) {
|
|
117
|
+
onSelect(cell.solar, cell.lunar);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const handleKeydown = (event, cell) => {
|
|
121
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
handleDayClick(cell);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
return /* @__PURE__ */ jsxs("div", { className: `lich-ta-calendar lichta-theme-${theme} ${className}`, children: [
|
|
127
|
+
/* @__PURE__ */ jsxs("div", { className: "lich-ta-calendar-header", children: [
|
|
128
|
+
/* @__PURE__ */ jsx("button", { className: "lich-ta-calendar-nav", onClick: prevMonth, "aria-label": "Th\xE1ng tr\u01B0\u1EDBc", children: "\u25C0" }),
|
|
129
|
+
/* @__PURE__ */ jsxs("div", { className: "lich-ta-calendar-title", children: [
|
|
130
|
+
/* @__PURE__ */ jsxs("span", { className: "lich-ta-calendar-month-year", children: [
|
|
131
|
+
monthNames[currentMonth - 1],
|
|
132
|
+
", ",
|
|
133
|
+
currentYear
|
|
134
|
+
] }),
|
|
135
|
+
/* @__PURE__ */ jsxs("span", { className: "lich-ta-calendar-canchi", children: [
|
|
136
|
+
yearInfo.can,
|
|
137
|
+
" ",
|
|
138
|
+
yearInfo.chi
|
|
139
|
+
] })
|
|
140
|
+
] }),
|
|
141
|
+
/* @__PURE__ */ jsx("button", { className: "lich-ta-calendar-nav", onClick: nextMonth, "aria-label": "Th\xE1ng sau", children: "\u25B6" })
|
|
142
|
+
] }),
|
|
143
|
+
/* @__PURE__ */ jsx("div", { className: "lich-ta-calendar-weekdays", children: weekDayLabels.map((label) => /* @__PURE__ */ jsx("div", { className: "lich-ta-calendar-weekday", children: label }, label)) }),
|
|
144
|
+
/* @__PURE__ */ jsx("div", { className: "lich-ta-calendar-grid", children: calendarGrid.map((cell, idx) => {
|
|
145
|
+
if (renderDay) {
|
|
146
|
+
return /* @__PURE__ */ jsx(React.Fragment, { children: renderDay(cell) }, idx);
|
|
147
|
+
}
|
|
148
|
+
let classes = "lich-ta-calendar-day";
|
|
149
|
+
if (cell.isToday) classes += " is-today";
|
|
150
|
+
if (cell.isSelected) classes += " is-selected";
|
|
151
|
+
if (!cell.isCurrentMonth) classes += " is-other-month";
|
|
152
|
+
if (cell.lunar.day === 1) classes += " is-first-lunar";
|
|
153
|
+
return /* @__PURE__ */ jsxs(
|
|
154
|
+
"button",
|
|
155
|
+
{
|
|
156
|
+
className: classes,
|
|
157
|
+
onClick: () => handleDayClick(cell),
|
|
158
|
+
onKeyDown: (e) => handleKeydown(e, cell),
|
|
159
|
+
tabIndex: cell.isCurrentMonth ? 0 : -1,
|
|
160
|
+
children: [
|
|
161
|
+
/* @__PURE__ */ jsx("span", { className: "solar-day", children: cell.solar.getDate() }),
|
|
162
|
+
showLunar && /* @__PURE__ */ jsx("span", { className: "lunar-day", children: cell.lunar.day === 1 ? `${cell.lunar.day}/${cell.lunar.month}${cell.lunar.isLeap ? "*" : ""}` : cell.lunar.day })
|
|
163
|
+
]
|
|
164
|
+
},
|
|
165
|
+
idx
|
|
166
|
+
);
|
|
167
|
+
}) }),
|
|
168
|
+
children && /* @__PURE__ */ jsx("div", { className: "lich-ta-calendar-footer", children })
|
|
169
|
+
] });
|
|
170
|
+
};
|
|
171
|
+
export {
|
|
172
|
+
Calendar
|
|
173
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lichta/react",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "React components for LichTa",
|
|
5
|
+
"author": "Zeforc Labs | Stridev",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.mjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@lichta/core": "2.0.0"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"react": ">=18.0.0",
|
|
19
|
+
"react-dom": ">=18.0.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/react": "^18.3.18",
|
|
23
|
+
"@types/react-dom": "^18.3.5",
|
|
24
|
+
"react": "^19.0.0",
|
|
25
|
+
"react-dom": "^19.0.0",
|
|
26
|
+
"tsup": "^8.3.6",
|
|
27
|
+
"typescript": "^5.7.3"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup",
|
|
31
|
+
"dev": "tsup --watch",
|
|
32
|
+
"lint": "eslint src/**/*.tsx",
|
|
33
|
+
"clean": "rm -rf dist"
|
|
34
|
+
}
|
|
35
|
+
}
|