From db41f5ad44d83a8dc632860faa79888ecb5bda8e Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 18 May 2021 10:09:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(Calendar):=20add=20top-info=E3=80=81bottom?= =?UTF-8?q?-info=20slot=20(#8716)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/calendar/Calendar.tsx | 1 + src/calendar/CalendarDay.tsx | 38 ++++++++++++++----- src/calendar/CalendarMonth.tsx | 5 ++- src/calendar/README.md | 14 ++++--- src/calendar/README.zh-CN.md | 10 +++-- .../test/__snapshots__/index.spec.ts.snap | 15 ++++++++ src/calendar/test/index.spec.ts | 20 ++++++++++ 7 files changed, 82 insertions(+), 21 deletions(-) diff --git a/src/calendar/Calendar.tsx b/src/calendar/Calendar.tsx index d95ef1255..e808814fa 100644 --- a/src/calendar/Calendar.tsx +++ b/src/calendar/Calendar.tsx @@ -416,6 +416,7 @@ export default defineComponent({ const showMonthTitle = index !== 0 || !props.showSubtitle; return ( { const { item, index, color, offset, rowHeight } = props; const style: CSSProperties = { @@ -84,17 +84,37 @@ export default defineComponent({ } }; + const renderTopInfo = () => { + const { topInfo } = props.item; + + if (topInfo || slots['top-info']) { + return ( +
+ {slots['top-info'] ? slots['top-info'](props.item) : topInfo} +
+ ); + } + }; + + const renderBottomInfo = () => { + const { bottomInfo } = props.item; + + if (bottomInfo || slots['bottom-info']) { + return ( +
+ {slots['bottom-info'] + ? slots['bottom-info'](props.item) + : bottomInfo} +
+ ); + } + }; + const renderContent = () => { const { item, color, rowHeight } = props; - const { type, text, topInfo, bottomInfo } = item; + const { type, text } = item; - const TopInfo = topInfo &&
{topInfo}
; - - const BottomInfo = bottomInfo && ( -
{bottomInfo}
- ); - - const Nodes = [TopInfo, text, BottomInfo]; + const Nodes = [renderTopInfo(), text, renderBottomInfo()]; if (type === 'selected') { return ( diff --git a/src/calendar/CalendarMonth.tsx b/src/calendar/CalendarMonth.tsx index 759e7cc0d..66b59aa0c 100644 --- a/src/calendar/CalendarMonth.tsx +++ b/src/calendar/CalendarMonth.tsx @@ -1,7 +1,7 @@ import { ref, computed, PropType, defineComponent } from 'vue'; // Utils -import { addUnit, setScrollTop, createNamespace } from '../utils'; +import { addUnit, setScrollTop, createNamespace, pick } from '../utils'; import { getMonthEndDay } from '../datetime-picker/utils'; import { t, @@ -55,7 +55,7 @@ export default defineComponent({ emits: ['click', 'update-height'], - setup(props, { emit }) { + setup(props, { emit, slots }) { const [visible, setVisible] = useToggle(); const daysRef = ref(); const monthRef = ref(); @@ -230,6 +230,7 @@ export default defineComponent({ const renderDay = (item: CalendarDayItem, index: number) => ( `; +exports[`should render top-info and bottom-info slot correctly 1`] = ` +
+
+ top: 1 +
+ 1 +
+ bottom: 1 +
+
+`; + exports[`title & footer slot 1`] = `
diff --git a/src/calendar/test/index.spec.ts b/src/calendar/test/index.spec.ts index b4c6741fc..8a7165e36 100644 --- a/src/calendar/test/index.spec.ts +++ b/src/calendar/test/index.spec.ts @@ -533,3 +533,23 @@ test('close event', async () => { expect(onClose).toHaveBeenCalledTimes(1); }); + +test('should render top-info and bottom-info slot correctly', async () => { + const wrapper = mount(Calendar, { + props: { + minDate, + maxDate, + poppable: false, + defaultDate: minDate, + lazyRender: false, + }, + slots: { + 'top-info': (item) => 'top: ' + item.text, + 'bottom-info': (item) => 'bottom: ' + item.text, + }, + }); + + await later(); + + expect(wrapper.find('.van-calendar__day').html()).toMatchSnapshot(); +});