ant-design/components/calendar/Header.tsx

171 lines
5.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as moment from 'moment';
2015-11-12 21:24:53 +08:00
import Select from '../select';
import { Group, Button, RadioChangeEvent } from '../radio';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2016-04-27 11:23:18 +08:00
const Option = Select.Option;
2015-11-12 21:24:53 +08:00
export interface HeaderProps {
prefixCls?: string;
locale?: any;
fullscreen?: boolean;
yearSelectOffset?: number;
yearSelectTotal?: number;
type?: string;
2017-11-22 11:05:19 +08:00
onValueChange?: (value: moment.Moment) => void;
onTypeChange?: (type: string) => void;
2016-08-24 16:09:55 +08:00
value: any;
2018-12-07 20:02:01 +08:00
validRange?: [moment.Moment, moment.Moment];
}
export default class Header extends React.Component<HeaderProps, any> {
static defaultProps = {
yearSelectOffset: 10,
yearSelectTotal: 20,
2016-07-13 11:14:24 +08:00
};
2017-11-22 11:05:19 +08:00
private calenderHeaderNode: HTMLDivElement;
getYearSelectElement(prefixCls: string, year: number) {
2018-12-07 20:02:01 +08:00
const { yearSelectOffset, yearSelectTotal, locale, fullscreen, validRange } = this.props;
let start = year - (yearSelectOffset as number);
let end = start + (yearSelectTotal as number);
if (validRange) {
start = validRange[0].get('year');
end = validRange[1].get('year') + 1;
}
2015-11-12 21:24:53 +08:00
const suffix = locale.year === '年' ? '年' : '';
2016-10-24 12:04:26 +08:00
const options: React.ReactElement<any>[] = [];
2015-11-12 21:24:53 +08:00
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-10-24 12:04:26 +08:00
size={fullscreen ? 'default' : 'small'}
2015-11-12 21:24:53 +08:00
dropdownMatchSelectWidth={false}
className={`${prefixCls}-year-select`}
onChange={this.onYearChange}
value={String(year)}
getPopupContainer={() => this.calenderHeaderNode}
>
{options}
2015-11-12 21:24:53 +08:00
</Select>
);
}
2016-10-24 12:04:26 +08:00
getMonthsLocale(value: moment.Moment) {
const current = value.clone();
const localeData = value.localeData();
2016-10-24 12:04:26 +08:00
const months: any[] = [];
for (let i = 0; i < 12; i++) {
current.month(i);
months.push(localeData.monthsShort(current));
}
return months;
}
2018-12-07 20:02:01 +08:00
getMonthSelectElement(prefixCls: string, month: number, months: number[]) {
const { fullscreen, validRange, value } = this.props;
2016-10-24 12:04:26 +08:00
const options: React.ReactElement<any>[] = [];
let start = 0;
let end = 12;
if (validRange) {
const [rangeStart, rangeEnd] = validRange;
const currentYear = value.get('year');
if (rangeEnd.get('year') === currentYear) {
end = rangeEnd.get('month') + 1;
}
if (rangeStart.get('year') === currentYear) {
start = rangeStart.get('month');
}
}
for (let index = start; index < end; index++) {
2015-11-12 21:24:53 +08:00
options.push(<Option key={`${index}`}>{months[index]}</Option>);
}
return (
<Select
2016-10-24 12:04:26 +08:00
size={fullscreen ? 'default' : 'small'}
2015-11-12 21:24:53 +08:00
dropdownMatchSelectWidth={false}
className={`${prefixCls}-month-select`}
value={String(month)}
onChange={this.onMonthChange}
getPopupContainer={() => this.calenderHeaderNode}
>
{options}
2015-11-12 21:24:53 +08:00
</Select>
);
}
2017-11-22 11:05:19 +08:00
onYearChange = (year: string) => {
const { value, validRange } = this.props;
const newValue = value.clone();
newValue.year(parseInt(year, 10));
// switch the month so that it remains within range when year changes
if (validRange) {
2018-12-07 20:02:01 +08:00
const [start, end] = validRange;
const newYear = newValue.get('year');
const newMonth = newValue.get('month');
if (newYear === end.get('year') && newMonth > end.get('month')) {
newValue.month(end.get('month'));
}
if (newYear === start.get('year') && newMonth < start.get('month')) {
newValue.month(start.get('month'));
}
}
2016-10-24 12:04:26 +08:00
const onValueChange = this.props.onValueChange;
if (onValueChange) {
onValueChange(newValue);
}
2018-12-07 20:02:01 +08:00
};
2015-11-12 21:24:53 +08:00
2017-11-22 11:05:19 +08:00
onMonthChange = (month: string) => {
2015-11-12 21:24:53 +08:00
const newValue = this.props.value.clone();
newValue.month(parseInt(month, 10));
2016-10-24 12:04:26 +08:00
const onValueChange = this.props.onValueChange;
if (onValueChange) {
onValueChange(newValue);
}
2018-12-07 20:02:01 +08:00
};
onTypeChange = (e: RadioChangeEvent) => {
2016-10-24 12:04:26 +08:00
const onTypeChange = this.props.onTypeChange;
if (onTypeChange) {
onTypeChange(e.target.value);
}
2018-12-07 20:02:01 +08:00
};
2017-11-22 11:05:19 +08:00
getCalenderHeaderNode = (node: HTMLDivElement) => {
this.calenderHeaderNode = node;
2018-12-07 20:02:01 +08:00
};
renderHeader = ({ getPrefixCls }: ConfigConsumerProps) => {
2018-12-07 20:02:01 +08:00
const { prefixCls: customizePrefixCls, type, value, locale, fullscreen } = this.props;
const prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
const yearSelect = this.getYearSelectElement(prefixCls, value.year());
2018-12-07 20:02:01 +08:00
const monthSelect =
type === 'date'
? this.getMonthSelectElement(prefixCls, 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`} ref={this.getCalenderHeaderNode}>
{yearSelect}
{monthSelect}
{typeSwitch}
2015-11-12 21:24:53 +08:00
</div>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderHeader}</ConfigConsumer>;
}
2015-11-12 21:24:53 +08:00
}