2022-05-07 14:31:54 +08:00
|
|
|
import type { GenerateConfig } from 'rc-picker/lib/generate/index';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { Locale as RcPickerLocale, PickerMode } from 'rc-picker/lib/interface';
|
|
|
|
import type { SharedTimeProps } from 'rc-picker/lib/panels/TimePanel';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2020-04-02 15:46:07 +08:00
|
|
|
PickerBaseProps as RCPickerBaseProps,
|
|
|
|
PickerDateProps as RCPickerDateProps,
|
|
|
|
PickerTimeProps as RCPickerTimeProps,
|
|
|
|
} from 'rc-picker/lib/Picker';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2020-04-02 15:46:07 +08:00
|
|
|
RangePickerBaseProps as RCRangePickerBaseProps,
|
|
|
|
RangePickerDateProps as RCRangePickerDateProps,
|
|
|
|
RangePickerTimeProps as RCRangePickerTimeProps,
|
|
|
|
} from 'rc-picker/lib/RangePicker';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SizeType } from '../../config-provider/SizeContext';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TimePickerLocale } from '../../time-picker';
|
|
|
|
import type { InputStatus } from '../../_util/statusUtils';
|
2020-04-02 15:46:07 +08:00
|
|
|
import PickerButton from '../PickerButton';
|
|
|
|
import generateRangePicker from './generateRangePicker';
|
2022-06-22 14:57:09 +08:00
|
|
|
import generateSinglePicker from './generateSinglePicker';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-10-28 16:44:19 +08:00
|
|
|
export const Components = { button: PickerButton };
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
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-12-13 14:57:40 +08:00
|
|
|
const DataPickerPlacements = ['bottomLeft', 'bottomRight', 'topLeft', 'topRight'] as const;
|
2022-01-20 16:54:47 +08:00
|
|
|
type DataPickerPlacement = typeof DataPickerPlacements[number];
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
type InjectDefaultProps<Props> = Omit<
|
|
|
|
Props,
|
2023-04-11 19:01:12 +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;
|
2022-03-15 18:38:22 +08:00
|
|
|
status?: InputStatus;
|
2020-04-02 15:46:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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];
|
2022-02-17 13:38:49 +08:00
|
|
|
rangeQuarterPlaceholder?: [string, string];
|
2020-04-02 15:46:07 +08:00
|
|
|
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;
|