fix(Calendar): row-height not work

This commit is contained in:
陈嘉涵 2019-12-25 16:13:43 +08:00 committed by neverland
parent 51c4dc180b
commit d9da405b95
3 changed files with 28 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import { createNamespace } from '../../utils'; import { createNamespace } from '../../utils';
import { t, bem, compareDay, formatMonthTitle } from '../utils'; import { t, bem, compareDay, formatMonthTitle, ROW_HEIGHT } from '../utils';
import { getMonthEndDay } from '../../datetime-picker/utils'; import { getMonthEndDay } from '../../datetime-picker/utils';
const [createComponent] = createNamespace('calendar-month'); const [createComponent] = createNamespace('calendar-month');
@ -123,6 +123,20 @@ export default createComponent({
} }
}, },
getDayStyle(index) {
const style = {};
if (index === 0) {
style.marginLeft = `${(100 * this.offset) / 7}%`;
}
if (this.rowHeight !== ROW_HEIGHT) {
style.height = `${this.rowHeight}px`;
}
return style;
},
genTitle() { genTitle() {
if (this.showTitle) { if (this.showTitle) {
return <div class={bem('month-title')}>{this.title}</div>; return <div class={bem('month-title')}>{this.title}</div>;
@ -148,13 +162,7 @@ export default createComponent({
genDay(item, index) { genDay(item, index) {
const { type } = item; const { type } = item;
const style = this.getDayStyle(index);
let style;
if (index === 0) {
style = {
marginLeft: `${(100 * this.offset) / 7}%`
};
}
const onClick = () => { const onClick = () => {
if (type !== 'disabled') { if (type !== 'disabled') {

View File

@ -6,7 +6,9 @@ import {
getNextDay, getNextDay,
compareDay, compareDay,
compareMonth, compareMonth,
createComponent createComponent,
ROW_HEIGHT,
RENDER_OFFSET
} from './utils'; } from './utils';
import Button from '../button'; import Button from '../button';
@ -38,7 +40,7 @@ export default createComponent({
}, },
rowHeight: { rowHeight: {
type: Number, type: Number,
default: 64 default: ROW_HEIGHT
}, },
showMark: { showMark: {
type: Boolean, type: Boolean,
@ -98,10 +100,13 @@ export default createComponent({
} }
}, },
// calculate the position of the elements
// and find the elements that needs to be rendered
onScroll() { onScroll() {
const { body, months } = this.$refs; const { body, months } = this.$refs;
const top = getScrollTop(body); const scrollTop = getScrollTop(body);
const bottom = top + this.bodyHeight; const top = scrollTop - RENDER_OFFSET;
const bottom = scrollTop + this.bodyHeight + RENDER_OFFSET;
const heights = months.map(item => item.height); const heights = months.map(item => item.height);
let height = 0; let height = 0;

View File

@ -5,6 +5,9 @@ const [createComponent, bem, t] = createNamespace('calendar');
export { createComponent, bem, t }; export { createComponent, bem, t };
export const ROW_HEIGHT = 64;
export const RENDER_OFFSET = 150;
export function formatMonthTitle(date: Date) { export function formatMonthTitle(date: Date) {
return t('monthTitle', date.getFullYear(), padZero(date.getMonth() + 1)); return t('monthTitle', date.getFullYear(), padZero(date.getMonth() + 1));
} }