2016-09-09 13:55:21 +08:00
|
|
|
import React from 'react';
|
2016-07-07 20:25:03 +08:00
|
|
|
import { 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';
|
2016-04-27 11:23:18 +08:00
|
|
|
const Option = Select.Option;
|
2015-11-12 21:24:53 +08:00
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
2016-08-19 17:11:06 +08:00
|
|
|
export interface HeaderProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
locale?: any;
|
|
|
|
fullscreen?: boolean;
|
|
|
|
yearSelectOffset?: number;
|
|
|
|
yearSelectTotal?: number;
|
|
|
|
type?: string;
|
|
|
|
onValueChange?: (value) => void;
|
|
|
|
onTypeChange?: (type: string) => void;
|
2016-08-24 16:09:55 +08:00
|
|
|
value: any;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Header extends React.Component<HeaderProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: `${PREFIX_CLS}-header`,
|
|
|
|
yearSelectOffset: 10,
|
|
|
|
yearSelectTotal: 20,
|
|
|
|
onValueChange: noop,
|
|
|
|
onTypeChange: noop,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
|
|
|
static 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,
|
2016-08-31 14:45:38 +08:00
|
|
|
fullscreen: PropTypes.bool,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
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
|
2016-04-29 12:13:27 +08:00
|
|
|
size={fullscreen ? null : 'small'}
|
2015-11-12 21:24:53 +08:00
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
className={`${prefixCls}-year-select`}
|
2016-03-23 19:50:44 +08:00
|
|
|
onChange={this.onYearChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
value={String(year)}
|
|
|
|
>
|
2016-04-29 12:13:27 +08:00
|
|
|
{options}
|
2015-11-12 21:24:53 +08:00
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-09-09 13:55:21 +08:00
|
|
|
getMonthsLocale(value) {
|
|
|
|
const current = value.clone();
|
|
|
|
const localeData = value.localeData();
|
|
|
|
const months = [];
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
current.month(i);
|
|
|
|
months.push(localeData.monthsShort(current));
|
|
|
|
}
|
|
|
|
return months;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMonthSelectElement(month, months) {
|
2015-11-12 21:24:53 +08:00
|
|
|
const props = this.props;
|
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
|
2016-04-29 12:13:27 +08:00
|
|
|
size={fullscreen ? null : 'small'}
|
2015-11-12 21:24:53 +08:00
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
className={`${prefixCls}-month-select`}
|
|
|
|
value={String(month)}
|
2016-06-06 13:54:10 +08:00
|
|
|
onChange={this.onMonthChange}
|
|
|
|
>
|
2016-04-29 12:13:27 +08:00
|
|
|
{options}
|
2015-11-12 21:24:53 +08:00
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-03-23 19:50:44 +08:00
|
|
|
onYearChange = (year) => {
|
2015-11-12 21:24:53 +08:00
|
|
|
const newValue = this.props.value.clone();
|
2016-09-09 13:55:21 +08:00
|
|
|
newValue.year(parseInt(year, 10));
|
2015-11-12 21:24:53 +08:00
|
|
|
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();
|
2016-09-09 13:55:21 +08:00
|
|
|
newValue.month(parseInt(month, 10));
|
2015-11-12 21:24:53 +08:00
|
|
|
this.props.onValueChange(newValue);
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-03-23 19:50:44 +08:00
|
|
|
onTypeChange = (e) => {
|
2015-11-12 21:24:53 +08:00
|
|
|
this.props.onTypeChange(e.target.value);
|
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2015-11-12 21:24:53 +08:00
|
|
|
render() {
|
2016-08-30 21:30:55 +08:00
|
|
|
const { type, value, prefixCls, locale, fullscreen } = this.props;
|
2016-09-09 13:55:21 +08:00
|
|
|
const yearSelect = this.getYearSelectElement(value.year());
|
|
|
|
const monthSelect = type === 'date' ?
|
|
|
|
this.getMonthSelectElement(value.month(), this.getMonthsLocale(value)) : null;
|
2016-08-31 15:00:13 +08:00
|
|
|
const size = (fullscreen ? 'default' : 'small') as any;
|
2015-11-12 21:24:53 +08:00
|
|
|
const typeSwitch = (
|
2016-08-30 21:30:55 +08:00
|
|
|
<Group onChange={this.onTypeChange} value={type} size={size}>
|
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`}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{yearSelect}
|
|
|
|
{monthSelect}
|
|
|
|
{typeSwitch}
|
2015-11-12 21:24:53 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|