iview/src/components/cascader/cascader.vue

211 lines
6.7 KiB
Vue
Raw Normal View History

<template>
2016-11-15 19:17:54 +08:00
<div :class="classes" v-clickoutside="handleClose">
<div :class="[prefixCls + '-rel']" @click="toggleOpen">
<slot>
<i-input
readonly
:disabled="disabled"
:value.sync="displayRender"
:size="size"
:placeholder="placeholder"></i-input>
<Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.stop="clearSelect"></Icon>
<Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
</slot>
</div>
2016-11-15 19:17:54 +08:00
<Dropdown v-show="visible" transition="slide-up">
<div>
<Caspanel
2016-11-15 23:23:16 +08:00
v-ref:caspanel
2016-11-15 19:17:54 +08:00
:prefix-cls="prefixCls"
:data.sync="data"
:disabled="disabled"
2016-11-15 23:23:16 +08:00
:change-on-select="changeOnSelect"
:trigger="trigger"></Caspanel>
2016-11-15 19:17:54 +08:00
</div>
</Dropdown>
</div>
</template>
<script>
import iInput from '../input/input.vue';
import Dropdown from '../select/dropdown.vue';
2016-11-15 19:17:54 +08:00
import Icon from '../icon/icon.vue';
import Caspanel from './caspanel.vue';
import clickoutside from '../../directives/clickoutside';
2016-11-15 19:17:54 +08:00
import { oneOf } from '../../utils/assist';
const prefixCls = 'ivu-cascader';
export default {
2016-11-15 19:17:54 +08:00
components: { iInput, Dropdown, Icon, Caspanel },
directives: { clickoutside },
props: {
data: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
}
},
value: {
2016-11-16 11:01:54 +08:00
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
2016-11-16 11:01:54 +08:00
}
},
disabled: {
type: Boolean,
default: false
},
clearable: {
type: Boolean,
2016-11-15 19:17:54 +08:00
default: true
},
placeholder: {
type: String,
default: '请选择'
},
size: {
validator (value) {
return oneOf(value, ['small', 'large']);
}
},
trigger: {
validator (value) {
return oneOf(value, ['click', 'hover']);
},
default: 'click'
},
changeOnSelect: {
type: Boolean,
default: false
},
renderFormat: {
type: Function,
2016-11-16 12:44:47 +08:00
default (label) {
2016-11-15 23:23:16 +08:00
return label.join(' / ');
}
}
},
data () {
return {
prefixCls: prefixCls,
2016-11-15 19:17:54 +08:00
visible: false,
selected: [],
2017-01-10 19:29:59 +08:00
tmpSelected: [],
updatingValue: false // to fix set value in changeOnSelect type
2016-12-25 22:49:42 +08:00
};
},
computed: {
2016-11-15 19:17:54 +08:00
classes () {
return [
`${prefixCls}`,
{
2016-11-16 10:07:03 +08:00
[`${prefixCls}-show-clear`]: this.showCloseIcon,
2016-11-16 12:44:47 +08:00
[`${prefixCls}-visible`]: this.visible,
[`${prefixCls}-disabled`]: this.disabled
2016-11-15 19:17:54 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-11-15 19:17:54 +08:00
},
showCloseIcon () {
return this.value && this.value.length && this.clearable;
},
displayRender () {
let label = [];
for (let i = 0; i < this.selected.length; i++) {
label.push(this.selected[i].label);
}
2016-11-16 10:07:03 +08:00
return this.renderFormat(label, this.selected);
}
},
methods: {
2016-11-15 19:17:54 +08:00
clearSelect () {
2016-11-16 10:07:03 +08:00
const oldVal = JSON.stringify(this.value);
this.value = this.selected = this.tmpSelected = [];
this.handleClose();
this.emitValue(this.value, oldVal);
this.$broadcast('on-clear');
2016-11-15 19:17:54 +08:00
},
handleClose () {
this.visible = false;
},
toggleOpen () {
if (this.disabled) return false;
if (this.visible) {
this.handleClose();
} else {
this.onFocus();
}
},
2016-11-15 19:17:54 +08:00
onFocus () {
this.visible = true;
2016-11-16 10:07:03 +08:00
if (!this.value.length) {
this.$broadcast('on-clear');
}
2016-11-15 19:17:54 +08:00
},
updateResult (result) {
2016-11-15 23:23:16 +08:00
this.tmpSelected = result;
},
2016-11-16 10:07:03 +08:00
updateSelected (init = false) {
if (!this.changeOnSelect || init) {
this.$broadcast('on-find-selected', this.value);
}
},
emitValue (val, oldVal) {
if (JSON.stringify(val) !== oldVal) {
this.$emit('on-change', this.value, JSON.parse(JSON.stringify(this.selected)));
2017-01-04 15:01:58 +08:00
this.$dispatch('on-form-change', this.value, JSON.parse(JSON.stringify(this.selected)));
2016-11-16 10:07:03 +08:00
}
2016-11-15 19:17:54 +08:00
}
},
ready () {
2016-11-16 10:07:03 +08:00
this.updateSelected(true);
2016-11-15 23:23:16 +08:00
},
events: {
// lastValue: is click the final val
// fromInit: is this emit from update value
'on-result-change' (lastValue, changeOnSelect, fromInit) {
if (lastValue || changeOnSelect) {
const oldVal = JSON.stringify(this.value);
this.selected = this.tmpSelected;
let newVal = [];
this.selected.forEach((item) => {
newVal.push(item.value);
});
2016-11-15 19:17:54 +08:00
2016-11-16 10:07:03 +08:00
if (!fromInit) {
2017-01-10 19:29:59 +08:00
this.updatingValue = true;
2016-11-16 10:07:03 +08:00
this.value = newVal;
this.emitValue(this.value, oldVal);
2016-11-15 23:23:16 +08:00
}
}
if (lastValue && !fromInit) {
this.handleClose();
}
2017-01-04 15:01:58 +08:00
},
'on-form-blur' () {
return false;
},
'on-form-change' () {
return false;
2016-11-15 23:23:16 +08:00
}
},
watch: {
visible (val) {
if (val) {
if (this.value.length) {
this.updateSelected();
}
}
2016-12-15 20:33:16 +08:00
},
value () {
2017-01-10 19:29:59 +08:00
if (this.updatingValue) {
this.updatingValue = false;
return;
}
this.updateSelected(true);
2016-11-15 23:23:16 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>