2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { forwardRef, useContext, useImperativeHandle } from 'react';
|
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';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-10-01 22:17:14 +08:00
|
|
|
import type { RangePickerProps } from '.';
|
2023-05-12 14:53:47 +08:00
|
|
|
import { getMergedStatus, getStatusClassNames } from '../../_util/statusUtils';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../../_util/warning';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
|
|
|
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';
|
2022-10-18 16:23:10 +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 {
|
|
|
|
getRangePlaceholder,
|
|
|
|
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, PickerComponentClass } from './interface';
|
2023-10-24 21:54:36 +08:00
|
|
|
import { useZIndex } from '../../_util/hooks/useZIndex';
|
2023-11-09 16:48:45 +08:00
|
|
|
import useCSSVarCls from '../../config-provider/hooks/useCSSVarCls';
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2022-11-07 23:51:56 +08:00
|
|
|
export default function generateRangePicker<DateType>(generateConfig: GenerateConfig<DateType>) {
|
2022-05-09 10:34:10 +08:00
|
|
|
type InternalRangePickerProps = RangePickerProps<DateType> & {};
|
2022-11-01 12:00:16 +08:00
|
|
|
type DateRangePickerProps = RangePickerProps<DateType> & {
|
2022-11-04 11:40:06 +08:00
|
|
|
/**
|
|
|
|
* @deprecated `dropdownClassName` is deprecated which will be removed in next major
|
|
|
|
* version.Please use `popupClassName` instead.
|
|
|
|
*/
|
|
|
|
dropdownClassName?: string;
|
|
|
|
popupClassName?: string;
|
2023-08-07 18:05:34 +08:00
|
|
|
rootClassName?: string;
|
2022-11-04 11:40:06 +08:00
|
|
|
};
|
2022-04-19 16:54:52 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
const RangePicker = forwardRef<
|
|
|
|
InternalRangePickerProps | CommonPickerMethods,
|
2022-11-01 12:00:16 +08:00
|
|
|
DateRangePickerProps
|
2022-05-14 16:34:35 +08:00
|
|
|
>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
getPopupContainer: customGetPopupContainer,
|
|
|
|
className,
|
2023-10-24 00:57:38 +08:00
|
|
|
style,
|
2022-05-14 16:34:35 +08:00
|
|
|
placement,
|
|
|
|
size: customizeSize,
|
|
|
|
disabled: customDisabled,
|
|
|
|
bordered = true,
|
|
|
|
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
|
|
|
status: customStatus,
|
2023-08-02 14:21:11 +08:00
|
|
|
clearIcon,
|
2023-08-04 16:14:56 +08:00
|
|
|
allowClear,
|
2023-08-07 18:05:34 +08:00
|
|
|
rootClassName,
|
2022-05-14 16:34:35 +08:00
|
|
|
...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);
|
2023-10-24 00:57:38 +08:00
|
|
|
const { getPrefixCls, direction, getPopupContainer, rangePicker } = 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 { format, showTime, picker } = props as any;
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2023-11-09 16:48:45 +08:00
|
|
|
const cssVarCls = useCSSVarCls(prefixCls);
|
2023-11-29 17:23:45 +08:00
|
|
|
const [wrapCSSVar, hashId] = useStyle(prefixCls, cssVarCls);
|
2020-04-02 15:46:07 +08:00
|
|
|
|
2023-08-14 20:59:34 +08:00
|
|
|
const additionalOverrideProps: any = {
|
2022-05-14 16:34:35 +08:00
|
|
|
...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}),
|
|
|
|
...(picker === 'time' ? getTimeProps({ format, ...props, picker }) : {}),
|
|
|
|
};
|
2022-03-25 17:48:12 +08:00
|
|
|
|
2022-09-08 14:33:11 +08:00
|
|
|
// =================== Warning =====================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('DatePicker.RangePicker');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!dropdownClassName, 'dropdownClassName', 'popupClassName');
|
2022-09-08 14:33:11 +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-03-25 17:48:12 +08:00
|
|
|
|
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-09 10:34:10 +08:00
|
|
|
|
2022-05-14 16:34:35 +08:00
|
|
|
// ===================== FormItemInput =====================
|
|
|
|
const formItemContext = useContext(FormItemInputContext);
|
|
|
|
const { hasFeedback, status: contextStatus, feedbackIcon } = formItemContext;
|
2020-04-02 15:46:07 +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-04-15 10:45:20 +08:00
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
const [contextLocale] = useLocale('Calendar', enUS);
|
2023-02-22 18:18:26 +08:00
|
|
|
|
|
|
|
const locale = { ...contextLocale, ...props.locale! };
|
|
|
|
|
2023-10-24 21:54:36 +08:00
|
|
|
// ============================ zIndex ============================
|
|
|
|
const [zIndex] = useZIndex('DatePicker', props.popupStyle?.zIndex as number);
|
|
|
|
|
2023-11-09 16:48:45 +08:00
|
|
|
return wrapCSSVar(
|
2023-02-22 18:18:26 +08:00
|
|
|
<RCRangePicker<DateType>
|
|
|
|
separator={
|
|
|
|
<span aria-label="to" className={`${prefixCls}-separator`}>
|
|
|
|
<SwapRightOutlined />
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
disabled={mergedDisabled}
|
|
|
|
ref={innerRef}
|
|
|
|
dropdownAlign={transPlacement2DropdownAlign(direction, placement)}
|
|
|
|
placeholder={getRangePlaceholder(locale, picker, placeholder)}
|
|
|
|
suffixIcon={suffixNode}
|
|
|
|
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`}
|
|
|
|
{...restProps}
|
|
|
|
{...additionalOverrideProps}
|
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
2023-05-12 14:53:47 +08:00
|
|
|
getStatusClassNames(prefixCls, getMergedStatus(contextStatus, customStatus), hasFeedback),
|
2023-02-22 18:18:26 +08:00
|
|
|
hashId,
|
|
|
|
compactItemClassnames,
|
|
|
|
className,
|
2023-10-24 00:57:38 +08:00
|
|
|
rangePicker?.className,
|
2023-11-09 16:48:45 +08:00
|
|
|
cssVarCls,
|
2023-08-07 18:05:34 +08:00
|
|
|
rootClassName,
|
2023-02-22 18:18:26 +08:00
|
|
|
)}
|
2023-10-24 00:57:38 +08:00
|
|
|
style={{ ...rangePicker?.style, ...style }}
|
2023-02-22 18:18:26 +08:00
|
|
|
locale={locale.lang}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={customGetPopupContainer || getPopupContainer}
|
|
|
|
generateConfig={generateConfig}
|
|
|
|
components={Components}
|
|
|
|
direction={direction}
|
2023-11-09 16:48:45 +08:00
|
|
|
dropdownClassName={classNames(
|
|
|
|
hashId,
|
|
|
|
popupClassName || dropdownClassName,
|
|
|
|
cssVarCls,
|
|
|
|
rootClassName,
|
|
|
|
)}
|
2023-10-24 21:54:36 +08:00
|
|
|
popupStyle={{
|
|
|
|
...props.popupStyle,
|
|
|
|
zIndex,
|
|
|
|
}}
|
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-19 16:54:52 +08:00
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
RangePicker.displayName = 'RangePicker';
|
|
|
|
}
|
|
|
|
|
2022-11-01 12:00:16 +08:00
|
|
|
return RangePicker as unknown as PickerComponentClass<DateRangePickerProps>;
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|