2016-03-26 17:22:39 +08:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-01-05 14:42:06 +08:00
|
|
|
import { PREFIX_CLS } from './Constants';
|
2015-11-12 21:24:53 +08:00
|
|
|
import Select from '../select';
|
2016-01-05 14:42:06 +08:00
|
|
|
import { Group, Button } from '../radio';
|
2015-11-12 21:24:53 +08:00
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
2016-03-26 17:22:39 +08:00
|
|
|
export default class Header extends React.Component {
|
2015-11-12 21:24:53 +08:00
|
|
|
getYearSelectElement(year) {
|
2016-01-05 14:42:06 +08:00
|
|
|
const { yearSelectOffset, yearSelectTotal, locale, prefixCls, fullscreen } = this.props;
|
2015-11-12 21:24:53 +08:00
|
|
|
const start = year - yearSelectOffset;
|
|
|
|
const end = start + yearSelectTotal;
|
|
|
|
const suffix = locale.year === '年' ? '年' : '';
|
|
|
|
|
|
|
|
const options = [];
|
|
|
|
for (let index = start; index < end; index++) {
|
2016-01-05 14:42:06 +08:00
|
|
|
options.push(<Option key={`${index}`}>{index + suffix}</Option>);
|
2015-11-12 21:24:53 +08:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Select
|
2015-11-13 18:20:22 +08:00
|
|
|
style={{ width: 75 }}
|
2015-11-12 23:51:38 +08:00
|
|
|
size={ fullscreen ? null : 'small' }
|
2015-11-12 21:24:53 +08:00
|
|
|
dropdownMatchSelectWidth={false}
|
2015-11-16 15:07:02 +08:00
|
|
|
dropdownMenuStyle={{ minWidth: 103 }}
|
2015-11-12 21:24:53 +08:00
|
|
|
className={`${prefixCls}-year-select`}
|
2016-03-23 19:50:44 +08:00
|
|
|
onChange={this.onYearChange}
|
2015-11-12 21:24:53 +08:00
|
|
|
value={String(year)}>
|
|
|
|
{ options }
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
getMonthSelectElement(month) {
|
|
|
|
const props = this.props;
|
|
|
|
const months = props.locale.format.months;
|
2016-01-05 14:42:06 +08:00
|
|
|
const { prefixCls, fullscreen } = props;
|
2015-11-12 21:24:53 +08:00
|
|
|
const options = [];
|
|
|
|
|
|
|
|
for (let index = 0; index < 12; index++) {
|
|
|
|
options.push(<Option key={`${index}`}>{months[index]}</Option>);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
2015-11-16 20:24:37 +08:00
|
|
|
style={{ minWidth: 70 }}
|
|
|
|
dropdownMenuStyle={{ minWidth: 125 }}
|
2015-11-12 23:51:38 +08:00
|
|
|
size={ fullscreen ? null : 'small' }
|
2015-11-12 21:24:53 +08:00
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
className={`${prefixCls}-month-select`}
|
|
|
|
value={String(month)}
|
2016-03-23 19:50:44 +08:00
|
|
|
onChange={this.onMonthChange}>
|
2015-11-12 21:24:53 +08:00
|
|
|
{ options }
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
2016-03-23 19:50:44 +08:00
|
|
|
onYearChange = (year) => {
|
2015-11-12 21:24:53 +08:00
|
|
|
const newValue = this.props.value.clone();
|
|
|
|
newValue.setYear(parseInt(year, 10));
|
|
|
|
this.props.onValueChange(newValue);
|
|
|
|
}
|
|
|
|
|
2016-03-23 19:50:44 +08:00
|
|
|
onMonthChange = (month) => {
|
2015-11-12 21:24:53 +08:00
|
|
|
const newValue = this.props.value.clone();
|
|
|
|
newValue.setMonth(parseInt(month, 10));
|
|
|
|
this.props.onValueChange(newValue);
|
|
|
|
}
|
2016-03-23 19:50:44 +08:00
|
|
|
onTypeChange = (e) => {
|
2015-11-12 21:24:53 +08:00
|
|
|
this.props.onTypeChange(e.target.value);
|
|
|
|
}
|
|
|
|
render() {
|
2016-01-05 14:42:06 +08:00
|
|
|
const { type, value, prefixCls, locale } = this.props;
|
2015-11-12 21:24:53 +08:00
|
|
|
const yearSelect = this.getYearSelectElement(value.getYear());
|
|
|
|
const monthSelect = type === 'date' ? this.getMonthSelectElement(value.getMonth()) : null;
|
|
|
|
const typeSwitch = (
|
2016-03-23 19:50:44 +08:00
|
|
|
<Group onChange={this.onTypeChange} value={type}>
|
2015-11-12 21:24:53 +08:00
|
|
|
<Button value="date">{locale.month}</Button>
|
|
|
|
<Button value="month">{locale.year}</Button>
|
|
|
|
</Group>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={`${prefixCls}-header`}>
|
|
|
|
{ yearSelect }
|
|
|
|
{ monthSelect }
|
|
|
|
{ typeSwitch }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 19:56:34 +08:00
|
|
|
|
2015-11-12 21:24:53 +08:00
|
|
|
Header.propTypes = {
|
|
|
|
value: PropTypes.object,
|
|
|
|
locale: PropTypes.object,
|
|
|
|
yearSelectOffset: PropTypes.number,
|
|
|
|
yearSelectTotal: PropTypes.number,
|
|
|
|
onValueChange: PropTypes.func,
|
|
|
|
onTypeChange: PropTypes.func,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
selectPrefixCls: PropTypes.string,
|
|
|
|
type: PropTypes.string,
|
|
|
|
};
|
2015-11-13 19:56:34 +08:00
|
|
|
|
2015-11-12 21:24:53 +08:00
|
|
|
Header.defaultProps = {
|
|
|
|
prefixCls: `${PREFIX_CLS}-header`,
|
|
|
|
yearSelectOffset: 10,
|
|
|
|
yearSelectTotal: 20,
|
|
|
|
onValueChange: noop,
|
|
|
|
onTypeChange: noop,
|
|
|
|
};
|