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';
|
2020-04-09 13:18:13 +08:00
|
|
|
import SwapRightOutlined from '@ant-design/icons/SwapRightOutlined';
|
2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
2020-04-02 15:46:07 +08:00
|
|
|
import { RangePicker as RCRangePicker } 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 * as React from 'react';
|
|
|
|
import { forwardRef, useContext, useImperativeHandle } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { PickerLocale, RangePickerProps } from '.';
|
|
|
|
import { Components, getTimeProps } from '.';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
|
|
|
import DisabledContext from '../../config-provider/DisabledContext';
|
|
|
|
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-03-25 17:48:12 +08:00
|
|
|
import { getMergedStatus, getStatusClassNames } from '../../_util/statusUtils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import enUS from '../locale/en_US';
|
|
|
|
import { getRangePlaceholder, transPlacement2DropdownAlign } from '../util';
|
|
|
|
import type { CommonPickerMethods, PickerComponentClass } from './interface';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
export default function generateRangePicker<DateType>(
|
|
|
|
generateConfig: GenerateConfig<DateType>,
|
2021-09-03 13:24:48 +08:00
|
|
|
): PickerComponentClass<RangePickerProps<DateType>> {
|
2022-05-09 10:34:10 +08:00
|
|
|
type InternalRangePickerProps = RangePickerProps<DateType> & {};
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
const RangePicker = forwardRef<
|
|
|
|
InternalRangePickerProps | CommonPickerMethods,
|
|
|
|
RangePickerProps<DateType>
|
|
|
|
>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
getPopupContainer: customGetPopupContainer,
|
|
|
|
className,
|
|
|
|
placement,
|
|
|
|
size: customizeSize,
|
|
|
|
disabled: customDisabled,
|
|
|
|
bordered = true,
|
|
|
|
placeholder,
|
|
|
|
status: customStatus,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
const innerRef = React.useRef<RCRangePicker<DateType>>(null);
|
|
|
|
const { getPrefixCls, direction, getPopupContainer } = useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('picker', customizePrefixCls);
|
|
|
|
const { format, showTime, picker } = props as any;
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
let additionalOverrideProps: any = {};
|
|
|
|
additionalOverrideProps = {
|
|
|
|
...additionalOverrideProps,
|
|
|
|
...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}),
|
|
|
|
...(picker === 'time' ? getTimeProps({ format, ...props, picker }) : {}),
|
|
|
|
};
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
// ===================== Size =====================
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
const mergedSize = customizeSize || size;
|
2022-03-25 17:48:12 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled || disabled;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
// ===================== FormItemInput =====================
|
|
|
|
const formItemContext = useContext(FormItemInputContext);
|
|
|
|
const { hasFeedback, status: contextStatus, feedbackIcon } = formItemContext;
|
2022-05-09 10:34:10 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
const suffixNode = (
|
|
|
|
<>
|
|
|
|
{picker === 'time' ? <ClockCircleOutlined /> : <CalendarOutlined />}
|
|
|
|
{hasFeedback && feedbackIcon}
|
|
|
|
</>
|
|
|
|
);
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
focus: () => innerRef.current?.focus(),
|
|
|
|
blur: () => innerRef.current?.blur(),
|
|
|
|
}));
|
2022-05-09 10:34:10 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
return (
|
|
|
|
<LocaleReceiver componentName="DatePicker" defaultLocale={enUS}>
|
|
|
|
{(contextLocale: PickerLocale) => {
|
|
|
|
const locale = { ...contextLocale, ...props.locale };
|
2022-05-09 10:34:10 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
return (
|
|
|
|
<RCRangePicker<DateType>
|
|
|
|
separator={
|
|
|
|
<span aria-label="to" className={`${prefixCls}-separator`}>
|
|
|
|
<SwapRightOutlined />
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
disabled={mergedDisabled}
|
|
|
|
ref={innerRef}
|
|
|
|
dropdownAlign={transPlacement2DropdownAlign(direction, placement)}
|
|
|
|
placeholder={getRangePlaceholder(picker, locale, placeholder)}
|
|
|
|
suffixIcon={suffixNode}
|
|
|
|
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`}
|
|
|
|
{...restProps}
|
|
|
|
{...additionalOverrideProps}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
getStatusClassNames(
|
|
|
|
prefixCls as string,
|
|
|
|
getMergedStatus(contextStatus, customStatus),
|
|
|
|
hasFeedback,
|
|
|
|
),
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
locale={locale!.lang}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={customGetPopupContainer || getPopupContainer}
|
|
|
|
generateConfig={generateConfig}
|
|
|
|
components={Components}
|
|
|
|
direction={direction}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
});
|
2022-04-15 10:45:20 +08:00
|
|
|
|
2022-05-09 10:34:10 +08:00
|
|
|
return RangePicker as unknown as PickerComponentClass<RangePickerProps<DateType>>;
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|