2019-04-17 01:00:27 +00:00
|
|
|
import omit from 'omit.js';
|
2019-01-12 03:33:27 +00:00
|
|
|
import VcTimePicker from '../vc-time-picker';
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2019-04-17 01:00:27 +00:00
|
|
|
import warning from '../_util/warning';
|
2020-03-29 05:17:25 +00:00
|
|
|
import ClockCircleOutlined from '@ant-design/icons-vue/ClockCircleOutlined';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
2020-03-07 11:45:13 +00:00
|
|
|
import enUS from './locale/en_US';
|
2019-01-12 03:33:27 +00:00
|
|
|
import {
|
|
|
|
initDefaultProps,
|
|
|
|
hasProp,
|
|
|
|
getOptionProps,
|
|
|
|
getComponentFromProp,
|
|
|
|
isValidElement,
|
2020-01-19 08:58:38 +00:00
|
|
|
getListeners,
|
2019-01-12 03:33:27 +00:00
|
|
|
} from '../_util/props-util';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2019-04-17 01:00:27 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-08-28 02:50:19 +00:00
|
|
|
import Base from '../base';
|
2020-04-29 10:44:11 +00:00
|
|
|
import {
|
|
|
|
checkValidate,
|
|
|
|
stringToMoment,
|
|
|
|
momentToString,
|
|
|
|
TimeOrTimesType,
|
|
|
|
} from '../_util/moment-util';
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export function generateShowHourMinuteSecond(format) {
|
2018-03-07 14:21:55 +00:00
|
|
|
// Ref: http://momentjs.com/docs/#/parsing/string-format/
|
|
|
|
return {
|
2019-01-12 03:33:27 +00:00
|
|
|
showHour: format.indexOf('H') > -1 || format.indexOf('h') > -1 || format.indexOf('k') > -1,
|
2018-03-07 14:21:55 +00:00
|
|
|
showMinute: format.indexOf('m') > -1,
|
|
|
|
showSecond: format.indexOf('s') > -1,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-07 14:21:55 +00:00
|
|
|
}
|
2020-04-29 10:44:11 +00:00
|
|
|
|
2018-03-16 03:19:02 +00:00
|
|
|
export const TimePickerProps = () => ({
|
2018-03-08 15:02:04 +00:00
|
|
|
size: PropTypes.oneOf(['large', 'default', 'small']),
|
2020-04-29 10:44:11 +00:00
|
|
|
value: TimeOrTimesType,
|
|
|
|
defaultValue: TimeOrTimesType,
|
2018-03-08 15:02:04 +00:00
|
|
|
open: PropTypes.bool,
|
|
|
|
format: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
hideDisabledOptions: PropTypes.bool,
|
|
|
|
disabledHours: PropTypes.func,
|
|
|
|
disabledMinutes: PropTypes.func,
|
|
|
|
disabledSeconds: PropTypes.func,
|
|
|
|
getPopupContainer: PropTypes.func,
|
|
|
|
use12Hours: PropTypes.bool,
|
|
|
|
focusOnOpen: PropTypes.bool,
|
|
|
|
hourStep: PropTypes.number,
|
|
|
|
minuteStep: PropTypes.number,
|
|
|
|
secondStep: PropTypes.number,
|
|
|
|
allowEmpty: PropTypes.bool,
|
2019-04-17 01:00:27 +00:00
|
|
|
allowClear: PropTypes.bool,
|
2018-04-07 06:29:59 +00:00
|
|
|
inputReadOnly: PropTypes.bool,
|
2018-03-08 15:02:04 +00:00
|
|
|
clearText: PropTypes.string,
|
|
|
|
defaultOpenValue: PropTypes.object,
|
|
|
|
popupClassName: PropTypes.string,
|
2019-04-17 01:00:27 +00:00
|
|
|
popupStyle: PropTypes.object,
|
2018-12-09 04:50:26 +00:00
|
|
|
suffixIcon: PropTypes.any,
|
2018-03-08 15:02:04 +00:00
|
|
|
align: PropTypes.object,
|
|
|
|
placement: PropTypes.any,
|
|
|
|
transitionName: PropTypes.string,
|
2018-06-17 07:56:12 +00:00
|
|
|
autoFocus: PropTypes.bool,
|
|
|
|
addon: PropTypes.any,
|
2020-03-07 11:45:13 +00:00
|
|
|
clearIcon: PropTypes.any,
|
|
|
|
locale: PropTypes.object,
|
2020-04-29 10:44:11 +00:00
|
|
|
valueFormat: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
const TimePicker = {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ATimePicker',
|
2018-03-08 15:02:04 +00:00
|
|
|
mixins: [BaseMixin],
|
2018-03-16 03:19:02 +00:00
|
|
|
props: initDefaultProps(TimePickerProps(), {
|
2018-03-07 14:21:55 +00:00
|
|
|
align: {
|
|
|
|
offset: [0, -2],
|
|
|
|
},
|
|
|
|
disabled: false,
|
|
|
|
disabledHours: undefined,
|
|
|
|
disabledMinutes: undefined,
|
|
|
|
disabledSeconds: undefined,
|
|
|
|
hideDisabledOptions: false,
|
|
|
|
placement: 'bottomLeft',
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
focusOnOpen: true,
|
2020-03-07 11:45:13 +00:00
|
|
|
allowClear: true,
|
2018-03-08 15:02:04 +00:00
|
|
|
}),
|
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change',
|
|
|
|
},
|
2019-07-04 10:12:52 +00:00
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
savePopupRef: this.savePopupRef,
|
|
|
|
};
|
|
|
|
},
|
2019-01-07 12:53:42 +00:00
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-01-07 12:53:42 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2020-04-29 10:44:11 +00:00
|
|
|
const { value, defaultValue, valueFormat } = this;
|
|
|
|
|
|
|
|
checkValidate('TimePicker', defaultValue, 'defaultValue', valueFormat);
|
|
|
|
checkValidate('TimePicker', value, 'value', valueFormat);
|
2019-04-17 01:00:27 +00:00
|
|
|
warning(
|
|
|
|
!hasProp(this, 'allowEmpty'),
|
2020-03-07 11:45:13 +00:00
|
|
|
'TimePicker',
|
|
|
|
'`allowEmpty` is deprecated. Please use `allowClear` instead.',
|
2019-04-17 01:00:27 +00:00
|
|
|
);
|
2018-03-08 15:02:04 +00:00
|
|
|
return {
|
2020-04-29 10:44:11 +00:00
|
|
|
sValue: stringToMoment(value || defaultValue, valueFormat),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
value(val) {
|
2020-04-29 10:44:11 +00:00
|
|
|
checkValidate('TimePicker', val, 'value', this.valueFormat);
|
|
|
|
this.setState({ sValue: stringToMoment(val, this.valueFormat) });
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
2020-03-07 11:45:13 +00:00
|
|
|
getDefaultFormat() {
|
|
|
|
const { format, use12Hours } = this;
|
|
|
|
if (format) {
|
|
|
|
return format;
|
|
|
|
} else if (use12Hours) {
|
|
|
|
return 'h:mm:ss a';
|
|
|
|
}
|
|
|
|
return 'HH:mm:ss';
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllowClear() {
|
|
|
|
const { allowClear, allowEmpty } = this.$props;
|
|
|
|
if (hasProp(this, 'allowClear')) {
|
|
|
|
return allowClear;
|
|
|
|
}
|
|
|
|
return allowEmpty;
|
|
|
|
},
|
|
|
|
getDefaultLocale() {
|
|
|
|
const defaultLocale = {
|
|
|
|
...enUS,
|
|
|
|
...this.$props.locale,
|
|
|
|
};
|
|
|
|
return defaultLocale;
|
|
|
|
},
|
2019-07-04 10:12:52 +00:00
|
|
|
savePopupRef(ref) {
|
|
|
|
this.popupRef = ref;
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
handleChange(value) {
|
2018-03-08 15:02:04 +00:00
|
|
|
if (!hasProp(this, 'value')) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState({ sValue: value });
|
2018-03-08 15:02:04 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const { format = 'HH:mm:ss' } = this;
|
2020-04-29 10:44:11 +00:00
|
|
|
this.$emit(
|
|
|
|
'change',
|
|
|
|
this.valueFormat ? momentToString(value, this.valueFormat) : value,
|
|
|
|
(value && value.format(format)) || '',
|
|
|
|
);
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleOpenClose({ open }) {
|
|
|
|
this.$emit('openChange', open);
|
|
|
|
this.$emit('update:open', open);
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
|
|
|
this.$refs.timePicker.focus();
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
|
|
|
this.$refs.timePicker.blur();
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-04-17 01:00:27 +00:00
|
|
|
renderInputIcon(prefixCls) {
|
2019-01-12 03:33:27 +00:00
|
|
|
let suffixIcon = getComponentFromProp(this, 'suffixIcon');
|
|
|
|
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
|
|
|
|
const clockIcon = (suffixIcon &&
|
2020-03-07 11:45:13 +00:00
|
|
|
isValidElement(suffixIcon) &&
|
|
|
|
cloneElement(suffixIcon, {
|
|
|
|
class: `${prefixCls}-clock-icon`,
|
2020-03-29 05:17:25 +00:00
|
|
|
})) || <ClockCircleOutlined class={`${prefixCls}-clock-icon`} />;
|
2018-12-09 04:50:26 +00:00
|
|
|
|
2019-04-17 01:00:27 +00:00
|
|
|
return <span class={`${prefixCls}-icon`}>{clockIcon}</span>;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderClearIcon(prefixCls) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const clearIcon = getComponentFromProp(this, 'clearIcon');
|
|
|
|
const clearIconPrefixCls = `${prefixCls}-clear`;
|
|
|
|
|
|
|
|
if (clearIcon && isValidElement(clearIcon)) {
|
|
|
|
return cloneElement(clearIcon, {
|
|
|
|
class: clearIconPrefixCls,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-29 05:17:25 +00:00
|
|
|
return <CloseCircleFilled class={clearIconPrefixCls} />;
|
2019-04-17 01:00:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
renderTimePicker(locale) {
|
|
|
|
let props = getOptionProps(this);
|
|
|
|
props = omit(props, ['defaultValue', 'suffixIcon', 'allowEmpty', 'allowClear']);
|
2018-12-09 04:50:26 +00:00
|
|
|
|
2019-05-28 03:37:38 +00:00
|
|
|
const { prefixCls: customizePrefixCls, getPopupContainer, placeholder, size } = props;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-17 01:00:27 +00:00
|
|
|
const prefixCls = getPrefixCls('time-picker', customizePrefixCls);
|
|
|
|
|
|
|
|
const format = this.getDefaultFormat();
|
|
|
|
const pickerClassName = {
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
|
|
|
};
|
|
|
|
const tempAddon = getComponentFromProp(this, 'addon', {}, false);
|
|
|
|
const pickerAddon = panel => {
|
|
|
|
return tempAddon ? (
|
|
|
|
<div class={`${prefixCls}-panel-addon`}>
|
|
|
|
{typeof tempAddon === 'function' ? tempAddon(panel) : tempAddon}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
};
|
|
|
|
const inputIcon = this.renderInputIcon(prefixCls);
|
|
|
|
const clearIcon = this.renderClearIcon(prefixCls);
|
2019-01-12 03:33:27 +00:00
|
|
|
const { getPopupContainer: getContextPopupContainer } = this.configProvider;
|
2018-03-08 15:02:04 +00:00
|
|
|
const timeProps = {
|
|
|
|
props: {
|
|
|
|
...generateShowHourMinuteSecond(format),
|
|
|
|
...props,
|
2019-09-20 11:19:59 +00:00
|
|
|
allowEmpty: this.getAllowClear(),
|
2019-04-17 01:00:27 +00:00
|
|
|
prefixCls,
|
2019-01-07 12:53:42 +00:00
|
|
|
getPopupContainer: getPopupContainer || getContextPopupContainer,
|
2018-03-08 15:02:04 +00:00
|
|
|
format,
|
|
|
|
value: this.sValue,
|
2019-04-17 01:00:27 +00:00
|
|
|
placeholder: placeholder === undefined ? locale.placeholder : placeholder,
|
|
|
|
addon: pickerAddon,
|
2018-12-09 04:50:26 +00:00
|
|
|
inputIcon,
|
|
|
|
clearIcon,
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2019-04-17 01:00:27 +00:00
|
|
|
class: pickerClassName,
|
2018-03-08 15:02:04 +00:00
|
|
|
ref: 'timePicker',
|
|
|
|
on: {
|
2020-01-19 08:58:38 +00:00
|
|
|
...getListeners(this),
|
2018-03-08 15:02:04 +00:00
|
|
|
change: this.handleChange,
|
|
|
|
open: this.handleOpenClose,
|
|
|
|
close: this.handleOpenClose,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
return <VcTimePicker {...timeProps} />;
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2018-03-07 14:21:55 +00:00
|
|
|
return (
|
|
|
|
<LocaleReceiver
|
2019-01-12 03:33:27 +00:00
|
|
|
componentName="TimePicker"
|
2020-03-07 11:45:13 +00:00
|
|
|
defaultLocale={this.getDefaultLocale()}
|
2019-01-12 03:33:27 +00:00
|
|
|
scopedSlots={{ default: this.renderTimePicker }}
|
2018-03-08 15:02:04 +00:00
|
|
|
/>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2019-01-12 03:33:27 +00:00
|
|
|
TimePicker.install = function(Vue) {
|
2019-08-28 02:50:19 +00:00
|
|
|
Vue.use(Base);
|
2019-01-12 03:33:27 +00:00
|
|
|
Vue.component(TimePicker.name, TimePicker);
|
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default TimePicker;
|