2019-01-12 11:33:27 +08:00
|
|
|
import TimePickerPanel from '../vc-time-picker/Panel';
|
|
|
|
import classNames from 'classnames';
|
2020-03-07 19:45:13 +08:00
|
|
|
import * as moment from 'moment';
|
2019-01-12 11:33:27 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
|
|
import { generateShowHourMinuteSecond } from '../time-picker';
|
|
|
|
import enUS from './locale/en_US';
|
2020-03-07 19:45:13 +08:00
|
|
|
import interopDefault from '../_util/interopDefault';
|
2020-01-19 16:58:38 +08:00
|
|
|
import { getOptionProps, initDefaultProps, getListeners } from '../_util/props-util';
|
2019-04-20 12:34:24 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-03-07 19:45:13 +08:00
|
|
|
import warning from '../_util/warning';
|
|
|
|
|
|
|
|
function checkValidate(value, propName) {
|
|
|
|
const values = Array.isArray(value) ? value : [value];
|
|
|
|
values.forEach(val => {
|
|
|
|
if (!val) return;
|
|
|
|
|
|
|
|
warning(
|
|
|
|
!interopDefault(moment).isMoment(val) || val.isValid(),
|
|
|
|
'DatePicker',
|
|
|
|
`\`${propName}\` provides invalidate moment time. If you want to set empty value, use \`null\` instead.`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2019-04-20 12:34:24 +08:00
|
|
|
|
|
|
|
const DEFAULT_FORMAT = {
|
|
|
|
date: 'YYYY-MM-DD',
|
|
|
|
dateTime: 'YYYY-MM-DD HH:mm:ss',
|
|
|
|
week: 'gggg-wo',
|
|
|
|
month: 'YYYY-MM',
|
|
|
|
};
|
|
|
|
|
|
|
|
const LOCALE_FORMAT_MAPPING = {
|
|
|
|
date: 'dateFormat',
|
|
|
|
dateTime: 'dateTimeFormat',
|
|
|
|
week: 'weekFormat',
|
|
|
|
month: 'monthFormat',
|
|
|
|
};
|
2018-03-15 21:40:34 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getColumns({ showHour, showMinute, showSecond, use12Hours }) {
|
|
|
|
let column = 0;
|
2018-03-15 21:40:34 +08:00
|
|
|
if (showHour) {
|
2019-01-12 11:33:27 +08:00
|
|
|
column += 1;
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|
|
|
|
if (showMinute) {
|
2019-01-12 11:33:27 +08:00
|
|
|
column += 1;
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|
|
|
|
if (showSecond) {
|
2019-01-12 11:33:27 +08:00
|
|
|
column += 1;
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|
|
|
|
if (use12Hours) {
|
2019-01-12 11:33:27 +08:00
|
|
|
column += 1;
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return column;
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|
|
|
|
|
2019-04-20 12:34:24 +08:00
|
|
|
export default function wrapPicker(Picker, props, pickerType) {
|
2018-03-15 21:40:34 +08:00
|
|
|
return {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: Picker.name,
|
2018-03-17 21:38:29 +08:00
|
|
|
props: initDefaultProps(props, {
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
popupStyle: {},
|
|
|
|
locale: {},
|
|
|
|
}),
|
2018-04-14 21:22:09 +08:00
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change',
|
|
|
|
},
|
2019-04-20 12:34:24 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-04-20 12:34:24 +08:00
|
|
|
},
|
2019-07-04 18:12:52 +08:00
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
savePopupRef: this.savePopupRef,
|
|
|
|
};
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { autoFocus, disabled, value, defaultValue } = this;
|
|
|
|
checkValidate(defaultValue, 'defaultValue');
|
|
|
|
checkValidate(value, 'value');
|
2018-03-15 21:40:34 +08:00
|
|
|
if (autoFocus && !disabled) {
|
|
|
|
this.$nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.focus();
|
|
|
|
});
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
watch: {
|
|
|
|
value(val) {
|
|
|
|
checkValidate(val, 'value');
|
|
|
|
},
|
|
|
|
},
|
2018-03-15 21:40:34 +08:00
|
|
|
methods: {
|
2020-03-07 19:45:13 +08:00
|
|
|
getDefaultLocale() {
|
|
|
|
const result = {
|
|
|
|
...enUS,
|
|
|
|
...this.locale,
|
|
|
|
};
|
|
|
|
result.lang = {
|
|
|
|
...result.lang,
|
|
|
|
...(this.locale || {}).lang,
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2019-07-04 18:12:52 +08:00
|
|
|
savePopupRef(ref) {
|
|
|
|
this.popupRef = ref;
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
handleOpenChange(open) {
|
|
|
|
this.$emit('openChange', open);
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleFocus(e) {
|
|
|
|
this.$emit('focus', e);
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleBlur(e) {
|
|
|
|
this.$emit('blur', e);
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleMouseEnter(e) {
|
|
|
|
this.$emit('mouseenter', e);
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleMouseLeave(e) {
|
|
|
|
this.$emit('mouseleave', e);
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
focus() {
|
|
|
|
this.$refs.picker.focus();
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
blur() {
|
|
|
|
this.$refs.picker.blur();
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
renderPicker(locale, localeCode) {
|
|
|
|
const props = getOptionProps(this);
|
2019-04-20 12:34:24 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
inputPrefixCls: customizeInputPrefixCls,
|
2020-03-07 19:45:13 +08:00
|
|
|
getCalendarContainer,
|
2019-04-20 12:34:24 +08:00
|
|
|
size,
|
|
|
|
showTime,
|
|
|
|
disabled,
|
|
|
|
format,
|
|
|
|
} = props;
|
|
|
|
const mergedPickerType = showTime ? `${pickerType}Time` : pickerType;
|
|
|
|
const mergedFormat =
|
2019-05-28 11:37:38 +08:00
|
|
|
format ||
|
|
|
|
locale[LOCALE_FORMAT_MAPPING[mergedPickerType]] ||
|
|
|
|
DEFAULT_FORMAT[mergedPickerType];
|
2019-04-20 12:34:24 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
const { getPrefixCls, getPopupContainer: getContextPopupContainer } = this.configProvider;
|
|
|
|
const getPopupContainer = getCalendarContainer || getContextPopupContainer;
|
2019-04-20 12:34:24 +08:00
|
|
|
const prefixCls = getPrefixCls('calendar', customizePrefixCls);
|
|
|
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
|
|
|
|
2018-03-15 21:40:34 +08:00
|
|
|
const pickerClass = classNames(`${prefixCls}-picker`, {
|
|
|
|
[`${prefixCls}-picker-${size}`]: !!size,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-15 21:40:34 +08:00
|
|
|
const pickerInputClass = classNames(`${prefixCls}-picker-input`, inputPrefixCls, {
|
|
|
|
[`${inputPrefixCls}-lg`]: size === 'large',
|
|
|
|
[`${inputPrefixCls}-sm`]: size === 'small',
|
|
|
|
[`${inputPrefixCls}-disabled`]: disabled,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-15 21:40:34 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const timeFormat = (showTime && showTime.format) || 'HH:mm:ss';
|
2018-03-15 21:40:34 +08:00
|
|
|
const vcTimePickerProps = {
|
|
|
|
...generateShowHourMinuteSecond(timeFormat),
|
|
|
|
format: timeFormat,
|
2019-01-12 11:33:27 +08:00
|
|
|
use12Hours: showTime && showTime.use12Hours,
|
|
|
|
};
|
|
|
|
const columns = getColumns(vcTimePickerProps);
|
|
|
|
const timePickerCls = `${prefixCls}-time-picker-column-${columns}`;
|
2018-03-15 21:40:34 +08:00
|
|
|
const timePickerPanelProps = {
|
|
|
|
props: {
|
|
|
|
...vcTimePickerProps,
|
|
|
|
...showTime,
|
|
|
|
prefixCls: `${prefixCls}-time-picker`,
|
|
|
|
placeholder: locale.timePickerLocale.placeholder,
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
},
|
|
|
|
class: timePickerCls,
|
2020-03-07 19:45:13 +08:00
|
|
|
on: {
|
|
|
|
esc: () => {},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
const timePicker = showTime ? <TimePickerPanel {...timePickerPanelProps} /> : null;
|
2018-03-15 21:40:34 +08:00
|
|
|
const pickerProps = {
|
|
|
|
props: {
|
|
|
|
...props,
|
2020-03-07 19:45:13 +08:00
|
|
|
getCalendarContainer: getPopupContainer,
|
2019-04-20 12:34:24 +08:00
|
|
|
format: mergedFormat,
|
2018-03-15 21:40:34 +08:00
|
|
|
pickerClass,
|
|
|
|
pickerInputClass,
|
|
|
|
locale,
|
|
|
|
localeCode,
|
|
|
|
timePicker,
|
|
|
|
},
|
|
|
|
on: {
|
2020-01-19 16:58:38 +08:00
|
|
|
...getListeners(this),
|
2018-03-15 21:40:34 +08:00
|
|
|
openChange: this.handleOpenChange,
|
|
|
|
focus: this.handleFocus,
|
|
|
|
blur: this.handleBlur,
|
2018-09-05 21:28:54 +08:00
|
|
|
mouseenter: this.handleMouseEnter,
|
|
|
|
mouseleave: this.handleMouseLeave,
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
ref: 'picker',
|
2018-03-17 21:38:29 +08:00
|
|
|
scopedSlots: this.$scopedSlots || {},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-15 21:40:34 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<Picker {...pickerProps}>
|
|
|
|
{this.$slots &&
|
|
|
|
Object.keys(this.$slots).map(key => (
|
|
|
|
<template slot={key} key={key}>
|
|
|
|
{this.$slots[key]}
|
|
|
|
</template>
|
|
|
|
))}
|
2018-03-18 22:05:55 +08:00
|
|
|
</Picker>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2018-03-15 21:40:34 +08:00
|
|
|
return (
|
|
|
|
<LocaleReceiver
|
2019-01-12 11:33:27 +08:00
|
|
|
componentName="DatePicker"
|
2018-03-15 21:40:34 +08:00
|
|
|
defaultLocale={this.getDefaultLocale}
|
2019-01-12 11:33:27 +08:00
|
|
|
scopedSlots={{ default: this.renderPicker }}
|
2018-03-15 21:40:34 +08:00
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-15 21:40:34 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-15 21:40:34 +08:00
|
|
|
}
|