-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
253 lines (214 loc) · 5.81 KB
/
index.jsx
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import React from 'react';
import PropTypes from 'prop-types';
import Icon from 'antd/lib/icon';
import './styles.scss';
export default class Calendar extends React.Component {
static defaultProps = {
minDate: '',
maxDate: '',
onItemClick: () => {},
}
static propTypes = {
minDate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Date),
]),
maxDate: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Date),
]),
onItemClick: PropTypes.func,
}
static WeekDays = ['日', '一', '二', '三', '四', '五', '六']
static Head = CalendarHead
static Body = CalendarBody
state = {
year: '',
month: '',
days: '', // all valid days in the given date range
date: '',
}
componentWillMount() {
let { minDate, maxDate } = this.props;
this.setState({
current: minDate
});
minDate = toValidDate(toValidDate(minDate).setHours(0, 0, 0, 0));
maxDate = toValidDate(toValidDate(maxDate).setHours(23, 59, 59, 999));
this.minDate = {
year: minDate.getFullYear(),
month: minDate.getMonth(),
date: minDate.getDate(),
valueOf() {
return +minDate;
}
};
this.maxDate = {
year: maxDate.getFullYear(),
month: maxDate.getMonth(),
date: maxDate.getDate(),
valueOf() {
return +maxDate;
}
};
this.geneValidDays();
}
preventCalendar = (ele) => {
this.calender = ele;
// 阻止页面滚动
this.calender.addEventListener('touchmove', (e) => {
e.preventDefault();
}, false);
}
/**
* generates days
* @param {Boolean} operate (true => 'plus') or (false => 'minus')
*/
geneValidDays = (operate = undefined) => {
let { year, month, date } = this.minDate;
this.setState(prevState => {
const { year: prevYear, month: prevMonth } = prevState;
// handle the left or right toggle of the pannel
if (`${operate}` && operate !== undefined) {
year = prevYear !== undefined ? prevYear : year;
month = prevMonth !== undefined ? prevMonth : month;
if (operate) {
if (month + 1 > 11) {
year += 1;
month = 0;
} else {
month += 1;
}
} else if (operate === false) {
if (month - 1 < 0) {
year -= 1;
month = 11;
} else {
month -= 1;
}
}
}
// generates the all days of current month
let days = Array.from({ length: new Date(year, month + 1, 0).getDate() }, (_, i) => {
const dayVal = i + 1;
const dateVal = new Date(year, month, dayVal);
return {
disabled: !validRange(+dateVal, [+this.minDate, +this.maxDate]),
currentMonth: true,
value: +dateVal,
date: dateVal,
valueOf() {
return dayVal;
},
toString() {
return date;
}
};
});
// all empty date cells at current month start pos
const startDay = days[0].date.getDay();
// all empty date cells at current month end pos
const restDayLen = 42 - startDay - days.length;
days = [
...Array(startDay).fill(null),
...days,
...Array(restDayLen < 7 ? restDayLen : restDayLen - 7).fill(null)
];
return ({
year,
month,
days,
date,
});
});
}
handleItemClick = (t) => {
const { onDateClick } = this.props;
const { current } = this.state;
if (t.disabled) return null;
else if (current !== t.value) {
this.setState({
current: t.value
}, () => {
onDateClick && typeof onDateClick === 'function' && onDateClick(t.value);
});
}
}
render() {
return (
<div className="calendar">
<Calendar.Head {...this.state} onArrowClick={this.geneValidDays} />
<Calendar.Body {...this.state} onItemClick={this.handleItemClick} />
</div>
);
}
}
/**
* toValidDate
* @param {*} param
*/
function toValidDate(param) {
if (!param) throw new TypeError();
if (!(param instanceof Date)) {
param = new Date(param);
}
return param;
}
/**
* validRange
* @param {*} current
* @param {*} range [start, end)
*/
const validRange = (
current,
range = [0, Infinity]
) => {
range = range.map(t => +t);
const min = Math.min(...range);
const max = Math.max(...range);
return (current >= min && current < max);
};
function CalendarHead({ year, month, onArrowClick }) {
return (
<div className="calendar-head">
<div className="calendar-head__date">
<Icon type="left" onClick={() => onArrowClick(false)} />
<div className="calendar-head__date__time">
{`${year}年${month + 1}月`}
</div>
<Icon type="right" onClick={() => onArrowClick(true)} />
</div>
<div className="calendar-head__week">
{
Calendar.WeekDays.map(t => (
<div key={t} className="calendar-head__week__cell">{t}</div>
))
}
</div>
</div>
);
}
function CalendarBody({ current, days, onItemClick }) {
return (
<div className="calendar-body">
{
days.map((t, i) => {
const classNames = ['calendar-body__cell__item'];
if (t && typeof t === 'object') {
(t.disabled) && classNames.push('disabled');
(+current >= t.value && +current < t.date.setHours(23, 59, 59, 999)) && classNames.push('current');
}
return (
<div key={t ? t.value : i} className="calendar-body__cell">
<div className={classNames.join(' ')} onClick={() => onItemClick(t)}>
{t && +t}
</div>
</div>
);
})
}
</div>
);
}