forked from gpbl/react-day-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavbar.js
151 lines (135 loc) · 3.94 KB
/
Navbar.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import defaultClassNames from './classNames';
import { SPACE, ENTER } from './keys';
export default class Navbar extends Component {
static defaultProps = {
classNames: defaultClassNames,
dir: 'ltr',
labels: {
previousMonth: 'Previous Month',
nextMonth: 'Next Month',
},
showPreviousButton: true,
showNextButton: true,
};
static propTypes = {
classNames: PropTypes.shape({
navBar: PropTypes.string.isRequired,
navButtonPrev: PropTypes.string.isRequired,
navButtonNext: PropTypes.string.isRequired,
}),
className: PropTypes.string,
showPreviousButton: PropTypes.bool,
showNextButton: PropTypes.bool,
onPreviousClick: PropTypes.func,
onNextClick: PropTypes.func,
dir: PropTypes.string,
labels: PropTypes.shape({
previousMonth: PropTypes.string.isRequired,
nextMonth: PropTypes.string.isRequired,
}),
};
shouldComponentUpdate(nextProps) {
return (
nextProps.labels !== this.props.labels ||
nextProps.dir !== this.props.dir ||
this.props.showPreviousButton !== nextProps.showPreviousButton ||
this.props.showNextButton !== nextProps.showNextButton
);
}
handleNextClick = () => {
if (this.props.onNextClick) {
this.props.onNextClick();
}
};
handlePreviousClick = () => {
if (this.props.onPreviousClick) {
this.props.onPreviousClick();
}
};
handleNextKeyDown = e => {
if (e.keyCode !== ENTER && e.keyCode !== SPACE) {
return;
}
e.preventDefault();
this.handleNextClick();
};
handlePreviousKeyDown = e => {
if (e.keyCode !== ENTER && e.keyCode !== SPACE) {
return;
}
e.preventDefault();
this.handlePreviousClick();
};
render() {
const {
classNames,
className,
showPreviousButton,
showNextButton,
labels,
dir,
} = this.props;
let previousClickHandler;
let nextClickHandler;
let previousKeyDownHandler;
let nextKeyDownHandler;
let shouldShowPrevious;
let shouldShowNext;
if (dir === 'rtl') {
previousClickHandler = this.handleNextClick;
nextClickHandler = this.handlePreviousClick;
previousKeyDownHandler = this.handleNextKeyDown;
nextKeyDownHandler = this.handlePreviousKeyDown;
shouldShowNext = showPreviousButton;
shouldShowPrevious = showNextButton;
} else {
previousClickHandler = this.handlePreviousClick;
nextClickHandler = this.handleNextClick;
previousKeyDownHandler = this.handlePreviousKeyDown;
nextKeyDownHandler = this.handleNextKeyDown;
shouldShowNext = showNextButton;
shouldShowPrevious = showPreviousButton;
}
const previousClassName = shouldShowPrevious
? classNames.navButtonPrev
: `${classNames.navButtonPrev} ${
classNames.navButtonInteractionDisabled
}`;
const nextClassName = shouldShowNext
? classNames.navButtonNext
: `${classNames.navButtonNext} ${
classNames.navButtonInteractionDisabled
}`;
const previousButton = (
<span
tabIndex="0"
role="button"
aria-label={labels.previousMonth}
key="previous"
className={previousClassName}
onKeyDown={shouldShowPrevious ? previousKeyDownHandler : undefined}
onClick={shouldShowPrevious ? previousClickHandler : undefined}
/>
);
const nextButton = (
<span
tabIndex="0"
role="button"
aria-label={labels.nextMonth}
key="right"
className={nextClassName}
onKeyDown={shouldShowNext ? nextKeyDownHandler : undefined}
onClick={shouldShowNext ? nextClickHandler : undefined}
/>
);
return (
<div className={className || classNames.navBar}>
{dir === 'rtl'
? [nextButton, previousButton]
: [previousButton, nextButton]}
</div>
);
}
}