2016-07-07 20:25:03 +08:00
|
|
|
import * as React from 'react';
|
2016-03-30 09:50:44 +08:00
|
|
|
import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
|
|
|
|
import RcDatePicker from 'rc-calendar/lib/Picker';
|
|
|
|
import GregorianCalendar from 'gregorian-calendar';
|
|
|
|
import classNames from 'classnames';
|
2016-06-22 13:18:43 +08:00
|
|
|
import assign from 'object-assign';
|
2016-05-23 15:58:10 +08:00
|
|
|
import Icon from '../icon';
|
2016-03-30 09:50:44 +08:00
|
|
|
|
2016-08-24 16:56:29 +08:00
|
|
|
export interface PickerProps {
|
|
|
|
parseDateFromValue?: Function;
|
|
|
|
value?: string | Date;
|
|
|
|
}
|
|
|
|
|
2016-03-30 10:52:15 +08:00
|
|
|
export default function createPicker(TheCalendar) {
|
2016-06-22 13:18:43 +08:00
|
|
|
// use class typescript error
|
|
|
|
const CalenderWrapper = React.createClass({
|
2016-04-13 17:23:14 +08:00
|
|
|
|
2016-06-22 13:18:43 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
2016-04-13 17:23:14 +08:00
|
|
|
value: this.props.parseDateFromValue(this.props.value || this.props.defaultValue),
|
2016-03-30 09:50:44 +08:00
|
|
|
};
|
2016-06-22 13:18:43 +08:00
|
|
|
},
|
2016-04-13 17:23:14 +08:00
|
|
|
|
2016-08-24 16:56:29 +08:00
|
|
|
componentWillReceiveProps(nextProps: PickerProps) {
|
2016-03-30 09:50:44 +08:00
|
|
|
if ('value' in nextProps) {
|
|
|
|
this.setState({
|
2016-05-11 09:32:33 +08:00
|
|
|
value: nextProps.parseDateFromValue(nextProps.value),
|
2016-03-30 09:50:44 +08:00
|
|
|
});
|
|
|
|
}
|
2016-06-22 13:18:43 +08:00
|
|
|
},
|
2016-04-13 17:23:14 +08:00
|
|
|
|
2016-06-24 14:43:46 +08:00
|
|
|
clearSelection(e) {
|
2016-05-23 15:58:10 +08:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.setState({ value: null });
|
|
|
|
this.handleChange(null);
|
2016-06-23 23:11:14 +08:00
|
|
|
},
|
|
|
|
|
2016-06-24 14:43:46 +08:00
|
|
|
handleChange(value) {
|
2016-03-30 10:52:15 +08:00
|
|
|
const props = this.props;
|
|
|
|
if (!('value' in props)) {
|
2016-06-24 14:43:46 +08:00
|
|
|
this.setState({ value });
|
2016-03-30 09:50:44 +08:00
|
|
|
}
|
|
|
|
const timeValue = value ? new Date(value.getTime()) : null;
|
2016-03-30 10:52:15 +08:00
|
|
|
props.onChange(timeValue, value ? props.getFormatter().format(value) : '');
|
2016-06-22 13:18:43 +08:00
|
|
|
},
|
2016-04-13 17:23:14 +08:00
|
|
|
|
2016-03-30 09:50:44 +08:00
|
|
|
render() {
|
|
|
|
const props = this.props;
|
2016-03-30 10:52:15 +08:00
|
|
|
const locale = props.locale;
|
2016-03-30 09:50:44 +08:00
|
|
|
// 以下两行代码
|
|
|
|
// 给没有初始值的日期选择框提供本地化信息
|
|
|
|
// 否则会以周日开始排
|
|
|
|
let defaultCalendarValue = new GregorianCalendar(locale);
|
|
|
|
defaultCalendarValue.setTime(Date.now());
|
|
|
|
|
|
|
|
const placeholder = ('placeholder' in props)
|
|
|
|
? props.placeholder : locale.lang.placeholder;
|
|
|
|
|
|
|
|
const disabledTime = props.showTime ? props.disabledTime : null;
|
|
|
|
|
|
|
|
const calendarClassName = classNames({
|
2016-05-10 15:15:09 +08:00
|
|
|
'ant-calendar-time': props.showTime,
|
|
|
|
'ant-calendar-month': MonthCalendar === TheCalendar,
|
2016-03-30 09:50:44 +08:00
|
|
|
});
|
|
|
|
|
2016-07-15 16:20:23 +08:00
|
|
|
// 需要选择时间时,点击 ok 时才触发 onChange
|
2016-08-22 17:26:14 +08:00
|
|
|
let pickerChangeHandler: Object = {
|
2016-07-15 16:20:23 +08:00
|
|
|
onChange: this.handleChange,
|
|
|
|
};
|
2016-08-22 17:26:14 +08:00
|
|
|
let calendarHandler: Object = {
|
2016-07-15 16:20:23 +08:00
|
|
|
onOk: this.handleChange,
|
2016-07-22 16:14:50 +08:00
|
|
|
// fix https://github.com/ant-design/ant-design/issues/1902
|
|
|
|
onSelect: (value, cause) => {
|
|
|
|
if (cause && cause.source === 'todayButton') {
|
|
|
|
this.handleChange(value);
|
|
|
|
}
|
|
|
|
},
|
2016-07-15 16:20:23 +08:00
|
|
|
};
|
|
|
|
if (props.showTime) {
|
|
|
|
pickerChangeHandler = {};
|
|
|
|
} else {
|
|
|
|
calendarHandler = {};
|
|
|
|
}
|
|
|
|
|
2016-03-30 09:50:44 +08:00
|
|
|
const calendar = (
|
|
|
|
<TheCalendar
|
2016-04-13 15:33:44 +08:00
|
|
|
formatter={props.getFormatter()}
|
2016-03-30 09:50:44 +08:00
|
|
|
disabledDate={props.disabledDate}
|
|
|
|
disabledTime={disabledTime}
|
|
|
|
locale={locale.lang}
|
2016-03-30 10:52:15 +08:00
|
|
|
timePicker={props.timePicker}
|
2016-03-30 09:50:44 +08:00
|
|
|
defaultValue={defaultCalendarValue}
|
|
|
|
dateInputPlaceholder={placeholder}
|
|
|
|
prefixCls="ant-calendar"
|
|
|
|
className={calendarClassName}
|
2016-07-15 16:20:23 +08:00
|
|
|
onOk={props.onOk}
|
|
|
|
{...calendarHandler}
|
2016-06-02 21:19:28 +08:00
|
|
|
/>
|
2016-03-30 09:50:44 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// default width for showTime
|
2016-08-22 17:26:14 +08:00
|
|
|
const pickerStyle = { width: undefined };
|
2016-03-30 09:50:44 +08:00
|
|
|
if (props.showTime) {
|
|
|
|
pickerStyle.width = 180;
|
|
|
|
}
|
|
|
|
|
2016-05-23 15:58:10 +08:00
|
|
|
const clearIcon = (!props.disabled && this.state.value) ?
|
|
|
|
<Icon type="cross-circle"
|
|
|
|
className="ant-calendar-picker-clear"
|
2016-06-23 15:38:55 +08:00
|
|
|
onClick={this.clearSelection}
|
|
|
|
/> : null;
|
2016-03-30 09:50:44 +08:00
|
|
|
return (
|
2016-06-22 13:18:43 +08:00
|
|
|
<span className={props.pickerClass} style={assign({}, pickerStyle, props.style)}>
|
2016-03-30 09:50:44 +08:00
|
|
|
<RcDatePicker
|
|
|
|
transitionName={props.transitionName}
|
|
|
|
disabled={props.disabled}
|
|
|
|
calendar={calendar}
|
|
|
|
value={this.state.value}
|
|
|
|
prefixCls="ant-calendar-picker-container"
|
|
|
|
style={props.popupStyle}
|
|
|
|
align={props.align}
|
|
|
|
getCalendarContainer={props.getCalendarContainer}
|
2016-06-15 17:46:25 +08:00
|
|
|
open={props.open}
|
2016-03-30 10:52:15 +08:00
|
|
|
onOpen={props.toggleOpen}
|
|
|
|
onClose={props.toggleOpen}
|
2016-07-15 16:20:23 +08:00
|
|
|
{...pickerChangeHandler}
|
2016-03-30 09:50:44 +08:00
|
|
|
>
|
|
|
|
{
|
|
|
|
({ value }) => {
|
|
|
|
return (
|
2016-06-22 13:18:43 +08:00
|
|
|
<span>
|
2016-03-30 09:50:44 +08:00
|
|
|
<input
|
|
|
|
disabled={props.disabled}
|
2016-06-15 17:49:16 +08:00
|
|
|
readOnly
|
2016-04-07 18:07:18 +08:00
|
|
|
value={value ? props.getFormatter().format(value) : ''}
|
2016-03-30 09:50:44 +08:00
|
|
|
placeholder={placeholder}
|
2016-06-06 13:54:10 +08:00
|
|
|
className={props.pickerInputClass}
|
|
|
|
/>
|
2016-05-23 15:58:10 +08:00
|
|
|
{clearIcon}
|
2016-03-30 09:50:44 +08:00
|
|
|
<span className="ant-calendar-picker-icon" />
|
|
|
|
</span>
|
2016-06-22 13:18:43 +08:00
|
|
|
);
|
|
|
|
}
|
2016-03-30 09:50:44 +08:00
|
|
|
}
|
|
|
|
</RcDatePicker>
|
|
|
|
</span>
|
|
|
|
);
|
2016-06-22 13:18:43 +08:00
|
|
|
},
|
|
|
|
});
|
2016-06-23 23:11:14 +08:00
|
|
|
|
2016-06-22 13:18:43 +08:00
|
|
|
return CalenderWrapper;
|
2016-03-30 09:50:44 +08:00
|
|
|
}
|