import * as React from 'react'; import { useContext, useMemo } from 'react'; import type { GenerateConfig } from 'rc-picker/lib/generate'; import type { Locale } from 'rc-picker/lib/interface'; import { FormItemInputContext } from '../form/context'; import { Button, Group } from '../radio'; import Select from '../select'; import type { CalendarMode, SelectInfo } from './generateCalendar'; const YearSelectOffset = 10; const YearSelectTotal = 20; interface SharedProps { prefixCls: string; value: DateType; validRange?: [DateType, DateType]; generateConfig: GenerateConfig; locale: Locale; fullscreen: boolean; divRef: React.RefObject; onChange: (year: DateType) => void; } function YearSelect(props: SharedProps) { 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 ( { onChange(generateConfig.setMonth(value, newMonth)); }} getPopupContainer={() => divRef!.current!} /> ); } interface ModeSwitchProps extends Omit, 'onChange'> { mode: CalendarMode; onModeChange: (type: CalendarMode) => void; } function ModeSwitch(props: ModeSwitchProps) { const { prefixCls, locale, mode, fullscreen, onModeChange } = props; return ( { onModeChange(value); }} value={mode} size={fullscreen ? undefined : 'small'} className={`${prefixCls}-mode-switch`} > ); } export interface CalendarHeaderProps { prefixCls: string; value: DateType; validRange?: [DateType, DateType]; generateConfig: GenerateConfig; locale: Locale; mode: CalendarMode; fullscreen: boolean; onChange: (date: DateType, source: SelectInfo['source']) => void; onModeChange: (mode: CalendarMode) => void; } function CalendarHeader(props: CalendarHeaderProps) { const { prefixCls, fullscreen, mode, onChange, onModeChange } = props; const divRef = React.useRef(null); const formItemInputContext = useContext(FormItemInputContext); const mergedFormItemInputContext = useMemo( () => ({ ...formItemInputContext, isFormItemInput: false, }), [formItemInputContext], ); const sharedProps = { ...props, fullscreen, divRef, }; return (
{ onChange(v, 'year'); }} /> {mode === 'month' && ( { onChange(v, 'month'); }} /> )}
); } export default CalendarHeader;