-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatePicker.js
248 lines (223 loc) · 6.92 KB
/
datePicker.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
/**
* Created by gx on 2016/5/23.
*/
import './datePicker.scss'
import React, {Component} from 'react'
import * as Immutable from 'immutable'
import DatePickerInput from './datePickerInput'
class DatePickerClass {
constructor(year, month) {
let thisDate = new Date()
this.year = year || thisDate.getFullYear()
this.month = month || thisDate.getMonth() + 1
this.days = []
this.getDays()
}
//获取某年某月展示在ui上的日期 一共42天
getDays() {
const ONEDAY = 86400000
let date = new Date()
date.setFullYear(this.year, this.month - 1, 1)
console.log('full year' + date.toDateString())
let day1 = date.getTime()
console.log('get time' + day1)
let prevDayCount = 1
let thisDayCount = 0
//获取上一个月的天数
while (true) {
date.setTime(day1 - prevDayCount * ONEDAY)
let d = date.getDay()
var newDate = new Date(date.getTime())
this.days.unshift(newDate)
console.log(newDate)
if (d == 0 && prevDayCount != 1) {
break
}
prevDayCount++
}
console.log(prevDayCount)
//获取当月及下一月
while (42 - prevDayCount - thisDayCount) {
date.setTime(day1 + thisDayCount * ONEDAY)
var newDate = new Date(date.getTime())
this.days.push(newDate)
console.log(newDate)
thisDayCount++
}
this.chunk()
console.log(this.days)
}
next() {
if (this.month == 12) {
return new DatePickerClass(this.year + 1, 1)
} else {
return new DatePickerClass(this.year, this.month + 1)
}
}
prev() {
if (this.month == 1) {
return new DatePickerClass(this.year - 1, 12)
} else {
return new DatePickerClass(this.year, this.month - 1)
}
}
chunk() {
let chunked = [[], [], [], [], [], []]
let chunkIndex = 0
this.days.forEach((x, i, c)=> {
if ((i % 7 == 0 || i == c.length) && i != 0) {
chunkIndex++
}
chunked[chunkIndex].push(x)
})
this.days = chunked
}
}
export default class DatePicker extends Component {
constructor(props) {
super(props)
this.state = {
dp: new DatePickerClass(),
selectedDay: Immutable.fromJS([]),
show: false
}
}
getDayClass(n) {
const d = n.getDay()
if (!this.props.oneDay) {
//端点样式
let sd = this.state.selectedDay
if (sd.indexOf(n) > -1) {
return 'startOrEnd'
}
if (sd.size == 2 && n.getTime() < sd.get(1) && n.getTime() > sd.get(0)) {
return 'inArea'
}
}
if (this.props.oneDay && this.state.selectedDay.get(0) == n) {
return 'selected'
}
if (n.getMonth() + 1 == this.state.dp.month) {
if (d == 0 || d == 6) {
return "weekEnd"
} else {
return 'currentMonth'
}
} else {
return 'notCurrentMonth'
}
}
handlePrev() {
this.setState({
dp: this.state.dp.prev()
})
}
handleNext() {
this.setState({
dp: this.state.dp.next()
})
}
handleFocus() {
this.setState({
show: true
})
}
handleSelectedDay(n, e) {
let sd = this.state.selectedDay
if (this.props.oneDay) {
//只选一天
this.setState({
selectedDay: Immutable.fromJS([n])
})
} else {
//选择时间段
if (sd.includes(n)) {
return
}
if (sd.size == 2) {
sd = sd.shift()
}
let a = sd.push(n).sort((x, y)=> {
return x.getTime() - y.getTime()
})
this.setState({
selectedDay: a
})
}
}
handleConfirm() {
this.setState({
show: false
})
this.props.onSelected(this.state.selectedDay)
}
handleCancel() {
this.setState({
selectedDay: Immutable.fromJS([]),
show:false
})
}
setInputValue() {
let sd = this.state.selectedDay
if (sd.size == 0) {
return ''
}
if (this.props.oneDay) {
//只选一天
return sd.get(0).toLocaleDateString()
} else {
//选择时间段
if(sd.size==1)return ""
return sd.get(0).toLocaleDateString()+" - "+ sd.get(1).toLocaleDateString()
}
}
render() {
const dp = this.state.dp
return (<div>
<div className="date">
<DatePickerInput type="text" onFocus={this.handleFocus.bind(this)}
V={this.setInputValue()}/>
</div>
<div className={"container "+(this.state.show?'show':'hide')}>
<div className="header">
<div className="triangle-left" onClick={this.handlePrev.bind(this)}></div>
<div className="triangle-right" onClick={this.handleNext.bind(this)}></div>
{dp.year + '年' + dp.month + "月"}
</div>
<table>
<thead>
<tr>
<td className="weekEnd">日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td className="weekEnd">六</td>
</tr>
</thead>
<tbody>
{dp.days.map((x)=> {
return (
<tr>
{x.map((n)=> {
return <td className={this.getDayClass(n)}
onClick={this.handleSelectedDay.bind(this,n)}>{n.getDate()}</td>
})}
</tr>
)
})}
</tbody>
</table>
<div className="footer">
<button onClick={this.handleConfirm.bind(this)}>确定</button>
<button onClick={this.handleCancel.bind(this)}>取消</button>
</div>
</div>
</div>
)
}
}
DatePicker.propTypes = {
onSelected: React.PropTypes.func.isRequired
}