From 04c412c99b2e19348c31802bc323821656aeecbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Tue, 24 Dec 2019 11:15:29 +0800 Subject: [PATCH] feat(Calendar): add Header component --- src/calendar/Header.js | 49 +++++++++++++++++++++++++++++++++++++ src/calendar/index.js | 53 +++++++++-------------------------------- src/calendar/index.less | 43 ++++++++++++++++++--------------- src/calendar/utils.ts | 10 ++++++++ 4 files changed, 94 insertions(+), 61 deletions(-) create mode 100644 src/calendar/Header.js diff --git a/src/calendar/Header.js b/src/calendar/Header.js new file mode 100644 index 000000000..17306245c --- /dev/null +++ b/src/calendar/Header.js @@ -0,0 +1,49 @@ +import { createNamespace } from '../utils'; +import { t, formatMonthTitle } from './utils'; + +const [createComponent, bem] = createNamespace('calendar-header'); + +export default createComponent({ + props: { + title: String, + currentMonth: Date + }, + + methods: { + genTitle() { + if (this.title) { + return
{this.title}
; + } + }, + + genMonth() { + return ( +
+ {formatMonthTitle(this.currentMonth)} +
+ ); + }, + + genWeekDays() { + const weekdays = t('weekdays'); + + return ( +
+ {weekdays.map(item => ( + {item} + ))} +
+ ); + } + }, + + render() { + return ( +
+ {this.genTitle()} + {this.genMonth()} + {this.genWeekDays()} +
+ ); + } +}); diff --git a/src/calendar/index.js b/src/calendar/index.js index e883c9780..7838a0581 100644 --- a/src/calendar/index.js +++ b/src/calendar/index.js @@ -1,12 +1,6 @@ -import { createNamespace } from '../utils'; import { isDate } from '../utils/validate/date'; -import { compareMonth } from './utils'; - -const [createComponent, bem, t] = createNamespace('calendar'); - -function formatMonthTitle(date) { - return t('monthTitle', date.getFullYear(), date.getMonth() + 1); -} +import { createComponent, bem, compareMonth, formatMonthTitle } from './utils'; +import Header from './Header'; function getDays(date) { const days = []; @@ -49,7 +43,7 @@ export default createComponent({ data() { return { - currentDate: this.minDate + currentMonth: this.minDate }; }, @@ -64,7 +58,8 @@ export default createComponent({ do { months.push({ date: new Date(cursor), - days: getDays(cursor) + days: getDays(cursor), + title: formatMonthTitle(cursor) }); cursor.setMonth(cursor.getMonth() + 1); @@ -75,37 +70,15 @@ export default createComponent({ }, methods: { - genTitle() { - if (this.title) { - return
{this.title}
; - } - }, - - genWeekDays() { - const weekdays = t('weekdays'); - - return ( -
- {weekdays.map(item => ( - {item} - ))} -
- ); - }, - - genMonthTitle(date) { - return
{formatMonthTitle(date)}
; - }, - - genMonth(monthItem, index) { - const Title = index !== 0 ? this.genMonthTitle(monthItem.date) : null; + genMonth(month, index) { + const showTitle = index !== 0; return (
- {Title} + {showTitle &&
{month.title}
}
- {monthItem.days.map(dayItem => ( -
{dayItem.day}
+ {month.days.map(item => ( +
{item.day}
))}
@@ -116,11 +89,7 @@ export default createComponent({ render() { return (
-
- {this.genTitle()} - {this.genMonthTitle(this.currentDate)} - {this.genWeekDays()} -
+
{this.months.map(this.genMonth)}
); diff --git a/src/calendar/index.less b/src/calendar/index.less index a807fef9d..54357843f 100644 --- a/src/calendar/index.less +++ b/src/calendar/index.less @@ -5,12 +5,8 @@ flex-direction: column; height: 80vh; - &__header { - flex-shrink: 0; - box-shadow: 0 2px 10px rgba(125, 126, 128, .16); - } - - &__title, + &-header__title, + &-header__month, &__month-title { height: 44px; font-weight: @font-weight-bold; @@ -18,25 +14,34 @@ text-align: center; } - &__title { - font-size: @font-size-lg; + &-header { + flex-shrink: 0; + box-shadow: 0 2px 10px rgba(125, 126, 128, 0.16); + + &__title { + font-size: @font-size-lg; + } + + &__month { + font-size: @font-size-md; + } + + &__weekdays { + display: flex; + } + + &__weekday { + flex: 1; + font-size: @font-size-sm; + line-height: 30px; + text-align: center; + } } &__month-title { font-size: @font-size-md; } - &__weekdays { - display: flex; - } - - &__weekday { - flex: 1; - font-size: @font-size-sm; - line-height: 30px; - text-align: center; - } - &__body { flex: 1; overflow: auto; diff --git a/src/calendar/utils.ts b/src/calendar/utils.ts index d140cc05e..e6d6e99fe 100644 --- a/src/calendar/utils.ts +++ b/src/calendar/utils.ts @@ -1,3 +1,13 @@ +import { createNamespace } from '../utils'; + +const [createComponent, bem, t] = createNamespace('calendar'); + +export { createComponent, bem, t }; + +export function formatMonthTitle(date: Date) { + return t('monthTitle', date.getFullYear(), date.getMonth() + 1); +} + export function compareMonth(date1: Date, date2: Date) { const year1 = date1.getFullYear(); const year2 = date2.getFullYear();