-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathDatePickerCaption.tsx
91 lines (85 loc) · 2.15 KB
/
DatePickerCaption.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { useMemo } from 'react';
import { IProps, HTMLDivProps } from '@uiw/utils';
import './style/caption.less';
function classnames(...arg: (string | null | undefined)[]) {
return [...arg].filter(Boolean).join(' ').trim();
}
export type DatePickerCaptionType =
| 'prev'
| 'month'
| 'year'
| 'today'
| 'next';
function noop() {}
export interface DatePickerCaptionProps extends IProps, HTMLDivProps {
panelDate?: Date;
monthLabel?: React.ReactNode[];
todayButton?: string;
onSelected?: (captionType: DatePickerCaptionType) => void;
}
export function DatePickerCaption(props: DatePickerCaptionProps) {
const {
prefixCls = 'w-datepicker',
className,
panelDate = new Date(),
monthLabel,
onSelected = noop,
todayButton,
...other
} = props;
const renderMonth = useMemo(() => {
const month = panelDate.getMonth();
return (monthLabel && monthLabel[month]) || month + 1;
}, [panelDate.toDateString(), monthLabel]);
return (
<div
className={classnames(
prefixCls ? `${prefixCls}-caption` : null,
className,
)}
{...other}
>
<div
className={classnames(
prefixCls ? `${prefixCls}-caption-pane` : null,
'prev',
)}
onClick={() => onSelected('prev')}
/>
<div
className={classnames(
prefixCls ? `${prefixCls}-caption-pane` : null,
'month',
)}
onClick={() => onSelected('month')}
>
{renderMonth}
</div>
<div
className={classnames(
prefixCls ? `${prefixCls}-caption-pane` : null,
'year',
)}
onClick={() => onSelected('year')}
>
{panelDate!.getFullYear()}
</div>
{todayButton && (
<div
className={classnames(
prefixCls ? `${prefixCls}-caption-today` : null,
)}
onClick={() => onSelected('today')}
title={todayButton}
/>
)}
<div
className={classnames(
prefixCls ? `${prefixCls}-caption-pane` : null,
'next',
)}
onClick={() => onSelected('next')}
/>
</div>
);
}