index.js
5.91 KB
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
import React, { Component } from 'react'
import { connect } from 'dva'
import { DatePicker, List } from 'antd-mobile';
import enUs from 'antd-mobile/lib/date-picker/locale/en_US';
const nowTimeStamp = new Date('1963-01-01');
const today = new Date();
const now = new Date();
// GMT is not currently observed in the UK. So use UTC now.
const utcNow = new Date(now.getTime() + (now.getTimezoneOffset() * 60000));
// Make sure that in `time` mode, the maxDate and minDate are within one day.
let minDate = new Date('1963-01-01');
const maxDate = new Date(nowTimeStamp );
console.log(minDate, maxDate);
if (minDate.getDate() !== maxDate.getDate()) {
// set the minDate to the 0 of maxDate
minDate = new Date(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate());
}
function formatDate(date) {
/* eslint no-confusing-arrow: 0 */
const pad = n => n < 10 ? `0${n}` : n;
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
return `${dateStr} ${timeStr}`;
}
// If not using `List.Item` as children
// The `onClick / extra` props need to be processed within the component
const CustomChildren = ({ extra, onClick, children }) => (
<div
onClick={onClick}
style={{ backgroundColor: '#fff', height: '45px', lineHeight: '45px', padding: '0 15px' }}
>
{children}
<span style={{ float: 'right', color: '#888' }}>{extra}</span>
</div>
);
class datePicker extends Component {
state = {
date: now,
time: now,
utcDate: utcNow,
dpValue: null,
customChildValue: null,
visible: false,
}
componentDidMount() {
this.props.onRef(this)
this.setState({
date: this.props.birthday ? new Date(this.props.birthday) : now
})
console.log('componentDidMount---',this.props.birthday)
}
componentWillReceiveProps(nextProps) { // 父组件重传props时就会调用这个方法
if(nextProps.birthday && nextProps.birthday != this.state.date){
this.setState({
date:new Date(nextProps.birthday)
})
}
}
showDate=()=>{
document.getElementsByTagName('body')[0].style.overflowY = 'hidden';
this.setState({
visible: true,
});
}
showDate1=()=> {
document.getElementsByTagName('body')[0].style.overflowY = 'hidden';
this.setState({
visible: true,
});
}
render() {
let defaultdate = today.getFullYear()-101;//默认范围值为100岁
console.log('render---',this.props.birthday)
let minDate = this.props.minYear ? new Date(this.props.minYear, today.getMonth(), today.getDate() + 1) : new Date(defaultdate, 0, 1, 0, 0, 0);
return (
<List className="date-picker-list" style={{ backgroundColor: 'white',opacity:0 ,width:0,display:'none'}}>
{/*<DatePicker
value={this.state.date}
onChange={date => this.setState({ date })}
>
<List.Item arrow="horizontal">Datetime</List.Item>
</DatePicker>*/}
<DatePicker
visible={this.state.visible}
mode="date"
title="出生日期"
extra="Optional"
value={this.state.date}
minDate={minDate}
maxDate={new Date()}
onChange={date => {
console.log('--DatePicker--onChange--->',date)
//this.setState({ date })
}}
onValueChange={(date,index) => {
let data1 = new Date(date[0],date[1],date[2]);
console.log('--DatePicker--onValueChange--->',data1)
this.setState({ date:data1 })
}}
onOk = {
(date)=>{
console.log('--DatePicker--ok--->',date)
this.setState({ visible:false })
this.props.getDate(date)
}
}
onDismiss={
()=>{
this.setState({ visible:false,date:this.props.birthday ? new Date(this.props.birthday) : now })
console.log('--DatePicker--onDismiss--->',this.state.date)
}
}
>
<List.Item arrow="horizontal">Date</List.Item>
</DatePicker>
{/* <DatePicker
mode="time"
minuteStep={2}
use12Hours
value={this.state.time}
onChange={time => this.setState({ time })}
>
<List.Item arrow="horizontal">Time (am/pm)</List.Item>
</DatePicker>
<DatePicker
mode="time"
minDate={minDate}
maxDate={maxDate}
value={this.state.time}
onChange={time => this.setState({ time })}
>
<List.Item arrow="horizontal">Limited time</List.Item>
</DatePicker>
<DatePicker
mode="time"
locale={enUs}
format={val => `UTC Time: ${formatDate(val).split(' ')[1]}`}
value={this.state.utcDate}
onChange={date => this.setState({ utcDate: date })}
>
<List.Item arrow="horizontal">UTC time</List.Item>
</DatePicker>
<List.Item
extra={this.state.dpValue && formatDate(this.state.dpValue)}
onClick={() => this.setState({ visible: true })}
>
External control visible state
</List.Item>
<DatePicker
visible={this.state.visible}
value={this.state.dpValue}
onOk={date => this.setState({ dpValue: date, visible: false })}
onDismiss={() => this.setState({ visible: false })}
/>
<DatePicker
mode="time"
format="HH:mm"
title="Select Time"
value={this.state.customChildValue}
onChange={v => this.setState({ customChildValue: v })}
extra="click to choose"
>
<CustomChildren>With customized children</CustomChildren>
</DatePicker>*/}
</List>
);
}
}
export default connect()(datePicker)
/*ReactDOM.render(<Demo />, mountNode);
.date-picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}*/