ant-design/components/datepicker/index.jsx

63 lines
1.8 KiB
React
Raw Normal View History

2015-06-10 17:59:32 +08:00
'use strict';
2015-05-21 20:36:13 +08:00
var React = require('react');
var Calendar = require('rc-calendar');
2015-06-09 14:50:55 +08:00
var Datepicker = Calendar.Picker;
2015-05-21 20:36:13 +08:00
var GregorianCalendar = require('gregorian-calendar');
var zhCn = require('gregorian-calendar/lib/locale/zh-cn');
2015-05-22 12:02:56 +08:00
var CalendarLocale = require('rc-calendar/lib/locale/zh-cn');
2015-05-25 17:20:49 +08:00
var DateTimeFormat = require('gregorian-calendar-format');
2015-06-03 20:12:49 +08:00
var defaultCalendarValue = new GregorianCalendar(zhCn);
defaultCalendarValue.setTime(Date.now());
2015-05-21 20:36:13 +08:00
module.exports = React.createClass({
getInitialState: function () {
return {
2015-05-29 21:28:01 +08:00
value: ''
2015-05-21 20:36:13 +08:00
};
},
2015-05-25 17:20:49 +08:00
getDefaultProps: function () {
return {
2015-06-01 17:58:02 +08:00
format: 'yyyy-MM-dd',
placeholder: '请选择日期'
2015-05-25 17:20:49 +08:00
};
},
2015-05-26 12:03:49 +08:00
componentDidMount: function () {
2015-05-29 15:06:45 +08:00
var state = {};
2015-05-26 12:03:49 +08:00
if (this.props.value) {
var value = new GregorianCalendar(zhCn);
value.setTime(new Date(this.props.value));
2015-05-29 15:06:45 +08:00
state.value = value;
2015-05-26 12:03:49 +08:00
}
2015-05-29 15:06:45 +08:00
state.disabled = this.props.disabled || function() {};
this.setState(state);
2015-05-26 12:03:49 +08:00
},
2015-05-26 13:58:35 +08:00
handleChange: function() {
this.props.onSelect(new Date(this.state.value.getTime()));
},
2015-05-21 20:36:13 +08:00
render: function () {
var calendar = (
<Calendar
2015-05-29 15:06:45 +08:00
disabledDate={this.state.disabled}
2015-05-22 12:02:56 +08:00
locale={CalendarLocale}
2015-05-21 20:36:13 +08:00
orient={['top', 'left']}
2015-06-03 20:12:49 +08:00
defaultValue={defaultCalendarValue}
2015-05-29 21:07:46 +08:00
showTime={this.props.showTime}
2015-06-10 19:46:41 +08:00
prefixCls="ant-calendar"
2015-06-11 18:08:00 +08:00
showOk={this.props.showTime}
2015-05-29 21:07:46 +08:00
showClear={false} />
2015-05-29 15:29:18 +08:00
);
return (
2015-06-09 14:50:55 +08:00
<Datepicker
2015-06-10 19:46:41 +08:00
trigger={<span className="ant-calendar-picker-icon" />}
2015-05-29 15:29:18 +08:00
calendar={calendar}
formatter={new DateTimeFormat(this.props.format)}
value={this.state.value}
2015-06-10 19:46:41 +08:00
prefixCls="ant-calendar-picker"
2015-05-29 15:29:18 +08:00
onChange={this.props.onSelect}>
2015-06-10 19:46:41 +08:00
<input placeholder={this.props.placeholder} className="ant-calendar-picker-input" />
2015-06-09 14:50:55 +08:00
</Datepicker>
2015-05-29 15:29:18 +08:00
);
2015-05-21 20:36:13 +08:00
}
});