Skip to content

Commit

Permalink
feat: 日历支持节假日调休阴历显示
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangkx committed Aug 2, 2024
1 parent 6948a25 commit a964294
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>react-tempate</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.12.2/lottie.min.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.12.2/lottie.min.js"></script> -->
<script src="/lunar.min.js"></script>
</head>

Expand Down
46 changes: 34 additions & 12 deletions src/pages/calendar/components/monthItem.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import React from "react";
import { Fragment } from "react";
import clsx from "clsx"

export function MonthItem({ months }) {
// const mData = months.getMonth()
console.log(months, '8888')
function calcLunar(date) {
const luna = Lunar.fromDate(new Date(date))
return luna.getDayInChinese()
}
function calcFestival(w) {
const festivals = Solar.fromYmd(w.getYear(), w.getMonth(), w.getDay())
const result = festivals.getFestivals()
return result
}
function isWork(w) {
const holi = HolidayUtil.getHoliday(w.getYear(), w.getMonth(), w.getDay())
return holi?.isWork()
}
return <>
{
months.map((item, index) => <ul className="bg-gray-100 relative grid grid-cols-7 text-center z-20" key={index}>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
months.map((mon, index) => <ul className="bg-gray-50 relative grid grid-cols-7 auto-rows-max auto-cols-max text-center z-20 py-1" key={index}>
<li className="text-slate-900"></li>
<li className="text-slate-900"></li>
<li className="text-slate-900"></li>
<li className="text-slate-900"></li>
<li className="text-slate-900"></li>
<li className="text-slate-900"></li>
<li className="text-slate-900"></li>
<h2 className="absolute left-[50%] top-[50%] translate-x-[-50%] translate-y-[-50%] text-7xl text-gray-300 text-opacity-80 font-bold z-[-1]">{index + 1}</h2>
{item.map((it, idx) => <Fragment key={idx}>
{it.map((is, isx) => <li key={isx}>{is.getDay()}</li>)}
{mon.map((week, idx) => <Fragment key={idx}>
{week.map((w, isx) => <li key={isx}
className='relative h-10 my-2 box-content'>
<span className={clsx({ 'text-slate-900': w.getMonth() === index + 1, 'text-slate-400': w.getMonth() !== index + 1 })}>{w.getDay()}</span>
<i className={clsx({ 'w-[100%] text-clip overflow-hidden whitespace-nowrap absolute bottom-0.5 left-[50%] translate-x-[-50%] text-xs not-italic': true }
, { 'text-slate-400': w.getMonth() !== index + 1 }, { 'text-gray-500': w.getMonth() === index + 1 }, { 'text-rose-500': calcFestival(w).length > 0 })}>
{calcFestival(w).length > 0 ? calcFestival(w) : calcLunar(w.toString())}
</i>
<i className={clsx({ 'absolute top-0.5 right-2 fsizes-10': true }, { 'text-lime-500': isWork(w) === false })}>
{isWork(w) === false ? '休' : isWork(w) === true ? '班' : ''}
</i>
</li>)}
</Fragment>)}
</ul>)
}
Expand Down
12 changes: 5 additions & 7 deletions src/pages/calendar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import { useState } from "react";
export default function Calendar() {
const [months, setMonths] = useState([])
useMount(() => {
var d = SolarYear.fromDate(new Date());
var months = d.getMonths();
const d = SolarYear.fromDate(new Date());
const months = d.getMonths();
let monthList = []
for (let i = 0, j = months.length; i < j; i++) {
for (let i = 0; i < months.length; i++) {
const weeks = months[i].getWeeks()
monthList[i] = []
console.log(i, months.length, weeks.length)
for(let j = 0; j < weeks.length; j++) {
for (let j = 0; j < weeks.length; j++) {
monthList[i].push(weeks[j].getDays())
}
}
console.log(monthList, '8888999', months)
setMonths(monthList)
})
return <div className="grid grid-cols-3 w-[90%] mx-auto mt-5 mb-5 gap-4">
<MonthItem months={months}/>
<MonthItem months={months} />
</div>
}
2 changes: 1 addition & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
fsizes: createFontSize(12, 50),
fsizes: createFontSize(10, 50),
extend: {},
},
plugins: [
Expand Down

0 comments on commit a964294

Please sign in to comment.