1
- import dayjs from 'dayjs' ;
2
- import { isArray } from 'lodash-es' ;
3
- import { isFunction } from 'lodash-es' ;
4
1
import { isObject } from 'lodash-es' ;
5
- import dayJsIsBetween from 'dayjs/plugin/isBetween' ;
6
-
7
- dayjs . extend ( dayJsIsBetween ) ;
8
-
2
+ import dayjs from 'dayjs' ;
9
3
import type { TdDatePickerProps , TdDateRangePickerProps } from '../type' ;
4
+ import { isEnabledDate } from '@tdesign/common-js/date-picker/utils' ;
10
5
11
6
export interface disableDateProps {
12
7
disableDate ?: TdDatePickerProps [ 'disableDate' ] | TdDateRangePickerProps [ 'disableDate' ] ;
@@ -17,60 +12,15 @@ export interface disableDateProps {
17
12
}
18
13
19
14
export function useDisableDate ( props : disableDateProps ) {
15
+ const { disableDate, format, mode, start, end } = props ;
16
+
20
17
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 } ) ,
23
19
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 ,
25
23
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 ,
27
25
} ;
28
26
}
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