2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as moment from 'moment';
|
2015-11-12 21:24:53 +08:00
|
|
|
import Select from '../select';
|
2018-03-08 20:34:40 +08:00
|
|
|
import { Group, Button, RadioChangeEvent } from '../radio';
|
2018-12-05 19:12:18 +08:00
|
|
|
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
|
|
|
|
2019-05-26 13:09:06 +08:00
|
|
|
export interface RenderHeader {
|
|
|
|
value: moment.Moment;
|
|
|
|
onChange?: (value: moment.Moment) => void;
|
|
|
|
type: string;
|
|
|
|
onTypeChange: (type: string) => void;
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:11:06 +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;
|
2016-08-19 17:11:06 +08:00
|
|
|
onTypeChange?: (type: string) => void;
|
2019-05-26 13:09:06 +08:00
|
|
|
value: moment.Moment;
|
2018-12-07 20:02:01 +08:00
|
|
|
validRange?: [moment.Moment, moment.Moment];
|
2019-05-26 13:09:06 +08:00
|
|
|
headerRender: (header: RenderHeader) => React.ReactNode;
|
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 = {
|
|
|
|
yearSelectOffset: 10,
|
|
|
|
yearSelectTotal: 20,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-22 11:05:19 +08:00
|
|
|
private calenderHeaderNode: HTMLDivElement;
|
2017-06-19 10:47:22 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
getYearSelectElement(prefixCls: string, year: number) {
|
2018-12-07 20:02:01 +08:00
|
|
|
const { yearSelectOffset, yearSelectTotal, locale, fullscreen, validRange } = this.props;
|
2018-02-03 18:54:04 +08:00
|
|
|
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`}
|
2016-03-23 19:50:44 +08:00
|
|
|
onChange={this.onYearChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
value={String(year)}
|
2017-06-19 10:47:22 +08:00
|
|
|
getPopupContainer={() => this.calenderHeaderNode}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
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-10-24 12:04:26 +08:00
|
|
|
getMonthsLocale(value: moment.Moment) {
|
2016-09-09 13:55:21 +08:00
|
|
|
const current = value.clone();
|
|
|
|
const localeData = value.localeData();
|
2016-10-24 12:04:26 +08:00
|
|
|
const months: any[] = [];
|
2016-09-09 13:55:21 +08:00
|
|
|
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[]) {
|
2018-12-05 19:12:18 +08:00
|
|
|
const { fullscreen, validRange, value } = this.props;
|
2016-10-24 12:04:26 +08:00
|
|
|
const options: React.ReactElement<any>[] = [];
|
2018-02-03 18:54:04 +08:00
|
|
|
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;
|
2019-01-06 13:25:59 +08:00
|
|
|
}
|
|
|
|
if (rangeStart.get('year') === currentYear) {
|
2018-02-03 18:54:04 +08:00
|
|
|
start = rangeStart.get('month');
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2018-02-03 18:54:04 +08:00
|
|
|
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)}
|
2016-06-06 13:54:10 +08:00
|
|
|
onChange={this.onMonthChange}
|
2017-06-19 10:47:22 +08:00
|
|
|
getPopupContainer={() => this.calenderHeaderNode}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
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
|
|
|
|
2017-11-22 11:05:19 +08:00
|
|
|
onYearChange = (year: string) => {
|
2018-02-03 18:54:04 +08:00
|
|
|
const { value, validRange } = this.props;
|
|
|
|
const newValue = value.clone();
|
2016-09-09 13:55:21 +08:00
|
|
|
newValue.year(parseInt(year, 10));
|
2018-02-03 18:54:04 +08:00
|
|
|
// 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;
|
2018-02-03 18:54:04 +08:00
|
|
|
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();
|
2016-09-09 13:55:21 +08:00
|
|
|
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
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2019-05-26 13:09:06 +08:00
|
|
|
onInternalTypeChange = (e: RadioChangeEvent) => {
|
|
|
|
this.onTypeChange(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
onTypeChange = (type: string) => {
|
2016-10-24 12:04:26 +08:00
|
|
|
const onTypeChange = this.props.onTypeChange;
|
|
|
|
if (onTypeChange) {
|
2019-05-26 13:09:06 +08:00
|
|
|
onTypeChange(type);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-11-22 11:05:19 +08:00
|
|
|
getCalenderHeaderNode = (node: HTMLDivElement) => {
|
2017-06-19 10:47:22 +08:00
|
|
|
this.calenderHeaderNode = node;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-06-19 10:47:22 +08:00
|
|
|
|
2019-05-26 13:09:06 +08:00
|
|
|
getMonthYearSelections = (getPrefixCls: ConfigConsumerProps['getPrefixCls']) => {
|
|
|
|
const { prefixCls: customizePrefixCls, type, value } = this.props;
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
|
2019-05-26 13:09:06 +08:00
|
|
|
const yearReactNode = this.getYearSelectElement(prefixCls, value.year());
|
|
|
|
const monthReactNode =
|
2019-03-12 13:35:39 +08:00
|
|
|
type === 'month'
|
2018-12-07 20:02:01 +08:00
|
|
|
? this.getMonthSelectElement(prefixCls, value.month(), this.getMonthsLocale(value))
|
|
|
|
: null;
|
2019-05-26 13:09:06 +08:00
|
|
|
return {
|
|
|
|
yearReactNode,
|
|
|
|
monthReactNode,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
getTypeSwitch = () => {
|
|
|
|
const { locale, type, fullscreen } = this.props;
|
2019-03-09 18:25:09 +08:00
|
|
|
const size = fullscreen ? 'default' : 'small';
|
2019-05-26 13:09:06 +08:00
|
|
|
return (
|
|
|
|
<Group onChange={this.onInternalTypeChange} value={type} size={size}>
|
2019-03-12 13:35:39 +08:00
|
|
|
<Button value="month">{locale.month}</Button>
|
|
|
|
<Button value="year">{locale.year}</Button>
|
2015-11-12 21:24:53 +08:00
|
|
|
</Group>
|
|
|
|
);
|
2019-05-26 13:09:06 +08:00
|
|
|
};
|
2015-11-12 21:24:53 +08:00
|
|
|
|
2019-05-26 13:09:06 +08:00
|
|
|
headerRenderCustom = (): React.ReactNode => {
|
|
|
|
const { headerRender, type, onValueChange, value } = this.props;
|
|
|
|
|
|
|
|
return headerRender({
|
|
|
|
value,
|
|
|
|
type: type || 'month',
|
|
|
|
onChange: onValueChange,
|
|
|
|
onTypeChange: this.onTypeChange,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
renderHeader = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls, headerRender } = this.props;
|
|
|
|
const typeSwitch = this.getTypeSwitch();
|
|
|
|
const { yearReactNode, monthReactNode } = this.getMonthYearSelections(getPrefixCls);
|
|
|
|
return headerRender ? (
|
|
|
|
this.headerRenderCustom()
|
|
|
|
) : (
|
2017-06-19 10:47:22 +08:00
|
|
|
<div className={`${prefixCls}-header`} ref={this.getCalenderHeaderNode}>
|
2019-05-26 13:09:06 +08:00
|
|
|
{yearReactNode}
|
|
|
|
{monthReactNode}
|
2016-04-29 12:13:27 +08:00
|
|
|
{typeSwitch}
|
2015-11-12 21:24:53 +08:00
|
|
|
</div>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderHeader}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2015-11-12 21:24:53 +08:00
|
|
|
}
|