generateCalendar.js
7.9 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
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import * as React from 'react';
import useMergedState from "rc-util/es/hooks/useMergedState";
import classNames from 'classnames';
import padStart from 'lodash/padStart';
import { PickerPanel as RCPickerPanel } from 'rc-picker';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import enUS from './locale/en_US';
import { ConfigContext } from '../config-provider';
import CalendarHeader from './Header';
function generateCalendar(generateConfig) {
function isSameYear(date1, date2) {
return date1 && date2 && generateConfig.getYear(date1) === generateConfig.getYear(date2);
}
function isSameMonth(date1, date2) {
return isSameYear(date1, date2) && generateConfig.getMonth(date1) === generateConfig.getMonth(date2);
}
function isSameDate(date1, date2) {
return isSameMonth(date1, date2) && generateConfig.getDate(date1) === generateConfig.getDate(date2);
}
var Calendar = function Calendar(props) {
var customizePrefixCls = props.prefixCls,
className = props.className,
style = props.style,
dateFullCellRender = props.dateFullCellRender,
dateCellRender = props.dateCellRender,
monthFullCellRender = props.monthFullCellRender,
monthCellRender = props.monthCellRender,
headerRender = props.headerRender,
value = props.value,
defaultValue = props.defaultValue,
disabledDate = props.disabledDate,
mode = props.mode,
validRange = props.validRange,
_props$fullscreen = props.fullscreen,
fullscreen = _props$fullscreen === void 0 ? true : _props$fullscreen,
onChange = props.onChange,
onPanelChange = props.onPanelChange,
onSelect = props.onSelect;
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var prefixCls = getPrefixCls('picker', customizePrefixCls);
var calendarPrefixCls = "".concat(prefixCls, "-calendar");
var today = generateConfig.getNow(); // ====================== State =======================
// Value
var _useMergedState = useMergedState(function () {
return value || generateConfig.getNow();
}, {
defaultValue: defaultValue,
value: value
}),
_useMergedState2 = _slicedToArray(_useMergedState, 2),
mergedValue = _useMergedState2[0],
setMergedValue = _useMergedState2[1]; // Mode
var _useMergedState3 = useMergedState('month', {
value: mode
}),
_useMergedState4 = _slicedToArray(_useMergedState3, 2),
mergedMode = _useMergedState4[0],
setMergedMode = _useMergedState4[1];
var panelMode = React.useMemo(function () {
return mergedMode === 'year' ? 'month' : 'date';
}, [mergedMode]); // Disabled Date
var mergedDisabledDate = React.useCallback(function (date) {
var notInRange = validRange ? generateConfig.isAfter(validRange[0], date) || generateConfig.isAfter(date, validRange[1]) : false;
return notInRange || !!(disabledDate === null || disabledDate === void 0 ? void 0 : disabledDate(date));
}, [disabledDate, validRange]); // ====================== Events ======================
var triggerPanelChange = function triggerPanelChange(date, newMode) {
if (onPanelChange) {
onPanelChange(date, newMode);
}
};
var triggerChange = function triggerChange(date) {
setMergedValue(date);
if (!isSameDate(date, mergedValue)) {
// Trigger when month panel switch month
if (panelMode === 'date' && !isSameMonth(date, mergedValue) || panelMode === 'month' && !isSameYear(date, mergedValue)) {
triggerPanelChange(date, mergedMode);
}
if (onChange) {
onChange(date);
}
}
};
var triggerModeChange = function triggerModeChange(newMode) {
setMergedMode(newMode);
triggerPanelChange(mergedValue, newMode);
};
var onInternalSelect = function onInternalSelect(date) {
triggerChange(date);
if (onSelect) {
onSelect(date);
}
}; // ====================== Locale ======================
var getDefaultLocale = function getDefaultLocale() {
var locale = props.locale;
var result = _extends(_extends({}, enUS), locale);
result.lang = _extends(_extends({}, result.lang), (locale || {}).lang);
return result;
}; // ====================== Render ======================
var dateRender = React.useCallback(function (date) {
if (dateFullCellRender) {
return dateFullCellRender(date);
}
return /*#__PURE__*/React.createElement("div", {
className: classNames("".concat(prefixCls, "-cell-inner"), "".concat(calendarPrefixCls, "-date"), _defineProperty({}, "".concat(calendarPrefixCls, "-date-today"), isSameDate(today, date)))
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(calendarPrefixCls, "-date-value")
}, padStart(String(generateConfig.getDate(date)), 2, '0')), /*#__PURE__*/React.createElement("div", {
className: "".concat(calendarPrefixCls, "-date-content")
}, dateCellRender && dateCellRender(date)));
}, [dateFullCellRender, dateCellRender]);
var monthRender = React.useCallback(function (date, locale) {
if (monthFullCellRender) {
return monthFullCellRender(date);
}
var months = locale.shortMonths || generateConfig.locale.getShortMonths(locale.locale);
return /*#__PURE__*/React.createElement("div", {
className: classNames("".concat(prefixCls, "-cell-inner"), "".concat(calendarPrefixCls, "-date"), _defineProperty({}, "".concat(calendarPrefixCls, "-date-today"), isSameMonth(today, date)))
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(calendarPrefixCls, "-date-value")
}, months[generateConfig.getMonth(date)]), /*#__PURE__*/React.createElement("div", {
className: "".concat(calendarPrefixCls, "-date-content")
}, monthCellRender && monthCellRender(date)));
}, [monthFullCellRender, monthCellRender]);
return /*#__PURE__*/React.createElement(LocaleReceiver, {
componentName: "Calendar",
defaultLocale: getDefaultLocale
}, function (mergedLocale) {
var _classNames3;
return /*#__PURE__*/React.createElement("div", {
className: classNames(calendarPrefixCls, (_classNames3 = {}, _defineProperty(_classNames3, "".concat(calendarPrefixCls, "-full"), fullscreen), _defineProperty(_classNames3, "".concat(calendarPrefixCls, "-mini"), !fullscreen), _defineProperty(_classNames3, "".concat(calendarPrefixCls, "-rtl"), direction === 'rtl'), _classNames3), className),
style: style
}, headerRender ? headerRender({
value: mergedValue,
type: mergedMode,
onChange: onInternalSelect,
onTypeChange: triggerModeChange
}) : /*#__PURE__*/React.createElement(CalendarHeader, {
prefixCls: calendarPrefixCls,
value: mergedValue,
generateConfig: generateConfig,
mode: mergedMode,
fullscreen: fullscreen,
locale: mergedLocale.lang,
validRange: validRange,
onChange: onInternalSelect,
onModeChange: triggerModeChange
}), /*#__PURE__*/React.createElement(RCPickerPanel, {
value: mergedValue,
prefixCls: prefixCls,
locale: mergedLocale.lang,
generateConfig: generateConfig,
dateRender: dateRender,
monthCellRender: function monthCellRender(date) {
return monthRender(date, mergedLocale.lang);
},
onSelect: onInternalSelect,
mode: panelMode,
picker: panelMode,
disabledDate: mergedDisabledDate,
hideHeader: true
}));
});
};
return Calendar;
}
export default generateCalendar;