-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.js
288 lines (264 loc) · 7.51 KB
/
calendar.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* @Author: chengbs
* @Date: 2018-04-11 17:22:40
* @Last Modified by: chengbs
* @Last Modified time: 2018-05-15 18:03:56
*/
import React, { Component } from 'react'
import style from './calendar.css'
import H from './H'
class Calendar extends Component {
constructor(props) {
super(props)
this.state = {
currentYear: H.getFullYear(),
currentMonth: H.getMonth(),
currentDay: H.getDate(),
selectYear: H.getFullYear(),
selectMonth: H.getMonth(),
selectDay: H.getDate(),
historyYear: undefined,
historyMonth: undefined,
historyDay: undefined,
dateNumArray: []
}
}
static defaultProps = {
rowNumber: 6,
colNumber: 7
}
componentWillReceiveProps(nextProps) {
console.log(nextProps)
}
componentDidMount() {
let { year, month, day } = this.props
// 初始化状态
if (year && month && day) {
let dateNumArray = this._initMonthDayNumber(year)
let firstDay = H.weekOfMonth(new Date(year, month - 1))
this.setState({
selectYear: year,
selectMonth: month - 1,
selectDay: day,
dateNumArray: dateNumArray,
firstDay: firstDay
})
}
}
/**
* 给月份数组附上每月天数
* @param year 年份
* @private
*/
_initMonthDayNumber(year) {
let dateArray = []
for (var i = 0; i < 12; i++) {
switch (i + 1) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
dateArray.push(31)
break
case 4:
case 6:
case 9:
case 11:
dateArray.push(30)
break
case 2:
if (H.isLeapYear(year)) {
dateArray.push(29)
} else {
dateArray.push(28)
}
break
default:
break
}
}
return dateArray
}
/**
* 组件将要挂载
* 设置月份数组以及计算出每月的第一天星期几
*/
componentWillMount() {
let dateNumArray = this._initMonthDayNumber(this.state.currentYear)
let firstDay = H.weekOfMonth()
this.setState({ dateNumArray: dateNumArray, firstDay: firstDay })
}
/**
* 日期选择
* @param sDay
*/
selectDate(sDay) {
let { selectYear, selectMonth } = this.state
this.setState({
historyYear: selectYear,
historyMonth: selectMonth,
historyDay: sDay,
selectDay: sDay
}, () => {
this.props.onSelectDate(selectYear, selectMonth + 1, sDay)
})
}
/**
* 前一个月
*/
previousMonth() {
let { currentYear, currentMonth, currentDay, selectYear, selectMonth, selectDay, dateNumArray, firstDay } = this.state
if (selectMonth === 0) {
selectYear = +selectYear - 1
selectMonth = 11
dateNumArray = this._initMonthDayNumber(selectYear)
} else {
selectMonth = +selectMonth - 1
}
firstDay = H.weekOfMonth(new Date(selectYear, selectMonth))
if (currentYear === selectYear && currentMonth === selectMonth) {
selectDay = currentDay
} else {
selectDay = undefined
}
this.setState({
selectYear: selectYear,
selectMonth: selectMonth,
selectDay: selectDay,
dateNumArray: dateNumArray,
firstDay: firstDay
})
}
/**
* 之后一个月
*/
nextMonth() {
let { currentYear, currentMonth, currentDay, selectYear, selectMonth, selectDay, dateNumArray, firstDay } = this.state
if (selectMonth === 11) {
selectYear = selectYear + 1
selectMonth = 0
dateNumArray = this._initMonthDayNumber(selectYear)
} else {
selectMonth = selectMonth + 1
}
firstDay = H.weekOfMonth(new Date(selectYear, selectMonth))
if (currentYear === selectYear && currentMonth === selectMonth) {
selectDay = currentDay
} else {
selectDay = undefined
}
this.setState({
selectYear: selectYear,
selectMonth: selectMonth,
selectDay: selectDay,
dateNumArray: dateNumArray,
firstDay: firstDay
})
}
render() {
let { rowNumber, colNumber, tags } = this.props
let { currentYear, currentMonth, currentDay, selectYear, selectMonth, historyYear, historyMonth, historyDay, dateNumArray, firstDay } = this.state
let monthDay = dateNumArray[selectMonth]
let nDay = rowNumber * colNumber - firstDay - monthDay
let previousMonthDays = null
let previousDays = []
let currentDays = []
let nextDays = []
let totalDays = []
let previousMonth = null
if (selectMonth === 0) {
previousMonth = 11
} else {
previousMonth = selectMonth - 1
}
previousMonthDays = dateNumArray[previousMonth]
for (let i = 0; i < firstDay; i++) {
let previousLink = (<li className={style['item-gray']} key={'previous' + i}>
<a href='javascript:'>{previousMonthDays - (firstDay - i) + 1}</a>
</li>)
previousDays.push(previousLink)
}
let currentClassName = ''
let currentText = ''
for (let i = 0; i < monthDay; i++) {
// 今天样式
if (currentYear === selectYear && currentMonth === selectMonth && currentDay === (i + 1)) {
currentClassName = style['item-current']
currentText = '今天'
} else {
currentText = i + 1
// 判断选择样式与历史样式是否相等,相等激活
if (selectYear === historyYear && selectMonth === historyMonth && historyDay === (i + 1)) {
currentClassName = style['item-active']
} else {
currentClassName = ''
}
}
// 添加tag样式
if (tags.length > 0) {
for (let j = 0; j < tags.length; j++) {
if ((i + 1) === tags[j]) {
currentClassName += style['item-tag']
break
}
}
}
let currentLink = (<li className={currentClassName} key={'current' + i}>
<a href='javascript:' onClick={this.selectDate.bind(this, i + 1)}>
{currentText}
</a>
</li>)
currentDays.push(currentLink)
}
for (let i = 0; i < nDay; i++) {
let nextLink = (<li className={style['item-gray']} key={'next' + i}>
<a href='javascript:'>{i + 1}</a>
</li>)
nextDays.push(nextLink)
}
totalDays = previousDays.concat(currentDays, nextDays)
let ulList = []
if (totalDays.length > 0) {
for (let i = 0; i < rowNumber; i++) {
let liList = []
let startIndex = i * colNumber
let endIndex = (i + 1) * colNumber
for (let j = startIndex; j < endIndex; j++) {
liList.push(totalDays[j])
}
ulList.push(liList)
}
}
return (
<div className={style['calendar']}>
<div className={style['calendar-header']}>
<i className={style['icon-left']} onClick={this.previousMonth.bind(this)}></i>
<span>{selectYear} 年 {selectMonth + 1} 月</span>
<i className={style['icon-right']} onClick={this.nextMonth.bind(this)}></i>
</div>
<div className={style['calendar-body']}>
<ul className={style['c-body-head']}>
<li>日</li>
<li>一</li>
<li>二</li>
<li>三</li>
<li>四</li>
<li>五</li>
<li>六</li>
</ul>
<div className={style['c-body-content']}>
{
ulList.map((u, index) => {
return (<ul key={'ul' + index} className={style['content-row']}>{u}</ul>)
})
}
</div>
</div>
</div>
)
}
}
export default Calendar