feat(Calendar): add params for subtitle slot (#11168)

* feat(Calendar): add params for subtitle slot

* chore: fix
This commit is contained in:
neverland 2022-10-23 22:47:33 +08:00 committed by GitHub
parent 859e5ee32c
commit 6848550243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 20 deletions

View File

@ -175,7 +175,10 @@ export default defineComponent({
const bodyRef = ref<HTMLElement>(); const bodyRef = ref<HTMLElement>();
const subtitle = ref(''); const subtitle = ref<{ text: string; date?: Date }>({
text: '',
date: undefined,
});
const currentDate = ref(getInitialDate()); const currentDate = ref(getInitialDate());
const [monthRefs, setMonthRefs] = useRefs<CalendarMonthInstance>(); const [monthRefs, setMonthRefs] = useRefs<CalendarMonthInstance>();
@ -267,7 +270,10 @@ export default defineComponent({
/* istanbul ignore else */ /* istanbul ignore else */
if (currentMonth) { if (currentMonth) {
subtitle.value = currentMonth.getTitle(); subtitle.value = {
text: currentMonth.getTitle(),
date: currentMonth.date,
};
} }
}; };
@ -525,8 +531,9 @@ export default defineComponent({
<div class={bem()}> <div class={bem()}>
<CalendarHeader <CalendarHeader
v-slots={pick(slots, ['title', 'subtitle'])} v-slots={pick(slots, ['title', 'subtitle'])}
date={subtitle.value.date}
title={props.title} title={props.title}
subtitle={subtitle.value} subtitle={subtitle.value.text}
showTitle={props.showTitle} showTitle={props.showTitle}
showSubtitle={props.showSubtitle} showSubtitle={props.showSubtitle}
firstDayOfWeek={dayOffset.value} firstDayOfWeek={dayOffset.value}

View File

@ -8,6 +8,7 @@ export default defineComponent({
name, name,
props: { props: {
date: Date,
title: String, title: String,
subtitle: String, subtitle: String,
showTitle: Boolean, showTitle: Boolean,
@ -30,7 +31,12 @@ export default defineComponent({
const renderSubtitle = () => { const renderSubtitle = () => {
if (props.showSubtitle) { if (props.showSubtitle) {
const title = slots.subtitle ? slots.subtitle() : props.subtitle; const title = slots.subtitle
? slots.subtitle({
date: props.date,
text: props.subtitle,
})
: props.subtitle;
return ( return (
<div class={bem('header-subtitle')} onClick={onClickSubtitle}> <div class={bem('header-subtitle')} onClick={onClickSubtitle}>
{title} {title}

View File

@ -331,14 +331,14 @@ Following props are supported when the type is multiple
### Slots ### Slots
| Name | Description | SlotProps | | Name | Description | SlotProps |
| --------------------- | ------------------------- | ----------------------- | | --- | --- | --- |
| title | Custom title | - | | title | Custom title | - |
| subtitle `v3.1.3` | Custom subtitle | - | | subtitle `v3.1.3` | Custom subtitle | _{ text: string, date?: Date }_ |
| footer | Custom footer | - | | footer | Custom footer | - |
| confirm-text `v3.2.6` | Custom confirm text | _{ disabled: boolean }_ | | confirm-text `v3.2.6` | Custom confirm text | _{ disabled: boolean }_ |
| top-info `v3.0.17` | Custom top info of day | _day: Day_ | | top-info `v3.0.17` | Custom top info of day | _day: Day_ |
| bottom-info `v3.0.17` | Custom bottom info of day | _day: Day_ | | bottom-info `v3.0.17` | Custom bottom info of day | _day: Day_ |
### Methods ### Methods

View File

@ -337,14 +337,14 @@ export default {
### Slots ### Slots
| 名称 | 说明 | 参数 | | 名称 | 说明 | 参数 |
| --------------------- | ------------------------ | ----------------------- | | --- | --- | --- |
| title | 自定义标题 | - | | title | 自定义标题 | - |
| subtitle `v3.1.3` | 自定义日历副标题 | - | | subtitle `v3.1.3` | 自定义日历副标题 | _{ text: string, date?: Date }_ |
| footer | 自定义底部区域内容 | - | | footer | 自定义底部区域内容 | - |
| confirm-text `v3.2.6` | 自定义确认按钮的内容 | _{ disabled: boolean }_ | | confirm-text `v3.2.6` | 自定义确认按钮的内容 | _{ disabled: boolean }_ |
| top-info `v3.0.17` | 自定义日期上方的提示信息 | _day: Day_ | | top-info `v3.0.17` | 自定义日期上方的提示信息 | _day: Day_ |
| bottom-info `v3.0.17` | 自定义日期下方的提示信息 | _day: Day_ | | bottom-info `v3.0.17` | 自定义日期下方的提示信息 | _day: Day_ |
### 方法 ### 方法

View File

@ -934,6 +934,12 @@ exports[`should render confirm-text slot correctly 1`] = `
</button> </button>
`; `;
exports[`should render subtitle slot with params 1`] = `
<div class="van-calendar__header-subtitle">
Custom Subtitle
</div>
`;
exports[`should render title、footer、subtitle slot correctly 1`] = ` exports[`should render title、footer、subtitle slot correctly 1`] = `
<div class="van-calendar"> <div class="van-calendar">
<div class="van-calendar__header"> <div class="van-calendar__header">

View File

@ -365,6 +365,27 @@ test('should render title、footer、subtitle slot correctly', async () => {
expect(wrapper.html()).toMatchSnapshot(); expect(wrapper.html()).toMatchSnapshot();
}); });
test('should render subtitle slot with params', async () => {
const wrapper = mount(Calendar, {
props: {
minDate,
maxDate,
poppable: false,
defaultDate: minDate,
lazyRender: false,
},
slots: {
subtitle: ({ text }) => `Custom Subtitle ${text}`,
},
});
await later();
expect(
wrapper.find('.van-calendar__header-subtitle').html()
).toMatchSnapshot();
});
test('should reset when type changed', async () => { test('should reset when type changed', async () => {
const wrapper = mount(Calendar, { const wrapper = mount(Calendar, {
props: { props: {