ant-design/components/datepicker/index.jsx

143 lines
4.2 KiB
React
Raw Normal View History

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';
import zhCn from 'gregorian-calendar/lib/locale/zh-cn';
import CalendarLocale from 'rc-calendar/lib/locale/zh-cn';
import DateTimeFormat from 'gregorian-calendar-format';
2015-06-15 16:42:27 +08:00
// 和顶部文案保持一致
2015-06-19 12:29:22 +08:00
import Locale from 'gregorian-calendar-format/lib/locale/zh-cn';
Locale.shortMonths = ['1月', '2月', '3月', '4月', '5月', '6月',
2015-08-04 18:42:41 +08:00
'7月', '8月', '9月', '10月', '11月', '12月'];
2015-06-19 12:29:22 +08:00
2015-09-01 16:18:46 +08:00
function createPicker(TheCalendar) {
2015-08-25 17:27:38 +08:00
return React.createClass({
getInitialState() {
2015-09-01 16:18:46 +08:00
let value;
2015-08-25 17:27:38 +08:00
if (this.props.value) {
value = new GregorianCalendar(zhCn);
value.setTime(new Date(this.props.value).valueOf());
}
return {
2015-06-30 19:28:56 +08:00
value: value
2015-08-25 17:27:38 +08:00
};
},
componentWillReceiveProps(nextProps) {
2015-09-01 10:59:12 +08:00
if ('value' in nextProps) {
let value = null;
if (nextProps.value) {
value = new GregorianCalendar(zhCn);
value.setTime(new Date(nextProps.value).valueOf());
}
2015-08-25 17:27:38 +08:00
this.setState({
value: value
});
}
},
2015-10-20 16:47:55 +08:00
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];
},
2015-08-25 17:27:38 +08:00
getDefaultProps() {
return {
format: 'yyyy-MM-dd',
placeholder: '请选择日期',
transitionName: 'slide-up',
onSelect: null, //向前兼容
2015-10-20 16:50:42 +08:00
calendarStyle: {},
2015-10-20 16:47:55 +08:00
onChange() {
} //onChange可用于Validator
2015-08-25 17:27:38 +08:00
};
},
2015-10-22 17:16:02 +08:00
handleInputChange() {},
2015-08-25 17:27:38 +08:00
handleChange(v) {
this.setState({
value: v
2015-06-30 19:28:56 +08:00
});
2015-10-21 16:27:48 +08:00
let timeValue = null;
if (v) {
timeValue = new Date(v.getTime());
}
//onSelect为向前兼容.
2015-10-13 13:30:37 +08:00
if (this.props.onSelect) {
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-09-01 16:18:46 +08:00
let calendar = (
<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}
locale={CalendarLocale}
showTime={this.props.showTime}
prefixCls="ant-calendar"
showOk={this.props.showTime}
2015-10-20 16:47:55 +08:00
showClear={false}/>
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
}
let defaultValue;
if (this.props.defaultValue) {
defaultValue = new GregorianCalendar(zhCn);
defaultValue.setTime(new Date(this.props.defaultValue).valueOf());
}
2015-08-25 17:27:38 +08:00
return (
<Datepicker
transitionName={this.props.transitionName}
disabled={this.props.disabled}
calendar={calendar}
value={this.state.value}
defaultValue={defaultValue}
2015-08-25 17:27:38 +08:00
prefixCls="ant-calendar-picker"
style={this.props.style}
onChange={this.handleChange}>
2015-10-20 16:47:55 +08:00
{
({value}) => {
return ([<input
disabled={this.props.disabled}
2015-10-22 17:16:02 +08:00
onChange={this.handleInputChange}
2015-10-20 16:47:55 +08:00
value={value && this.getFormatter().format(value)}
placeholder={this.props.placeholder}
className={'ant-calendar-picker-input ant-input' + sizeClass}/>,
<span className="ant-calendar-picker-icon"/>]);
}
}
2015-08-25 17:27:38 +08:00
</Datepicker>
);
}
});
}
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() {
return <Calendar {...this.props}/>;
}
});
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;