feat: update time-picker

pull/802/head
wangxueliang 6 years ago
parent e6754c5c52
commit 48f2facb44

@ -1,9 +1,20 @@
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import VcTimePicker from '../../vc-time-picker/TimePicker'; import VcTimePicker from '../../vc-time-picker/TimePicker';
import TimePicker from '..'; import TimePicker from '..';
import moment from 'moment';
import focusTest from '../../../tests/shared/focusTest'; import focusTest from '../../../tests/shared/focusTest';
describe('TimePicker', () => { describe('TimePicker', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
afterEach(() => {
errorSpy.mockReset();
});
afterAll(() => {
errorSpy.mockRestore();
});
focusTest(TimePicker); focusTest(TimePicker);
it('renders addon correctly', () => { it('renders addon correctly', () => {
@ -20,4 +31,26 @@ describe('TimePicker', () => {
}); });
expect(addonWrapper.html()).toMatchSnapshot(); expect(addonWrapper.html()).toMatchSnapshot();
}); });
it('allowEmpty deprecated', () => {
mount({
render() {
return <TimePicker allowEmpty />;
},
});
expect(errorSpy).toBeCalledWith(
'Warning: `allowEmpty` in TimePicker is deprecated. Please use `allowClear` instead.',
);
});
it('not render clean icon when allowClear is false', () => {
const wrapper = mount(
{
render() {
return <TimePicker defaultValue={moment('2000-01-01 00:00:00')} allowClear={false} />;
},
}
);
expect(wrapper.html()).toMatchSnapshot();
});
}); });

