update TimePicker

update TimePicker
This commit is contained in:
梁灏 2016-12-27 17:41:35 +08:00
parent 2dbbd7dea6
commit af713093ab
6 changed files with 62 additions and 10 deletions

View file

@ -45,7 +45,8 @@
}, },
data () { data () {
return { return {
prefixCls: prefixCls prefixCls: prefixCls,
compiled: false
}; };
}, },
computed: { computed: {
@ -155,11 +156,16 @@
data[type] = cell.text; data[type] = cell.text;
this.$emit('on-change', data); this.$emit('on-change', data);
const from = this.$els[type].scrollTop; // const from = this.$els[type].scrollTop;
const to = 24 * this.getScrollIndex(type, cell.text); // const to = 24 * this.getScrollIndex(type, cell.text);
scrollTop(this.$els[type], from, to, 500); // scrollTop(this.$els[type], from, to, 500);
} }
}, },
scroll (type, index) {
const from = this.$els[type].scrollTop;
const to = 24 * this.getScrollIndex(type, index);
scrollTop(this.$els[type], from, to, 500);
},
getScrollIndex (type, index) { getScrollIndex (type, index) {
const Type = firstUpperCase(type); const Type = firstUpperCase(type);
const disabled = this[`disabled${Type}`]; const disabled = this[`disabled${Type}`];
@ -181,8 +187,23 @@
}); });
} }
}, },
watch: {
hours (val) {
if (!this.compiled) return;
this.scroll('hours', val);
},
minutes (val) {
if (!this.compiled) return;
this.scroll('minutes', val);
},
seconds (val) {
if (!this.compiled) return;
this.scroll('seconds', val);
}
},
compiled () { compiled () {
this.updateScroll(); this.updateScroll();
this.$nextTick(() => this.compiled = true);
} }
}; };
</script> </script>

View file

@ -75,7 +75,6 @@
this.hours = ''; this.hours = '';
this.minutes = ''; this.minutes = '';
this.seconds = ''; this.seconds = '';
this.$emit('on-pick', '');
}, },
handleChange (date, emit = true) { handleChange (date, emit = true) {
if (date.hours !== undefined) { if (date.hours !== undefined) {

View file

@ -30,7 +30,7 @@
import Drop from '../../components/select/dropdown.vue'; import Drop from '../../components/select/dropdown.vue';
import clickoutside from '../../directives/clickoutside'; import clickoutside from '../../directives/clickoutside';
import { oneOf } from '../../utils/assist'; import { oneOf } from '../../utils/assist';
import { formatDate, parseDate, initTimeDate } from './util'; import { formatDate, parseDate } from './util';
const prefixCls = 'ivu-date-picker'; const prefixCls = 'ivu-date-picker';
@ -294,6 +294,30 @@
} }
correctDate = parser(correctValue, format); correctDate = parser(correctValue, format);
} else if (type === 'time') {
const parsedDate = parseDate(value, format);
if (parsedDate instanceof Date) {
if (this.disabledHours.length || this.disabledMinutes.length || this.disabledSeconds.length) {
const hours = parsedDate.getHours();
const minutes = parsedDate.getMinutes();
const seconds = parsedDate.getSeconds();
if ((this.disabledHours.length && this.disabledHours.indexOf(hours) > -1) ||
(this.disabledMinutes.length && this.disabledMinutes.indexOf(minutes) > -1) ||
(this.disabledSeconds.length && this.disabledSeconds.indexOf(seconds) > -1)) {
correctValue = oldValue;
} else {
correctValue = formatDate(parsedDate, format);
}
} else {
correctValue = formatDate(parsedDate, format);
}
} else {
correctValue = oldValue;
}
correctDate = parseDate(correctValue, format);
} else { } else {
const parsedDate = parseDate(value, format); const parsedDate = parseDate(value, format);

View file

@ -76,7 +76,7 @@ export const nextMonth = function(src) {
return new Date(src.getTime()); return new Date(src.getTime());
}; };
export const initTimeDate = function (time) { export const initTimeDate = function () {
const date = new Date(); const date = new Date();
date.setHours(0); date.setHours(0);
date.setMinutes(0); date.setMinutes(0);

View file

@ -144,7 +144,7 @@ export function scrollTop(el, from = 0, to, duration = 500) {
function (callback) { function (callback) {
return window.setTimeout(callback, 1000/60); return window.setTimeout(callback, 1000/60);
} }
) );
} }
const difference = Math.abs(from - to); const difference = Math.abs(from - to);
const step = Math.ceil(difference / duration * 50); const step = Math.ceil(difference / duration * 50);

View file

@ -6,7 +6,7 @@
<template> <template>
<row> <row>
<i-col span="12"> <i-col span="12">
<date-picker type="date" placeholder="选择日期" style="width: 200px"></date-picker> <date-picker type="date" placeholder="选择日期" style="width: 200px" @on-ok="ok" confirm @on-clear="clear"></date-picker>
</i-col> </i-col>
<i-col span="12"> <i-col span="12">
<date-picker type="daterange" placement="bottom-end" placeholder="选择日期" style="width: 200px"></date-picker> <date-picker type="daterange" placement="bottom-end" placeholder="选择日期" style="width: 200px"></date-picker>
@ -20,6 +20,8 @@
:hide-disabled-options="false" :hide-disabled-options="false"
:disabled-hours="[1,2,5,10,11]" :disabled-hours="[1,2,5,10,11]"
@on-change="c" @on-change="c"
@on-ok="ok"
@on-clear="clear"
style="width: 168px"></time-picker> style="width: 168px"></time-picker>
</i-col> </i-col>
</row> </row>
@ -34,8 +36,14 @@
}, },
methods: { methods: {
c (s) { c (s) {
console.log(s) console.log(1,s);
this.value = s; this.value = s;
},
ok () {
console.log('ok');
},
clear () {
console.log('clear');
} }
} }
} }