2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useContext, useMemo } from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { GenerateConfig } from 'rc-picker/lib/generate';
|
|
|
|
import type { Locale } from 'rc-picker/lib/interface';
|
|
|
|
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { Button, Group } from '../radio';
|
2015-11-12 21:24:53 +08:00
|
|
|
import Select from '../select';
|
2023-05-17 21:18:51 +08:00
|
|
|
import type { CalendarMode, SelectInfo } from './generateCalendar';
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
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>) {
|
2021-12-21 21:38:11 +08:00
|
|
|
const { fullscreen, validRange, generateConfig, locale, prefixCls, value, onChange, divRef } =
|
|
|
|
props;
|
2019-12-11 23:32:19 +08:00
|
|
|
|
2021-05-08 16:59:03 +08:00
|
|
|
const year = generateConfig.getYear(value || generateConfig.getNow());
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
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`}
|
2022-11-19 13:47:33 +08:00
|
|
|
onChange={(numYear) => {
|
2019-12-11 23:32:19 +08:00
|
|
|
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>) {
|
2021-12-21 21:38:11 +08:00
|
|
|
const { prefixCls, fullscreen, validRange, value, generateConfig, locale, onChange, divRef } =
|
|
|
|
props;
|
2021-05-08 16:59:03 +08:00
|
|
|
const month = generateConfig.getMonth(value || generateConfig.getNow());
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
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}
|
2022-11-19 13:47:33 +08:00
|
|
|
onChange={(newMonth) => {
|
2019-12-11 23:32:19 +08:00
|
|
|
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;
|
2023-05-17 21:18:51 +08:00
|
|
|
onChange: (date: DateType, source: SelectInfo['source']) => void;
|
2019-12-11 23:32:19 +08:00
|
|
|
onModeChange: (mode: CalendarMode) => void;
|
|
|
|
}
|
|
|
|
function CalendarHeader<DateType>(props: CalendarHeaderProps<DateType>) {
|
|
|
|
const { prefixCls, fullscreen, mode, onChange, onModeChange } = props;
|
|
|
|
const divRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
2022-03-24 21:54:20 +08:00
|
|
|
const formItemInputContext = useContext(FormItemInputContext);
|
|
|
|
const mergedFormItemInputContext = useMemo(
|
|
|
|
() => ({
|
|
|
|
...formItemInputContext,
|
|
|
|
isFormItemInput: false,
|
|
|
|
}),
|
|
|
|
[formItemInputContext],
|
|
|
|
);
|
|
|
|
|
2019-12-11 23:32:19 +08:00
|
|
|
const sharedProps = {
|
|
|
|
...props,
|
|
|
|
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}>
|
2022-03-24 21:54:20 +08:00
|
|
|
<FormItemInputContext.Provider value={mergedFormItemInputContext}>
|
2023-05-17 21:18:51 +08:00
|
|
|
<YearSelect
|
|
|
|
{...sharedProps}
|
|
|
|
onChange={(v) => {
|
|
|
|
onChange(v, 'year');
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{mode === 'month' && (
|
|
|
|
<MonthSelect
|
|
|
|
{...sharedProps}
|
|
|
|
onChange={(v) => {
|
|
|
|
onChange(v, 'month');
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-24 21:54:20 +08:00
|
|
|
</FormItemInputContext.Provider>
|
2019-12-11 23:32:19 +08:00
|
|
|
<ModeSwitch {...sharedProps} onModeChange={onModeChange} />
|
|
|
|
</div>
|
|
|
|
);
|
2015-11-12 21:24:53 +08:00
|
|
|
}
|
2019-12-11 23:32:19 +08:00
|
|
|
|
|
|
|
export default CalendarHeader;
|