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

125 lines
3.8 KiB
React
Raw Normal View History

2016-03-30 09:50:44 +08:00
import React from 'react';
import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
import RcDatePicker from 'rc-calendar/lib/Picker';
import GregorianCalendar from 'gregorian-calendar';
import classNames from 'classnames';
2016-05-23 15:58:10 +08:00
import Icon from '../icon';
2016-03-30 09:50:44 +08:00
2016-03-30 10:52:15 +08:00
export default function createPicker(TheCalendar) {
2016-04-13 17:23:14 +08:00
return class CalenderWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
value: this.props.parseDateFromValue(this.props.value || this.props.defaultValue),
2016-03-30 09:50:44 +08:00
};
2016-04-13 17:23:14 +08:00
}
2016-03-30 09:50:44 +08:00
componentWillReceiveProps(nextProps) {
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-04-13 17:23:14 +08:00
}
2016-05-23 15:58:10 +08:00
clearSelection = (e) => {
e.preventDefault();
e.stopPropagation();
this.setState({ value: null });
this.handleChange(null);
}
2016-04-13 17:23:14 +08:00
handleChange = (value) => {
2016-03-30 10:52:15 +08:00
const props = this.props;
if (!('value' in props)) {
2016-03-30 09:50:44 +08:00
this.setState({ value });
}
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-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({
'ant-calendar-time': props.showTime,
'ant-calendar-month': MonthCalendar === TheCalendar,
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-03-30 09:50:44 +08:00
);
// default width for showTime
const pickerStyle = {};
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"
onClick={this.clearSelection}
/> : null;
2016-03-30 09:50:44 +08:00
return (
2016-03-30 10:52:15 +08:00
<span className={props.pickerClass} style={{ ...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}
open={props.open}
2016-03-30 10:52:15 +08:00
onOpen={props.toggleOpen}
onClose={props.toggleOpen}
onChange={this.handleChange}
2016-03-30 09:50:44 +08:00
>
{
({ value }) => {
return (
<span>
<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}
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>
);
}
}
</RcDatePicker>
</span>
);
}
2016-04-13 17:23:14 +08:00
};
2016-03-30 09:50:44 +08:00
}