2020-04-02 15:46:07 +08:00
|
|
|
import * as React 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';
|
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';
|
|
|
|
import { ConfigContext, ConfigConsumerProps } from '../../config-provider';
|
|
|
|
import SizeContext from '../../config-provider/SizeContext';
|
|
|
|
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
|
|
|
|
import { getRangePlaceholder } from '../util';
|
|
|
|
import { RangePickerProps, PickerLocale, getTimeProps, Components } from '.';
|
|
|
|
|
|
|
|
export default function generateRangePicker<DateType>(
|
|
|
|
generateConfig: GenerateConfig<DateType>,
|
|
|
|
): React.ComponentClass<RangePickerProps<DateType>> {
|
|
|
|
class RangePicker extends React.Component<RangePickerProps<DateType>> {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getDefaultLocale = () => {
|
|
|
|
const { locale } = this.props;
|
|
|
|
const result = {
|
|
|
|
...enUS,
|
|
|
|
...locale,
|
|
|
|
};
|
|
|
|
result.lang = {
|
|
|
|
...result.lang,
|
|
|
|
...((locale || {}) as PickerLocale).lang,
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
renderPicker = (locale: PickerLocale) => {
|
2020-04-25 22:46:21 +08:00
|
|
|
const { getPrefixCls, direction, getPopupContainer } = this.context;
|
2020-04-02 15:46:07 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2020-04-25 22:46:21 +08:00
|
|
|
getPopupContainer: customGetPopupContainer,
|
2020-04-02 15:46:07 +08:00
|
|
|
className,
|
|
|
|
size: customizeSize,
|
|
|
|
bordered = true,
|
2020-05-02 21:55:23 +08:00
|
|
|
placeholder,
|
2020-04-02 15:46:07 +08:00
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
const { format, showTime, picker } = this.props as any;
|
|
|
|
const prefixCls = getPrefixCls('picker', customizePrefixCls);
|
|
|
|
|
|
|
|
let additionalOverrideProps: any = {};
|
|
|
|
|
|
|
|
additionalOverrideProps = {
|
|
|
|
...additionalOverrideProps,
|
|
|
|
...(showTime ? getTimeProps({ format, picker, ...showTime }) : {}),
|
|
|
|
...(picker === 'time' ? getTimeProps({ format, ...this.props, picker }) : {}),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SizeContext.Consumer>
|
|
|
|
{size => {
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RCRangePicker<DateType>
|
2020-04-09 13:18:13 +08:00
|
|
|
separator={
|
|
|
|
<span aria-label="to" className={`${prefixCls}-separator`}>
|
|
|
|
<SwapRightOutlined />
|
|
|
|
</span>
|
|
|
|
}
|
2020-04-02 15:46:07 +08:00
|
|
|
ref={this.pickerRef}
|
2020-05-02 21:55:23 +08:00
|
|
|
placeholder={getRangePlaceholder(picker, locale, placeholder)}
|
2020-04-02 15:46:07 +08:00
|
|
|
suffixIcon={picker === 'time' ? <ClockCircleOutlined /> : <CalendarOutlined />}
|
|
|
|
clearIcon={<CloseCircleFilled />}
|
|
|
|
allowClear
|
|
|
|
transitionName="slide-up"
|
|
|
|
{...restProps}
|
2020-06-06 14:37:08 +08:00
|
|
|
{...additionalOverrideProps}
|
2020-09-06 13:07:39 +08:00
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
2020-04-02 15:46:07 +08:00
|
|
|
locale={locale!.lang}
|
|
|
|
prefixCls={prefixCls}
|
2020-04-25 22:46:21 +08:00
|
|
|
getPopupContainer={customGetPopupContainer || getPopupContainer}
|
2020-04-02 15:46:07 +08:00
|
|
|
generateConfig={generateConfig}
|
|
|
|
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`} />}
|
|
|
|
components={Components}
|
|
|
|
direction={direction}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SizeContext.Consumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<LocaleReceiver componentName="DatePicker" defaultLocale={this.getDefaultLocale}>
|
|
|
|
{this.renderPicker}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RangePicker;
|
|
|
|
}
|