2020-04-02 15:46:07 +08:00
|
|
|
import * as React from 'react';
|
2022-04-15 10:45:20 +08:00
|
|
|
import { forwardRef, useContext } from 'react';
|
2020-04-02 15:46:07 +08:00
|
|
|
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';
|
2020-04-09 13:18:13 +08:00
|
|
|
import SwapRightOutlined from '@ant-design/icons/SwapRightOutlined';
|
2020-04-02 15:46:07 +08:00
|
|
|
import { RangePicker as RCRangePicker } from 'rc-picker';
|
|
|
|
import { GenerateConfig } from 'rc-picker/lib/generate/index';
|
|
|
|
import enUS from '../locale/en_US';
|
2022-04-15 10:45:20 +08:00
|
|
|
import { ConfigConsumerProps, ConfigContext } from '../../config-provider';
|
2020-04-02 15:46:07 +08:00
|
|
|
import SizeContext from '../../config-provider/SizeContext';
|
|
|
|
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
|
2022-01-20 16:54:47 +08:00
|
|
|
import { getRangePlaceholder, transPlacement2DropdownAlign } from '../util';
|
2022-04-15 10:45:20 +08:00
|
|
|
import { Components, getTimeProps, PickerLocale, RangePickerProps } from '.';
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext } from '../../form/context';
|
2022-03-25 17:48:12 +08:00
|
|
|
import { getMergedStatus, getStatusClassNames } from '../../_util/statusUtils';
|
2022-04-15 10:45:20 +08:00
|
|
|
import { PickerComponentClass } from './interface';
|
2022-04-19 16:54:52 +08:00
|
|
|
import useStyle from '../style';
|
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-04-19 16:54:52 +08:00
|
|
|
type InternalRangePickerProps = RangePickerProps<DateType> & {
|
|
|
|
hashId?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RangePicker extends React.Component<InternalRangePickerProps> {
|
2020-04-02 15:46:07 +08:00
|
|
|
static contextType = ConfigContext;
|
|
|
|
|
|
|
|
context: ConfigConsumerProps;
|
|
|
|
|
|
|
|
pickerRef = React.createRef<RCRangePicker<DateType>>();
|
|
|
|
|
|
|
|
focus = () => {
|
|
|
|
if (this.pickerRef.current) {
|
|
|
|
this.pickerRef.current.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
blur = () => {
|
|
|
|
if (this.pickerRef.current) {
|
|
|
|
this.pickerRef.current.blur();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-05 22:28:56 +08:00
|
|
|
renderPicker = (contextLocale: PickerLocale) => {
|
|
|
|
const locale = { ...contextLocale, ...this.props.locale };
|
2020-04-25 22:46:21 +08:00
|
|
|
const { getPrefixCls, direction, getPopupContainer } = this.context;
|
2020-04-02 15:46:07 +08:00
|
|
|
const {
|
2022-04-15 10:45:20 +08:00
|
|
|
prefixCls,
|
2020-04-25 22:46:21 +08:00
|
|
|
getPopupContainer: customGetPopupContainer,
|
2020-04-02 15:46:07 +08:00
|
|
|
className,
|
2022-01-20 16:54:47 +08:00
|
|
|
placement,
|
2020-04-02 15:46:07 +08:00
|
|
|
size: customizeSize,
|
|
|
|
bordered = true,
|
2020-05-02 21:55:23 +08:00
|
|
|
placeholder,
|
2022-03-15 18:38:22 +08:00
|
|
|
status: customStatus,
|
2022-04-19 16:54:52 +08:00
|
|
|
dropdownClassName,
|
|
|
|
hashId,
|
2020-04-02 15:46:07 +08:00
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
const { format, showTime, picker } = this.props as any;
|
|
|
|
|
|
|
|
let additionalOverrideProps: any = {};
|
|
|
|
|
|
|
|
additionalOverrideProps = {
|
|
|
|
...additionalOverrideProps,
|
|
|
|
...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}),
|
|
|
|
...(picker === 'time' ? getTimeProps({ format, ...this.props, picker }) : {}),
|
|
|
|
};
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-04-02 15:46:07 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SizeContext.Consumer>
|
|
|
|
{size => {
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
|
|
|
|
return (
|
2022-03-24 21:54:20 +08:00
|
|
|
<FormItemInputContext.Consumer>
|
2022-03-25 17:48:12 +08:00
|
|
|
{({ hasFeedback, status: contextStatus, feedbackIcon }) => {
|
|
|
|
const suffixNode = (
|
|
|
|
<>
|
|
|
|
{picker === 'time' ? <ClockCircleOutlined /> : <CalendarOutlined />}
|
|
|
|
{hasFeedback && feedbackIcon}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RCRangePicker<DateType>
|
|
|
|
separator={
|
|
|
|
<span aria-label="to" className={`${prefixCls}-separator`}>
|
|
|
|
<SwapRightOutlined />
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
ref={this.pickerRef}
|
|
|
|
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(
|
2022-04-15 10:45:20 +08:00
|
|
|
prefixCls as string,
|
2022-03-25 17:48:12 +08:00
|
|
|
getMergedStatus(contextStatus, customStatus),
|
|
|
|
hasFeedback,
|
|
|
|
),
|
2022-04-19 16:54:52 +08:00
|
|
|
hashId,
|
2022-03-25 17:48:12 +08:00
|
|
|
className,
|
|
|
|
)}
|
|
|
|
locale={locale!.lang}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={customGetPopupContainer || getPopupContainer}
|
|
|
|
generateConfig={generateConfig}
|
|
|
|
components={Components}
|
|
|
|
direction={direction}
|
2022-04-19 16:54:52 +08:00
|
|
|
dropdownClassName={classNames(hashId, dropdownClassName)}
|
2022-03-25 17:48:12 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2022-03-24 21:54:20 +08:00
|
|
|
</FormItemInputContext.Consumer>
|
2020-04-02 15:46:07 +08:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SizeContext.Consumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-05-05 22:28:56 +08:00
|
|
|
<LocaleReceiver componentName="DatePicker" defaultLocale={enUS}>
|
2020-04-02 15:46:07 +08:00
|
|
|
{this.renderPicker}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:45:20 +08:00
|
|
|
return forwardRef<RangePicker, RangePickerProps<DateType>>((props, ref) => {
|
|
|
|
const { prefixCls: customizePrefixCls } = props;
|
|
|
|
|
|
|
|
const { getPrefixCls } = useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('picker', customizePrefixCls);
|
|
|
|
|
2022-04-19 16:54:52 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
|
|
|
return wrapSSR(<RangePicker {...props} prefixCls={prefixCls} ref={ref} hashId={hashId} />);
|
2022-04-15 10:45:20 +08:00
|
|
|
}) as unknown as PickerComponentClass<RangePickerProps<DateType>>;
|
2020-04-02 15:46:07 +08:00
|
|
|
}
|