mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-28 11:08:30 +08:00
d78c0f2707
* adjust table fixed column z-index (#14036) fix #13930 * 📝 Add instruction for one column without width left in fixed table * Add full PR template link in template (#14061) * add full tmpl link * adjust * simplify it * add cn link * Fix Affix flickering when scrolling * update * fix: top border disappeared under some ie9 * fix calendar month range display (#14049) * Fix tslint warning * Correct typescript usage (#14074) * 🐛 Fix steps style under IE9 close #14001 * 🐛 tweak style for not affecting vertical steps * 📝 enhance upload documentation * Input.Group with TimePicker disappear icon: https://codepen.io/mraiguo/pen/QzvvvE?editors=0010 * 🐛 Fix disabled button style in DatePicker panel https://user-images.githubusercontent.com/507615/50693143-17505400-1071-11e9-9114-b150bef8f7f6.png * 📝 site: fix BackTop been covered by footer close #14093 * Update PULL_REQUEST_TEMPLATE.md * Update pr_cn.md * 🐛 Fix nested Timeline last item missing line (#14110) close #14108 * init Spin should also support delay trigger (#14105) fix #14100 * Update table docs * bugfix * Format * update snapshot * test: fix spin test
171 lines
5.2 KiB
TypeScript
171 lines
5.2 KiB
TypeScript
import * as React from 'react';
|
|
import * as moment from 'moment';
|
|
import Select from '../select';
|
|
import { Group, Button, RadioChangeEvent } from '../radio';
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
const Option = Select.Option;
|
|
|
|
export interface HeaderProps {
|
|
prefixCls?: string;
|
|
locale?: any;
|
|
fullscreen?: boolean;
|
|
yearSelectOffset?: number;
|
|
yearSelectTotal?: number;
|
|
type?: string;
|
|
onValueChange?: (value: moment.Moment) => void;
|
|
onTypeChange?: (type: string) => void;
|
|
value: any;
|
|
validRange?: [moment.Moment, moment.Moment];
|
|
}
|
|
|
|
export default class Header extends React.Component<HeaderProps, any> {
|
|
static defaultProps = {
|
|
yearSelectOffset: 10,
|
|
yearSelectTotal: 20,
|
|
};
|
|
|
|
private calenderHeaderNode: HTMLDivElement;
|
|
|
|
getYearSelectElement(prefixCls: string, year: number) {
|
|
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;
|
|
}
|
|
const suffix = locale.year === '年' ? '年' : '';
|
|
const options: React.ReactElement<any>[] = [];
|
|
for (let index = start; index < end; index++) {
|
|
options.push(<Option key={`${index}`}>{index + suffix}</Option>);
|
|
}
|
|
return (
|
|
<Select
|
|
size={fullscreen ? 'default' : 'small'}
|
|
dropdownMatchSelectWidth={false}
|
|
className={`${prefixCls}-year-select`}
|
|
onChange={this.onYearChange}
|
|
value={String(year)}
|
|
getPopupContainer={() => this.calenderHeaderNode}
|
|
>
|
|
{options}
|
|
</Select>
|
|
);
|
|
}
|
|
|
|
getMonthsLocale(value: moment.Moment) {
|
|
const current = value.clone();
|
|
const localeData = value.localeData();
|
|
const months: any[] = [];
|
|
for (let i = 0; i < 12; i++) {
|
|
current.month(i);
|
|
months.push(localeData.monthsShort(current));
|
|
}
|
|
return months;
|
|
}
|
|
|
|
getMonthSelectElement(prefixCls: string, month: number, months: number[]) {
|
|
const { fullscreen, validRange, value } = this.props;
|
|
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++) {
|
|
options.push(<Option key={`${index}`}>{months[index]}</Option>);
|
|
}
|
|
return (
|
|
<Select
|
|
size={fullscreen ? 'default' : 'small'}
|
|
dropdownMatchSelectWidth={false}
|
|
className={`${prefixCls}-month-select`}
|
|
value={String(month)}
|
|
onChange={this.onMonthChange}
|
|
getPopupContainer={() => this.calenderHeaderNode}
|
|
>
|
|
{options}
|
|
</Select>
|
|
);
|
|
}
|
|
|
|
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) {
|
|
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'));
|
|
}
|
|
}
|
|
|
|
const onValueChange = this.props.onValueChange;
|
|
if (onValueChange) {
|
|
onValueChange(newValue);
|
|
}
|
|
};
|
|
|
|
onMonthChange = (month: string) => {
|
|
const newValue = this.props.value.clone();
|
|
newValue.month(parseInt(month, 10));
|
|
const onValueChange = this.props.onValueChange;
|
|
if (onValueChange) {
|
|
onValueChange(newValue);
|
|
}
|
|
};
|
|
|
|
onTypeChange = (e: RadioChangeEvent) => {
|
|
const onTypeChange = this.props.onTypeChange;
|
|
if (onTypeChange) {
|
|
onTypeChange(e.target.value);
|
|
}
|
|
};
|
|
|
|
getCalenderHeaderNode = (node: HTMLDivElement) => {
|
|
this.calenderHeaderNode = node;
|
|
};
|
|
|
|
renderHeader = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
const { prefixCls: customizePrefixCls, type, value, locale, fullscreen } = this.props;
|
|
const prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
|
|
const yearSelect = this.getYearSelectElement(prefixCls, value.year());
|
|
const monthSelect =
|
|
type === 'date'
|
|
? this.getMonthSelectElement(prefixCls, value.month(), this.getMonthsLocale(value))
|
|
: null;
|
|
const size = (fullscreen ? 'default' : 'small') as any;
|
|
const typeSwitch = (
|
|
<Group onChange={this.onTypeChange} value={type} size={size}>
|
|
<Button value="date">{locale.month}</Button>
|
|
<Button value="month">{locale.year}</Button>
|
|
</Group>
|
|
);
|
|
|
|
return (
|
|
<div className={`${prefixCls}-header`} ref={this.getCalenderHeaderNode}>
|
|
{yearSelect}
|
|
{monthSelect}
|
|
{typeSwitch}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
render() {
|
|
return <ConfigConsumer>{this.renderHeader}</ConfigConsumer>;
|
|
}
|
|
}
|