mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 07:09:55 +08:00
407a41a142
* init generate * basic style * basic panel style * update mode panel style * update style * generate More picker * default clear icon * chore: Update separator type * feat: Add ranged start & end className * update range style * Add transition effect * support size config * adjust range style * chore: Auto fill time by showTime * auto set time by format * update disabled style * update seperator style * ranges style * support extra footer style * remove useless test case part is not usable anymore part is already tested in rc-picker * init calendar * all demos * fix calendar basic test * fix time-picker test case * update snapshot * fix tooltip test case & lint * fix locale & style lint * fix compile * fix style * fix style lint * fix calendar style * update rc-picker version * adjust style * move picker placeholder into locale file * update snapshot * add hover style * update picker version * fix icon position & style * update picker version * update deps for pading * fix: align of suffix * feat: Year & Month support range effect * adjust range style to support up-down placement * update rc-picker * update range picker style * adjust extra footer line style * update snapshot * fix: Locale error * fix: style lint * fix: add missing button style deps * update test case * fix firefox additional white line style issue * rollback demo * fix ff additional blue color * docs: Remove placeholder in demo * rangepicker ranges is tag now * connect start / end background color with picker range * update deps * update deps for fixing blur text issue * hide start-end demo * range hover style update * hover range with ranged value * black magic of inner hover style * hover style of range adjust * fix css select miss hit on DatePicker * remove one eslint rule * fade range hovered color * week should alway not show the cell selection * update style of selection * update snapshot * fix style * add margin back * update rc-picker deps * update date & time picker & form style * fix disabled demo & update form style * update docs about allowEmpty * hide arrow in time range picker * add hover & focused style * fix lint * fix style & update snapshot * raise disabled selector proirity * fix disabled today border color * extra footer provides an bottom line * time picker hover support transition background * add padding style * fix Firefox not correct calculate inline-flex * fix style * fix week picker missing today border color * rm useless padding * Force padding to 0 * test coverage * dedup eslint rule * adjust logic to imporve coverage * fix render cell logic
189 lines
5.0 KiB
TypeScript
189 lines
5.0 KiB
TypeScript
import * as React from 'react';
|
|
import { GenerateConfig } from 'rc-picker/lib/generate';
|
|
import { Locale } from 'rc-picker/lib/interface';
|
|
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);
|
|
|
|
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 ? 'default' : '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>) {
|
|
const {
|
|
prefixCls,
|
|
fullscreen,
|
|
validRange,
|
|
value,
|
|
generateConfig,
|
|
locale,
|
|
onChange,
|
|
divRef,
|
|
} = props;
|
|
const month = generateConfig.getMonth(value);
|
|
|
|
let start = 0;
|
|
let end = 12;
|
|
|
|
if (validRange) {
|
|
const [rangeStart, rangeEnd] = validRange;
|
|
const currentYear = generateConfig.getYear(value);
|
|
if (generateConfig.getYear(rangeEnd) === currentYear) {
|
|
end = generateConfig.getMonth(rangeEnd);
|
|
}
|
|
if (generateConfig.getYear(rangeStart) === currentYear) {
|
|
start = generateConfig.getMonth(rangeStart);
|
|
}
|
|
}
|
|
|
|
const months = locale.shortMonths || generateConfig.locale.getShortMonths!(locale.locale);
|
|
const options: { label: string; value: number }[] = [];
|
|
for (let index = start; index < end; index += 1) {
|
|
options.push({
|
|
label: months[index],
|
|
value: index,
|
|
});
|
|
}
|
|
|
|
return (
|
|
<Select
|
|
size={fullscreen ? 'default' : 'small'}
|
|
dropdownMatchSelectWidth={100}
|
|
className={`${prefixCls}-month-select`}
|
|
value={month}
|
|
options={options}
|
|
onChange={newMonth => {
|
|
onChange(generateConfig.setMonth(value, newMonth));
|
|
}}
|
|
getPopupContainer={() => divRef!.current!}
|
|
/>
|
|
);
|
|
}
|
|
|
|
interface ModeSwitchProps<DateType> extends Omit<SharedProps<DateType>, 'onChange'> {
|
|
mode: CalendarMode;
|
|
onModeChange: (type: CalendarMode) => void;
|
|
}
|
|
|
|
function ModeSwitch<DateType>(props: ModeSwitchProps<DateType>) {
|
|
const { prefixCls, locale, mode, fullscreen, onModeChange } = props;
|
|
return (
|
|
<Group
|
|
onChange={({ target: { value } }) => {
|
|
onModeChange(value);
|
|
}}
|
|
value={mode}
|
|
size={fullscreen ? 'default' : 'small'}
|
|
className={`${prefixCls}-mode-switch`}
|
|
>
|
|
<Button value="month">{locale.month}</Button>
|
|
<Button value="year">{locale.year}</Button>
|
|
</Group>
|
|
);
|
|
}
|
|
|
|
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,
|
|
};
|
|
|
|
return (
|
|
<div className={`${prefixCls}-header`} ref={divRef}>
|
|
<YearSelect {...sharedProps} />
|
|
{mode === 'month' && <MonthSelect {...sharedProps} />}
|
|
<ModeSwitch {...sharedProps} onModeChange={onModeChange} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default CalendarHeader;
|