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';
|
|
|
|
import * as React from 'react';
|
2022-05-14 16:34:35 +08:00
|
|
|
import { forwardRef, useContext, useImperativeHandle } from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { PickerDateProps, PickerLocale, PickerProps, PickerTimeProps } from '.';
|
|
|
|
import { Components, getTimeProps } from '.';
|
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';
|
2022-06-22 14:57:09 +08:00
|
|
|
import SizeContext from '../../config-provider/SizeContext';
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext } from '../../form/context';
|
2022-06-22 14:57:09 +08:00
|
|
|
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputStatus } from '../../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../../_util/statusUtils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import warning from '../../_util/warning';
|
|
|
|
import enUS from '../locale/en_US';
|
|
|
|
import { getPlaceholder, transPlacement2DropdownAlign } from '../util';
|
|
|
|
import type { CommonPickerMethods, DatePickRef, PickerComponentClass } from './interface';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-06-22 15:18:03 +08:00
|
|
|
import useStyle from '../style';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
export default function generatePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
|
2022-02-16 21:14:51 +08:00
|
|
|
type DatePickerProps = PickerProps<DateType> & {
|
|
|
|
status?: InputStatus;
|
2022-04-19 16:54:52 +08:00
|
|
|
hashId?: string;
|
2022-08-05 11:21:07 +08:00
|
|
|
/**
|
|
|
|
* @deprecated `dropdownClassName` is deprecated which will be removed in next major
|
|
|
|
* version.Please use `popupClassName` instead.
|
|
|
|
*/
|
|
|
|
dropdownClassName?: string;
|
|
|
|
popupClassName?: string;
|
2022-02-16 21:14:51 +08:00
|
|
|
};
|
2020-04-02 15:46:07 +08:00
|
|
|
function getPicker<InnerPickerProps extends DatePickerProps>(
|
|
|
|
picker?: PickerMode,
|
|
|
|
displayName?: string,
|
|
|
|
) {
|
2022-05-14 16:34:35 +08:00
|
|
|
const Picker = forwardRef<DatePickRef<DateType> | CommonPickerMethods, InnerPickerProps>(
|
|
|
|
(props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
getPopupContainer: customizeGetPopupContainer,
|
|
|
|
className,
|
|
|
|
size: customizeSize,
|
|
|
|
bordered = true,
|
|
|
|
placement,
|
|
|
|
placeholder,
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName,
|
|
|
|
dropdownClassName,
|
2022-05-14 16:34:35 +08:00
|
|
|
disabled: customDisabled,
|
|
|
|
status: customStatus,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { getPrefixCls, direction, getPopupContainer } = useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('picker', customizePrefixCls);
|
|
|
|
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 =====================
|
|
|
|
warning(
|
|
|
|
picker !== 'quarter',
|
|
|
|
displayName!,
|
|
|
|
`DatePicker.${displayName} is legacy usage. Please use DatePicker[picker='${picker}'] directly.`,
|
|
|
|
);
|
|
|
|
|
|
|
|
warning(
|
|
|
|
!dropdownClassName,
|
|
|
|
'DatePicker',
|
|
|
|
'`dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.',
|
|
|
|
);
|
2022-05-14 16:34:35 +08:00
|
|
|
// ===================== Size =====================
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled || disabled;
|
|
|
|
|
|
|
|
// ===================== FormItemInput =====================
|
|
|
|
const formItemContext = useContext(FormItemInputContext);
|
|
|
|
const { hasFeedback, status: contextStatus, feedbackIcon } = formItemContext;
|
|
|
|
|
|
|
|
const suffixNode = (
|
|
|
|
<>
|
|
|
|
{mergedPicker === 'time' ? <ClockCircleOutlined /> : <CalendarOutlined />}
|
|
|
|
{hasFeedback && feedbackIcon}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2022-05-16 17:16:52 +08:00
|
|
|
return wrapSSR(
|
2022-05-14 16:34:35 +08:00
|
|
|
<LocaleReceiver componentName="DatePicker" defaultLocale={enUS}>
|
|
|
|
{(contextLocale: PickerLocale) => {
|
|
|
|
const locale = { ...contextLocale, ...props.locale };
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RCPicker<DateType>
|
|
|
|
ref={innerRef}
|
|
|
|
placeholder={getPlaceholder(mergedPicker, locale, placeholder)}
|
|
|
|
suffixIcon={suffixNode}
|
|
|
|
dropdownAlign={transPlacement2DropdownAlign(direction, placement)}
|
|
|
|
clearIcon={<CloseCircleFilled />}
|
|
|
|
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`} />}
|
|
|
|
allowClear
|
|
|
|
transitionName={`${rootPrefixCls}-slide-up`}
|
|
|
|
{...additionalProps}
|
|
|
|
{...restProps}
|
|
|
|
{...additionalOverrideProps}
|
|
|
|
locale={locale!.lang}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
getStatusClassNames(
|
|
|
|
prefixCls as string,
|
|
|
|
getMergedStatus(contextStatus, customStatus),
|
|
|
|
hasFeedback,
|
|
|
|
),
|
2022-05-16 17:16:52 +08:00
|
|
|
hashId,
|
2022-05-14 16:34:35 +08:00
|
|
|
className,
|
|
|
|
)}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={customizeGetPopupContainer || getPopupContainer}
|
|
|
|
generateConfig={generateConfig}
|
|
|
|
components={Components}
|
|
|
|
direction={direction}
|
|
|
|
disabled={mergedDisabled}
|
2022-08-05 15:23:36 +08:00
|
|
|
dropdownClassName={classNames(hashId, popupClassName || dropdownClassName)}
|
2022-05-14 16:34:35 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2022-05-16 17:16:52 +08:00
|
|
|
</LocaleReceiver>,
|
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>();
|
|
|
|
const WeekPicker = getPicker<Omit<PickerDateProps<DateType>, 'picker'>>('week', 'WeekPicker');
|
|
|
|
const MonthPicker = getPicker<Omit<PickerDateProps<DateType>, 'picker'>>('month', 'MonthPicker');
|
|
|
|
const YearPicker = getPicker<Omit<PickerDateProps<DateType>, 'picker'>>('year', 'YearPicker');
|
|
|
|
const TimePicker = getPicker<Omit<PickerTimeProps<DateType>, 'picker'>>('time', 'TimePicker');
|
2020-07-23 11:25:17 +08:00
|
|
|
const QuarterPicker = getPicker<Omit<PickerTimeProps<DateType>, '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
|
|
|
}
|