|
|
|
import * as React from 'react';
|
|
|
|
import { GenerateConfig } from 'rc-picker/lib/generate';
|
|
|
|
import { Locale } from 'rc-picker/lib/interface';
|
|
|
|
import { useContext, useMemo } from 'react';
|
|
|
|
import { FormItemInputContext } from '../form/context';
|
|
|
|
import Select from '../select';
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function YearSelect<DateType>(props: SharedProps<DateType>) {
|
|
|
|
const { fullscreen, validRange, generateConfig, locale, prefixCls, value, onChange, divRef } =
|
|
|
|
props;
|
|
|
|
|
|
|
|
const year = generateConfig.getYear(value || generateConfig.getNow());
|
|
|
|
|
|
|
|
let start = year - YearSelectOffset;
|
|
|
|
let end = start + YearSelectTotal;
|
|
|
|
|
|
|
|
if (validRange) {
|
|
|
|
start = generateConfig.getYear(validRange[0]);
|
|
|
|
end = generateConfig.getYear(validRange[1]) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
size={fullscreen ? undefined : 'small'}
|
|
|
|
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!}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function MonthSelect<DateType>(props: SharedProps<DateType>) {
|
|