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

128 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-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-03-30 10:52:15 +08:00
value: nextProps.parseDateFromValue(nextProps.value)
2016-03-30 09:50:44 +08:00
});
}
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,
});
let pickerChangeHandler = {
onChange: this.handleChange,
};
let calendarHandler = {
onOk: this.handleChange,
};
if (props.showTime) {
pickerChangeHandler.onChange = (value) => {
// Click clear button
if (value === null) {
this.handleChange(value);
}
};
} else {
calendarHandler = {};
}
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}
{...calendarHandler} />
);
// default width for showTime
const pickerStyle = {};
if (props.showTime) {
pickerStyle.width = 180;
}
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}
2016-03-30 10:52:15 +08:00
onOpen={props.toggleOpen}
onClose={props.toggleOpen}
2016-03-30 09:50:44 +08:00
{...pickerChangeHandler}
>
{
({ value }) => {
return (
<span>
<input
disabled={props.disabled}
2016-03-30 10:52:15 +08:00
onChange={props.handleInputChange}
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-03-30 10:52:15 +08:00
className={props.pickerInputClass} />
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
}