forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day.jsx
211 lines (184 loc) · 5.49 KB
/
day.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
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import {
getDay,
getMonth,
getDate,
newDate,
isSameDay,
isDayDisabled,
isDayInRange,
isEqual,
isBefore,
isAfter,
getDayOfWeekCode,
formatDate
} from './date_utils';
export default class Day extends React.Component {
static propTypes = {
disabledKeyboardNavigation: PropTypes.bool,
day: PropTypes.instanceOf(Date).isRequired,
dayClassName: PropTypes.func,
endDate: PropTypes.instanceOf(Date),
highlightDates: PropTypes.instanceOf(Map),
inline: PropTypes.bool,
month: PropTypes.number,
onClick: PropTypes.func,
onMouseEnter: PropTypes.func,
preSelection: PropTypes.instanceOf(Date),
selected: PropTypes.object,
selectingDate: PropTypes.instanceOf(Date),
selectsEnd: PropTypes.bool,
selectsStart: PropTypes.bool,
startDate: PropTypes.instanceOf(Date),
renderDayContents: PropTypes.func
};
handleClick = event => {
if (!this.isDisabled() && this.props.onClick) {
this.props.onClick(event);
}
};
handleMouseEnter = event => {
if (!this.isDisabled() && this.props.onMouseEnter) {
this.props.onMouseEnter(event);
}
};
isSameDay = other => isSameDay(this.props.day, other);
isKeyboardSelected = () =>
!this.props.disabledKeyboardNavigation &&
!this.props.inline &&
!this.isSameDay(this.props.selected) &&
this.isSameDay(this.props.preSelection);
isDisabled = () => isDayDisabled(this.props.day, this.props);
getHighLightedClass = defaultClassName => {
const { day, highlightDates } = this.props;
if (!highlightDates) {
return false;
}
// Looking for className in the Map of {'day string, 'className'}
const dayStr = formatDate(day, 'MM.dd.yyyy');
return highlightDates.get(dayStr);
};
isInRange = () => {
const { day, startDate, endDate } = this.props;
if (!startDate || !endDate) {
return false;
}
return isDayInRange(day, startDate, endDate);
};
isInSelectingRange = () => {
const {
day,
selectsStart,
selectsEnd,
selectingDate,
startDate,
endDate
} = this.props;
if (!(selectsStart || selectsEnd) || !selectingDate || this.isDisabled()) {
return false;
}
if (
selectsStart &&
endDate &&
(isBefore(selectingDate, endDate) || isEqual(selectingDate, endDate))
) {
return isDayInRange(day, selectingDate, endDate);
}
if (
selectsEnd &&
startDate &&
(isAfter(selectingDate, startDate) || isEqual(selectingDate, startDate))
) {
return isDayInRange(day, startDate, selectingDate);
}
return false;
};
isSelectingRangeStart = () => {
if (!this.isInSelectingRange()) {
return false;
}
const { day, selectingDate, startDate, selectsStart } = this.props;
if (selectsStart) {
return isSameDay(day, selectingDate);
} else {
return isSameDay(day, startDate);
}
};
isSelectingRangeEnd = () => {
if (!this.isInSelectingRange()) {
return false;
}
const { day, selectingDate, endDate, selectsEnd } = this.props;
if (selectsEnd) {
return isSameDay(day, selectingDate);
} else {
return isSameDay(day, endDate);
}
};
isRangeStart = () => {
const { day, startDate, endDate } = this.props;
if (!startDate || !endDate) {
return false;
}
return isSameDay(startDate, day);
};
isRangeEnd = () => {
const { day, startDate, endDate } = this.props;
if (!startDate || !endDate) {
return false;
}
return isSameDay(endDate, day);
};
isWeekend = () => {
const weekday = getDay(this.props.day);
return weekday === 0 || weekday === 6;
};
isOutsideMonth = () => {
return (
this.props.month !== undefined &&
this.props.month !== getMonth(this.props.day)
);
};
getClassNames = date => {
const dayClassName = this.props.dayClassName
? this.props.dayClassName(date)
: undefined;
return classnames(
'react-datepicker__day',
dayClassName,
'react-datepicker__day--' + getDayOfWeekCode(this.props.day),
{
'react-datepicker__day--disabled': this.isDisabled(),
'react-datepicker__day--selected': this.isSameDay(this.props.selected),
'react-datepicker__day--keyboard-selected': this.isKeyboardSelected(),
'react-datepicker__day--range-start': this.isRangeStart(),
'react-datepicker__day--range-end': this.isRangeEnd(),
'react-datepicker__day--in-range': this.isInRange(),
'react-datepicker__day--in-selecting-range': this.isInSelectingRange(),
'react-datepicker__day--selecting-range-start': this.isSelectingRangeStart(),
'react-datepicker__day--selecting-range-end': this.isSelectingRangeEnd(),
'react-datepicker__day--today': this.isSameDay(newDate()),
'react-datepicker__day--weekend': this.isWeekend(),
'react-datepicker__day--outside-month': this.isOutsideMonth()
},
this.getHighLightedClass('react-datepicker__day--highlighted')
);
};
render() {
return (
<div
className={this.getClassNames(this.props.day)}
onClick={this.handleClick}
onMouseEnter={this.handleMouseEnter}
aria-label={`day-${getDate(this.props.day)}`}
role="option"
>
{this.props.renderDayContents
? this.props.renderDayContents(getDate(this.props.day))
: getDate(this.props.day)}
</div>
);
}
}