ant-design/components/datepicker/index.jsx

115 lines
3.3 KiB
React
Raw Normal View History

2015-06-19 12:29:22 +08:00
import React from 'react';
2015-08-25 17:27:38 +08:00
import Calendar, {MonthCalendar, Picker as Datepicker} from 'rc-calendar';
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-06-17 19:57:05 +08:00
// 以下两行代码
// 给没有初始值的日期选择框提供本地化信息
2015-06-19 12:29:22 +08:00
let defaultCalendarValue = new GregorianCalendar(zhCn);
2015-06-03 20:12:49 +08:00
defaultCalendarValue.setTime(Date.now());
2015-05-21 20:36:13 +08:00
2015-08-25 17:27:38 +08:00
2015-08-28 00:11:56 +08:00
function createPicker(Calendar) {
2015-08-25 17:27:38 +08:00
return React.createClass({
getInitialState() {
var value;
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) {
if (nextProps.value) {
var value = new GregorianCalendar(zhCn);
value.setTime(new Date(nextProps.value).valueOf());
this.setState({
value: value
});
}
},
getDefaultProps() {
return {
format: 'yyyy-MM-dd',
placeholder: '请选择日期',
transitionName: 'slide-up',
onSelect() {
}
};
},
handleChange(v) {
this.setState({
value: v
2015-06-30 19:28:56 +08:00
});
2015-08-25 17:27:38 +08:00
this.props.onSelect(new Date(v.getTime()));
},
render() {
var calendar = (
<Calendar
disabledDate={this.props.disabledDate}
locale={CalendarLocale}
orient={['top', 'left']}
defaultValue={defaultCalendarValue}
showTime={this.props.showTime}
prefixCls="ant-calendar"
showOk={this.props.showTime}
showClear={false}/>
);
var sizeClass = '';
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-08-25 17:27:38 +08:00
return (
<Datepicker
transitionName={this.props.transitionName}
disabled={this.props.disabled}
trigger={<span className="ant-calendar-picker-icon" />}
calendar={calendar}
2015-08-25 17:33:24 +08:00
adjustOrientOnCalendarOverflow={{x: true, y: false}}
2015-08-25 17:27:38 +08:00
formatter={new DateTimeFormat(this.props.format)}
value={this.state.value}
defaultValue={this.props.defaultValue}
prefixCls="ant-calendar-picker"
style={this.props.style}
onChange={this.handleChange}>
<input
placeholder={this.props.placeholder}
className={'ant-calendar-picker-input ant-input' + sizeClass}/>
</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;