forked from gpbl/react-day-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaption.js
67 lines (59 loc) · 1.62 KB
/
Caption.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import LocaleUtils from './LocaleUtils';
import { ENTER } from './keys';
import { RoleTypesShape, defaultRoles } from './PropTypes';
export default class Caption extends Component {
static propTypes = {
date: PropTypes.instanceOf(Date),
months: PropTypes.arrayOf(PropTypes.string),
locale: PropTypes.string,
localeUtils: PropTypes.object,
onClick: PropTypes.func,
classNames: PropTypes.shape({
caption: PropTypes.string.isRequired,
}).isRequired,
roles: PropTypes.shape(RoleTypesShape),
};
static defaultProps = {
localeUtils: LocaleUtils,
roles: defaultRoles,
};
constructor(props) {
super(props);
this.handleKeyUp = this.handleKeyUp.bind(this);
}
shouldComponentUpdate(nextProps) {
return (
nextProps.locale !== this.props.locale ||
nextProps.classNames !== this.props.classNames ||
nextProps.date.getMonth() !== this.props.date.getMonth() ||
nextProps.date.getFullYear() !== this.props.date.getFullYear()
);
}
handleKeyUp(e) {
if (e.keyCode === ENTER) {
this.props.onClick(e);
}
}
render() {
const {
classNames,
date,
months,
locale,
localeUtils,
onClick,
roles,
} = this.props;
return (
<div className={classNames.caption} role={roles.caption}>
<div onClick={onClick} onKeyUp={this.handleKeyUp}>
{months
? `${months[date.getMonth()]} ${date.getFullYear()}`
: localeUtils.formatMonthTitle(date, locale)}
</div>
</div>
);
}
}