/
CalendarMonthGrid.jsx
199 lines (169 loc) · 5.44 KB
/
CalendarMonthGrid.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
import React, { PropTypes } from 'react';
import shallowCompare from 'react-addons-shallow-compare';
import momentPropTypes from 'react-moment-proptypes';
import { forbidExtraProps } from 'airbnb-prop-types';
import moment from 'moment';
import cx from 'classnames';
import { addEventListener, removeEventListener } from 'consolidated-events';
import CalendarMonth from './CalendarMonth';
import isTransitionEndSupported from '../utils/isTransitionEndSupported';
import getTransformStyles from '../utils/getTransformStyles';
import ScrollableOrientationShape from '../shapes/ScrollableOrientationShape';
import {
HORIZONTAL_ORIENTATION,
VERTICAL_ORIENTATION,
VERTICAL_SCROLLABLE,
} from '../../constants';
const propTypes = forbidExtraProps({
enableOutsideDays: PropTypes.bool,
firstVisibleMonthIndex: PropTypes.number,
initialMonth: momentPropTypes.momentObj,
isAnimating: PropTypes.bool,
numberOfMonths: PropTypes.number,
modifiers: PropTypes.object,
orientation: ScrollableOrientationShape,
onDayClick: PropTypes.func,
onDayMouseEnter: PropTypes.func,
onDayMouseLeave: PropTypes.func,
onMonthTransitionEnd: PropTypes.func,
renderDay: PropTypes.func,
transformValue: PropTypes.string,
// i18n
monthFormat: PropTypes.string,
});
const defaultProps = {
enableOutsideDays: false,
firstVisibleMonthIndex: 0,
initialMonth: moment(),
isAnimating: false,
numberOfMonths: 1,
modifiers: {},
orientation: HORIZONTAL_ORIENTATION,
onDayClick() {},
onDayMouseEnter() {},
onDayMouseLeave() {},
onMonthTransitionEnd() {},
renderDay: null,
transformValue: 'none',
// i18n
monthFormat: 'MMMM YYYY', // english locale
};
function getMonths(initialMonth, numberOfMonths) {
let month = initialMonth.clone().subtract(1, 'month');
const months = [];
for (let i = 0; i < numberOfMonths + 2; i += 1) {
months.push(month);
month = month.clone().add(1, 'month');
}
return months;
}
export default class CalendarMonthGrid extends React.Component {
constructor(props) {
super(props);
this.state = {
months: getMonths(props.initialMonth, props.numberOfMonths),
};
this.isTransitionEndSupported = isTransitionEndSupported();
this.onTransitionEnd = this.onTransitionEnd.bind(this);
}
componentDidMount() {
this.eventHandle = addEventListener(
this.container,
'transitionend',
this.onTransitionEnd,
);
}
componentWillReceiveProps(nextProps) {
const { initialMonth, numberOfMonths } = nextProps;
const { months } = this.state;
const hasMonthChanged = !this.props.initialMonth.isSame(initialMonth, 'month');
const hasNumberOfMonthsChanged = this.props.numberOfMonths !== numberOfMonths;
let newMonths = months;
if (hasMonthChanged && !hasNumberOfMonthsChanged) {
if (initialMonth.isAfter(this.props.initialMonth)) {
newMonths = months.slice(1);
newMonths.push(months[months.length - 1].clone().add(1, 'month'));
} else {
newMonths = months.slice(0, months.length - 1);
newMonths.unshift(months[0].clone().subtract(1, 'month'));
}
}
if (hasNumberOfMonthsChanged) {
newMonths = getMonths(initialMonth, numberOfMonths);
}
this.setState({
months: newMonths,
});
}
shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
componentDidUpdate() {
const { isAnimating, onMonthTransitionEnd } = this.props;
// For IE9, immediately call onMonthTransitionEnd instead of
// waiting for the animation to complete
if (!this.isTransitionEndSupported && isAnimating) {
onMonthTransitionEnd();
}
}
componentWillUnmount() {
removeEventListener(this.eventHandle);
}
onTransitionEnd() {
this.props.onMonthTransitionEnd();
}
render() {
const {
enableOutsideDays,
firstVisibleMonthIndex,
isAnimating,
modifiers,
numberOfMonths,
monthFormat,
orientation,
transformValue,
onDayMouseEnter,
onDayMouseLeave,
onDayClick,
renderDay,
onMonthTransitionEnd,
} = this.props;
const { months } = this.state;
const className = cx('CalendarMonthGrid', {
'CalendarMonthGrid--horizontal': orientation === HORIZONTAL_ORIENTATION,
'CalendarMonthGrid--vertical': orientation === VERTICAL_ORIENTATION,
'CalendarMonthGrid--vertical-scrollable': orientation === VERTICAL_SCROLLABLE,
'CalendarMonthGrid--animating': isAnimating,
});
return (
<div
ref={(ref) => { this.container = ref; }}
className={className}
style={getTransformStyles(transformValue)}
onTransitionEnd={onMonthTransitionEnd}
>
{months.map((month, i) => {
const isVisible =
(i >= firstVisibleMonthIndex) && (i < firstVisibleMonthIndex + numberOfMonths);
return (
<CalendarMonth
key={month.format('YYYY-MM')}
month={month}
isVisible={isVisible}
enableOutsideDays={enableOutsideDays}
modifiers={modifiers}
monthFormat={monthFormat}
orientation={orientation}
onDayMouseEnter={onDayMouseEnter}
onDayMouseLeave={onDayMouseLeave}
onDayClick={onDayClick}
renderDay={renderDay}
/>
);
})}
</div>
);
}
}
CalendarMonthGrid.propTypes = propTypes;
CalendarMonthGrid.defaultProps = defaultProps;