forked from wusuopu/react-native-wheel-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date-picker.android.js
241 lines (221 loc) · 7.45 KB
/
date-picker.android.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
'use strict';
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { View } from 'react-native'
import Picker from './picker'
import moment from 'moment'
import _ from 'lodash'
const styles = {
picker: {
flex: 1
}
}
const dayNumOfMonth = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] // 各个月份的天数
export default class DatePicker extends Component {
constructor(props) {
super(props)
this.state = { date: this.props.date }
let date = moment(this.state.date)
this.newValue = {
year: this.state.date.getFullYear(),
month: this.state.date.getMonth(),
day: this.state.date.getDate(),
hour: this.state.date.getHours(),
minute: this.state.date.getMinutes()
}
let dayNum = this._getCurrentDayNumOfMonth()
this.state.dayRange = this._genDateRange(dayNum)
// 生成年份范围
let minYear = this.props.minimumDate.getFullYear()
let maxYear = this.props.maximumDate.getFullYear()
if ((maxYear - minYear) >= 1) {
this.state.yearRange = _.map(_.range(minYear, maxYear + 1), (n) => {
return { value: n, label: `${n}${this.props.labelUnit.year}` }
})
} else {
this.state.yearRange = [ { value: minYear, label: `${minYear}${this.props.labelUnit.year}` } ]
}
// 生成月份范围
this.state.monthRange = _.times(12, (n) => {
return { value: n + 1, label: `${n + 1}${this.props.labelUnit.month}` }
})
}
static propTypes = {
labelUnit: PropTypes.shape({
year: PropTypes.string,
month: PropTypes.string,
day: PropTypes.string
}),
date: PropTypes.instanceOf(Date).isRequired,
maximumDate: PropTypes.instanceOf(Date),
minimumDate: PropTypes.instanceOf(Date),
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
onDateChange: PropTypes.func
}
static defaultProps = {
labelUnit: { year: '年', month: '月', day: '日' },
mode: 'date',
maximumDate: moment().add( 10, 'years' ).toDate(),
minimumDate: moment().add( -10, 'years' ).toDate(),
date: new Date()
}
render() {
return (
<View style={{ flexDirection: 'row' }}>
{this.datePicker}
{this.timePicker}
</View>
)
}
get datePicker() {
if (!_.includes(['date', 'datetime'], this.props.mode)) {
return []
}
return [
<View key="year" style={styles.picker}>
<Picker
ref="year"
selectedValue={this.state.date.getFullYear()}
pickerData={this.state.yearRange}
onValueChange={(value) => {
let oldYear = this.newValue.year
this.newValue.year = value
this._checkDate(oldYear, this.newValue.month, this.newValue.day)
this.props.onDateChange && this.props.onDateChange(this.getValue())
}}
/>
</View>,
<View key="month" style={styles.picker}>
<Picker
ref="month"
selectedValue={this.state.date.getMonth() + 1}
pickerData={this.state.monthRange}
onValueChange={(value) => {
let oldMonth = this.newValue.month
this.newValue.month = value - 1
this._checkDate(this.newValue.year, oldMonth, this.newValue.day)
this.props.onDateChange && this.props.onDateChange(this.getValue())
}}
/>
</View>,
<View key="date" style={styles.picker}>
<Picker
ref="date"
selectedValue={this.state.date.getDate()}
pickerData={this.state.dayRange}
onValueChange={(value) => {
let oldDay = this.newValue.month
this.newValue.day = value
this._checkDate(this.newValue.year, this.newValue.month, oldDay)
this.props.onDateChange && this.props.onDateChange(this.getValue())
}}
/>
</View>
]
}
get timePicker() {
if (!_.includes(['time', 'datetime'], this.props.mode)) {
return []
}
return [
<View key="hour" style={styles.picker}>
<Picker
ref="hour"
selectedValue={this.state.date.getHours()}
pickerData={_.range(0, 24)}
onValueChange={(value) => {
this.newValue.hour = value
this.props.onDateChange && this.props.onDateChange(this.getValue())
}}
/>
</View>,
<View key="minute" style={styles.picker}>
<Picker
ref="minute"
selectedValue={this.state.date.getMinutes()}
pickerData={_.range(0, 60)}
onValueChange={(value) => {
this.newValue.minute = value
this.props.onDateChange && this.props.onDateChange(this.getValue())
}}
/>
</View>
]
}
_checkDate(oldYear, oldMonth, oldDay) { // 检查新的值是否合法
let currentMonth = this.newValue.month
let currentYear = this.newValue.year
let currentDay = this.newValue.day
let dayRange = this.state.dayRange
let dayNum = dayRange.length
if (oldMonth !== currentMonth || oldYear !== currentYear) { // 月份有发动
dayNum = this._getCurrentDayNumOfMonth()
}
if (dayNum !== dayRange.length) { // 天数有变
dayRange = this._genDateRange(dayNum)
if (currentDay > dayNum) {
currentDay = this.newValue.day = dayNum
this.refs.date.state.selectedValue = dayNum
}
this.setState({ dayRange })
}
let unit = undefined
if (this.props.mode === 'date') {
unit = 'day'
}
let { year, month, day, hour, minute } = this.newValue
let currentTime = moment([year, month, day, hour, minute])
let min = moment(this.props.minimumDate)
let max = moment(this.props.maximumDate)
let adjustToTime = currentTime
if (currentTime.isBefore(min, unit)) { // 超出最小值
adjustToTime = min
}
if (currentTime.isAfter(max, unit)) { // 超出最大值
adjustToTime = max
}
if (!currentTime.isSame(adjustToTime)) { // 超出选择范围
year = adjustToTime.get('year')
month = adjustToTime.get('month') + 1
day = adjustToTime.get('date')
hour = adjustToTime.get('hour')
minute = adjustToTime.get('minute')
this.refs.year && this.refs.year.setState({selectedValue: year})
this.refs.month && this.refs.month.setState({selectedValue: month})
this.refs.date && this.refs.date.setState({selectedValue: day})
this.refs.hour && this.refs.hour.setState({selectedValue: hour})
this.refs.minute && this.refs.minute.setState({selectedValue: minute})
}
}
_getCurrentDayNumOfMonth() {
let currentMonth = this.newValue.month
let currentYear = this.newValue.year
let dayNum
if (currentMonth === 1) { // 当前为2月份
if (moment([currentYear]).isLeapYear()) {
dayNum = 29 // 闰年2月29天
} else {
dayNum = 28 // 否则28天
}
} else {
dayNum = dayNumOfMonth[currentMonth]
}
return dayNum
}
_genDateRange(dayNum) { // 生成日期范围
return _.times(dayNum, (n) => {
return { value: n + 1, label: `${n + 1}${this.props.labelUnit.day}` }
})
}
getValue() {
const { year, month, day, hour, minute } = this.newValue
let date = new Date(year, month, day, hour, minute)
if (date < this.props.minimumDate) {
date = this.props.minimumDate
}
if (date > this.props.maximumDate) {
date = this.props.maximumDate
}
return date
}
}