feat(Calendar): lazy render

This commit is contained in:
陈嘉涵 2019-12-25 15:54:07 +08:00 committed by neverland
parent 0b721fd87c
commit bfb08c37cb
3 changed files with 158 additions and 102 deletions

View File

@ -1,12 +1,12 @@
import { createNamespace } from '../../utils'; import { createNamespace } from '../../utils';
import { t, bem, formatMonthTitle } from '../utils'; import { t, bem } from '../utils';
const [createComponent] = createNamespace('calendar-header'); const [createComponent] = createNamespace('calendar-header');
export default createComponent({ export default createComponent({
props: { props: {
title: String, title: String,
currentMonth: Date monthTitle: String
}, },
methods: { methods: {
@ -19,11 +19,7 @@ export default createComponent({
}, },
genMonth() { genMonth() {
return ( return <div class={bem('month-title')}>{this.monthTitle}</div>;
<div class={bem('month-title')}>
{formatMonthTitle(this.currentMonth)}
</div>
);
}, },
genWeekDays() { genWeekDays() {

View File

@ -1,14 +1,73 @@
import { createNamespace } from '../../utils'; import { createNamespace } from '../../utils';
import { t, bem } from '../utils'; import { t, bem, compareDay, formatMonthTitle } from '../utils';
import { getMonthEndDay } from '../../datetime-picker/utils';
const [createComponent] = createNamespace('calendar-month'); const [createComponent] = createNamespace('calendar-month');
export default createComponent({ export default createComponent({
props: { props: {
date: Date, date: Date,
days: Array, type: String,
title: String, minDate: Date,
showMark: Boolean maxDate: Date,
showMark: Boolean,
showTitle: Boolean,
rowHeight: Number,
currentValue: [Date, Array]
},
data() {
return {
visible: false
};
},
computed: {
title() {
return formatMonthTitle(this.date);
},
offset() {
const day = this.date.getDay();
return day > 0 ? day - 1 : 6;
},
totalDay() {
return getMonthEndDay(this.date.getFullYear(), this.date.getMonth() + 1);
},
monthStyle() {
if (!this.visible) {
const padding =
Math.ceil((this.totalDay + this.offset) / 7) * this.rowHeight;
return {
paddingBottom: `${padding}px`
};
}
},
days() {
if (!this.visible) {
return null;
}
const days = [];
const year = this.date.getFullYear();
const month = this.date.getMonth();
for (let i = 1; i <= this.totalDay; i++) {
const date = new Date(year, month, i);
days.push({
day: i,
date,
type: this.getDayType(date)
});
}
return days;
}
}, },
mounted() { mounted() {
@ -16,6 +75,44 @@ export default createComponent({
}, },
methods: { methods: {
getDayType(day) {
const { type, minDate, maxDate, currentValue } = this;
if (compareDay(day, minDate) < 0 || compareDay(day, maxDate) > 0) {
return 'disabled';
}
if (type === 'single') {
return compareDay(day, currentValue) === 0 ? 'selected' : '';
}
if (type === 'range') {
const [startDay, endDay] = this.currentValue;
if (!startDay) {
return;
}
const compareToStart = compareDay(day, startDay);
if (compareToStart === 0) {
return 'start';
}
if (!endDay) {
return;
}
const compareToEnd = compareDay(day, endDay);
if (compareToEnd === 0) {
return 'end';
}
if (compareToStart > 0 && compareToEnd < 0) {
return 'middle';
}
}
},
getLabel(item) { getLabel(item) {
if (item.type === 'start') { if (item.type === 'start') {
return t('start'); return t('start');
@ -27,7 +124,7 @@ export default createComponent({
}, },
genTitle() { genTitle() {
if (this.title) { if (this.showTitle) {
return <div class={bem('month-title')}>{this.title}</div>; return <div class={bem('month-title')}>{this.title}</div>;
} }
}, },
@ -38,9 +135,27 @@ export default createComponent({
} }
}, },
genDay(item) { genDays() {
if (this.visible) {
return (
<div class={bem('days')}>
{this.genMark()}
{this.days.map(this.genDay)}
</div>
);
}
},
genDay(item, index) {
const { type } = item; const { type } = item;
let style;
if (index === 0) {
style = {
marginLeft: `${(100 * this.offset) / 7}%`
};
}
const onClick = () => { const onClick = () => {
if (type !== 'disabled') { if (type !== 'disabled') {
this.$emit('click', item); this.$emit('click', item);
@ -49,7 +164,7 @@ export default createComponent({
if (type === 'selected') { if (type === 'selected') {
return ( return (
<div class={bem('day')} onClick={onClick}> <div style={style} class={bem('day')} onClick={onClick}>
<div class={bem('selected-day')}>{item.day}</div> <div class={bem('selected-day')}>{item.day}</div>
</div> </div>
); );
@ -59,7 +174,7 @@ export default createComponent({
const Label = label && <div class={bem('day-label')}>{label}</div>; const Label = label && <div class={bem('day-label')}>{label}</div>;
return ( return (
<div class={bem('day', [type])} onClick={onClick}> <div style={style} class={bem('day', [type])} onClick={onClick}>
{item.day} {item.day}
{Label} {Label}
</div> </div>
@ -69,12 +184,9 @@ export default createComponent({
render() { render() {
return ( return (
<div class={bem('month')}> <div class={bem('month')} style={this.monthStyle}>
{this.genTitle()} {this.genTitle()}
<div class={bem('days')}> {this.genDays()}
{this.genMark()}
{this.days.map(this.genDay)}
</div>
</div> </div>
); );
} }

View File

@ -6,8 +6,7 @@ import {
getNextDay, getNextDay,
compareDay, compareDay,
compareMonth, compareMonth,
createComponent, createComponent
formatMonthTitle
} from './utils'; } from './utils';
import Button from '../button'; import Button from '../button';
@ -37,6 +36,10 @@ export default createComponent({
}, },
validator: isDate validator: isDate
}, },
rowHeight: {
type: Number,
default: 64
},
showMark: { showMark: {
type: Boolean, type: Boolean,
default: true default: true
@ -49,7 +52,7 @@ export default createComponent({
data() { data() {
return { return {
currentMonth: this.minDate, monthTitle: '',
currentValue: this.getDefaultValue() currentValue: this.getDefaultValue()
}; };
}, },
@ -57,20 +60,14 @@ export default createComponent({
computed: { computed: {
months() { months() {
const months = []; const months = [];
const { minDate, maxDate } = this; const cursor = new Date(this.minDate);
const cursor = new Date(minDate);
cursor.setDate(1); cursor.setDate(1);
do { do {
months.push({ months.push(new Date(cursor));
date: new Date(cursor),
days: this.getDays(cursor),
title: formatMonthTitle(cursor)
});
cursor.setMonth(cursor.getMonth() + 1); cursor.setMonth(cursor.getMonth() + 1);
} while (compareMonth(cursor, maxDate) !== 1); } while (compareMonth(cursor, this.maxDate) !== 1);
return months; return months;
} }
@ -82,6 +79,11 @@ export default createComponent({
} }
}, },
mounted() {
this.bodyHeight = this.$refs.body.getBoundingClientRect().height;
this.onScroll();
},
methods: { methods: {
getDefaultValue() { getDefaultValue() {
const { type, value, minDate } = this; const { type, value, minDate } = this;
@ -96,77 +98,19 @@ export default createComponent({
} }
}, },
getDayType(day) {
const { type, minDate, maxDate, currentValue } = this;
if (compareDay(day, minDate) < 0 || compareDay(day, maxDate) > 0) {
return 'disabled';
}
if (type === 'single') {
return compareDay(day, currentValue) === 0 ? 'selected' : '';
}
if (type === 'range') {
const [startDay, endDay] = this.currentValue;
if (!startDay) {
return;
}
const compareToStart = compareDay(day, startDay);
if (compareToStart === 0) {
return 'start';
}
if (!endDay) {
return;
}
const compareToEnd = compareDay(day, endDay);
if (compareToEnd === 0) {
return 'end';
}
if (compareToStart > 0 && compareToEnd < 0) {
return 'middle';
}
}
},
getDays(date) {
const days = [];
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),
type: this.getDayType(cursor)
});
cursor.setDate(cursor.getDate() + 1);
} while (compareMonth(cursor, date) === 0);
return days;
},
onScroll() { onScroll() {
const scrollTop = getScrollTop(this.$refs.body); const scrollTop = getScrollTop(this.$refs.body);
const monthsHeight = this.$refs.month.map(item => item.height); const monthsHeight = this.$refs.month.map(item => item.height);
const bodyTop = scrollTop;
const bodyBottom = scrollTop + this.bodyHeight;
let height = 0; let height = 0;
for (let i = 0; i < this.months.length; i++) { for (let i = 0; i < this.months.length; i++) {
if (scrollTop < height) { const monthTop = height;
this.currentMonth = this.months[i - 1].date; const monthBottom = height + monthsHeight[i];
return; const visible = monthTop <= bodyBottom && monthBottom >= bodyTop;
}
this.$refs.month[i].visible = visible;
height += monthsHeight[i]; height += monthsHeight[i];
} }
@ -204,15 +148,19 @@ export default createComponent({
this.$emit('select', this.currentValue); this.$emit('select', this.currentValue);
}, },
genMonth(month, index) { genMonth(date, index) {
return ( return (
<Month <Month
ref="month" ref="month"
refInFor refInFor
days={month.days} type={this.type}
date={month.date} date={date}
minDate={this.minDate}
maxDate={this.maxDate}
showMark={this.showMark} showMark={this.showMark}
title={index !== 0 ? month.title : ''} rowHeight={this.rowHeight}
showTitle={index !== 0}
currentValue={this.currentValue}
onClick={this.onClickDay} onClick={this.onClickDay}
/> />
); );
@ -262,7 +210,7 @@ export default createComponent({
<div class={bem()}> <div class={bem()}>
<Header <Header
title={this.title} title={this.title}
currentMonth={this.currentMonth} monthTitle={this.monthTitle}
scopedSlots={{ scopedSlots={{
title: () => this.slots('title') title: () => this.slots('title')
}} }}