2020-04-02 15:46:07 +08:00
|
|
|
import { GenerateConfig } from 'rc-picker/lib/generate/index';
|
|
|
|
import {
|
|
|
|
PickerBaseProps as RCPickerBaseProps,
|
|
|
|
PickerDateProps as RCPickerDateProps,
|
|
|
|
PickerTimeProps as RCPickerTimeProps,
|
|
|
|
} from 'rc-picker/lib/Picker';
|
|
|
|
import { SharedTimeProps } from 'rc-picker/lib/panels/TimePanel';
|
|
|
|
import {
|
|
|
|
RangePickerBaseProps as RCRangePickerBaseProps,
|
|
|
|
RangePickerDateProps as RCRangePickerDateProps,
|
|
|
|
RangePickerTimeProps as RCRangePickerTimeProps,
|
|
|
|
} from 'rc-picker/lib/RangePicker';
|
|
|
|
import { PickerMode, Locale as RcPickerLocale } from 'rc-picker/lib/interface';
|
|
|
|
import { SizeType } from '../../config-provider/SizeContext';
|
|
|
|
import PickerButton from '../PickerButton';
|
|
|
|
import PickerTag from '../PickerTag';
|
|
|
|
import { TimePickerLocale } from '../../time-picker';
|
|
|
|
import generateSinglePicker from './generateSinglePicker';
|
|
|
|
import generateRangePicker from './generateRangePicker';
|
2022-01-20 16:54:47 +08:00
|
|
|
import { tuple } from '../../_util/type';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
export const Components = { button: PickerButton, rangeItem: PickerTag };
|
|
|
|
|
|
|
|
function toArray<T>(list: T | T[]): T[] {
|
|
|
|
if (!list) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return Array.isArray(list) ? list : [list];
|
|
|
|
}
|
|
|
|
|
2021-12-31 11:28:02 +08:00
|
|
|
export function getTimeProps<DateType, DisabledTime>(
|
|
|
|
props: { format?: string; picker?: PickerMode } & Omit<
|
|
|
|
SharedTimeProps<DateType>,
|
|
|
|
'disabledTime'
|
|
|
|
> & {
|
|
|
|
disabledTime?: DisabledTime;
|
|
|
|
},
|
2020-04-02 15:46:07 +08:00
|
|
|
) {
|
|
|
|
const { format, picker, showHour, showMinute, showSecond, use12Hours } = props;
|
|
|
|
|
|
|
|
const firstFormat = toArray(format)[0];
|
2021-12-31 11:28:02 +08:00
|
|
|
const showTimeObj = { ...props };
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2020-09-22 15:43:11 +08:00
|
|
|
if (firstFormat && typeof firstFormat === 'string') {
|
2020-04-02 15:46:07 +08:00
|
|
|
if (!firstFormat.includes('s') && showSecond === undefined) {
|
|
|
|
showTimeObj.showSecond = false;
|
|
|
|
}
|
|
|
|
if (!firstFormat.includes('m') && showMinute === undefined) {
|
|
|
|
showTimeObj.showMinute = false;
|
|
|
|
}
|
|
|
|
if (!firstFormat.includes('H') && !firstFormat.includes('h') && showHour === undefined) {
|
|
|
|
showTimeObj.showHour = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((firstFormat.includes('a') || firstFormat.includes('A')) && use12Hours === undefined) {
|
|
|
|
showTimeObj.use12Hours = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (picker === 'time') {
|
|
|
|
return showTimeObj;
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:00:18 +08:00
|
|
|
if (typeof firstFormat === 'function') {
|
|
|
|
// format of showTime should use default when format is custom format function
|
|
|
|
delete showTimeObj.format;
|
|
|
|
}
|
|
|
|
|
2020-04-02 15:46:07 +08:00
|
|
|
return {
|
|
|
|
showTime: showTimeObj,
|
|
|
|
};
|
|
|
|
}
|
2022-01-20 16:54:47 +08:00
|
|
|
const DataPickerPlacements = tuple('bottomLeft', 'bottomRight', 'topLeft', 'topRight');
|
|
|
|
type DataPickerPlacement = typeof DataPickerPlacements[number];
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
type InjectDefaultProps<Props> = Omit<
|
|
|
|
Props,
|
2021-12-31 11:28:02 +08:00
|
|
|
'locale' | 'generateConfig' | 'hideHeader' | 'components'
|
2020-04-02 15:46:07 +08:00
|
|
|
> & {
|
|
|
|
locale?: PickerLocale;
|
|
|
|
size?: SizeType;
|
2022-01-20 16:54:47 +08:00
|
|
|
placement?: DataPickerPlacement;
|
2020-04-02 15:46:07 +08:00
|
|
|
bordered?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type PickerLocale = {
|
|
|
|
lang: RcPickerLocale & AdditionalPickerLocaleLangProps;
|
|
|
|
timePickerLocale: TimePickerLocale;
|
|
|
|
} & AdditionalPickerLocaleProps;
|
|
|
|
|
|
|
|
export type AdditionalPickerLocaleProps = {
|
|
|
|
dateFormat?: string;
|
|
|
|
dateTimeFormat?: string;
|
|
|
|
weekFormat?: string;
|
|
|
|
monthFormat?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type AdditionalPickerLocaleLangProps = {
|
|
|
|
placeholder: string;
|
|
|
|
yearPlaceholder?: string;
|
|
|
|
quarterPlaceholder?: string;
|
|
|
|
monthPlaceholder?: string;
|
|
|
|
weekPlaceholder?: string;
|
|
|
|
rangeYearPlaceholder?: [string, string];
|
|
|
|
rangeMonthPlaceholder?: [string, string];
|
|
|
|
rangeWeekPlaceholder?: [string, string];
|
|
|
|
rangePlaceholder?: [string, string];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Picker Props
|
|
|
|
export type PickerBaseProps<DateType> = InjectDefaultProps<RCPickerBaseProps<DateType>>;
|
|
|
|
export type PickerDateProps<DateType> = InjectDefaultProps<RCPickerDateProps<DateType>>;
|
|
|
|
export type PickerTimeProps<DateType> = InjectDefaultProps<RCPickerTimeProps<DateType>>;
|
|
|
|
|
|
|
|
export type PickerProps<DateType> =
|
|
|
|
| PickerBaseProps<DateType>
|
|
|
|
| PickerDateProps<DateType>
|
|
|
|
| PickerTimeProps<DateType>;
|
|
|
|
|
|
|
|
// Range Picker Props
|
|
|
|
export type RangePickerBaseProps<DateType> = InjectDefaultProps<RCRangePickerBaseProps<DateType>>;
|
|
|
|
export type RangePickerDateProps<DateType> = InjectDefaultProps<RCRangePickerDateProps<DateType>>;
|
|
|
|
export type RangePickerTimeProps<DateType> = InjectDefaultProps<RCRangePickerTimeProps<DateType>>;
|
|
|
|
|
|
|
|
export type RangePickerProps<DateType> =
|
|
|
|
| RangePickerBaseProps<DateType>
|
|
|
|
| RangePickerDateProps<DateType>
|
|
|
|
| RangePickerTimeProps<DateType>;
|
|
|
|
|
|
|
|
function generatePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
|
|
|
|
// =========================== Picker ===========================
|
2021-09-03 13:24:48 +08:00
|
|
|
const { DatePicker, WeekPicker, MonthPicker, YearPicker, TimePicker, QuarterPicker } =
|
|
|
|
generateSinglePicker(generateConfig);
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
// ======================== Range Picker ========================
|
|
|
|
const RangePicker = generateRangePicker(generateConfig);
|
|
|
|
|
|
|
|
// =========================== Export ===========================
|
2020-09-16 11:43:55 +08:00
|
|
|
type MergedDatePickerType = typeof DatePicker & {
|
2020-04-02 15:46:07 +08:00
|
|
|
WeekPicker: typeof WeekPicker;
|
|
|
|
MonthPicker: typeof MonthPicker;
|
|
|
|
YearPicker: typeof YearPicker;
|
2021-09-03 13:24:48 +08:00
|
|
|
RangePicker: typeof RangePicker;
|
2020-04-02 15:46:07 +08:00
|
|
|
TimePicker: typeof TimePicker;
|
2020-07-23 11:25:17 +08:00
|
|
|
QuarterPicker: typeof QuarterPicker;
|
2020-04-02 15:46:07 +08:00
|
|
|
};
|
|
|
|
|
2020-09-16 11:43:55 +08:00
|
|
|
const MergedDatePicker = DatePicker as MergedDatePickerType;
|
2020-04-02 15:46:07 +08:00
|
|
|
MergedDatePicker.WeekPicker = WeekPicker;
|
|
|
|
MergedDatePicker.MonthPicker = MonthPicker;
|
|
|
|
MergedDatePicker.YearPicker = YearPicker;
|
|
|
|
MergedDatePicker.RangePicker = RangePicker;
|
|
|
|
MergedDatePicker.TimePicker = TimePicker;
|
2020-07-23 11:25:17 +08:00
|
|
|
MergedDatePicker.QuarterPicker = QuarterPicker;
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
return MergedDatePicker;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default generatePicker;
|