2020-07-22 09:47:08 +00:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import * as moment from 'moment';
|
|
|
|
import omit from 'lodash/omit';
|
|
|
|
import MonthCalendar from '../vc-calendar/src/MonthCalendar';
|
|
|
|
import VcDatePicker from '../vc-calendar/src/Picker';
|
|
|
|
import classNames from 'classnames';
|
2020-03-28 07:45:37 +00:00
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
|
|
|
import CalendarOutlined from '@ant-design/icons-vue/CalendarOutlined';
|
2019-04-20 04:34:24 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-01-12 03:33:27 +00:00
|
|
|
import interopDefault from '../_util/interopDefault';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import {
|
|
|
|
hasProp,
|
|
|
|
getOptionProps,
|
|
|
|
initDefaultProps,
|
2020-07-19 15:02:34 +00:00
|
|
|
getComponent,
|
2019-01-12 03:33:27 +00:00
|
|
|
isValidElement,
|
|
|
|
} from '../_util/props-util';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { formatDate } from './utils';
|
2018-03-16 03:19:02 +00:00
|
|
|
|
|
|
|
// export const PickerProps = {
|
|
|
|
// value?: moment.Moment;
|
|
|
|
// prefixCls: string;
|
|
|
|
// }
|
2019-01-12 03:33:27 +00:00
|
|
|
export default function createPicker(TheCalendar, props) {
|
2018-03-16 03:19:02 +00:00
|
|
|
return {
|
2020-07-22 09:47:08 +00:00
|
|
|
inheritAttrs: false,
|
2018-03-17 13:38:29 +00:00
|
|
|
props: initDefaultProps(props, {
|
|
|
|
allowClear: true,
|
|
|
|
showToday: true,
|
|
|
|
}),
|
2018-03-16 03:19:02 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-07-22 09:47:08 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
};
|
2019-04-20 04:34:24 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
const value = this.value || this.defaultValue;
|
2018-04-07 06:29:59 +00:00
|
|
|
if (value && !interopDefault(moment).isMoment(value)) {
|
2018-03-16 03:19:02 +00:00
|
|
|
throw new Error(
|
2019-01-12 03:33:27 +00:00
|
|
|
'The value/defaultValue of DatePicker or MonthPicker must be ' + 'a moment object',
|
|
|
|
);
|
2018-03-16 03:19:02 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
sValue: value,
|
|
|
|
showDate: value,
|
2018-12-08 08:47:05 +00:00
|
|
|
_open: !!this.open,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
open(val) {
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
const state = {};
|
|
|
|
state._open = val;
|
2018-12-08 08:47:05 +00:00
|
|
|
if ('value' in props && !val && props.value !== this.showDate) {
|
2019-01-12 03:33:27 +00:00
|
|
|
state.showDate = props.value;
|
2018-12-08 08:47:05 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState(state);
|
2018-12-08 08:47:05 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
value(val) {
|
|
|
|
const state = {};
|
|
|
|
state.sValue = val;
|
2018-12-08 08:47:05 +00:00
|
|
|
if (val !== this.sValue) {
|
2019-01-12 03:33:27 +00:00
|
|
|
state.showDate = val;
|
2018-12-08 08:47:05 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState(state);
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
2019-09-10 10:57:08 +00:00
|
|
|
_open(val, oldVal) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (!hasProp(this, 'open') && oldVal && !val) {
|
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2019-04-20 04:34:24 +00:00
|
|
|
},
|
2018-03-16 03:19:02 +00:00
|
|
|
methods: {
|
2020-07-22 09:47:08 +00:00
|
|
|
saveInput(node) {
|
|
|
|
this.input = node;
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
clearSelection(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.handleChange(null);
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleChange(value) {
|
2018-03-16 03:19:02 +00:00
|
|
|
if (!hasProp(this, 'value')) {
|
|
|
|
this.setState({
|
|
|
|
sValue: value,
|
|
|
|
showDate: value,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-16 03:19:02 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$emit('change', value, formatDate(value, this.format));
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleCalendarChange(value) {
|
|
|
|
this.setState({ showDate: value });
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
handleOpenChange(open) {
|
|
|
|
const props = getOptionProps(this);
|
2018-12-08 08:47:05 +00:00
|
|
|
if (!('open' in props)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState({ _open: open });
|
2018-12-08 08:47:05 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$emit('openChange', open);
|
2018-12-08 08:47:05 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
2020-07-23 07:15:38 +00:00
|
|
|
this.input?.focus();
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
2020-07-23 07:15:38 +00:00
|
|
|
this.input?.blur();
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
renderFooter(...args) {
|
2020-07-22 09:47:08 +00:00
|
|
|
const { $slots, _prefixCls: prefixCls } = this;
|
|
|
|
const renderExtraFooter = this.renderExtraFooter || $slots.renderExtraFooter;
|
2020-03-07 11:45:13 +00:00
|
|
|
return renderExtraFooter ? (
|
|
|
|
<div class={`${prefixCls}-footer-extra`}>
|
|
|
|
{typeof renderExtraFooter === 'function'
|
|
|
|
? renderExtraFooter(...args)
|
|
|
|
: renderExtraFooter}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onMouseEnter(e) {
|
|
|
|
this.$emit('mouseenter', e);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onMouseLeave(e) {
|
|
|
|
this.$emit('mouseleave', e);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-07-22 09:47:08 +00:00
|
|
|
const { $slots } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
const { sValue: value, showDate, _open: open } = this.$data;
|
2020-07-19 15:02:34 +00:00
|
|
|
let suffixIcon = getComponent(this, 'suffixIcon');
|
2019-01-12 03:33:27 +00:00
|
|
|
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
|
2020-07-22 09:47:08 +00:00
|
|
|
const props = omit({ ...getOptionProps(this), ...this.$attrs }, ['onChange']);
|
2019-04-20 04:34:24 +00:00
|
|
|
|
2020-04-26 08:37:15 +00:00
|
|
|
const { prefixCls: customizePrefixCls, locale, localeCode, inputReadOnly } = props;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-20 04:34:24 +00:00
|
|
|
const prefixCls = getPrefixCls('calendar', customizePrefixCls);
|
|
|
|
this._prefixCls = prefixCls;
|
|
|
|
|
2020-07-22 09:47:08 +00:00
|
|
|
const dateRender = props.dateRender || $slots.dateRender;
|
|
|
|
const monthCellContentRender = props.monthCellContentRender || $slots.monthCellContentRender;
|
2019-01-12 03:33:27 +00:00
|
|
|
const placeholder = 'placeholder' in props ? props.placeholder : locale.lang.placeholder;
|
2018-03-16 03:19:02 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const disabledTime = props.showTime ? props.disabledTime : null;
|
2018-03-16 03:19:02 +00:00
|
|
|
|
|
|
|
const calendarClassName = classNames({
|
|
|
|
[`${prefixCls}-time`]: props.showTime,
|
|
|
|
[`${prefixCls}-month`]: MonthCalendar === TheCalendar,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-16 03:19:02 +00:00
|
|
|
|
|
|
|
if (value && localeCode) {
|
2019-01-12 03:33:27 +00:00
|
|
|
value.locale(localeCode);
|
2018-03-16 03:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 09:47:08 +00:00
|
|
|
const pickerProps = {};
|
|
|
|
const calendarProps = {};
|
2019-01-12 03:33:27 +00:00
|
|
|
const pickerStyle = {};
|
2018-03-16 03:19:02 +00:00
|
|
|
if (props.showTime) {
|
|
|
|
// fix https://github.com/ant-design/ant-design/issues/1902
|
2020-07-22 09:47:08 +00:00
|
|
|
calendarProps.onSelect = this.handleChange;
|
2020-03-07 11:45:13 +00:00
|
|
|
pickerStyle.minWidth = '195px';
|
2018-03-16 03:19:02 +00:00
|
|
|
} else {
|
2020-07-22 09:47:08 +00:00
|
|
|
pickerProps.onChange = this.handleChange;
|
2018-03-16 03:19:02 +00:00
|
|
|
}
|
|
|
|
if ('mode' in props) {
|
2020-07-22 09:47:08 +00:00
|
|
|
calendarProps.mode = props.mode;
|
2018-03-16 03:19:02 +00:00
|
|
|
}
|
2020-07-22 09:47:08 +00:00
|
|
|
const theCalendarProps = {
|
|
|
|
...calendarProps,
|
|
|
|
disabledDate: props.disabledDate,
|
|
|
|
disabledTime,
|
|
|
|
locale: locale.lang,
|
|
|
|
timePicker: props.timePicker,
|
|
|
|
defaultValue: props.defaultPickerValue || interopDefault(moment)(),
|
|
|
|
dateInputPlaceholder: placeholder,
|
|
|
|
prefixCls,
|
|
|
|
dateRender,
|
|
|
|
format: props.format,
|
|
|
|
showToday: props.showToday,
|
|
|
|
monthCellContentRender,
|
|
|
|
renderFooter: this.renderFooter,
|
|
|
|
value: showDate,
|
|
|
|
inputReadOnly,
|
|
|
|
onOk: props.onOk,
|
|
|
|
onPanelChange: props.onPanelChange,
|
|
|
|
onChange: this.handleCalendarChange,
|
2018-03-17 13:38:29 +00:00
|
|
|
class: calendarClassName,
|
2020-07-22 09:47:08 +00:00
|
|
|
};
|
|
|
|
const calendar = <TheCalendar {...theCalendarProps} vSlots={$slots} />;
|
2018-03-16 03:19:02 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const clearIcon =
|
|
|
|
!props.disabled && props.allowClear && value ? (
|
2020-03-28 07:45:37 +00:00
|
|
|
<CloseCircleFilled class={`${prefixCls}-picker-clear`} onClick={this.clearSelection} />
|
2019-01-12 03:33:27 +00:00
|
|
|
) : null;
|
2018-03-16 03:19:02 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const inputIcon = (suffixIcon &&
|
|
|
|
(isValidElement(suffixIcon) ? (
|
|
|
|
cloneElement(suffixIcon, {
|
|
|
|
class: `${prefixCls}-picker-icon`,
|
|
|
|
})
|
|
|
|
) : (
|
|
|
|
<span class={`${prefixCls}-picker-icon`}>{suffixIcon}</span>
|
2020-03-28 07:45:37 +00:00
|
|
|
))) || <CalendarOutlined class={`${prefixCls}-picker-icon`} />;
|
2018-12-08 08:47:05 +00:00
|
|
|
|
2018-03-16 03:19:02 +00:00
|
|
|
const input = ({ value: inputValue }) => (
|
|
|
|
<div>
|
|
|
|
<input
|
2020-07-22 09:47:08 +00:00
|
|
|
ref={this.saveInput}
|
2018-03-16 03:19:02 +00:00
|
|
|
disabled={props.disabled}
|
2020-07-22 09:47:08 +00:00
|
|
|
onFocus={props.onFocus}
|
|
|
|
onBlur={props.onBlur}
|
2020-07-17 09:13:30 +00:00
|
|
|
readonly
|
2020-03-07 11:45:13 +00:00
|
|
|
value={formatDate(inputValue, this.format)}
|
2018-03-16 03:19:02 +00:00
|
|
|
placeholder={placeholder}
|
|
|
|
class={props.pickerInputClass}
|
2020-07-17 09:13:30 +00:00
|
|
|
tabindex={props.tabindex}
|
2020-03-07 11:45:13 +00:00
|
|
|
name={this.name}
|
2018-03-16 03:19:02 +00:00
|
|
|
/>
|
|
|
|
{clearIcon}
|
2018-12-08 08:47:05 +00:00
|
|
|
{inputIcon}
|
2018-03-16 03:19:02 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-16 03:19:02 +00:00
|
|
|
const vcDatePickerProps = {
|
2020-07-22 09:47:08 +00:00
|
|
|
...props,
|
|
|
|
...pickerProps,
|
|
|
|
calendar,
|
|
|
|
value,
|
|
|
|
prefixCls: `${prefixCls}-picker-container`,
|
|
|
|
open,
|
|
|
|
onOpenChange: this.handleOpenChange,
|
2018-03-16 03:19:02 +00:00
|
|
|
style: props.popupStyle,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-16 03:19:02 +00:00
|
|
|
return (
|
|
|
|
<span
|
2020-07-22 09:47:08 +00:00
|
|
|
id={props.id}
|
|
|
|
class={classNames(props.class, props.pickerClass)}
|
|
|
|
style={{ ...pickerStyle, ...props.style }}
|
2020-07-17 09:13:30 +00:00
|
|
|
// tabindex={props.disabled ? -1 : 0}
|
2018-05-24 10:49:04 +00:00
|
|
|
// onFocus={focus}
|
|
|
|
// onBlur={blur}
|
2018-09-05 13:28:54 +00:00
|
|
|
onMouseenter={this.onMouseEnter}
|
|
|
|
onMouseleave={this.onMouseLeave}
|
2018-03-16 03:19:02 +00:00
|
|
|
>
|
2020-07-23 07:15:38 +00:00
|
|
|
<VcDatePicker
|
|
|
|
{...vcDatePickerProps}
|
|
|
|
vSlots={{ default: input, ...$slots }}
|
|
|
|
></VcDatePicker>
|
2018-03-16 03:19:02 +00:00
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-16 03:19:02 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-16 03:19:02 +00:00
|
|
|
}
|