import * as React from 'react'; import { forwardRef, useContext } from 'react'; import classNames from 'classnames'; import CalendarOutlined from '@ant-design/icons/CalendarOutlined'; import ClockCircleOutlined from '@ant-design/icons/ClockCircleOutlined'; import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled'; import SwapRightOutlined from '@ant-design/icons/SwapRightOutlined'; import { RangePicker as RCRangePicker } from 'rc-picker'; import type { GenerateConfig } from 'rc-picker/lib/generate/index'; import enUS from '../locale/en_US'; import { ConfigContext } from '../../config-provider'; import SizeContext from '../../config-provider/SizeContext'; import DisabledContext from '../../config-provider/DisabledContext'; import LocaleReceiver from '../../locale-provider/LocaleReceiver'; import { getRangePlaceholder, transPlacement2DropdownAlign } from '../util'; import type { PickerLocale, RangePickerProps } from '.'; import { Components, getTimeProps } from '.'; import { FormItemInputContext } from '../../form/context'; import { getMergedStatus, getStatusClassNames } from '../../_util/statusUtils'; import useStyle from '../style'; import type { PickerComponentClass } from './interface'; export default function generateRangePicker( generateConfig: GenerateConfig, ): PickerComponentClass> { type InternalRangePickerProps = RangePickerProps & {}; const RangePicker = forwardRef>( (props, ref) => { const { prefixCls: customizePrefixCls, getPopupContainer: customGetPopupContainer, className, placement, size: customizeSize, disabled: customDisabled, bordered = true, placeholder, status: customStatus, onBlur, onFocus, dropdownClassName, ...restProps } = props; const pickerRef = (ref as any) || React.createRef>(); const { getPrefixCls, direction, getPopupContainer } = useContext(ConfigContext); const prefixCls = getPrefixCls('picker', customizePrefixCls); const { format, showTime, picker } = props as any; const rootPrefixCls = getPrefixCls(); const [wrapSSR, hashId] = useStyle(prefixCls); let additionalOverrideProps: any = {}; additionalOverrideProps = { ...additionalOverrideProps, ...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}), ...(picker === 'time' ? getTimeProps({ format, ...props, picker }) : {}), }; // ===================== 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 = ( <> {picker === 'time' ? : } {hasFeedback && feedbackIcon} ); const focus = () => { if (pickerRef.current) { pickerRef.current.focus(); } }; const blur = () => { if (pickerRef.current) { pickerRef.current.blur(); } }; return wrapSSR( {(contextLocale: PickerLocale) => { const locale = { ...contextLocale, ...props.locale }; return ( separator={ } disabled={mergedDisabled} ref={pickerRef} onBlur={onBlur || blur} onFocus={onFocus || focus} dropdownAlign={transPlacement2DropdownAlign(direction, placement)} placeholder={getRangePlaceholder(picker, locale, placeholder)} suffixIcon={suffixNode} clearIcon={} prevIcon={} nextIcon={} superPrevIcon={} superNextIcon={} allowClear transitionName={`${rootPrefixCls}-slide-up`} {...restProps} {...additionalOverrideProps} className={classNames( { [`${prefixCls}-${mergedSize}`]: mergedSize, [`${prefixCls}-borderless`]: !bordered, }, getStatusClassNames( prefixCls as string, getMergedStatus(contextStatus, customStatus), hasFeedback, ), hashId, className, )} locale={locale!.lang} prefixCls={prefixCls} getPopupContainer={customGetPopupContainer || getPopupContainer} generateConfig={generateConfig} components={Components} direction={direction} dropdownClassName={classNames(hashId, dropdownClassName)} /> ); }} , ); }, ); return RangePicker as unknown as PickerComponentClass>; }