refactor: time-picker to ts

This commit is contained in:
Amour1688 2020-10-20 12:55:09 +08:00
parent e28b004883
commit 7e5ee6757c
2 changed files with 18 additions and 21 deletions

View File

@ -1,5 +1,5 @@
import omit from 'omit.js'; import omit from 'omit.js';
import { inject, provide } from 'vue'; import { App, defineComponent, inject, provide } from 'vue';
import VcTimePicker from '../vc-time-picker'; import VcTimePicker from '../vc-time-picker';
import LocaleReceiver from '../locale-provider/LocaleReceiver'; import LocaleReceiver from '../locale-provider/LocaleReceiver';
import BaseMixin from '../_util/BaseMixin'; import BaseMixin from '../_util/BaseMixin';
@ -8,13 +8,8 @@ import warning from '../_util/warning';
import ClockCircleOutlined from '@ant-design/icons-vue/ClockCircleOutlined'; import ClockCircleOutlined from '@ant-design/icons-vue/ClockCircleOutlined';
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled'; import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
import enUS from './locale/en_US'; import enUS from './locale/en_US';
import { import { hasProp, getOptionProps, getComponent, isValidElement } from '../_util/props-util';
initDefaultProps, import initDefaultProps from '../_util/props-util/initDefaultProps';
hasProp,
getOptionProps,
getComponent,
isValidElement,
} from '../_util/props-util';
import { cloneElement } from '../_util/vnode'; import { cloneElement } from '../_util/vnode';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
import { import {
@ -23,8 +18,9 @@ import {
momentToString, momentToString,
TimeOrTimesType, TimeOrTimesType,
} from '../_util/moment-util'; } from '../_util/moment-util';
import { tuple } from '../_util/type';
export function generateShowHourMinuteSecond(format) { export function generateShowHourMinuteSecond(format: string) {
// Ref: http://momentjs.com/docs/#/parsing/string-format/ // Ref: http://momentjs.com/docs/#/parsing/string-format/
return { return {
showHour: format.indexOf('H') > -1 || format.indexOf('h') > -1 || format.indexOf('k') > -1, showHour: format.indexOf('H') > -1 || format.indexOf('h') > -1 || format.indexOf('k') > -1,
@ -34,7 +30,7 @@ export function generateShowHourMinuteSecond(format) {
} }
export const TimePickerProps = () => ({ export const TimePickerProps = () => ({
size: PropTypes.oneOf(['large', 'default', 'small']), size: PropTypes.oneOf(tuple('large', 'default', 'small')),
value: TimeOrTimesType, value: TimeOrTimesType,
defaultValue: TimeOrTimesType, defaultValue: TimeOrTimesType,
open: PropTypes.looseBool, open: PropTypes.looseBool,
@ -58,7 +54,7 @@ export const TimePickerProps = () => ({
clearText: PropTypes.string, clearText: PropTypes.string,
defaultOpenValue: PropTypes.object, defaultOpenValue: PropTypes.object,
popupClassName: PropTypes.string, popupClassName: PropTypes.string,
popupStyle: PropTypes.object, popupStyle: PropTypes.style,
suffixIcon: PropTypes.any, suffixIcon: PropTypes.any,
align: PropTypes.object, align: PropTypes.object,
placement: PropTypes.any, placement: PropTypes.any,
@ -76,14 +72,13 @@ export const TimePickerProps = () => ({
onBlur: PropTypes.func, onBlur: PropTypes.func,
onKeydown: PropTypes.func, onKeydown: PropTypes.func,
onOpenChange: PropTypes.func, onOpenChange: PropTypes.func,
'onUpdate:value': PropTypes.func,
'onUpdate:open': PropTypes.func,
}); });
const TimePicker = { const TimePicker = defineComponent({
name: 'ATimePicker', name: 'ATimePicker',
inheritAttrs: false, inheritAttrs: false,
mixins: [BaseMixin], mixins: [BaseMixin],
emits: ['update:value', 'update:open', 'change', 'openChange', 'focus', 'blur', 'keydown'],
props: initDefaultProps(TimePickerProps(), { props: initDefaultProps(TimePickerProps(), {
align: { align: {
offset: [0, -2], offset: [0, -2],
@ -103,6 +98,8 @@ const TimePicker = {
}, },
setup() { setup() {
return { return {
popupRef: null,
timePickerRef: null,
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
}; };
}, },
@ -174,14 +171,14 @@ const TimePicker = {
}, },
focus() { focus() {
this.timePickerRef.focus(); (this.timePickerRef as any).focus();
}, },
blur() { blur() {
this.timePickerRef.blur(); (this.timePickerRef as any).blur();
}, },
renderInputIcon(prefixCls) { renderInputIcon(prefixCls: string) {
let suffixIcon = getComponent(this, 'suffixIcon'); let suffixIcon = getComponent(this, 'suffixIcon');
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon; suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
const clockIcon = (suffixIcon && const clockIcon = (suffixIcon &&
@ -193,7 +190,7 @@ const TimePicker = {
return <span class={`${prefixCls}-icon`}>{clockIcon}</span>; return <span class={`${prefixCls}-icon`}>{clockIcon}</span>;
}, },
renderClearIcon(prefixCls) { renderClearIcon(prefixCls: string) {
const clearIcon = getComponent(this, 'clearIcon'); const clearIcon = getComponent(this, 'clearIcon');
const clearIconPrefixCls = `${prefixCls}-clear`; const clearIconPrefixCls = `${prefixCls}-clear`;
@ -216,7 +213,7 @@ const TimePicker = {
const format = this.getDefaultFormat(); const format = this.getDefaultFormat();
const pickerClassName = { const pickerClassName = {
[className]: className, [className as string]: className,
[`${prefixCls}-${size}`]: !!size, [`${prefixCls}-${size}`]: !!size,
}; };
const tempAddon = getComponent(this, 'addon', {}, false); const tempAddon = getComponent(this, 'addon', {}, false);
@ -262,10 +259,10 @@ const TimePicker = {
/> />
); );
}, },
}; });
/* istanbul ignore next */ /* istanbul ignore next */
TimePicker.install = function(app) { TimePicker.install = function(app: App) {
app.component(TimePicker.name, TimePicker); app.component(TimePicker.name, TimePicker);
return app; return app;
}; };