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'; import RCPicker from 'rc-picker'; import { PickerMode } from 'rc-picker/lib/interface'; import { GenerateConfig } from 'rc-picker/lib/generate/index'; import enUS from '../locale/en_US'; import { getPlaceholder } from '../util'; import devWarning from '../../_util/devWarning'; import { ConfigContext, ConfigConsumerProps } from '../../config-provider'; import LocaleReceiver from '../../locale-provider/LocaleReceiver'; import SizeContext from '../../config-provider/SizeContext'; import { PickerProps, PickerLocale, PickerDateProps, PickerTimeProps, getTimeProps, Components, } from '.'; export default function generatePicker(generateConfig: GenerateConfig) { type DatePickerProps = PickerProps; function getPicker( picker?: PickerMode, displayName?: string, ) { class Picker extends React.Component { static contextType = ConfigContext; static displayName: string; context: ConfigConsumerProps; pickerRef = React.createRef>(); constructor(props: InnerPickerProps) { super(props); devWarning( picker !== 'quarter', displayName!, `DatePicker.${displayName} is legacy usage. Please use DatePicker[picker='${picker}'] directly.`, ); } 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) => { const { getPrefixCls, direction, getPopupContainer } = this.context; const { prefixCls: customizePrefixCls, getPopupContainer: customizeGetPopupContainer, className, size: customizeSize, bordered = true, placeholder, ...restProps } = this.props; const { format, showTime } = this.props as any; const prefixCls = getPrefixCls('picker', customizePrefixCls); const additionalProps = { showToday: true, }; let additionalOverrideProps: any = {}; if (picker) { additionalOverrideProps.picker = picker; } const mergedPicker = picker || this.props.picker; additionalOverrideProps = { ...additionalOverrideProps, ...(showTime ? getTimeProps({ format, picker: mergedPicker, ...showTime }) : {}), ...(mergedPicker === 'time' ? getTimeProps({ format, ...this.props, picker: mergedPicker }) : {}), }; return ( {size => { const mergedSize = customizeSize || size; return ( ref={this.pickerRef} placeholder={getPlaceholder(mergedPicker, locale, placeholder)} suffixIcon={ mergedPicker === 'time' ? : } clearIcon={} allowClear transitionName="slide-up" {...additionalProps} {...restProps} {...additionalOverrideProps} locale={locale!.lang} className={classNames( { [`${prefixCls}-${mergedSize}`]: mergedSize, [`${prefixCls}-borderless`]: !bordered, }, className, )} prefixCls={prefixCls} getPopupContainer={customizeGetPopupContainer || getPopupContainer} generateConfig={generateConfig} prevIcon={} nextIcon={} superPrevIcon={} superNextIcon={} components={Components} direction={direction} /> ); }} ); }; render() { return ( {this.renderPicker} ); } } if (displayName) { Picker.displayName = displayName; } return Picker as React.ComponentClass; } const DatePicker = getPicker(); const WeekPicker = getPicker, 'picker'>>('week', 'WeekPicker'); const MonthPicker = getPicker, 'picker'>>('month', 'MonthPicker'); const YearPicker = getPicker, 'picker'>>('year', 'YearPicker'); const TimePicker = getPicker, 'picker'>>('time', 'TimePicker'); const QuarterPicker = getPicker, 'picker'>>( 'quarter', 'QuarterPicker', ); return { DatePicker, WeekPicker, MonthPicker, YearPicker, TimePicker, QuarterPicker }; }