-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathindex.tsx
More file actions
223 lines (203 loc) Β· 6.36 KB
/
index.tsx
File metadata and controls
223 lines (203 loc) Β· 6.36 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { clsx } from 'clsx';
import * as React from 'react';
import type { PanelMode, SharedPanelProps } from '../../interface';
import {
formatValue,
getWeekStartDate,
isSameDate,
isSameMonth,
WEEK_DAY_COUNT,
} from '../../utils/dateUtil';
import { PanelContext, useInfo } from '../context';
import PanelBody from '../PanelBody';
import PanelHeader from '../PanelHeader';
export interface DatePanelProps<DateType extends object> extends SharedPanelProps<DateType> {
panelName?: PanelMode;
rowClassName?: (date: DateType) => string;
/** Used for `WeekPanel` */
mode?: PanelMode;
cellSelection?: boolean;
}
export default function DatePanel<DateType extends object = any>(props: DatePanelProps<DateType>) {
const {
prefixCls,
panelName = 'date',
locale,
generateConfig,
pickerValue,
onPickerValueChange,
onModeChange,
mode = 'date',
disabledDate,
onSelect,
onHover,
showWeek,
} = props;
const panelPrefixCls = `${prefixCls}-${panelName}-panel`;
const cellPrefixCls = `${prefixCls}-cell`;
const isWeek = mode === 'week';
// ========================== Base ==========================
const [info, now] = useInfo(props, mode);
const weekFirstDay = generateConfig.locale.getWeekFirstDay(locale.locale);
const monthStartDate = generateConfig.setDate(pickerValue, 1);
const baseDate = getWeekStartDate(locale.locale, generateConfig, monthStartDate);
const month = generateConfig.getMonth(pickerValue);
// =========================== PrefixColumn ===========================
const showPrefixColumn = showWeek === undefined ? isWeek : showWeek;
const prefixColumn = showPrefixColumn
? (date: DateType) => {
// >>> Additional check for disabled
const disabled = disabledDate?.(date, { type: 'week' });
return (
<td
key="week"
className={clsx(cellPrefixCls, `${cellPrefixCls}-week`, {
[`${cellPrefixCls}-disabled`]: disabled,
})}
// Operation: Same as code in PanelBody
onClick={() => {
if (!disabled) {
onSelect(date);
}
}}
onMouseEnter={() => {
if (!disabled) {
onHover?.(date);
}
}}
onMouseLeave={() => {
if (!disabled) {
onHover?.(null);
}
}}
>
<div className={`${cellPrefixCls}-inner`}>
{generateConfig.locale.getWeek(locale.locale, date)}
</div>
</td>
);
}
: null;
// ========================= Cells ==========================
// >>> Header Cells
const headerCells: React.ReactNode[] = [];
const weekDaysLocale: string[] =
locale.shortWeekDays ||
(generateConfig.locale.getShortWeekDays
? generateConfig.locale.getShortWeekDays(locale.locale)
: []);
if (prefixColumn) {
headerCells.push(
<th key="empty">
<span style={{ width: 0, height: 0, position: 'absolute', overflow: 'hidden', opacity: 0 }}>
{locale.week}
</span>
</th>,
);
}
for (let i = 0; i < WEEK_DAY_COUNT; i += 1) {
headerCells.push(<th key={i}>{weekDaysLocale[(i + weekFirstDay) % WEEK_DAY_COUNT]}</th>);
}
// >>> Body Cells
const getCellDate = (date: DateType, offset: number) => {
return generateConfig.addDate(date, offset);
};
const getCellText = (date: DateType) => {
return formatValue(date, {
locale,
format: locale.cellDateFormat,
generateConfig,
});
};
const getCellClassName = (date: DateType) => {
const classObj = {
[`${prefixCls}-cell-in-view`]: isSameMonth(generateConfig, date, pickerValue),
[`${prefixCls}-cell-today`]: isSameDate(generateConfig, date, now),
};
return classObj;
};
// ========================= Header =========================
const monthsLocale: string[] =
locale.shortMonths ||
(generateConfig.locale.getShortMonths
? generateConfig.locale.getShortMonths(locale.locale)
: []);
const yearNode: React.ReactNode = (
<button
type="button"
aria-label={locale.yearSelect}
key="year"
onClick={() => {
onModeChange('year', pickerValue);
}}
tabIndex={-1}
className={`${prefixCls}-year-btn`}
>
{formatValue(pickerValue, {
locale,
format: locale.yearFormat,
generateConfig,
})}
</button>
);
const monthNode: React.ReactNode = (
<button
type="button"
aria-label={locale.monthSelect}
key="month"
onClick={() => {
onModeChange('month', pickerValue);
}}
tabIndex={-1}
className={`${prefixCls}-month-btn`}
>
{locale.monthFormat
? formatValue(pickerValue, {
locale,
format: locale.monthFormat,
generateConfig,
})
: monthsLocale[month]}
</button>
);
const monthYearNodes = locale.monthBeforeYear ? [monthNode, yearNode] : [yearNode, monthNode];
// ========================= Render =========================
return (
<PanelContext.Provider value={info}>
<div className={clsx(panelPrefixCls, showWeek && `${panelPrefixCls}-show-week`)}>
{/* Header */}
<PanelHeader<DateType>
offset={(distance) => generateConfig.addMonth(pickerValue, distance)}
superOffset={(distance) => generateConfig.addYear(pickerValue, distance)}
onChange={onPickerValueChange}
// Limitation
getStart={(date) => generateConfig.setDate(date, 1)}
getEnd={(date) => {
let clone = generateConfig.setDate(date, 1);
clone = generateConfig.addMonth(clone, 1);
return generateConfig.addDate(clone, -1);
}}
>
{monthYearNodes}
</PanelHeader>
{/* Body */}
<PanelBody
titleFormat={locale.fieldDateFormat}
{...props}
colNum={WEEK_DAY_COUNT}
rowNum={6}
baseDate={baseDate}
// Header
headerCells={headerCells}
// Body
getCellDate={getCellDate}
getCellText={getCellText}
getCellClassName={getCellClassName}
prefixColumn={prefixColumn}
cellSelection={!isWeek}
onChange={onPickerValueChange}
/>
</div>
</PanelContext.Provider>
);
}