2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2019-12-11 23:32:19 +08:00
|
|
|
import { GenerateConfig } from 'rc-picker/lib/generate';
|
|
|
|
import { Locale } from 'rc-picker/lib/interface';
|
2015-11-12 21:24:53 +08:00
|
|
|
import Select from '../select';
|
2019-12-11 23:32:19 +08:00
|
|
|
import { Group, Button } from '../radio';
|
|
|
|
import { CalendarMode } from './generateCalendar';
|
|
|
|
|
|
|
|
const YearSelectOffset = 10;
|
|
|
|
const YearSelectTotal = 20;
|
|
|
|
|
|
|
|
interface SharedProps<DateType> {
|
|
|
|
prefixCls: string;
|
|
|
|
value: DateType;
|
|
|
|
validRange?: [DateType, DateType];
|
|
|
|
generateConfig: GenerateConfig<DateType>;
|
|
|
|
locale: Locale;
|
|
|
|
fullscreen: boolean;
|
|
|
|
divRef: React.RefObject<HTMLDivElement>;
|
|
|
|
onChange: (year: DateType) => void;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
function YearSelect<DateType>(props: SharedProps<DateType>) {
|
|
|
|
const {
|
|
|
|
fullscreen,
|
|
|
|
validRange,
|
|
|
|
generateConfig,
|
|
|
|
locale,
|
|
|
|
prefixCls,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
divRef,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const year = generateConfig.getYear(value);
|
|
|
|
|
|
|
|
let start = year - YearSelectOffset;
|
|
|
|
let end = start + YearSelectTotal;
|
|
|
|
|
|
|
|
if (validRange) {
|
|
|
|
start = generateConfig.getYear(validRange[0]);
|
|
|
|
end = generateConfig.getYear(validRange[1]) + 1;
|
2015-11-12 21:24:53 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
const suffix = locale && locale.year === '年' ? '年' : '';
|
|
|
|
const options: { label: string; value: number }[] = [];
|
|
|
|
for (let index = start; index < end; index++) {
|
|
|
|
options.push({ label: `${index}${suffix}`, value: index });
|
2015-11-12 21:24:53 +08:00
|
|
|
}
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
return (
|
|
|
|
<Select
|
2020-01-03 13:38:16 +08:00
|
|
|
size={fullscreen ? undefined : 'small'}
|
2019-12-11 23:32:19 +08:00
|
|
|
options={options}
|
|
|
|
value={year}
|
|
|
|
className={`${prefixCls}-year-select`}
|
|
|
|
onChange={numYear => {
|
|
|
|
let newDate = generateConfig.setYear(value, numYear);
|
|
|
|
|
|
|
|
if (validRange) {
|
|
|
|
const [startDate, endDate] = validRange;
|
|
|
|
const newYear = generateConfig.getYear(newDate);
|
|
|
|
const newMonth = generateConfig.getMonth(newDate);
|
|
|
|
if (
|
|
|
|
newYear === generateConfig.getYear(endDate) &&
|
|
|
|
newMonth > generateConfig.getMonth(endDate)
|
|
|
|
) {
|
|
|
|
newDate = generateConfig.setMonth(newDate, generateConfig.getMonth(endDate));
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
newYear === generateConfig.getYear(startDate) &&
|
|
|
|
newMonth < generateConfig.getMonth(startDate)
|
|
|
|
) {
|
|
|
|
newDate = generateConfig.setMonth(newDate, generateConfig.getMonth(startDate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange(newDate);
|
|
|
|
}}
|
|
|
|
getPopupContainer={() => divRef!.current!}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2015-11-12 21:24:53 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
function MonthSelect<DateType>(props: SharedProps<DateType>) {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
fullscreen,
|
|
|
|
validRange,
|
|
|
|
value,
|
|
|
|
generateConfig,
|
|
|
|
locale,
|
|
|
|
onChange,
|
|
|
|
divRef,
|
|
|
|
} = props;
|
|
|
|
const month = generateConfig.getMonth(value);
|
|
|
|
|
|
|
|
let start = 0;
|
2020-07-21 16:29:27 +08:00
|
|
|
let end = 11;
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
if (validRange) {
|
|
|
|
const [rangeStart, rangeEnd] = validRange;
|
|
|
|
const currentYear = generateConfig.getYear(value);
|
|
|
|
if (generateConfig.getYear(rangeEnd) === currentYear) {
|
|
|
|
end = generateConfig.getMonth(rangeEnd);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2019-12-11 23:32:19 +08:00
|
|
|
if (generateConfig.getYear(rangeStart) === currentYear) {
|
|
|
|
start = generateConfig.getMonth(rangeStart);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2019-12-11 23:32:19 +08:00
|
|
|
}
|
2017-06-19 10:47:22 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
const months = locale.shortMonths || generateConfig.locale.getShortMonths!(locale.locale);
|
|
|
|
const options: { label: string; value: number }[] = [];
|
2020-07-21 16:29:27 +08:00
|
|
|
for (let index = start; index <= end; index += 1) {
|
2019-12-11 23:32:19 +08:00
|
|
|
options.push({
|
|
|
|
label: months[index],
|
|
|
|
value: index,
|
|
|
|
});
|
|
|
|
}
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
return (
|
|
|
|
<Select
|
2020-01-03 13:38:16 +08:00
|
|
|
size={fullscreen ? undefined : 'small'}
|
2019-12-11 23:32:19 +08:00
|
|
|
className={`${prefixCls}-month-select`}
|
|
|
|
value={month}
|
|
|
|
options={options}
|
|
|
|
onChange={newMonth => {
|
|
|
|
onChange(generateConfig.setMonth(value, newMonth));
|
|
|
|
}}
|
|
|
|
getPopupContainer={() => divRef!.current!}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2015-11-12 21:24:53 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
interface ModeSwitchProps<DateType> extends Omit<SharedProps<DateType>, 'onChange'> {
|
|
|
|
mode: CalendarMode;
|
|
|
|
onModeChange: (type: CalendarMode) => void;
|
|
|
|
}
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
function ModeSwitch<DateType>(props: ModeSwitchProps<DateType>) {
|
|
|
|
const { prefixCls, locale, mode, fullscreen, onModeChange } = props;
|
|
|
|
return (
|
|
|
|
<Group
|
|
|
|
onChange={({ target: { value } }) => {
|
|
|
|
onModeChange(value);
|
|
|
|
}}
|
|
|
|
value={mode}
|
2020-01-03 13:38:16 +08:00
|
|
|
size={fullscreen ? undefined : 'small'}
|
2019-12-11 23:32:19 +08:00
|
|
|
className={`${prefixCls}-mode-switch`}
|
|
|
|
>
|
|
|
|
<Button value="month">{locale.month}</Button>
|
|
|
|
<Button value="year">{locale.year}</Button>
|
|
|
|
</Group>
|
|
|
|
);
|
|
|
|
}
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
export interface CalendarHeaderProps<DateType> {
|
|
|
|
prefixCls: string;
|
|
|
|
value: DateType;
|
|
|
|
validRange?: [DateType, DateType];
|
|
|
|
generateConfig: GenerateConfig<DateType>;
|
|
|
|
locale: Locale;
|
|
|
|
mode: CalendarMode;
|
|
|
|
fullscreen: boolean;
|
|
|
|
onChange: (date: DateType) => void;
|
|
|
|
onModeChange: (mode: CalendarMode) => void;
|
|
|
|
}
|
|
|
|
function CalendarHeader<DateType>(props: CalendarHeaderProps<DateType>) {
|
|
|
|
const { prefixCls, fullscreen, mode, onChange, onModeChange } = props;
|
|
|
|
const divRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const sharedProps = {
|
|
|
|
...props,
|
|
|
|
onChange,
|
|
|
|
fullscreen,
|
|
|
|
divRef,
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
return (
|
|
|
|
<div className={`${prefixCls}-header`} ref={divRef}>
|
|
|
|
<YearSelect {...sharedProps} />
|
|
|
|
{mode === 'month' && <MonthSelect {...sharedProps} />}
|
|
|
|
<ModeSwitch {...sharedProps} onModeChange={onModeChange} />
|
|
|
|
</div>
|
|
|
|
);
|
2015-11-12 21:24:53 +08:00
|
|
|
}
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
export default CalendarHeader;
|