2020-04-02 15:46:07 +08:00
|
|
|
import CalendarOutlined from '@ant-design/icons/CalendarOutlined';
|
|
|
|
import ClockCircleOutlined from '@ant-design/icons/ClockCircleOutlined';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
2020-04-02 15:46:07 +08:00
|
|
|
import RCPicker from 'rc-picker';
|
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 { PickerMode } from 'rc-picker/lib/interface';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { forwardRef, useContext, useImperativeHandle } from 'react';
|
2022-11-04 11:40:06 +08:00
|
|
|
import type { PickerProps, PickerTimeProps } from '.';
|
2023-05-12 14:53:47 +08:00
|
|
|
import type { InputStatus } from '../../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../../_util/statusUtils';
|
|
|
|
import warning from '../../_util/warning';
|
2022-05-07 14:31:54 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
2022-04-29 20:48:10 +08:00
|
|
|
import DisabledContext from '../../config-provider/DisabledContext';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../../config-provider/hooks/useSize';
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext } from '../../form/context';
|
2023-03-21 13:08:43 +08:00
|
|
|
import { useLocale } from '../../locale';
|
2023-01-20 11:03:50 +08:00
|
|
|
import { useCompactItemContext } from '../../space/Compact';
|
2022-06-22 14:57:09 +08:00
|
|
|
import enUS from '../locale/en_US';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useStyle from '../style';
|
2023-08-04 16:14:56 +08:00
|
|
|
import {
|
|
|
|
getPlaceholder,
|
|
|
|
getTimeProps,
|
|
|
|
mergeAllowClear,
|
|
|
|
transPlacement2DropdownAlign,
|
|
|
|
} from '../util';
|
2023-06-25 10:16:02 +08:00
|
|
|
import Components from './Components';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { CommonPickerMethods, DatePickRef, PickerComponentClass } from './interface';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
export default function generatePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
|
2022-11-01 12:00:16 +08:00
|
|
|
type CustomPickerProps = {
|
2022-02-16 21:14:51 +08:00
|
|
|
status?: InputStatus;
|
2022-04-19 16:54:52 +08:00
|
|
|
hashId?: string;
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-02-16 21:14:51 +08:00
|
|
|
};
|
2022-11-01 12:00:16 +08:00
|
|
|
type DatePickerProps = PickerProps<DateType> & CustomPickerProps;
|
2022-11-07 23:32:46 +08:00
|
|
|
type TimePickerProps = PickerTimeProps<DateType> & CustomPickerProps;
|
2022-11-01 12:00:16 +08:00
|
|
|
|
2020-04-02 15:46:07 +08:00
|
|
|
function getPicker<InnerPickerProps extends DatePickerProps>(
|
|
|
|
picker?: PickerMode,
|
|
|
|
displayName?: string,
|
|
|
|
) {
|
2023-07-07 19:02:55 +08:00
|
|
|
const consumerName = displayName === 'TimePicker' ? 'timePicker' : 'datePicker';
|
2022-05-14 16:34:35 +08:00
|
|
|
const Picker = forwardRef<DatePickRef<DateType> | CommonPickerMethods, InnerPickerProps>(
|
|
|
|
(props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
getPopupContainer: customizeGetPopupContainer,
|
2023-07-07 19:02:55 +08:00
|
|
|
style,
|
2022-05-14 16:34:35 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-05-14 16:34:35 +08:00
|
|
|
size: customizeSize,
|
|
|
|
bordered = true,
|
|
|
|
placement,
|
|
|
|
placeholder,
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName,
|
2022-09-08 14:33:11 +08:00
|
|
|
dropdownClassName,
|
2022-05-14 16:34:35 +08:00
|
|
|
disabled: customDisabled,
|
|
|
|
status: customStatus,
|
2023-08-02 14:21:11 +08:00
|
|
|
clearIcon,
|
2023-08-04 16:14:56 +08:00
|
|
|
allowClear,
|
2022-05-14 16:34:35 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
2023-07-07 19:02:55 +08:00
|
|
|
const {
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
|
|
|
getPopupContainer,
|
|
|
|
// Consume different styles according to different names
|
|
|
|
[consumerName]: consumerStyle,
|
|
|
|
} = useContext(ConfigContext);
|
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
const prefixCls = getPrefixCls('picker', customizePrefixCls);
|
2022-10-18 16:23:10 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
2022-05-14 16:34:35 +08:00
|
|
|
const innerRef = React.useRef<RCPicker<DateType>>(null);
|
|
|
|
const { format, showTime } = props as any;
|
|
|
|
|
2022-05-16 17:16:52 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
focus: () => innerRef.current?.focus(),
|
|
|
|
blur: () => innerRef.current?.blur(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const additionalProps = {
|
|
|
|
showToday: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
let additionalOverrideProps: any = {};
|
|
|
|
if (picker) {
|
|
|
|
additionalOverrideProps.picker = picker;
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|
2022-05-14 16:34:35 +08:00
|
|
|
const mergedPicker = picker || props.picker;
|
|
|
|
|
|
|
|
additionalOverrideProps = {
|
|
|
|
...additionalOverrideProps,
|
|
|
|
...(showTime ? getTimeProps({ format, picker: mergedPicker, ...showTime }) : {}),
|
|
|
|
...(mergedPicker === 'time'
|
|
|
|
? getTimeProps({ format, ...props, picker: mergedPicker })
|
|
|
|
: {}),
|
|
|
|
};
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
|
2022-08-05 11:21:07 +08:00
|
|
|
// =================== Warning =====================
|
2022-09-08 14:33:11 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
|
|
|
picker !== 'quarter',
|
|
|
|
displayName!,
|
|
|
|
`DatePicker.${displayName} is legacy usage. Please use DatePicker[picker='${picker}'] directly.`,
|
|
|
|
);
|
|
|
|
|
|
|
|
warning(
|
|
|
|
!dropdownClassName,
|
|
|
|
displayName || 'DatePicker',
|
|
|
|
'`dropdownClassName` is deprecated. Please use `popupClassName` instead.',
|
|
|
|
);
|
|
|
|
}
|
2022-08-05 11:21:07 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
// ===================== Size =====================
|
2023-06-19 14:26:48 +08:00
|
|
|
const mergedSize = useSize((ctx) => customizeSize ?? compactSize ?? ctx);
|
2022-05-14 16:34:35 +08:00
|
|
|
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
2022-09-20 16:48:59 +08:00
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
2022-05-14 16:34:35 +08:00
|
|
|
|
|
|
|
// ===================== FormItemInput =====================
|
|
|
|
const formItemContext = useContext(FormItemInputContext);
|
|
|
|
const { hasFeedback, status: contextStatus, feedbackIcon } = formItemContext;
|
|
|
|
|
|
|
|
const suffixNode = (
|
|
|
|
<>
|
|
|
|
{mergedPicker === 'time' ? <ClockCircleOutlined /> : <CalendarOutlined />}
|
|
|
|
{hasFeedback && feedbackIcon}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
const [contextLocale] = useLocale('DatePicker', enUS);
|
2023-02-22 18:18:26 +08:00
|
|
|
|
|
|
|
const locale = { ...contextLocale, ...props.locale! };
|
|
|
|
|
2022-05-16 17:16:52 +08:00
|
|
|
return wrapSSR(
|
2023-02-22 18:18:26 +08:00
|
|
|
<RCPicker<DateType>
|
|
|
|
ref={innerRef}
|
|
|
|
placeholder={getPlaceholder(locale, mergedPicker, placeholder)}
|
|
|
|
suffixIcon={suffixNode}
|
|
|
|
dropdownAlign={transPlacement2DropdownAlign(direction, placement)}
|
|
|
|
prevIcon={<span className={`${prefixCls}-prev-icon`} />}
|
|
|
|
nextIcon={<span className={`${prefixCls}-next-icon`} />}
|
|
|
|
superPrevIcon={<span className={`${prefixCls}-super-prev-icon`} />}
|
|
|
|
superNextIcon={<span className={`${prefixCls}-super-next-icon`} />}
|
|
|
|
transitionName={`${rootPrefixCls}-slide-up`}
|
|
|
|
{...additionalProps}
|
|
|
|
{...restProps}
|
|
|
|
{...additionalOverrideProps}
|
|
|
|
locale={locale!.lang}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
getStatusClassNames(
|
2023-07-11 10:44:18 +08:00
|
|
|
prefixCls,
|
2023-02-22 18:18:26 +08:00
|
|
|
getMergedStatus(contextStatus, customStatus),
|
|
|
|
hasFeedback,
|
|
|
|
),
|
|
|
|
hashId,
|
|
|
|
compactItemClassnames,
|
2023-07-07 19:02:55 +08:00
|
|
|
consumerStyle?.className,
|
2023-02-22 18:18:26 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
)}
|
2023-07-07 19:02:55 +08:00
|
|
|
style={{ ...consumerStyle?.style, ...style }}
|
2023-02-22 18:18:26 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={customizeGetPopupContainer || getPopupContainer}
|
|
|
|
generateConfig={generateConfig}
|
|
|
|
components={Components}
|
|
|
|
direction={direction}
|
|
|
|
disabled={mergedDisabled}
|
|
|
|
dropdownClassName={classNames(
|
|
|
|
hashId,
|
|
|
|
rootClassName,
|
|
|
|
popupClassName || dropdownClassName,
|
|
|
|
)}
|
2023-08-04 16:14:56 +08:00
|
|
|
allowClear={mergeAllowClear(allowClear, clearIcon, <CloseCircleFilled />)}
|
2023-02-22 18:18:26 +08:00
|
|
|
/>,
|
2022-05-14 16:34:35 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2022-04-15 10:45:20 +08:00
|
|
|
|
2020-04-02 15:46:07 +08:00
|
|
|
if (displayName) {
|
2022-05-09 10:34:10 +08:00
|
|
|
Picker.displayName = displayName;
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|
|
|
|
|
2022-05-09 10:34:10 +08:00
|
|
|
return Picker as unknown as PickerComponentClass<InnerPickerProps>;
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const DatePicker = getPicker<DatePickerProps>();
|
2022-11-01 12:00:16 +08:00
|
|
|
const WeekPicker = getPicker<Omit<DatePickerProps, 'picker'>>('week', 'WeekPicker');
|
|
|
|
const MonthPicker = getPicker<Omit<DatePickerProps, 'picker'>>('month', 'MonthPicker');
|
|
|
|
const YearPicker = getPicker<Omit<DatePickerProps, 'picker'>>('year', 'YearPicker');
|
|
|
|
const TimePicker = getPicker<Omit<TimePickerProps, 'picker'>>('time', 'TimePicker');
|
2022-11-07 23:32:46 +08:00
|
|
|
const QuarterPicker = getPicker<Omit<TimePickerProps, 'picker'>>('quarter', 'QuarterPicker');
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2020-07-23 11:25:17 +08:00
|
|
|
return { DatePicker, WeekPicker, MonthPicker, YearPicker, TimePicker, QuarterPicker };
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|