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.

143 lines
4.2 KiB

10 years ago
import React from 'react';
9 years ago
import Calendar from 'rc-calendar';
import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
import Datepicker from 'rc-calendar/lib/Picker';
10 years ago
import GregorianCalendar from 'gregorian-calendar';
import zhCn from 'gregorian-calendar/lib/locale/zh-cn';
import CalendarLocale from 'rc-calendar/lib/locale/zh-cn';
import DateTimeFormat from 'gregorian-calendar-format';
10 years ago
// 和顶部文案保持一致
10 years ago
import Locale from 'gregorian-calendar-format/lib/locale/zh-cn';
Locale.shortMonths = ['1月', '2月', '3月', '4月', '5月', '6月',
'7月', '8月', '9月', '10月', '11月', '12月'];
10 years ago
function createPicker(TheCalendar) {
return React.createClass({
getInitialState() {
let value;
if (this.props.value) {
value = new GregorianCalendar(zhCn);
value.setTime(new Date(this.props.value).valueOf());
}
return {
10 years ago
value: value
};
},
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
let value = null;
if (nextProps.value) {
value = new GregorianCalendar(zhCn);
value.setTime(new Date(nextProps.value).valueOf());
}
this.setState({
value: value
});
}
},
9 years ago
getFormatter() {
let formats = this.formats = this.formats || {};
const format = this.props.format;
if (formats[format]) {
return formats[format];
}
formats[format] = new DateTimeFormat(format);
return formats[format];
},
getDefaultProps() {
return {
format: 'yyyy-MM-dd',
placeholder: '请选择日期',
transitionName: 'slide-up',
onSelect: null, //向前兼容
calendarStyle: {},
9 years ago
onChange() {
} //onChange可用于Validator
};
},
handleInputChange() {},
handleChange(v) {
this.setState({
value: v
10 years ago
});
let timeValue = null;
if (v) {
timeValue = new Date(v.getTime());
}
//onSelect为向前兼容.
if (this.props.onSelect) {
require('util-deprecate')(this.props.onSelect, 'onSelect property of Datepicker is deprecated, use onChange instead')(timeValue);
}
this.props.onChange(timeValue);
},
render() {
let calendar = (
<TheCalendar
style={this.props.calendarStyle}
disabledDate={this.props.disabledDate}
locale={CalendarLocale}
showTime={this.props.showTime}
prefixCls="ant-calendar"
showOk={this.props.showTime}
9 years ago
showClear={false}/>
);
let sizeClass = '';
if (this.props.size === 'large') {
sizeClass = ' ant-input-lg';
} else if (this.props.size === 'small') {
sizeClass = ' ant-input-sm';
}
let defaultValue;
if (this.props.defaultValue) {
defaultValue = new GregorianCalendar(zhCn);
defaultValue.setTime(new Date(this.props.defaultValue).valueOf());
}
return (
<Datepicker
transitionName={this.props.transitionName}
disabled={this.props.disabled}
calendar={calendar}
value={this.state.value}
defaultValue={defaultValue}
prefixCls="ant-calendar-picker"
style={this.props.style}
onChange={this.handleChange}>
9 years ago
{
({value}) => {
return ([<input
disabled={this.props.disabled}
onChange={this.handleInputChange}
9 years ago
value={value && this.getFormatter().format(value)}
placeholder={this.props.placeholder}
className={'ant-calendar-picker-input ant-input' + sizeClass}/>,
<span className="ant-calendar-picker-icon"/>]);
}
}
</Datepicker>
);
}
});
}
const AntDatePicker = createPicker(Calendar);
const AntMonthPicker = createPicker(MonthCalendar);
9 years ago
const AntCalendar = React.createClass({
getDefaultProps() {
return {
locale: CalendarLocale,
prefixCls: 'ant-calendar',
};
},
render() {
return <Calendar {...this.props}/>;
}
});
AntDatePicker.Calendar = AntCalendar;
AntDatePicker.MonthPicker = AntMonthPicker;
9 years ago
export default AntDatePicker;