ant-design-vue/components/vc-time-picker/Header.jsx

201 lines
5.4 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import moment from 'moment';
import { getComponentFromProp } from '../_util/props-util';
import { isIE, isIE9 } from '../_util/env';
2018-03-07 22:21:55 +08:00
2018-03-08 23:02:04 +08:00
const Header = {
mixins: [BaseMixin],
props: {
2018-03-07 22:21:55 +08:00
format: PropTypes.string,
prefixCls: PropTypes.string,
disabledDate: PropTypes.func,
placeholder: PropTypes.string,
clearText: PropTypes.string,
value: PropTypes.object,
2018-03-08 23:02:04 +08:00
inputReadOnly: PropTypes.bool.def(false),
2018-03-07 22:21:55 +08:00
hourOptions: PropTypes.array,
minuteOptions: PropTypes.array,
secondOptions: PropTypes.array,
disabledHours: PropTypes.func,
disabledMinutes: PropTypes.func,
disabledSeconds: PropTypes.func,
2018-03-08 23:02:04 +08:00
// onChange: PropTypes.func,
// onClear: PropTypes.func,
// onEsc: PropTypes.func,
2018-03-07 22:21:55 +08:00
allowEmpty: PropTypes.bool,
defaultOpenValue: PropTypes.object,
currentSelectPanel: PropTypes.string,
focusOnOpen: PropTypes.bool,
2018-03-08 23:02:04 +08:00
// onKeyDown: PropTypes.func,
clearIcon: PropTypes.any,
2018-03-08 23:02:04 +08:00
},
2019-01-12 11:33:27 +08:00
data() {
const { value, format } = this;
2018-03-08 23:02:04 +08:00
return {
2019-01-12 11:33:27 +08:00
str: (value && value.format(format)) || '',
2018-03-07 22:21:55 +08:00
invalid: false,
2019-01-12 11:33:27 +08:00
};
2018-03-08 23:02:04 +08:00
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
mounted() {
2018-03-08 23:02:04 +08:00
if (this.focusOnOpen) {
2018-03-07 22:21:55 +08:00
// Wait one frame for the panel to be positioned before focusing
2019-01-12 11:33:27 +08:00
const requestAnimationFrame = window.requestAnimationFrame || window.setTimeout;
2018-03-07 22:21:55 +08:00
requestAnimationFrame(() => {
2019-01-12 11:33:27 +08:00
this.$refs.input.focus();
this.$refs.input.select();
});
2018-03-07 22:21:55 +08:00
}
2018-03-08 23:02:04 +08:00
},
watch: {
2019-01-12 11:33:27 +08:00
$props: {
handler: function(nextProps) {
const { value, format } = nextProps;
2018-03-07 22:21:55 +08:00
this.setState({
2019-01-12 11:33:27 +08:00
str: (value && value.format(format)) || '',
2018-03-08 23:02:04 +08:00
invalid: false,
2019-01-12 11:33:27 +08:00
});
2018-03-08 23:02:04 +08:00
},
deep: true,
},
},
methods: {
2019-01-12 11:33:27 +08:00
onInputChange(event) {
if (event.target.composing) return;
2019-01-12 11:33:27 +08:00
const str = event.target.value;
// https://github.com/vueComponent/ant-design-vue/issues/92
if (isIE && !isIE9 && this.str === str) {
return;
}
2018-03-08 23:02:04 +08:00
this.setState({
str,
2019-01-12 11:33:27 +08:00
});
2018-03-08 23:02:04 +08:00
const {
2019-01-12 11:33:27 +08:00
format,
hourOptions,
minuteOptions,
secondOptions,
disabledHours,
disabledMinutes,
disabledSeconds,
allowEmpty,
2018-03-08 23:02:04 +08:00
value: originalValue,
2019-01-12 11:33:27 +08:00
} = this;
2018-03-08 23:02:04 +08:00
if (str) {
2019-01-12 11:33:27 +08:00
const value = this.getProtoValue().clone();
const parsed = moment(str, format, true);
2018-03-08 23:02:04 +08:00
if (!parsed.isValid()) {
this.setState({
invalid: true,
2019-01-12 11:33:27 +08:00
});
return;
2018-03-08 23:02:04 +08:00
}
2019-01-12 11:33:27 +08:00
value
.hour(parsed.hour())
.minute(parsed.minute())
.second(parsed.second());
2018-03-07 22:21:55 +08:00
2018-03-08 23:02:04 +08:00
// if time value not allowed, response warning.
if (
hourOptions.indexOf(value.hour()) < 0 ||
2019-01-12 11:33:27 +08:00
minuteOptions.indexOf(value.minute()) < 0 ||
secondOptions.indexOf(value.second()) < 0
2018-03-08 23:02:04 +08:00
) {
this.setState({
invalid: true,
2019-01-12 11:33:27 +08:00
});
return;
2018-03-08 23:02:04 +08:00
}
2018-03-07 22:21:55 +08:00
2018-03-08 23:02:04 +08:00
// if time value is disabled, response warning.
2019-01-12 11:33:27 +08:00
const disabledHourOptions = disabledHours();
const disabledMinuteOptions = disabledMinutes(value.hour());
const disabledSecondOptions = disabledSeconds(value.hour(), value.minute());
2018-03-08 23:02:04 +08:00
if (
(disabledHourOptions && disabledHourOptions.indexOf(value.hour()) >= 0) ||
2019-01-12 11:33:27 +08:00
(disabledMinuteOptions && disabledMinuteOptions.indexOf(value.minute()) >= 0) ||
(disabledSecondOptions && disabledSecondOptions.indexOf(value.second()) >= 0)
2018-03-08 23:02:04 +08:00
) {
this.setState({
invalid: true,
2019-01-12 11:33:27 +08:00
});
return;
2018-03-08 23:02:04 +08:00
}
2018-03-07 22:21:55 +08:00
2018-03-08 23:02:04 +08:00
if (originalValue) {
if (
originalValue.hour() !== value.hour() ||
2019-01-12 11:33:27 +08:00
originalValue.minute() !== value.minute() ||
originalValue.second() !== value.second()
2018-03-08 23:02:04 +08:00
) {
2019-01-12 11:33:27 +08:00
// keep other fields for rc-calendar
const changedValue = originalValue.clone();
changedValue.hour(value.hour());
changedValue.minute(value.minute());
changedValue.second(value.second());
this.__emit('change', changedValue);
2018-03-08 23:02:04 +08:00
}
} else if (originalValue !== value) {
2019-01-12 11:33:27 +08:00
this.__emit('change', value);
2018-03-07 22:21:55 +08:00
}
2018-03-08 23:02:04 +08:00
} else if (allowEmpty) {
2019-01-12 11:33:27 +08:00
this.__emit('change', null);
2018-03-08 23:02:04 +08:00
} else {
this.setState({
invalid: true,
2019-01-12 11:33:27 +08:00
});
return;
2018-03-07 22:21:55 +08:00
}
2018-03-08 23:02:04 +08:00
2018-03-07 22:21:55 +08:00
this.setState({
2018-03-08 23:02:04 +08:00
invalid: false,
2019-01-12 11:33:27 +08:00
});
2018-03-08 23:02:04 +08:00
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
onKeyDown(e) {
2018-03-08 23:02:04 +08:00
if (e.keyCode === 27) {
2019-01-12 11:33:27 +08:00
this.__emit('esc');
2018-03-08 23:02:04 +08:00
}
2019-01-12 11:33:27 +08:00
this.__emit('keydown', e);
2018-03-08 23:02:04 +08:00
},
2019-01-12 11:33:27 +08:00
getProtoValue() {
return this.value || this.defaultOpenValue;
2018-03-08 23:02:04 +08:00
},
2019-01-12 11:33:27 +08:00
getInput() {
const { prefixCls, placeholder, inputReadOnly, invalid, str } = this;
2019-01-12 11:33:27 +08:00
const invalidClass = invalid ? `${prefixCls}-input-invalid` : '';
2018-03-08 23:02:04 +08:00
return (
<input
class={`${prefixCls}-input ${invalidClass}`}
2019-01-12 11:33:27 +08:00
ref="input"
2018-03-08 23:02:04 +08:00
onKeydown={this.onKeyDown}
value={str}
2018-03-08 23:02:04 +08:00
placeholder={placeholder}
onInput={this.onInputChange}
readOnly={!!inputReadOnly}
2019-11-14 19:19:24 +08:00
{...{
directives: [
{
name: 'ant-input',
},
],
}}
2018-03-08 23:02:04 +08:00
/>
2019-01-12 11:33:27 +08:00
);
2018-03-08 23:02:04 +08:00
},
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
render() {
const { prefixCls } = this;
2019-05-28 11:37:38 +08:00
return <div class={`${prefixCls}-input-wrap`}>{this.getInput()}</div>;
2018-03-08 23:02:04 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-03-19 10:16:27 +08:00
2019-01-12 11:33:27 +08:00
export default Header;