ant-design/components/date-picker/RangePicker.jsx

166 lines
4.8 KiB
React
Raw Normal View History

2015-12-11 11:11:24 +08:00
import React from 'react';
import GregorianCalendar from 'gregorian-calendar';
import RangeCalendar from 'rc-calendar/lib/RangeCalendar';
import DatePicker from 'rc-calendar/lib/Picker';
import TimePicker from 'rc-time-picker';
2015-12-11 11:11:24 +08:00
import classNames from 'classnames';
import PickerMixin from './PickerMixin';
export default React.createClass({
getDefaultProps() {
return {
defaultValue: [],
format: 'yyyy-MM-dd',
2015-12-25 18:04:01 +08:00
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
2015-12-11 11:11:24 +08:00
transitionName: 'slide-up',
popupStyle: {},
2016-02-18 19:15:23 +08:00
onChange() {
},
onOk() {
},
2015-12-11 11:11:24 +08:00
locale: {},
align: {
offset: [0, -9],
},
open: false
};
},
getInitialState() {
2016-01-05 14:42:06 +08:00
const { value, defaultValue } = this.props;
2015-12-11 11:11:24 +08:00
const start = (value && value[0]) || defaultValue[0];
const end = (value && value[1]) || defaultValue[1];
return {
value: [
this.parseDateFromValue(start),
this.parseDateFromValue(end)
]
};
},
2016-01-05 14:42:06 +08:00
mixins: [PickerMixin],
2015-12-11 11:11:24 +08:00
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
2016-01-02 17:03:56 +08:00
const value = nextProps.value || [];
const start = this.parseDateFromValue(value[0]);
const end = this.parseDateFromValue(value[1]);
2015-12-11 11:11:24 +08:00
this.setState({
value: [start, end]
});
}
},
handleChange(value) {
if (!('value' in this.props)) {
2016-02-19 17:07:26 +08:00
this.setState({ value });
}
2015-12-11 11:11:24 +08:00
const startTime = value[0] ? new Date(value[0].getTime()) : null;
const endTime = value[1] ? new Date(value[1].getTime()) : null;
this.props.onChange([startTime, endTime]);
},
render() {
const locale = this.getLocale();
// 以下两行代码
// 给没有初始值的日期选择框提供本地化信息
// 否则会以周日开始排
let defaultCalendarValue = new GregorianCalendar(locale);
defaultCalendarValue.setTime(Date.now());
const { disabledDate, showTime, size, startPlaceholder, endPlaceholder, getCalendarContainer,
2016-02-18 19:15:23 +08:00
transitionName, disabled, popupStyle, align, style, onOk } = this.props;
2015-12-11 11:11:24 +08:00
const state = this.state;
2016-02-18 19:15:23 +08:00
let timePicker = null;
if (showTime) {
timePicker = (<TimePicker
prefixCls="ant-time-picker"
2016-01-05 14:42:06 +08:00
placeholder={locale.lang.timePlaceholder}
2016-02-18 19:15:23 +08:00
transitionName="slide-up"/>);
}
const calendarClassName = classNames({
['ant-calendar-time']: this.props.showTime,
});
2016-02-18 19:15:23 +08:00
let pickerChangeHandler = {
onChange: this.handleChange,
};
let calendarHandler = {
onOk: this.handleChange,
};
if (timePicker) {
pickerChangeHandler = {};
} else {
calendarHandler = {};
}
const calendar = (
2016-02-18 19:15:23 +08:00
<RangeCalendar
prefixCls="ant-calendar"
className={calendarClassName}
timePicker={timePicker}
disabledDate={disabledDate}
dateInputPlaceholder={[startPlaceholder, endPlaceholder]}
locale={locale.lang}
2016-02-02 17:49:44 +08:00
onOk={onOk}
2016-01-19 18:51:45 +08:00
defaultValue={[defaultCalendarValue, defaultCalendarValue]}
2016-02-18 19:15:23 +08:00
{...calendarHandler}
/>
);
2015-12-11 11:11:24 +08:00
const pickerClass = classNames({
'ant-calendar-picker': true,
'ant-calendar-picker-open': state.open
});
const pickerInputClass = classNames({
'ant-calendar-range-picker': true,
2015-12-11 11:11:24 +08:00
'ant-input': true,
'ant-input-lg': size === 'large',
'ant-input-sm': size === 'small',
2015-12-11 11:11:24 +08:00
});
return (<span className={pickerClass} style={style}>
<DatePicker
2015-12-11 11:11:24 +08:00
transitionName={transitionName}
disabled={disabled}
calendar={calendar}
value={state.value}
prefixCls="ant-calendar-picker-container"
style={popupStyle}
align={align}
getCalendarContainer={getCalendarContainer}
2015-12-11 11:11:24 +08:00
onOpen={this.toggleOpen}
onClose={this.toggleOpen}
2016-02-18 19:15:23 +08:00
{...pickerChangeHandler}
>
2015-12-11 11:11:24 +08:00
{
2016-01-05 14:42:06 +08:00
({ value }) => {
2015-12-11 11:11:24 +08:00
const start = value[0];
const end = value[1];
return (
<span className={pickerInputClass} disabled={disabled}>
2016-02-18 19:15:23 +08:00
<input
disabled={disabled}
onChange={this.handleInputChange}
value={start && this.getFormatter().format(start)}
placeholder={startPlaceholder}
2016-02-18 19:15:23 +08:00
className="ant-calendar-range-picker-input"/>
2015-12-25 15:27:24 +08:00
<span className="ant-calendar-range-picker-separator"> ~ </span>
2016-02-18 19:15:23 +08:00
<input
disabled={disabled}
onChange={this.handleInputChange}
value={end && this.getFormatter().format(end)}
placeholder={endPlaceholder}
2016-02-18 19:15:23 +08:00
className="ant-calendar-range-picker-input"/>
<span className="ant-calendar-picker-icon"/>
</span>
);
2015-12-11 11:11:24 +08:00
}
}
</DatePicker>
2015-12-11 11:11:24 +08:00
</span>);
}
});