2015-06-19 12:29:22 +08:00
|
|
|
import React from 'react';
|
2015-10-20 16:47:55 +08:00
|
|
|
import Calendar from 'rc-calendar';
|
|
|
|
import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
|
|
|
|
import Datepicker from 'rc-calendar/lib/Picker';
|
2015-06-19 12:29:22 +08:00
|
|
|
import GregorianCalendar from 'gregorian-calendar';
|
2015-10-23 17:57:02 +08:00
|
|
|
import defaultLocale from './locale';
|
2015-06-19 12:29:22 +08:00
|
|
|
import CalendarLocale from 'rc-calendar/lib/locale/zh-cn';
|
|
|
|
import DateTimeFormat from 'gregorian-calendar-format';
|
2015-10-23 17:57:02 +08:00
|
|
|
import objectAssign from 'object-assign';
|
2015-06-19 12:29:22 +08:00
|
|
|
|
2015-10-23 17:57:02 +08:00
|
|
|
// 转换 locale 为 rc-calender 接收的格式
|
|
|
|
function getCalendarLocale(locale) {
|
|
|
|
locale.format = locale.format || {};
|
|
|
|
[
|
|
|
|
'eras',
|
|
|
|
'months',
|
|
|
|
'shortMonths',
|
|
|
|
'weekdays',
|
|
|
|
'shortWeekdays',
|
|
|
|
'veryShortWeekdays',
|
|
|
|
'ampms',
|
|
|
|
'datePatterns',
|
|
|
|
'timePatterns',
|
|
|
|
'dateTimePattern'
|
|
|
|
].forEach(function(key) {
|
|
|
|
locale.format[key] = locale[key];
|
|
|
|
});
|
|
|
|
return locale;
|
|
|
|
}
|
2015-10-23 11:52:05 +08:00
|
|
|
|
2015-09-01 16:18:46 +08:00
|
|
|
function createPicker(TheCalendar) {
|
2015-08-25 17:27:38 +08:00
|
|
|
return React.createClass({
|
2015-10-23 11:52:05 +08:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
format: 'yyyy-MM-dd',
|
|
|
|
placeholder: '请选择日期',
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
calendarStyle: {},
|
2015-10-23 17:57:02 +08:00
|
|
|
onSelect: null, // 向前兼容
|
2015-10-23 15:55:48 +08:00
|
|
|
onChange() {}, // onChange 可用于 Validator
|
2015-10-28 19:48:32 +08:00
|
|
|
locale: {},
|
2015-10-29 19:59:48 +08:00
|
|
|
// 自动换方向有很多视觉和交互问题
|
|
|
|
// 需求不是很大,和设计师协商后不做
|
2015-10-31 15:15:55 +08:00
|
|
|
align: {
|
2015-10-28 19:48:32 +08:00
|
|
|
points: ['tl', 'tl'],
|
|
|
|
overflow: { adjustX: 0, adjustY: 0 },
|
2015-11-02 14:46:06 +08:00
|
|
|
offset: [0, -10],
|
2015-10-28 19:48:32 +08:00
|
|
|
targetOffset: [0, 0]
|
|
|
|
}
|
2015-10-23 11:52:05 +08:00
|
|
|
};
|
|
|
|
},
|
2015-08-25 17:27:38 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
2015-10-23 11:52:05 +08:00
|
|
|
value: this.parseDateFromValue(this.props.value)
|
2015-08-25 17:27:38 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2015-09-01 10:59:12 +08:00
|
|
|
if ('value' in nextProps) {
|
2015-08-25 17:27:38 +08:00
|
|
|
this.setState({
|
2015-10-23 11:52:05 +08:00
|
|
|
value: this.parseDateFromValue(nextProps.value)
|
2015-08-25 17:27:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2015-10-23 17:57:02 +08:00
|
|
|
getLocale() {
|
|
|
|
// 统一合并为完整的 Locale
|
|
|
|
let locale = objectAssign({}, defaultLocale, this.props.locale);
|
|
|
|
locale.lang = objectAssign({}, defaultLocale.lang, this.props.locale.lang);
|
|
|
|
return locale;
|
|
|
|
},
|
2015-10-20 16:47:55 +08:00
|
|
|
getFormatter() {
|
2015-10-23 10:51:56 +08:00
|
|
|
const formats = this.formats = this.formats || {};
|
2015-10-20 16:47:55 +08:00
|
|
|
const format = this.props.format;
|
|
|
|
if (formats[format]) {
|
|
|
|
return formats[format];
|
|
|
|
}
|
|
|
|
formats[format] = new DateTimeFormat(format);
|
|
|
|
return formats[format];
|
|
|
|
},
|
2015-10-23 11:52:05 +08:00
|
|
|
parseDateFromValue(value) {
|
|
|
|
if (value) {
|
|
|
|
if (typeof value === 'string') {
|
2015-10-23 17:57:02 +08:00
|
|
|
return new DateTimeFormat(this.props.format).parse(value, this.getLocale());
|
2015-10-23 11:52:05 +08:00
|
|
|
} else if (value instanceof Date) {
|
2015-10-23 17:57:02 +08:00
|
|
|
let date = new GregorianCalendar(this.getLocale());
|
2015-10-23 11:52:05 +08:00
|
|
|
date.setTime(value);
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 19:37:39 +08:00
|
|
|
return undefined;
|
2015-08-25 17:27:38 +08:00
|
|
|
},
|
2015-10-23 15:55:48 +08:00
|
|
|
// remove input readonly warning
|
2015-10-22 17:16:02 +08:00
|
|
|
handleInputChange() {},
|
2015-10-23 15:55:48 +08:00
|
|
|
handleChange(value) {
|
|
|
|
this.setState({ value });
|
2015-10-29 19:37:39 +08:00
|
|
|
const timeValue = value ? new Date(value.getTime()) : undefined;
|
2015-10-23 15:55:48 +08:00
|
|
|
// onSelect 为向前兼容.
|
2015-10-13 13:30:37 +08:00
|
|
|
if (this.props.onSelect) {
|
2015-10-12 17:53:01 +08:00
|
|
|
require('util-deprecate')(this.props.onSelect, 'onSelect property of Datepicker is deprecated, use onChange instead')(timeValue);
|
|
|
|
}
|
|
|
|
this.props.onChange(timeValue);
|
2015-08-25 17:27:38 +08:00
|
|
|
},
|
|
|
|
render() {
|
2015-10-23 17:57:02 +08:00
|
|
|
// 以下两行代码
|
|
|
|
// 给没有初始值的日期选择框提供本地化信息
|
|
|
|
// 否则会以周日开始排
|
|
|
|
let defaultCalendarValue = new GregorianCalendar(this.getLocale());
|
|
|
|
defaultCalendarValue.setTime(Date.now());
|
2015-10-23 10:51:56 +08:00
|
|
|
const calendar = (
|
2015-09-01 16:18:46 +08:00
|
|
|
<TheCalendar
|
2015-10-20 16:50:42 +08:00
|
|
|
style={this.props.calendarStyle}
|
2015-08-25 17:27:38 +08:00
|
|
|
disabledDate={this.props.disabledDate}
|
2015-10-23 17:57:02 +08:00
|
|
|
locale={getCalendarLocale(this.getLocale().lang)}
|
2015-10-23 11:52:05 +08:00
|
|
|
defaultValue={defaultCalendarValue}
|
2015-10-31 15:15:55 +08:00
|
|
|
dateInputPlaceholder={this.props.placeholder}
|
2015-08-25 17:27:38 +08:00
|
|
|
showTime={this.props.showTime}
|
|
|
|
prefixCls="ant-calendar"
|
|
|
|
showOk={this.props.showTime}
|
2015-10-28 19:48:32 +08:00
|
|
|
showClear={true} />
|
2015-08-25 17:27:38 +08:00
|
|
|
);
|
2015-09-01 16:18:46 +08:00
|
|
|
let sizeClass = '';
|
2015-08-25 17:27:38 +08:00
|
|
|
if (this.props.size === 'large') {
|
|
|
|
sizeClass = ' ant-input-lg';
|
|
|
|
} else if (this.props.size === 'small') {
|
|
|
|
sizeClass = ' ant-input-sm';
|
2015-08-04 18:42:41 +08:00
|
|
|
}
|
2015-10-23 11:52:05 +08:00
|
|
|
let defaultValue = this.parseDateFromValue(this.props.defaultValue);
|
2015-10-31 15:15:55 +08:00
|
|
|
return <span className="ant-calendar-picker">
|
2015-08-25 17:27:38 +08:00
|
|
|
<Datepicker
|
|
|
|
transitionName={this.props.transitionName}
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
calendar={calendar}
|
|
|
|
value={this.state.value}
|
2015-10-14 18:09:28 +08:00
|
|
|
defaultValue={defaultValue}
|
2015-10-31 15:15:55 +08:00
|
|
|
prefixCls="ant-calendar-picker-container"
|
2015-08-25 17:27:38 +08:00
|
|
|
style={this.props.style}
|
2015-10-31 15:15:55 +08:00
|
|
|
align={this.props.align}
|
2015-08-25 17:27:38 +08:00
|
|
|
onChange={this.handleChange}>
|
2015-10-20 16:47:55 +08:00
|
|
|
{
|
|
|
|
({value}) => {
|
2015-10-29 19:59:48 +08:00
|
|
|
return <span>
|
|
|
|
<input disabled={this.props.disabled}
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
value={value && this.getFormatter().format(value)}
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
className={'ant-calendar-picker-input ant-input' + sizeClass} />
|
|
|
|
<span className="ant-calendar-picker-icon" />
|
|
|
|
</span>;
|
2015-10-20 16:47:55 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-25 17:27:38 +08:00
|
|
|
</Datepicker>
|
2015-10-31 15:15:55 +08:00
|
|
|
</span>;
|
2015-08-25 17:27:38 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const AntDatePicker = createPicker(Calendar);
|
|
|
|
const AntMonthPicker = createPicker(MonthCalendar);
|
2015-08-24 14:30:08 +08:00
|
|
|
|
|
|
|
const AntCalendar = React.createClass({
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
locale: CalendarLocale,
|
|
|
|
prefixCls: 'ant-calendar',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
render() {
|
2015-10-23 10:51:56 +08:00
|
|
|
return <Calendar {...this.props} />;
|
2015-08-24 14:30:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-25 17:27:38 +08:00
|
|
|
AntDatePicker.Calendar = AntCalendar;
|
|
|
|
AntDatePicker.MonthPicker = AntMonthPicker;
|
2015-08-24 14:30:08 +08:00
|
|
|
|
2015-08-25 17:27:38 +08:00
|
|
|
export default AntDatePicker;
|