Skip to content

Commit 3649aad

Browse files
authored
refactor(DatePicker): useDisableDate to use common isEnabledDate (#5119)
fix(date-picker): disable date range error chore: remove unnecessary explicit type declarations close #4927
1 parent ca2697f commit 3649aad

File tree

2 files changed

+17
-67
lines changed

2 files changed

+17
-67
lines changed

packages/components/date-picker/components/panel/RangePanel.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ export default defineComponent({
7979
const startTableData = computed(() =>
8080
useTableData({
8181
isRange: true,
82-
start: props.value[0] ? parseToDayjs(props.value[0] as string, format.value).toDate() : undefined,
83-
end: props.value[1] ? parseToDayjs(props.value[1] as string, format.value).toDate() : undefined,
82+
start: props.value[0] ? parseToDayjs(props.value[0], format.value).toDate() : undefined,
83+
end: props.value[1] ? parseToDayjs(props.value[1], format.value).toDate() : undefined,
8484
hoverStart:
8585
!hidePreselection && props.hoverValue[0]
86-
? parseToDayjs(props.hoverValue[0] as string, format.value).toDate()
86+
? parseToDayjs(props.hoverValue[0], format.value).toDate()
8787
: undefined,
8888
hoverEnd:
8989
!hidePreselection && props.hoverValue[1]
90-
? parseToDayjs(props.hoverValue[1] as string, format.value).toDate()
90+
? parseToDayjs(props.hoverValue[1], format.value).toDate()
9191
: undefined,
9292
year: props.year[0],
9393
month: props.month[0],
@@ -101,15 +101,15 @@ export default defineComponent({
101101
const endTableData = computed(() =>
102102
useTableData({
103103
isRange: true,
104-
start: props.value[0] ? parseToDayjs(props.value[0] as string, format.value).toDate() : undefined,
105-
end: props.value[1] ? parseToDayjs(props.value[1] as string, format.value).toDate() : undefined,
104+
start: props.value[0] ? parseToDayjs(props.value[0], format.value).toDate() : undefined,
105+
end: props.value[1] ? parseToDayjs(props.value[1], format.value).toDate() : undefined,
106106
hoverStart:
107107
!hidePreselection && props.hoverValue[0]
108-
? parseToDayjs(props.hoverValue[0] as string, format.value).toDate()
108+
? parseToDayjs(props.hoverValue[0], format.value).toDate()
109109
: undefined,
110110
hoverEnd:
111111
!hidePreselection && props.hoverValue[1]
112-
? parseToDayjs(props.hoverValue[1] as string, format.value).toDate()
112+
? parseToDayjs(props.hoverValue[1], format.value).toDate()
113113
: undefined,
114114
year: props.year[1],
115115
month: props.month[1],
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import dayjs from 'dayjs';
2-
import { isArray } from 'lodash-es';
3-
import { isFunction } from 'lodash-es';
41
import { isObject } from 'lodash-es';
5-
import dayJsIsBetween from 'dayjs/plugin/isBetween';
6-
7-
dayjs.extend(dayJsIsBetween);
8-
2+
import dayjs from 'dayjs';
93
import type { TdDatePickerProps, TdDateRangePickerProps } from '../type';
4+
import { isEnabledDate } from '@tdesign/common-js/date-picker/utils';
105

116
export interface disableDateProps {
127
disableDate?: TdDatePickerProps['disableDate'] | TdDateRangePickerProps['disableDate'];
@@ -17,60 +12,15 @@ export interface disableDateProps {
1712
}
1813

1914
export function useDisableDate(props: disableDateProps) {
15+
const { disableDate, format, mode, start, end } = props;
16+
2017
return {
21-
disableDate: (value: Date) =>
22-
!isEnabled({ disableDate: props.disableDate, format: props.format, mode: props.mode, value }),
18+
disableDate: (value: Date) => !isEnabledDate({ disableDate, format, mode, value }),
2319
minDate:
24-
isObject(props.disableDate) && 'before' in props.disableDate ? new Date(props.disableDate.before) : props.start,
20+
isObject(disableDate) && 'before' in disableDate
21+
? new Date(dayjs(disableDate.before).startOf('day').format())
22+
: start,
2523
maxDate:
26-
isObject(props.disableDate) && 'after' in props.disableDate ? new Date(props.disableDate.after) : props.end,
24+
isObject(disableDate) && 'after' in disableDate ? new Date(dayjs(disableDate.after).endOf('day').format()) : end,
2725
};
2826
}
29-
30-
function isEnabled(props: any): boolean {
31-
if (!props.disableDate) return true;
32-
33-
let isEnabled = true;
34-
// 值类型为 Function 则表示返回值为 true 的日期会被禁用
35-
if (isFunction(props.disableDate)) {
36-
return !props.disableDate(props.value);
37-
}
38-
39-
// 禁用日期,示例:['A', 'B'] 表示日期 A 和日期 B 会被禁用。
40-
if (isArray(props.disableDate)) {
41-
let isIncludes = false;
42-
const formatedDisabledDate = props.disableDate.map((item: string) => dayjs(item, props.format));
43-
formatedDisabledDate.forEach((item: any) => {
44-
if (item.isSame(dayjs(props.value))) {
45-
isIncludes = true;
46-
}
47-
});
48-
return !isIncludes;
49-
}
50-
51-
// { from: 'A', to: 'B' } 表示在 A 到 B 之间的日期会被禁用。
52-
if (props.disableDate.from && props.disableDate.to) {
53-
const compareMin = dayjs(new Date(props.disableDate.from));
54-
const compareMax = dayjs(new Date(props.disableDate.to));
55-
56-
return !dayjs(props.value).isBetween(compareMin, compareMax, props.mode, '[]');
57-
}
58-
59-
const min = props.disableDate.before ? new Date(props.disableDate.before) : null;
60-
const max = props.disableDate.after ? new Date(props.disableDate.after) : null;
61-
62-
// { before: 'A', after: 'B' } 表示在 A 之前和在 B 之后的日期都会被禁用。
63-
if (max && min) {
64-
const compareMin = dayjs(new Date(min));
65-
const compareMax = dayjs(new Date(max));
66-
67-
isEnabled = dayjs(props.value).isBetween(compareMin, compareMax, props.mode, '[]');
68-
} else if (min) {
69-
const compareMin = dayjs(new Date(min));
70-
isEnabled = !dayjs(props.value).isBefore(compareMin, props.mode);
71-
} else if (max) {
72-
const compareMax = dayjs(new Date(max));
73-
isEnabled = !dayjs(props.value).isAfter(compareMax, props.mode);
74-
}
75-
return isEnabled;
76-
}

0 commit comments

Comments
 (0)