ant-design/components/calendar/index.jsx

119 lines
3.3 KiB
React
Raw Normal View History

2015-11-11 00:15:12 +08:00
import React, {PropTypes, Component} from 'react';
2015-11-12 21:24:53 +08:00
import GregorianCalendar from 'gregorian-calendar';
2015-11-11 00:15:12 +08:00
import CalendarLocale from 'rc-calendar/lib/locale/zh_CN';
import FullCalendar from 'rc-calendar/lib/FullCalendar';
2015-11-11 12:22:14 +08:00
import {PREFIX_CLS} from './Constants';
2015-11-12 21:24:53 +08:00
import Header from './Header';
2015-11-11 00:15:12 +08:00
function noop () { return null; }
function zerofixed (v) {
if (v < 10) return '0' + v;
return v + '';
}
2015-11-13 19:56:34 +08:00
class Calendar extends Component {
2015-11-12 21:24:53 +08:00
constructor(props) {
super();
this.state = {
value: this.parseDateFromValue(props.value),
2015-11-12 21:24:53 +08:00
type: props.type,
};
}
parseDateFromValue(value) {
const date = new GregorianCalendar();
date.setTime(value);
return date;
}
2015-11-11 00:15:12 +08:00
monthCellRender(value, locale) {
2015-11-11 12:22:14 +08:00
const prefixCls = this.props.prefixCls;
2015-11-13 19:56:34 +08:00
const month = value.getMonth();
return <div className={`${prefixCls}-fullscreen-month`}>
<span>{locale.format.shortMonths[month]}</span>
{this.props.monthCellRender(value)}
</div>;
2015-11-11 00:15:12 +08:00
}
fullscreenDateCellRender(value) {
2015-11-11 12:22:14 +08:00
const prefixCls = this.props.prefixCls;
2015-11-13 19:56:34 +08:00
return <span className={`${prefixCls}-fullscreen-date`}>
<span>{ zerofixed(value.getDayOfMonth()) }</span>
{this.props.dateCellRender(value)}
</span>;
2015-11-11 00:15:12 +08:00
}
dateCellRender(value) {
2015-11-11 12:22:14 +08:00
const prefixCls = this.props.prefixCls;
2015-11-13 19:56:34 +08:00
return <div style={{ position: 'relative' }}>
<span className={`${prefixCls}-date ${prefixCls}-notes-date`}>
{zerofixed(value.getDayOfMonth())}
</span>
{this.props.dateCellRender(value)}
</div>;
2015-11-11 00:15:12 +08:00
}
2015-11-12 21:24:53 +08:00
setValue(value) {
2015-11-12 23:51:38 +08:00
if (this.state.value !== value) {
this.setState({ value });
this.props.onChange(value);
}
2015-11-12 21:24:53 +08:00
}
setType(type) {
2015-11-13 15:11:38 +08:00
const oldType = this.state.type;
2015-11-12 21:24:53 +08:00
this.setState({ type });
2015-11-13 15:11:38 +08:00
this.props.onTypeChange(type, oldType);
2015-11-12 21:24:53 +08:00
}
2015-11-11 00:15:12 +08:00
render() {
const props = this.props;
2015-11-12 21:24:53 +08:00
const {value, type} = this.state;
2015-11-13 19:19:08 +08:00
const {locale, prefixCls, style, className, fullscreen} = props;
const dateCellRender = fullscreen
? this.fullscreenDateCellRender : this.dateCellRender;
2015-11-11 00:15:12 +08:00
2015-11-12 21:24:53 +08:00
return (
<div className={prefixCls + '-wrapper' + (className ? ' ' + className : '') + (fullscreen ? ' ' + prefixCls + '-wrapper-fullscreen' : '' )} style={style}>
<Header
2015-11-12 23:51:38 +08:00
fullscreen={fullscreen}
2015-11-12 21:24:53 +08:00
type={type}
value={value}
locale={locale}
prefixCls={`${prefixCls}`}
onTypeChange={this.setType.bind(this)}
onValueChange={this.setValue.bind(this)}/>
<FullCalendar
{...props}
type={type}
prefixCls={`${prefixCls}`}
showHeader={false}
value={value}
2015-11-13 19:19:08 +08:00
monthCellRender={ this.monthCellRender.bind(this) }
dateCellRender={ dateCellRender.bind(this) } />
2015-11-12 21:24:53 +08:00
</div>
);
2015-11-11 00:15:12 +08:00
}
}
2015-11-13 18:20:22 +08:00
2015-11-13 19:56:34 +08:00
Calendar.propTypes = {
2015-11-11 00:15:12 +08:00
monthCellRender: PropTypes.func,
dateCellRender: PropTypes.func,
fullscreen: PropTypes.bool,
locale: PropTypes.object,
2015-11-11 12:22:14 +08:00
prefixCls: PropTypes.string,
2015-11-12 21:24:53 +08:00
className: PropTypes.string,
style: PropTypes.object,
2015-11-12 23:51:38 +08:00
onChange: PropTypes.func,
value: PropTypes.instanceOf(Date),
2015-11-13 15:11:38 +08:00
onTypeChange: PropTypes.func,
2015-11-11 00:15:12 +08:00
};
2015-11-13 19:19:08 +08:00
2015-11-13 19:56:34 +08:00
Calendar.defaultProps = {
monthCellRender: noop,
dateCellRender: noop,
2015-11-11 00:15:12 +08:00
locale: CalendarLocale,
2015-11-13 18:20:22 +08:00
fullscreen: true,
2015-11-11 12:22:14 +08:00
prefixCls: PREFIX_CLS,
2015-11-12 23:51:38 +08:00
onChange: noop,
2015-11-13 15:11:38 +08:00
onTypeChange: noop,
2015-11-12 21:24:53 +08:00
type: 'date',
value: Date.now(),
2015-11-11 00:15:12 +08:00
};
2015-11-13 19:56:34 +08:00
export default Calendar;