You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
53 lines
1.3 KiB
9 years ago
|
# 日期时间选择
|
||
10 years ago
|
|
||
|
- order: 4
|
||
10 years ago
|
|
||
9 years ago
|
和 [时间选择框](/components/timepicer) 配合使用。
|
||
10 years ago
|
|
||
|
---
|
||
|
|
||
|
````jsx
|
||
9 years ago
|
import { DatePicker, TimePicker } from 'antd';
|
||
10 years ago
|
|
||
9 years ago
|
const DateTimePicker = React.createClass({
|
||
|
handleChange(from, value) {
|
||
|
this.result = this.result || new Date();
|
||
|
if (!value) {
|
||
|
if (from === 'date') {
|
||
|
this.selectedDate = false;
|
||
|
} else {
|
||
|
this.selectedTime = false;
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
if (from === 'date') {
|
||
9 years ago
|
this.result.setFullYear(value.getFullYear());
|
||
|
this.result.setMonth(value.getMonth());
|
||
|
this.result.setDate(value.getDate());
|
||
|
this.selectedDate = true;
|
||
9 years ago
|
} else {
|
||
9 years ago
|
this.result.setHours(value.getHours());
|
||
|
this.result.setMinutes(value.getMinutes());
|
||
|
this.result.setSeconds(value.getSeconds());
|
||
|
this.selectedTime = true;
|
||
9 years ago
|
}
|
||
9 years ago
|
if (this.selectedDate && this.selectedTime) {
|
||
|
this.props.onSelect(this.result);
|
||
|
}
|
||
|
},
|
||
|
render() {
|
||
|
return <div>
|
||
9 years ago
|
<DatePicker onChange={this.handleChange.bind(null, 'date')} />
|
||
|
<TimePicker onChange={this.handleChange.bind(null, 'time')} />
|
||
9 years ago
|
</div>;
|
||
9 years ago
|
}
|
||
9 years ago
|
});
|
||
|
|
||
|
function onSelect(value) {
|
||
|
console.log('选择了时间:', value);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
ReactDOM.render(<DateTimePicker onSelect={onSelect} />
|
||
9 years ago
|
, document.getElementById('components-date-picker-demo-time'));
|
||
10 years ago
|
````
|