Skip to content

Commit dcadd0c

Browse files
betavsuyarn
authored andcommitted
fix(date-picker): improve date processing logic for multiple selections (#3658)
* fix(date-picker): improve date processing logic for multiple selections * fix(date-picker): add multiple prop to panel for consistent behavior
1 parent 58e7ce4 commit dcadd0c

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

src/date-picker/DatePicker.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,22 +249,27 @@ export default defineComponent({
249249
}
250250

251251
function processDate(date: Date) {
252-
const val = value.value as DateMultipleValue;
253-
const isSameDate = val.some((val) => isSame(dayjs(val).toDate(), date));
252+
let isSameDate: boolean;
253+
const currentValue = (value.value || []) as DateMultipleValue;
254+
const { dayjsLocale } = global.value;
255+
254256
let currentDate: DateMultipleValue;
257+
if (props.mode !== 'week') isSameDate = currentValue.some((val) => isSame(parseToDayjs(val, formatRef.value.format).toDate(), date, props.mode, dayjsLocale));
258+
else {
259+
isSameDate = currentValue.some((val) => val === dayjs(date).locale(dayjsLocale).format(formatRef.value.format));
260+
}
255261

256262
if (!isSameDate) {
257-
currentDate = val.concat(
263+
currentDate = currentValue.concat(
258264
formatDate(date, { format: formatRef.value.format, targetFormat: formatRef.value.valueType }),
259265
);
260266
} else {
261-
currentDate = val.filter(
267+
currentDate = currentValue.filter(
262268
(val) => formatDate(val, { format: formatRef.value.format, targetFormat: formatRef.value.valueType })
263269
!== formatDate(date, { format: formatRef.value.format, targetFormat: formatRef.value.valueType }),
264270
);
265271
}
266-
267-
return currentDate.sort((a, b) => dayjs(a).valueOf() - dayjs(b).valueOf());
272+
return currentDate;
268273
}
269274

270275
const onTagRemoveClick = (ctx: TagInputRemoveContext) => {

src/date-picker/base/Table.tsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { defineComponent, PropType, computed } from 'vue';
2+
import { isArray } from 'lodash-es';
23
import TDatePickerCell from './Cell';
34
import { useConfig, usePrefixClass } from '../../hooks/useConfig';
4-
import type { TdDatePickerProps } from '../type';
5+
import type { TdDatePickerProps, DateMultipleValue } from '../type';
56
import { parseToDayjs } from '../../_common/js/date-picker/format';
67

78
export default defineComponent({
@@ -12,6 +13,7 @@ export default defineComponent({
1213
default: 'date',
1314
},
1415
firstDayOfWeek: Number,
16+
multiple: Boolean,
1517
data: Array,
1618
time: String,
1719
value: [String, Number, Array, Date],
@@ -42,18 +44,18 @@ export default defineComponent({
4244
const showThead = computed(() => props.mode === 'date' || props.mode === 'week');
4345

4446
// 高亮周区间
45-
const weekRowClass = (value: any, format: string, targetValue: Date) => {
47+
const weekRowClass = (value: any, targetValue: Date) => {
4648
if (props.mode !== 'week' || !value) return {};
4749

48-
if (Array.isArray(value)) {
50+
if (isArray(value)) {
4951
if (!value.length) return {};
50-
const [startObj, endObj] = value.map((v) => v && parseToDayjs(v, format));
52+
const [startObj, endObj] = value.map((v) => v && parseToDayjs(v, props.format));
5153
const startYear = startObj && startObj.year();
5254
const startWeek = startObj?.locale?.(dayjsLocale)?.week?.();
5355
const endYear = endObj && endObj.year();
5456
const endWeek = endObj?.locale?.(dayjsLocale)?.week?.();
5557

56-
const targetObj = parseToDayjs(targetValue, format);
58+
const targetObj = parseToDayjs(targetValue, props.format);
5759
const targetYear = targetObj.year();
5860
const targetWeek = targetObj.week();
5961
const isActive = (targetYear === startYear && targetWeek === startWeek) || (targetYear === endYear && targetWeek === endWeek);
@@ -67,21 +69,34 @@ export default defineComponent({
6769

6870
return {
6971
[`${COMPONENT_NAME.value}-${props.mode}-row--active`]:
70-
parseToDayjs(value, format).locale(dayjsLocale).week()
71-
=== parseToDayjs(targetValue, format).locale(dayjsLocale).week(),
72+
parseToDayjs(value, props.format).locale(dayjsLocale).week()
73+
=== parseToDayjs(targetValue, props.format).locale(dayjsLocale).week(),
7274
};
7375
};
7476

77+
const multipleWeekRowClass = (value: DateMultipleValue, targetValue: Date) => {
78+
const targetDayjs = parseToDayjs(targetValue, props.format);
79+
if (props.mode !== 'week' || (Array.isArray(value) && !value.length)) return {};
80+
const isSomeYearWeek = value
81+
.map?.((v) => parseToDayjs(v, props.format))
82+
.some((item) => item.week() === targetDayjs.week() && item.year() === targetDayjs.year());
83+
return {
84+
[`${COMPONENT_NAME.value}-${props.mode}-row--active`]: isSomeYearWeek,
85+
};
86+
};
87+
88+
const activeRowCss = props.multiple ? multipleWeekRowClass : weekRowClass;
89+
7590
return {
7691
COMPONENT_NAME,
7792
weekArr,
7893
showThead,
79-
weekRowClass,
94+
activeRowCss,
8095
};
8196
},
8297
render() {
8398
const {
84-
COMPONENT_NAME, weekArr, showThead, weekRowClass,
99+
COMPONENT_NAME, weekArr, showThead, activeRowCss,
85100
} = this;
86101

87102
return (
@@ -102,7 +117,7 @@ export default defineComponent({
102117
key={i}
103118
class={{
104119
[`${COMPONENT_NAME}-${this.mode}-row`]: true,
105-
...weekRowClass(this.value, this.format, row[0].value),
120+
...activeRowCss(this.value as any | DateMultipleValue[], row[0].value),
106121
}}
107122
>
108123
{row.map((col: any, j: number) => (

src/date-picker/panel/PanelContent.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default defineComponent({
2222
month: Number,
2323
tableData: Array,
2424
time: String,
25+
multiple: Boolean,
2526
popupVisible: Boolean,
2627
firstDayOfWeek: Number,
2728
partial: String,
@@ -71,6 +72,7 @@ export default defineComponent({
7172
time: this.time,
7273
value: this.value,
7374
format: this.format,
75+
multiple: this.multiple,
7476
firstDayOfWeek: this.firstDayOfWeek,
7577
onCellClick: (date: Date, { e }: { e: MouseEvent }) => this.onCellClick?.(date, { e, partial: this.partial }),
7678
onCellMouseEnter: (date: Date) => this.onCellMouseEnter?.(date, { partial: this.partial }),

0 commit comments

Comments
 (0)