Compare commits

...

2 Commits

Author SHA1 Message Date
neverland
b5ea1cd3ad
feat(Calendar): add click-subtitle event (#8981) 2021-07-05 13:25:58 +08:00
neverland
779f3363c2
feat(Calendar): add subtitle slot (#8980) 2021-07-05 13:06:04 +08:00
6 changed files with 58 additions and 22 deletions

View File

@ -115,8 +115,9 @@ export default defineComponent({
'confirm',
'unselect',
'month-show',
'update:show',
'over-range',
'update:show',
'click-subtitle',
],
setup(props, { emit, slots }) {
@ -489,12 +490,15 @@ export default defineComponent({
const renderCalendar = () => (
<div class={bem()}>
<CalendarHeader
v-slots={{ title: slots.title }}
v-slots={pick(slots, ['title', 'subtitle'])}
title={props.title}
showTitle={props.showTitle}
subtitle={state.subtitle}
showTitle={props.showTitle}
showSubtitle={props.showSubtitle}
firstDayOfWeek={dayOffset.value}
onClick-subtitle={(event: MouseEvent) => {
emit('click-subtitle', event);
}}
/>
<div ref={bodyRef} class={bem('body')} onScroll={onScroll}>
{months.value.map(renderMonth)}

View File

@ -15,7 +15,9 @@ export default defineComponent({
firstDayOfWeek: Number,
},
setup(props, { slots }) {
emits: ['click-subtitle'],
setup(props, { slots, emit }) {
const renderTitle = () => {
if (props.showTitle) {
const text = props.title || t('title');
@ -24,9 +26,18 @@ export default defineComponent({
}
};
const onClickSubtitle = (event: MouseEvent) => {
emit('click-subtitle', event);
};
const renderSubtitle = () => {
if (props.showSubtitle) {
return <div class={bem('header-subtitle')}>{props.subtitle}</div>;
const title = slots.subtitle ? slots.subtitle() : props.subtitle;
return (
<div class={bem('header-subtitle')} onClick={onClickSubtitle}>
{title}
</div>
);
}
};

View File

@ -326,12 +326,14 @@ Following props are supported when the type is multiple
| unselect | Emitted when unselect date when type is multiple | _value: Date_ |
| month-show | Emitted when a month enters the visible area | _{ date: Date, title: string }_ |
| over-range | Emitted when exceeded max range | - |
| click-subtitle `v3.1.3` | Emitted when clicking the subtitle | _event: MouseEvent_ |
### Slots
| Name | Description | SlotProps |
| --------------------- | ------------------------- | ---------- |
| title | Custom title | - |
| subtitle `v3.1.3` | Custom subtitle | - |
| footer | Custom footer | - |
| top-info `v3.0.17` | Custom top info of day | _day: Day_ |
| bottom-info `v3.0.17` | Custom bottom info of day | _day: Day_ |

View File

@ -332,12 +332,14 @@ export default {
| unselect | 当日历组件的 `type``multiple` 时,取消选中日期时触发 | _value: Date_ |
| month-show | 当某个月份进入可视区域时触发 | _{ date: Date, title: string }_ |
| over-range | 范围选择超过最多可选天数时触发 | - |
| click-subtitle `v3.1.3` | 点击日历副标题时触发 | _event: MouseEvent_ |
### Slots
| 名称 | 说明 | 参数 |
| --------------------- | ------------------------ | ---------- |
| title | 自定义标题 | - |
| subtitle `v3.1.3` | 自定义日历副标题 | - |
| footer | 自定义底部区域内容 | - |
| top-info `v3.0.17` | 自定义日期上方的提示信息 | _day: Day_ |
| bottom-info `v3.0.17` | 自定义日期下方的提示信息 | _day: Day_ |

View File

@ -900,28 +900,14 @@ exports[`row-height prop 1`] = `
</div>
`;
exports[`should render top-info and bottom-info slot correctly 1`] = `
<div role="gridcell"
style="margin-left: 71.42857142857143%;"
class="van-calendar__day van-calendar__day--disabled"
>
<div class="van-calendar__top-info">
top: 1
</div>
1
<div class="van-calendar__bottom-info">
bottom: 1
</div>
</div>
`;
exports[`title & footer slot 1`] = `
exports[`should render title、footer、subtitle slot correctly 1`] = `
<div class="van-calendar">
<div class="van-calendar__header">
<div class="van-calendar__header-title">
Custom Title
</div>
<div class="van-calendar__header-subtitle">
Custom Subtitle
</div>
<div class="van-calendar__weekdays">
<span class="van-calendar__weekday">
@ -1132,3 +1118,18 @@ exports[`title & footer slot 1`] = `
</div>
</div>
`;
exports[`should render top-info and bottom-info slot correctly 1`] = `
<div role="gridcell"
style="margin-left: 71.42857142857143%;"
class="van-calendar__day van-calendar__day--disabled"
>
<div class="van-calendar__top-info">
top: 1
</div>
1
<div class="van-calendar__bottom-info">
bottom: 1
</div>
</div>
`;

View File

@ -344,7 +344,7 @@ test('formatter prop', async () => {
expect(wrapper.html()).toMatchSnapshot();
});
test('title & footer slot', async () => {
test('should render title、footer、subtitle slot correctly', async () => {
const wrapper = mount(Calendar, {
props: {
minDate,
@ -356,6 +356,7 @@ test('title & footer slot', async () => {
slots: {
title: () => 'Custom Title',
footer: () => 'Custom Footer',
subtitle: () => 'Custom Subtitle',
},
});
@ -553,3 +554,18 @@ test('should render top-info and bottom-info slot correctly', async () => {
expect(wrapper.find('.van-calendar__day').html()).toMatchSnapshot();
});
test('should emit click-subtitle event when clicking the subtitle', async () => {
const wrapper = mount(Calendar, {
props: {
minDate,
maxDate,
poppable: false,
lazyRender: false,
},
});
await later();
wrapper.find('.van-calendar__header-subtitle').trigger('click');
expect(wrapper.emitted('click-subtitle')).toBeTruthy();
});