mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 08:59:15 +08:00
af301a9822
* fix(date-picker):fix format type processing for showTime.
* test✅: Add showTime should work correctly when format is Array test
184 lines
4.7 KiB
TypeScript
184 lines
4.7 KiB
TypeScript
import type { AlignType } from '@rc-component/trigger';
|
|
import type { PickerMode } from 'rc-picker/lib/interface';
|
|
import type { SharedTimeProps } from 'rc-picker/lib/panels/TimePanel';
|
|
import type { SelectCommonPlacement } from '../_util/motion';
|
|
import type { DirectionType } from '../config-provider';
|
|
import type { PickerLocale, PickerProps } from './generatePicker';
|
|
|
|
export function getPlaceholder(
|
|
locale: PickerLocale,
|
|
picker?: PickerMode,
|
|
customizePlaceholder?: string,
|
|
): string {
|
|
if (customizePlaceholder !== undefined) {
|
|
return customizePlaceholder;
|
|
}
|
|
|
|
if (picker === 'year' && locale.lang.yearPlaceholder) {
|
|
return locale.lang.yearPlaceholder;
|
|
}
|
|
if (picker === 'quarter' && locale.lang.quarterPlaceholder) {
|
|
return locale.lang.quarterPlaceholder;
|
|
}
|
|
if (picker === 'month' && locale.lang.monthPlaceholder) {
|
|
return locale.lang.monthPlaceholder;
|
|
}
|
|
if (picker === 'week' && locale.lang.weekPlaceholder) {
|
|
return locale.lang.weekPlaceholder;
|
|
}
|
|
if (picker === 'time' && locale.timePickerLocale.placeholder) {
|
|
return locale!.timePickerLocale.placeholder;
|
|
}
|
|
return locale.lang.placeholder;
|
|
}
|
|
|
|
export function getRangePlaceholder(
|
|
locale: PickerLocale,
|
|
picker?: PickerMode,
|
|
customizePlaceholder?: [string, string],
|
|
) {
|
|
if (customizePlaceholder !== undefined) {
|
|
return customizePlaceholder;
|
|
}
|
|
|
|
if (picker === 'year' && locale.lang.yearPlaceholder) {
|
|
return locale.lang.rangeYearPlaceholder;
|
|
}
|
|
if (picker === 'quarter' && locale.lang.quarterPlaceholder) {
|
|
return locale.lang.rangeQuarterPlaceholder;
|
|
}
|
|
if (picker === 'month' && locale.lang.monthPlaceholder) {
|
|
return locale.lang.rangeMonthPlaceholder;
|
|
}
|
|
if (picker === 'week' && locale.lang.weekPlaceholder) {
|
|
return locale.lang.rangeWeekPlaceholder;
|
|
}
|
|
if (picker === 'time' && locale.timePickerLocale.placeholder) {
|
|
return locale!.timePickerLocale.rangePlaceholder;
|
|
}
|
|
return locale.lang.rangePlaceholder;
|
|
}
|
|
|
|
export function transPlacement2DropdownAlign(
|
|
direction: DirectionType,
|
|
placement?: SelectCommonPlacement,
|
|
): AlignType {
|
|
const overflow = {
|
|
adjustX: 1,
|
|
adjustY: 1,
|
|
};
|
|
switch (placement) {
|
|
case 'bottomLeft': {
|
|
return {
|
|
points: ['tl', 'bl'],
|
|
offset: [0, 4],
|
|
overflow,
|
|
};
|
|
}
|
|
case 'bottomRight': {
|
|
return {
|
|
points: ['tr', 'br'],
|
|
offset: [0, 4],
|
|
overflow,
|
|
};
|
|
}
|
|
case 'topLeft': {
|
|
return {
|
|
points: ['bl', 'tl'],
|
|
offset: [0, -4],
|
|
overflow,
|
|
};
|
|
}
|
|
case 'topRight': {
|
|
return {
|
|
points: ['br', 'tr'],
|
|
offset: [0, -4],
|
|
overflow,
|
|
};
|
|
}
|
|
default: {
|
|
return {
|
|
points: direction === 'rtl' ? ['tr', 'br'] : ['tl', 'bl'],
|
|
offset: [0, 4],
|
|
overflow,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
function toArray<T>(list: T | T[]): T[] {
|
|
if (!list) {
|
|
return [];
|
|
}
|
|
return Array.isArray(list) ? list : [list];
|
|
}
|
|
|
|
export function getTimeProps<DateType, DisabledTime>(
|
|
props: { format?: string; picker?: PickerMode } & Omit<
|
|
SharedTimeProps<DateType>,
|
|
'disabledTime'
|
|
> & {
|
|
disabledTime?: DisabledTime;
|
|
},
|
|
) {
|
|
const { format, picker, showHour, showMinute, showSecond, use12Hours } = props;
|
|
|
|
const firstFormat = toArray(format)[0];
|
|
const showTimeObj = { ...props };
|
|
|
|
// https://github.com/ant-design/ant-design/issues/44275
|
|
if (format && Array.isArray(format)) {
|
|
showTimeObj.format = firstFormat;
|
|
}
|
|
|
|
if (firstFormat && typeof firstFormat === 'string') {
|
|
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') &&
|
|
!firstFormat.includes('K') &&
|
|
!firstFormat.includes('k') &&
|
|
showHour === undefined
|
|
) {
|
|
showTimeObj.showHour = false;
|
|
}
|
|
if ((firstFormat.includes('a') || firstFormat.includes('A')) && use12Hours === undefined) {
|
|
showTimeObj.use12Hours = true;
|
|
}
|
|
}
|
|
|
|
if (picker === 'time') {
|
|
return showTimeObj;
|
|
}
|
|
|
|
if (typeof firstFormat === 'function') {
|
|
// format of showTime should use default when format is custom format function
|
|
delete showTimeObj.format;
|
|
}
|
|
|
|
return {
|
|
showTime: showTimeObj,
|
|
};
|
|
}
|
|
|
|
type AllowClear = PickerProps<unknown>['allowClear'];
|
|
type ClearIcon = PickerProps<unknown>['clearIcon'];
|
|
|
|
export function mergeAllowClear(
|
|
allowClear: AllowClear,
|
|
clearIcon: ClearIcon,
|
|
defaultClearIcon: NonNullable<ClearIcon>,
|
|
) {
|
|
if (allowClear === false) {
|
|
return false;
|
|
}
|
|
|
|
const defaults = { clearIcon: clearIcon ?? defaultClearIcon };
|
|
return typeof allowClear === 'object' ? { ...defaults, ...allowClear } : defaults;
|
|
}
|