You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.6 KiB

import React from 'react';
9 years ago
import { PropTypes } from 'react';
import { PREFIX_CLS } from './Constants';
9 years ago
import Select from '../select';
import { Group, Button } from '../radio';
const Option = Select.Option;
9 years ago
function noop() {}
export interface HeaderProps {
prefixCls?: string;
locale?: any;
fullscreen?: boolean;
yearSelectOffset?: number;
yearSelectTotal?: number;
type?: string;
onValueChange?: (value) => void;
onTypeChange?: (type: string) => void;
value: any;
}
export default class Header extends React.Component<HeaderProps, any> {
static defaultProps = {
prefixCls: `${PREFIX_CLS}-header`,
yearSelectOffset: 10,
yearSelectTotal: 20,
onValueChange: noop,
onTypeChange: noop,
};
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,
8 years ago
fullscreen: PropTypes.bool,
};
9 years ago
getYearSelectElement(year) {
const { yearSelectOffset, yearSelectTotal, locale, prefixCls, fullscreen } = this.props;
9 years ago
const start = year - yearSelectOffset;
const end = start + yearSelectTotal;
const suffix = locale.year === '年' ? '年' : '';
const options = [];
for (let index = start; index < end; index++) {
options.push(<Option key={`${index}`}>{index + suffix}</Option>);
9 years ago
}
return (
<Select
size={fullscreen ? null : 'small'}
9 years ago
dropdownMatchSelectWidth={false}
className={`${prefixCls}-year-select`}
onChange={this.onYearChange}
value={String(year)}
>
{options}
9 years ago
</Select>
);
}
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) {
9 years ago
const props = this.props;
const { prefixCls, fullscreen } = props;
9 years ago
const options = [];
for (let index = 0; index < 12; index++) {
options.push(<Option key={`${index}`}>{months[index]}</Option>);
}
return (
<Select
size={fullscreen ? null : 'small'}
9 years ago
dropdownMatchSelectWidth={false}
className={`${prefixCls}-month-select`}
value={String(month)}
onChange={this.onMonthChange}
>
{options}
9 years ago
</Select>
);
}
onYearChange = (year) => {
9 years ago
const newValue = this.props.value.clone();
newValue.year(parseInt(year, 10));
9 years ago
this.props.onValueChange(newValue);
}
onMonthChange = (month) => {
9 years ago
const newValue = this.props.value.clone();
newValue.month(parseInt(month, 10));
9 years ago
this.props.onValueChange(newValue);
}
onTypeChange = (e) => {
9 years ago
this.props.onTypeChange(e.target.value);
}
9 years ago
render() {
const { type, value, prefixCls, locale, fullscreen } = this.props;
const yearSelect = this.getYearSelectElement(value.year());
const monthSelect = type === 'date' ?
this.getMonthSelectElement(value.month(), this.getMonthsLocale(value)) : null;
const size = (fullscreen ? 'default' : 'small') as any;
9 years ago
const typeSwitch = (
<Group onChange={this.onTypeChange} value={type} size={size}>
9 years ago
<Button value="date">{locale.month}</Button>
<Button value="month">{locale.year}</Button>
</Group>
);
return (
<div className={`${prefixCls}-header`}>
{yearSelect}
{monthSelect}
{typeSwitch}
9 years ago
</div>
);
}
}