iview/src/components/date-picker/picker.vue

148 lines
4 KiB
Vue
Raw Normal View History

2016-12-12 10:37:52 +08:00
<template>
2016-12-12 20:34:28 +08:00
<div
:class="[prefixCls]"
v-clickoutside="handleClose"
@mouseenter="handleMouseenter"
@mouseleave="handleMouseleave">
<i-input
v-el:reference
:class="[prefixCls + '-editor']"
:readonly="!editable || readonly"
:disabled="disabled"
:size="size"
:placeholder="placeholder"
:value.sync="visualValue"
@on-focus="handleFocus"
@on-blur="handleBlur"
@on-click="handleIconClick"
:icon="iconType"></i-input>
<Drop v-show="visible" :placement="placement" transition="slide-up" v-ref:drop>
<div v-el:picker></div>
</Drop>
</div>
2016-12-12 10:37:52 +08:00
</template>
<script>
2016-12-12 20:34:28 +08:00
import Vue from 'vue';
import iInput from '../../components/input/input.vue';
import Drop from '../../components/select/dropdown.vue';
import clickoutside from '../../directives/clickoutside';
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-date-picker';
const DEFAULT_FORMATS = {
date: 'yyyy-MM-dd',
month: 'yyyy-MM',
datetime: 'yyyy-MM-dd HH:mm:ss',
time: 'HH:mm:ss',
timerange: 'HH:mm:ss',
daterange: 'yyyy-MM-dd',
datetimerange: 'yyyy-MM-dd HH:mm:ss'
};
const PLACEMENT_MAP = {
left: 'bottom-start',
center: 'bottom-center',
right: 'bottom-end'
};
2016-12-12 10:37:52 +08:00
export default {
2016-12-12 20:34:28 +08:00
components: { iInput, Drop },
directives: { clickoutside },
props: {
format: {
type: String
},
readonly: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
editable: {
type: Boolean,
default: true
},
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
placeholder: {
type: String,
default: ''
},
align: {
validator (value) {
return oneOf(value, ['left', 'center', 'right']);
},
default: 'left'
},
options: {
type: Object
}
},
2016-12-12 10:37:52 +08:00
data () {
2016-12-12 20:34:28 +08:00
return {
prefixCls: prefixCls,
showClose: false,
visualValue: '',
visible: false,
picker: null
}
},
computed: {
iconType () {
return this.showClose ? 'ios-close' : 'ios-calendar-outline';
},
placement () {
return PLACEMENT_MAP[this.align];
}
},
methods: {
handleClose () {
this.visible = false;
},
handleFocus () {
this.visible = true;
},
handleBlur () {
},
handleMouseenter () {
if (this.readonly || this.disabled) return;
if (this.visualValue) {
this.showClose = true;
}
},
handleMouseleave () {
this.showClose = false;
},
handleIconClick () {
},
showPicker () {
if (!this.picker) {
this.picker = new Vue(this.panel).$mount(this.$els.picker);
}
}
},
watch: {
visible (val) {
if (val) {
this.showPicker();
this.$refs.drop.update();
} else {
this.$refs.drop.destroy();
}
}
2016-12-12 10:37:52 +08:00
},
2016-12-12 20:34:28 +08:00
beforeDestroy () {
if (this.picker) {
this.picker.$destroy();
}
}
2016-12-12 10:37:52 +08:00
}
</script>