DateTimePickerDays.js
3.7 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
import React, { Component, PropTypes } from "react";
import moment from "moment";
import cx from "classnames";
import s from "./DateTimePicker.scss";
export default class DateTimePickerDays extends Component {
static propTypes = {
subtractMonth: PropTypes.func.isRequired,
addMonth: PropTypes.func.isRequired,
viewDate: PropTypes.object.isRequired,
selectedDate: PropTypes.object.isRequired,
showToday: PropTypes.bool,
daysOfWeekDisabled: PropTypes.array,
setSelectedDate: PropTypes.func.isRequired,
showMonths: PropTypes.func.isRequired,
minDate: PropTypes.object,
maxDate: PropTypes.object
}
static defaultProps = {
showToday: true
}
renderDays = () => {
var cells, classes, days, html, month, nextMonth, prevMonth, minDate, maxDate, row, year;
year = this.props.viewDate.year();
month = this.props.viewDate.month();
prevMonth = this.props.viewDate.clone().subtract(1, "months");
days = prevMonth.daysInMonth();
prevMonth.date(days).startOf("week");
nextMonth = moment(prevMonth).clone().add(42, "d");
minDate = this.props.minDate ? this.props.minDate.clone().subtract(1, "days") : this.props.minDate;
maxDate = this.props.maxDate ? this.props.maxDate.clone() : this.props.maxDate;
html = [];
cells = [];
while (prevMonth.isBefore(nextMonth)) {
classes = {
day: true
};
if (prevMonth.year() < year || (prevMonth.year() === year && prevMonth.month() < month)) {
classes.old = true;
} else if (prevMonth.year() > year || (prevMonth.year() === year && prevMonth.month() > month)) {
classes.new = true;
}
if (prevMonth.isSame(moment({
y: this.props.selectedDate.year(),
M: this.props.selectedDate.month(),
d: this.props.selectedDate.date()
}))) {
classes.active = true;
classes[s.active]=true;
}
if (this.props.showToday) {
if (prevMonth.isSame(moment(), "day")) {
classes.today = true;
classes[s.today] = true;
}
}
if ((minDate && prevMonth.isBefore(minDate)) || (maxDate && prevMonth.isAfter(maxDate))) {
classes.disabled = true;
}
if (this.props.daysOfWeekDisabled.length > 0) classes.disabled = this.props.daysOfWeekDisabled.indexOf(prevMonth.day()) !== -1;
cells.push(<td className={cx(classes,s.day)} key={prevMonth.month() + "-" + prevMonth.date()} onClick={this.props.setSelectedDate}>{prevMonth.date()}</td>);
if (prevMonth.weekday() === moment().endOf("week").weekday()) {
row = <tr key={prevMonth.month() + "-" + prevMonth.date()}>{cells}</tr>;
html.push(row);
cells = [];
}
prevMonth.add(1, "d");
}
return html;
}
render() {
return (
<div className="datepicker-days" style={{display: "block"}}>
<table className="table-condensed">
<thead>
<tr>
<th className="prev" onClick={this.props.subtractMonth}><span className={cx(s.arrow_left)} /></th>
<th className="switch" colSpan="5" onClick={this.props.showMonths}>{moment.months()[this.props.viewDate.month()]} {this.props.viewDate.year()}</th>
<th className="next" onClick={this.props.addMonth}><span className={cx(s.arrow_right)} /></th>
</tr>
<tr>
<th className="dow">一</th>
<th className="dow">二</th>
<th className="dow">三</th>
<th className="dow">四</th>
<th className="dow">五</th>
<th className="dow">六</th>
<th className="dow">日</th>
</tr>
</thead>
<tbody>
{this.renderDays()}
</tbody>
</table>
</div>
);
}
}