mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-09-10 14:59:46 +08:00
feat(Calendar): disable outranged days
This commit is contained in:
parent
9f1e3b0fde
commit
e21455f856
@ -3,27 +3,6 @@ import { getScrollTop } from '../utils/dom/scroll';
|
|||||||
import { createComponent, bem, compareMonth, formatMonthTitle } from './utils';
|
import { createComponent, bem, compareMonth, formatMonthTitle } from './utils';
|
||||||
import Header from './Header';
|
import Header from './Header';
|
||||||
|
|
||||||
function getDays(date) {
|
|
||||||
const days = [];
|
|
||||||
const cursor = new Date(date);
|
|
||||||
const placeholderCount = cursor.getDay() === 0 ? 6 : cursor.getDay() - 1;
|
|
||||||
|
|
||||||
for (let i = 1; i <= placeholderCount; i++) {
|
|
||||||
days.push({ day: '' });
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
days.push({
|
|
||||||
day: cursor.getDate(),
|
|
||||||
date: new Date(cursor)
|
|
||||||
});
|
|
||||||
|
|
||||||
cursor.setDate(cursor.getDate() + 1);
|
|
||||||
} while (compareMonth(cursor, date) === 0);
|
|
||||||
|
|
||||||
return days;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
props: {
|
props: {
|
||||||
title: String,
|
title: String,
|
||||||
@ -61,7 +40,7 @@ export default createComponent({
|
|||||||
do {
|
do {
|
||||||
months.push({
|
months.push({
|
||||||
date: new Date(cursor),
|
date: new Date(cursor),
|
||||||
days: getDays(cursor),
|
days: this.getDays(cursor),
|
||||||
title: formatMonthTitle(cursor)
|
title: formatMonthTitle(cursor)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -83,17 +62,60 @@ export default createComponent({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getDays(date) {
|
||||||
|
const days = [];
|
||||||
|
const { minDate, maxDate } = this;
|
||||||
|
const checkMinDate = compareMonth(date, minDate) === 0;
|
||||||
|
const checkMaxDate = compareMonth(date, maxDate) === 0;
|
||||||
|
|
||||||
|
function isDisabled(date) {
|
||||||
|
if (checkMaxDate && date.getDate() > maxDate.getDate()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkMinDate && date.getDate() < minDate.getDate()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const placeholderCount = date.getDay() === 0 ? 6 : date.getDay() - 1;
|
||||||
|
|
||||||
|
for (let i = 1; i <= placeholderCount; i++) {
|
||||||
|
days.push({ day: '' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const cursor = new Date(date);
|
||||||
|
|
||||||
|
do {
|
||||||
|
days.push({
|
||||||
|
day: cursor.getDate(),
|
||||||
|
date: new Date(cursor),
|
||||||
|
disabled: isDisabled(cursor)
|
||||||
|
});
|
||||||
|
|
||||||
|
cursor.setDate(cursor.getDate() + 1);
|
||||||
|
} while (compareMonth(cursor, date) === 0);
|
||||||
|
|
||||||
|
return days;
|
||||||
|
},
|
||||||
|
|
||||||
genMonth(month, index) {
|
genMonth(month, index) {
|
||||||
const showTitle = index !== 0;
|
const Title = index !== 0 && (
|
||||||
|
<div class={bem('month-title')}>{month.title}</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const Days = month.days.map(item => (
|
||||||
|
<div class={bem('day', { disabled: item.disabled })}>{item.day}</div>
|
||||||
|
));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={bem('month')} ref="month" refInFor>
|
<div class={bem('month')} ref="month" refInFor>
|
||||||
{showTitle && <div class={bem('month-title')}>{month.title}</div>}
|
{Title}
|
||||||
<div class={bem('days')}>
|
<div class={bem('days')}>
|
||||||
<div class={bem('month-mark')}>{month.date.getMonth() + 1}</div>
|
<div class={bem('month-mark')}>{month.date.getMonth() + 1}</div>
|
||||||
{month.days.map(item => (
|
{Days}
|
||||||
<div class={bem('day')}>{item.day}</div>
|
|
||||||
))}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -72,5 +72,9 @@
|
|||||||
width: 14.285%;
|
width: 14.285%;
|
||||||
height: 64px;
|
height: 64px;
|
||||||
font-size: @font-size-lg;
|
font-size: @font-size-lg;
|
||||||
|
|
||||||
|
&--disabled {
|
||||||
|
color: @gray-5;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user