-
Notifications
You must be signed in to change notification settings - Fork 183
/
BpkCalendarGrid.js
224 lines (207 loc) · 6.81 KB
/
BpkCalendarGrid.js
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
/*
* Backpack - Skyscanner's Design System
*
* Copyright 2016-2021 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { cssModules, isDeviceIos } from 'bpk-react-utils';
import BpkCalendarGridHeader from './BpkCalendarGridHeader';
import Week from './Week';
import {
formatIsoDate,
getCalendarMonthWeeks,
isSameMonth,
orderDaysOfWeek,
} from './date-utils';
import CustomPropTypes from './custom-proptypes';
import STYLES from './BpkCalendarGrid.module.scss';
import { addCalendarGridTransition } from './BpkCalendarGridTransition';
// This should be imported after `./BpkCalendarGrid.module.scss`.
// Because of how css specificity works the class `bpk-calendar-grid-transition__grid` needs to be defined
// after `bpk-calendar-grid` (defined by BpkCalendarGrid.module.scss) so it can override width and display of the calendar
// This is because the calendar with transiction is expected to have a fixed width and whenever `bpk-calendar-grid-transition__grid`
// class is applyed it should override the calendar style.
// NOTE that ./Week is also importing ./BpkCalendarGrid.module.scss so adding this after `./Week` would also do the job but
// for clarity we should leave it here.
const getClassName = cssModules(STYLES);
/*
BpkCalendarGrid - the grid representing a whole month
*/
class BpkCalendarGrid extends Component {
constructor(props) {
super(props);
// We cache expensive calculations (and identities) in state
this.state = {
calendarMonthWeeks: getCalendarMonthWeeks(
props.month,
props.weekStartsOn,
),
daysOfWeek: orderDaysOfWeek(props.daysOfWeek, props.weekStartsOn),
};
}
UNSAFE_componentWillReceiveProps(nextProps) {
// We cache expensive calculations (and identities) in state
if (
nextProps.daysOfWeek !== this.props.daysOfWeek ||
!isSameMonth(nextProps.month, this.props.month) ||
nextProps.weekStartsOn !== this.props.weekStartsOn
) {
this.setState({
calendarMonthWeeks: getCalendarMonthWeeks(
nextProps.month,
nextProps.weekStartsOn,
),
daysOfWeek: orderDaysOfWeek(
nextProps.daysOfWeek,
nextProps.weekStartsOn,
),
});
}
}
render() {
const {
month,
className,
DateComponent,
dateModifiers,
formatDateFull,
formatMonth,
onDateClick,
onDateKeyDown,
showWeekendSeparator,
weekStartsOn,
preventKeyboardFocus,
isKeyboardFocusable,
markToday,
markOutsideDays,
selectedDate,
selectionEnd,
selectionStart,
focusedDate,
minDate,
maxDate,
ignoreOutsideDate,
dateProps,
cellClassName,
} = this.props;
const { calendarMonthWeeks, daysOfWeek } = this.state;
const classNames = [getClassName('bpk-calendar-grid')];
if (className) {
classNames.push(className);
}
return (
<table
className={classNames.join(' ')}
aria-hidden={!isKeyboardFocusable}
>
<caption className={getClassName('bpk-calendar-grid__caption')} hidden>
{formatMonth(month)}
</caption>
<BpkCalendarGridHeader
isTableHead
daysOfWeek={daysOfWeek}
showWeekendSeparator={showWeekendSeparator}
weekStartsOn={weekStartsOn}
/>
<tbody>
{calendarMonthWeeks.map(dates => (
<Week
key={formatIsoDate(dates[0])}
month={month}
dates={dates}
onDateClick={onDateClick}
onDateKeyDown={onDateKeyDown}
showWeekendSeparator={showWeekendSeparator}
formatDateFull={formatDateFull}
DateComponent={DateComponent}
dateModifiers={dateModifiers}
daysOfWeek={daysOfWeek}
preventKeyboardFocus={preventKeyboardFocus}
isKeyboardFocusable={isKeyboardFocusable}
weekStartsOn={weekStartsOn}
markToday={markToday}
markOutsideDays={markOutsideDays}
selectedDate={selectedDate}
selectionEnd={selectionEnd}
selectionStart={selectionStart}
focusedDate={focusedDate}
minDate={minDate}
maxDate={maxDate}
ignoreOutsideDate={ignoreOutsideDate}
dateProps={dateProps}
cellClassName={cellClassName}
/>
))}
</tbody>
</table>
);
}
}
export const propTypes = {
// Required
DateComponent: PropTypes.elementType.isRequired,
daysOfWeek: CustomPropTypes.DaysOfWeek.isRequired,
formatDateFull: PropTypes.func.isRequired,
formatMonth: PropTypes.func.isRequired,
month: PropTypes.instanceOf(Date).isRequired,
weekStartsOn: PropTypes.number.isRequired,
// Optional
className: PropTypes.string,
cellClassName: PropTypes.string,
dateModifiers: CustomPropTypes.DateModifiers,
focusedDate: PropTypes.instanceOf(Date),
isKeyboardFocusable: PropTypes.bool,
markOutsideDays: PropTypes.bool,
markToday: PropTypes.bool,
maxDate: PropTypes.instanceOf(Date),
minDate: PropTypes.instanceOf(Date),
onDateClick: PropTypes.func,
onDateKeyDown: PropTypes.func,
preventKeyboardFocus: PropTypes.bool,
selectedDate: PropTypes.instanceOf(Date),
selectionEnd: PropTypes.instanceOf(Date),
selectionStart: PropTypes.instanceOf(Date),
showWeekendSeparator: PropTypes.bool,
ignoreOutsideDate: PropTypes.bool,
dateProps: PropTypes.object,
};
BpkCalendarGrid.propTypes = { ...propTypes };
BpkCalendarGrid.defaultProps = {
className: null,
dateModifiers: {},
focusedDate: null,
isKeyboardFocusable: true,
markOutsideDays: true,
markToday: true,
maxDate: null,
minDate: null,
onDateClick: null,
onDateKeyDown: null,
preventKeyboardFocus: false,
selectedDate: null,
selectionEnd: null,
selectionStart: null,
showWeekendSeparator: true,
ignoreOutsideDate: false,
dateProps: null,
cellClassName: null,
};
// On iOS, having transitions causes accessibility issues, so we disable it (KOA-4467).
const BpkCalendarGridWithTransition = isDeviceIos()
? BpkCalendarGrid
: addCalendarGridTransition(BpkCalendarGrid);
export default BpkCalendarGrid;
export { BpkCalendarGridWithTransition };