This repository was archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathmonth-view.js
97 lines (87 loc) · 2.26 KB
/
month-view.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
import React from 'react'
import PropTypes from 'prop-types'
import cs from 'classnames'
import moment from 'moment'
import 'moment-range'
import Cell from './cell'
import ViewHeader from './view-header'
export default class MonthView extends React.Component {
static propTypes = {
date: PropTypes.object.isRequired,
minDate: PropTypes.any,
maxDate: PropTypes.any,
setInternalDate: PropTypes.func
}
getMonth() {
const month = this.props.date.month()
return moment.monthsShort().map((item, i) => {
return {
label: item,
disabled: this.checkIfMonthDisabled(i),
curr: i === month
}
})
}
cellClick = e => {
const month = e.target.innerHTML
if (this.checkIfMonthDisabled(month)) return
const date = this.props.date.clone().month(month)
this.props.prevView(date)
}
checkIfMonthDisabled(month) {
const now = this.props.date
return (
now
.clone()
.month(month)
.endOf('month')
.isBefore(this.props.minDate, 'day') ||
now
.clone()
.month(month)
.startOf('month')
.isAfter(this.props.maxDate, 'day')
)
}
next = () => {
let nextDate = this.props.date.clone().add(1, 'years')
if (this.props.maxDate && nextDate.isAfter(this.props.maxDate, 'day')) {
nextDate = this.props.maxDate
}
this.props.setInternalDate(nextDate)
}
prev = () => {
let prevDate = this.props.date.clone().subtract(1, 'years')
if (this.props.minDate && prevDate.isBefore(this.props.minDate, 'day')) {
prevDate = this.props.minDate
}
this.props.setInternalDate(prevDate)
}
render() {
const currentDate = this.props.date.format('YYYY')
const months = this.getMonth().map((item, i) => (
<Cell
classes={cs({
month: true,
disabled: item.disabled,
current: item.curr
})}
key={i}
value={item.label}
/>
))
return (
<div className="months-view">
<ViewHeader
data={currentDate}
next={this.next}
prev={this.prev}
titleAction={this.props.nextView}
/>
<div className="months" onClick={this.cellClick}>
{months}
</div>
</div>
)
}
}