Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(calendar): multiple-selected-state #5783

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/calendar/calendar.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
date="{{ item }}"
type="{{ type }}"
color="{{ color }}"
extraColors="{{ extraColors }}"
minDate="{{ minDate }}"
maxDate="{{ maxDate }}"
showMark="{{ showMark }}"
formatter="{{ formatter }}"
rowHeight="{{ rowHeight }}"
currentDate="{{ currentDate }}"
currentDateSelectedState = "{{ currentDateSelectedState }}"
showSubtitle="{{ showSubtitle }}"
allowSameDay="{{ allowSameDay }}"
showMonthTitle="{{ index !== 0 || !showSubtitle }}"
Expand Down
21 changes: 21 additions & 0 deletions packages/calendar/components/month/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
export interface Day {
date: Date;
type: string;
selectedState: number;
text: number;
bottomInfo?: string;
}
Expand All @@ -24,6 +25,7 @@ VantComponent({
observer: 'setDays',
},
color: String,
extraColors: null,
minDate: {
type: null,
observer: 'setDays',
Expand All @@ -42,6 +44,10 @@ VantComponent({
type: null,
observer: 'setDays',
},
currentDateSelectedState: {
type: Array,
observer: 'setDays',
},
firstDayOfWeek: {
type: Number,
observer: 'setDays',
Expand Down Expand Up @@ -79,10 +85,12 @@ VantComponent({
for (let day = 1; day <= totalDay; day++) {
const date = new Date(year, month, day);
const type = this.getDayType(date);
const selectedState = this.getDaySelectedState(date, type);

let config: Day = {
date,
type,
selectedState,
text: day,
bottomInfo: this.getBottomInfo(type),
};
Expand Down Expand Up @@ -188,6 +196,19 @@ VantComponent({
return '';
},

getDaySelectedState(day: number | Date, dayType: string): number {
const { type, currentDate, currentDateSelectedState } = this.data;

if (type !== 'multiple' || dayType === 'disabled' || !dayType) {
return 0;
}

const index = currentDate.findIndex(
(item) => compareDay(item, day) === 0
);
return currentDateSelectedState[index] || 1;
},

getBottomInfo(type) {
if (this.data.type === 'range') {
if (type === 'start') {
Expand Down
4 changes: 2 additions & 2 deletions packages/calendar/components/month/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<view
wx:for="{{ days }}"
wx:key="index"
style="{{ computed.getDayStyle(item.type, index, date, rowHeight, color, firstDayOfWeek) }}"
class="{{ utils.bem('calendar__day', [item.type]) }} {{ item.className }}"
style="{{ computed.getDayStyle(item.type, index, date, rowHeight, color, firstDayOfWeek, item.selectedState, extraColors) }}"
class="{{ utils.bem('calendar__day', [item.type]) }} {{ item.className }} selected-state-{{ item.selectedState }}"
data-index="{{ index }}"
bindtap="onClick"
>
Expand Down
11 changes: 7 additions & 4 deletions packages/calendar/components/month/index.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function getMark(date) {

var ROW_HEIGHT = 64;

function getDayStyle(type, index, date, rowHeight, color, firstDayOfWeek) {
function getDayStyle(type, index, date, rowHeight, color, firstDayOfWeek, selectedState, extraColors) {
var style = [];
var current = getDate(date).getDay() || 7;
var offset = current < firstDayOfWeek ? (7 - firstDayOfWeek + current) :
Expand All @@ -22,16 +22,19 @@ function getDayStyle(type, index, date, rowHeight, color, firstDayOfWeek) {
style.push(['height', rowHeight + 'px']);
}

if (color) {
if (color || extraColors.length > 0) {
if (
type === 'start' ||
type === 'end' ||
type === 'start-end' ||
type === 'multiple-selected' ||
type === 'multiple-middle'
) {
style.push(['background', color]);
} else if (type === 'middle') {
var bgColor = (extraColors || [])[selectedState - 2] || color;
if (bgColor) {
style.push(['background', bgColor]);
}
} else if (type === 'middle' && color) {
style.push(['color', color]);
}
}
Expand Down
35 changes: 32 additions & 3 deletions packages/calendar/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ type Data = {
type?: 'single' | 'multiple' | 'range';
round?: boolean;
color?: string;
extraColors?: string[];
selectedStateCount?: number;
minDate?: number;
maxDate?: number;
maxRange?: any;
position?: 'top' | 'right' | 'bottom' | 'left';
formatter?: any;
defaultDate?: number | number[];
defaultDateSelectedState?: number[];
showConfirm?: boolean;
showCalendar?: boolean;
tiledMinDate?: number;
Expand All @@ -33,10 +37,12 @@ VantComponent({
customRange: null,
customDayText: [],
customPosition: null,
multipleSelectedState: [],
},
type: 'single',
round: true,
color: '',
extraColors: [] as string[],
minDate: Date.now(),
maxDate: new Date(
new Date().getFullYear(),
Expand All @@ -46,6 +52,8 @@ VantComponent({
maxRange: undefined,
position: 'bottom',
formatter: undefined,
defaultDate: [] as number | number[],
defaultDateSelectedState: [] as number[],
showConfirm: false,
showCalendar: false,
tiledMinDate: new Date(2012, 0, 10).getTime(),
Expand All @@ -60,10 +68,16 @@ VantComponent({
console.log(event);
this.setData({ showCalendar: false });

let value = Array.isArray(event.detail)
? event.detail.map((date) => date.valueOf())
: event.detail.valueOf();

if (this.data.type === 'multiple' && this.data.selectedStateCount > 1) {
value = event.detail.date.map((date) => date.valueOf());
}

this.setData({
[`date.${this.data.id}`]: Array.isArray(event.detail)
? event.detail.map((date) => date.valueOf())
: event.detail.valueOf(),
[`date.${this.data.id}`]: value,
});
},

Expand Down Expand Up @@ -91,10 +105,15 @@ VantComponent({
console.log('closed');
},

onSelectedStateChange(event) {
console.log('selectedStateChange', event);
},

resetSettings() {
this.setData({
round: true,
color: '',
extraColors: [] as string[],
minDate: Date.now(),
maxDate: new Date(
new Date().getFullYear(),
Expand Down Expand Up @@ -147,6 +166,16 @@ VantComponent({
case 'maxRange':
data.maxRange = 3;
break;
case 'multipleSelectedState':
data.selectedStateCount = 3;
data.extraColors = ['#2baeb3', '#ff6e3c'];
// data.defaultDate = [
// new Date(2024, 5, 10).getTime(),
// new Date(2024, 4, 26).getTime(),
// new Date(2024, 8, 10).getTime(),
// ];
// data.defaultDateSelectedState = [1, 2, 3];
break;
}

this.setData(data);
Expand Down
14 changes: 14 additions & 0 deletions packages/calendar/demo/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@
value="{{ computed.formatRange(date.maxRange) }}"
bind:click="show"
/>

<van-cell
is-link
title="多个日期多个选中态"
data-type="multiple"
data-id="multipleSelectedState"
value="{{ computed.formatMultiple(date.multipleSelectedState) }}"
bind:click="show"
/>
</demo-block>

<demo-block title="平铺展示">
Expand All @@ -121,6 +130,7 @@
show="{{ showCalendar }}"
type="{{ type }}"
color="{{ color }}"
extraColors="{{ extraColors }}"
round="{{ round }}"
position="{{ position }}"
min-date="{{ minDate }}"
Expand All @@ -131,12 +141,16 @@
confirm-text="{{ confirmText }}"
confirm-disabled-text="{{ confirmDisabledText }}"
first-day-of-week="{{ firstDayOfWeek }}"
selectedStateCount="{{ selectedStateCount }}"
defaultDate="{{ defaultDate }}"
defaultDateSelectedState="{{ defaultDateSelectedState }}"
bind:confirm="onConfirm"
bind:select="onSelect"
bind:unselect="onUnselect"
bind:open="onOpen"
bind:opened="onOpened"
bind:close="onClose"
bind:closed="onClosed"
bind:selectedStateChange="onSelectedStateChange"
>
</van-calendar>
Loading