@ -3,7 +3,7 @@
| Property | Description | Type | Default | | Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- | | -------- | ----------- | ---- | ------- |
| addon | some addon to timepicker panel bottom | slot \| slot-scope | - | | addon | some addon to timepicker panel bottom | slot \| slot-scope | - |
| allowEmpty | allow clearing text | boolean | true | | allowClear | allow clearing text | boolean | true |
| autoFocus | get focus when component mounted | boolean | false | | autoFocus | get focus when component mounted | boolean | false |
| clearText | clear tooltip of icon | string | clear | | clearText | clear tooltip of icon | string | clear |
| defaultOpenValue | default open panel value, used to set utcOffset,locale if value/defaultValue absent | [moment](http://momentjs.com/) | moment() | | defaultOpenValue | default open panel value, used to set utcOffset,locale if value/defaultValue absent | [moment](http://momentjs.com/) | moment() |
@ -21,6 +21,7 @@
| open(.sync) | whether to popup panel | boolean | false | | open(.sync) | whether to popup panel | boolean | false |
| placeholder | display when there's no value | string | "Select a time" | | placeholder | display when there's no value | string | "Select a time" |
| popupClassName | className of panel | string | '' | | popupClassName | className of panel | string | '' |
| popupStyle | style of panel | object | - |
| secondStep | interval between seconds in picker | number | 1 | | secondStep | interval between seconds in picker | number | 1 |
| suffixIcon | The custom suffix icon | string \| VNode \| slot | - | | suffixIcon | The custom suffix icon | string \| VNode \| slot | - |
| use12Hours | display as 12 hours format, with default format `h:mm:ss a` | boolean | false | | use12Hours | display as 12 hours format, with default format `h:mm:ss a` | boolean | false |

@ -1,9 +1,11 @@
import * as moment from 'moment'; import * as moment from 'moment';
import omit from 'omit.js';
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 defaultLocale from './locale/en_US'; import defaultLocale from './locale/en_US';
import BaseMixin from '../_util/BaseMixin'; import BaseMixin from '../_util/BaseMixin';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import warning from '../_util/warning';
import Icon from '../icon'; import Icon from '../icon';
import interopDefault from '../_util/interopDefault'; import interopDefault from '../_util/interopDefault';
import { import {
@ -14,6 +16,7 @@ import {
isValidElement, isValidElement,
} from '../_util/props-util'; } from '../_util/props-util';
import { cloneElement } from '../_util/vnode'; import { cloneElement } from '../_util/vnode';
import { ConfigConsumerProps } from '../config-provider';
export function generateShowHourMinuteSecond(format) { export function generateShowHourMinuteSecond(format) {
// Ref: http://momentjs.com/docs/#/parsing/string-format/ // Ref: http://momentjs.com/docs/#/parsing/string-format/
@ -53,10 +56,12 @@ export const TimePickerProps = () => ({
minuteStep: PropTypes.number, minuteStep: PropTypes.number,
secondStep: PropTypes.number, secondStep: PropTypes.number,
allowEmpty: PropTypes.bool, allowEmpty: PropTypes.bool,
allowClear: PropTypes.bool,
inputReadOnly: PropTypes.bool, inputReadOnly: PropTypes.bool,
clearText: PropTypes.string, clearText: PropTypes.string,
defaultOpenValue: PropTypes.object, defaultOpenValue: PropTypes.object,
popupClassName: PropTypes.string, popupClassName: PropTypes.string,
popupStyle: PropTypes.object,
suffixIcon: PropTypes.any, suffixIcon: PropTypes.any,
align: PropTypes.object, align: PropTypes.object,
placement: PropTypes.any, placement: PropTypes.any,
@ -69,7 +74,6 @@ const TimePicker = {
name: 'ATimePicker', name: 'ATimePicker',
mixins: [BaseMixin], mixins: [BaseMixin],
props: initDefaultProps(TimePickerProps(), { props: initDefaultProps(TimePickerProps(), {
prefixCls: 'ant-time-picker',
align: { align: {
offset: [0, -2], offset: [0, -2],
}, },
@ -94,6 +98,10 @@ const TimePicker = {
if (value && !interopDefault(moment).isMoment(value)) { if (value && !interopDefault(moment).isMoment(value)) {
throw new Error('The value/defaultValue of TimePicker must be a moment object, '); throw new Error('The value/defaultValue of TimePicker must be a moment object, ');
} }
warning(
!hasProp(this, 'allowEmpty'),
'`allowEmpty` in TimePicker is deprecated. Please use `allowClear` instead.',
);
return { return {
sValue: value, sValue: value,
}; };
@ -135,23 +143,15 @@ const TimePicker = {
return 'HH:mm:ss'; return 'HH:mm:ss';
}, },
renderTimePicker(locale) { getAllowClear() {
const props = getOptionProps(this); const { allowClear, allowEmpty } = this.$props;
delete props.defaultValue; if ('allowClear' in this.$props) {
return allowClear;
}
return allowEmpty;
},
const format = this.getDefaultFormat(); renderInputIcon(prefixCls) {
const className = {
[`${props.prefixCls}-${props.size}`]: !!props.size,
};
const tempAddon = getComponentFromProp(this, 'addon', {}, false);
const addon = panel => {
return tempAddon ? (
<div class={`${props.prefixCls}-panel-addon`}>
{typeof tempAddon === 'function' ? tempAddon(panel) : tempAddon}
</div>
) : null;
};
const { prefixCls, getPopupContainer } = props;
let suffixIcon = getComponentFromProp(this, 'suffixIcon'); let suffixIcon = getComponentFromProp(this, 'suffixIcon');
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon; suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
const clockIcon = (suffixIcon && const clockIcon = (suffixIcon &&
@ -163,25 +163,56 @@ const TimePicker = {
<span class={`${prefixCls}-clock-icon`}>{suffixIcon}</span> <span class={`${prefixCls}-clock-icon`}>{suffixIcon}</span>
))) || <Icon type="clock-circle" class={`${prefixCls}-clock-icon`} theme="outlined" />; ))) || <Icon type="clock-circle" class={`${prefixCls}-clock-icon`} theme="outlined" />;
const inputIcon = <span class={`${prefixCls}-icon`}>{clockIcon}</span>; return <span class={`${prefixCls}-icon`}>{clockIcon}</span>;
},
const clearIcon = ( renderClearIcon(prefixCls) {
<Icon type="close-circle" class={`${prefixCls}-panel-clear-btn-icon`} theme="filled" /> const clearIcon = <Icon type="close-circle" class={`${prefixCls}-clear`} theme="filled" />;
); return clearIcon;
},
renderTimePicker(locale) {
let props = getOptionProps(this);
props = omit(props, ['defaultValue', 'suffixIcon', 'allowEmpty', 'allowClear']);
const {
prefixCls: customizePrefixCls,
getPopupContainer,
placeholder,
size,
} = props;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
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);
const { getPopupContainer: getContextPopupContainer } = this.configProvider; const { getPopupContainer: getContextPopupContainer } = this.configProvider;
const timeProps = { const timeProps = {
props: { props: {
...generateShowHourMinuteSecond(format), ...generateShowHourMinuteSecond(format),
...props, ...props,
prefixCls,
getPopupContainer: getPopupContainer || getContextPopupContainer, getPopupContainer: getPopupContainer || getContextPopupContainer,
format, format,
value: this.sValue, value: this.sValue,
placeholder: props.placeholder === undefined ? locale.placeholder : props.placeholder, placeholder: placeholder === undefined ? locale.placeholder : placeholder,
addon, addon: pickerAddon,
inputIcon, inputIcon,
clearIcon, clearIcon,
}, },
class: className, class: pickerClassName,
ref: 'timePicker', ref: 'timePicker',
on: { on: {
...this.$listeners, ...this.$listeners,

@ -4,7 +4,7 @@
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| addon | 选择框底部显示自定义的内容 | slot \| slot-scope | 无 | | addon | 选择框底部显示自定义的内容 | slot \| slot-scope | 无 |
| allowEmpty | 是否展示清除按钮 | boolean | true | | allowClear | 是否展示清除按钮 | boolean | true |
| autoFocus | 自动获取焦点 | boolean | false | | autoFocus | 自动获取焦点 | boolean | false |
| clearText | 清除按钮的提示文案 | string | clear | | clearText | 清除按钮的提示文案 | string | clear |
| defaultOpenValue | 当 defaultValue/value 不存在时,可以设置面板打开时默认选中的值 | [moment](http://momentjs.com/) | moment() | | defaultOpenValue | 当 defaultValue/value 不存在时,可以设置面板打开时默认选中的值 | [moment](http://momentjs.com/) | moment() |
@ -22,6 +22,7 @@
| open(.sync) | 面板是否打开 | boolean | false | | open(.sync) | 面板是否打开 | boolean | false |
| placeholder | 没有值的时候显示的内容 | string | "请选择时间" | | placeholder | 没有值的时候显示的内容 | string | "请选择时间" |
| popupClassName | 弹出层类名 | string | '' | | popupClassName | 弹出层类名 | string | '' |
| popupStyle | 弹出层样式对象 | object | - |
| secondStep | 秒选项间隔 | number | 1 | | secondStep | 秒选项间隔 | number | 1 |
| suffixIcon | 自定义的选择框后缀图标 | string \| VNode \| slot | - | | suffixIcon | 自定义的选择框后缀图标 | string \| VNode \| slot | - |
| use12Hours | 使用 12 小时制,为 true 时 `format` 默认为 `h:mm:ss a` | boolean | false | | use12Hours | 使用 12 小时制,为 true 时 `format` 默认为 `h:mm:ss a` | boolean | false |

Loading…
Cancel
Save