2016-11-15 10:43:00 +08:00
|
|
|
<template>
|
2016-11-15 19:17:54 +08:00
|
|
|
<div :class="classes" v-clickoutside="handleClose">
|
2017-01-16 17:46:51 +08:00
|
|
|
<div :class="[prefixCls + '-rel']" @click="toggleOpen">
|
|
|
|
<slot>
|
|
|
|
<i-input
|
|
|
|
readonly
|
|
|
|
:disabled="disabled"
|
2017-03-06 17:30:39 +08:00
|
|
|
v-model="displayRender"
|
2017-01-16 17:46:51 +08:00
|
|
|
:size="size"
|
|
|
|
:placeholder="placeholder"></i-input>
|
2017-03-06 17:30:39 +08:00
|
|
|
<Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.native.stop="clearSelect"></Icon>
|
2017-01-16 17:46:51 +08:00
|
|
|
<Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
|
|
|
|
</slot>
|
|
|
|
</div>
|
2017-03-06 17:30:39 +08:00
|
|
|
<transition name="slide-up">
|
|
|
|
<Drop v-show="visible">
|
|
|
|
<div>
|
|
|
|
<Caspanel
|
|
|
|
ref="caspanel"
|
|
|
|
:prefix-cls="prefixCls"
|
|
|
|
:data="data"
|
|
|
|
:disabled="disabled"
|
|
|
|
:change-on-select="changeOnSelect"
|
|
|
|
:trigger="trigger"></Caspanel>
|
|
|
|
</div>
|
|
|
|
</Drop>
|
|
|
|
</transition>
|
2016-11-15 11:24:28 +08:00
|
|
|
</div>
|
2016-11-15 10:43:00 +08:00
|
|
|
</template>
|
|
|
|
<script>
|
2016-11-15 11:24:28 +08:00
|
|
|
import iInput from '../input/input.vue';
|
2017-03-06 17:30:39 +08:00
|
|
|
import Drop from '../select/dropdown.vue';
|
2016-11-15 19:17:54 +08:00
|
|
|
import Icon from '../icon/icon.vue';
|
|
|
|
import Caspanel from './caspanel.vue';
|
2016-11-15 11:24:28 +08:00
|
|
|
import clickoutside from '../../directives/clickoutside';
|
2016-11-15 19:17:54 +08:00
|
|
|
import { oneOf } from '../../utils/assist';
|
2017-03-06 17:30:39 +08:00
|
|
|
import Emitter from '../../mixins/emitter';
|
2016-11-15 11:24:28 +08:00
|
|
|
|
|
|
|
const prefixCls = 'ivu-cascader';
|
|
|
|
|
2016-11-15 10:43:00 +08:00
|
|
|
export default {
|
2017-03-02 17:40:19 +08:00
|
|
|
name: 'Cascader',
|
2017-03-06 17:30:39 +08:00
|
|
|
mixins: [ Emitter ],
|
|
|
|
components: { iInput, Drop, Icon, Caspanel },
|
2016-11-15 19:17:54 +08:00
|
|
|
directives: { clickoutside },
|
2016-11-15 10:43:00 +08:00
|
|
|
props: {
|
2016-11-15 11:24:28 +08:00
|
|
|
data: {
|
|
|
|
type: Array,
|
|
|
|
default () {
|
2016-12-25 22:49:42 +08:00
|
|
|
return [];
|
2016-11-15 11:24:28 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
2016-11-15 11:24:28 +08:00
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
clearable: {
|
|
|
|
type: Boolean,
|
2016-11-15 19:17:54 +08:00
|
|
|
default: true
|
2016-11-15 11:24:28 +08:00
|
|
|
},
|
|
|
|
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(' / ');
|
2016-11-15 11:24:28 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 10:43:00 +08:00
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
2016-11-15 11:24:28 +08:00
|
|
|
prefixCls: prefixCls,
|
2016-11-15 19:17:54 +08:00
|
|
|
visible: false,
|
|
|
|
selected: [],
|
2017-01-10 19:29:59 +08:00
|
|
|
tmpSelected: [],
|
2017-03-06 17:30:39 +08:00
|
|
|
updatingValue: false, // to fix set value in changeOnSelect type
|
|
|
|
currentValue: this.value
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
2016-11-15 10:43:00 +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 () {
|
2017-04-10 22:43:24 +08:00
|
|
|
return this.currentValue && this.currentValue.length && this.clearable && !this.disabled;
|
2016-11-15 19:17:54 +08:00
|
|
|
},
|
2016-11-15 11:24:28 +08:00
|
|
|
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);
|
2016-11-15 11:24:28 +08:00
|
|
|
}
|
2016-11-15 10:43:00 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2016-11-15 19:17:54 +08:00
|
|
|
clearSelect () {
|
2017-04-10 22:43:24 +08:00
|
|
|
if (this.disabled) return false;
|
2017-03-06 17:30:39 +08:00
|
|
|
const oldVal = JSON.stringify(this.currentValue);
|
|
|
|
this.currentValue = this.selected = this.tmpSelected = [];
|
2016-11-16 10:07:03 +08:00
|
|
|
this.handleClose();
|
2017-03-06 17:30:39 +08:00
|
|
|
this.emitValue(this.currentValue, oldVal);
|
|
|
|
// this.$broadcast('on-clear');
|
|
|
|
this.broadcast('Caspanel', 'on-clear');
|
2016-11-15 19:17:54 +08:00
|
|
|
},
|
|
|
|
handleClose () {
|
|
|
|
this.visible = false;
|
|
|
|
},
|
2017-01-16 17:46:51 +08:00
|
|
|
toggleOpen () {
|
2017-02-24 21:04:39 -06:00
|
|
|
if (this.disabled) return false;
|
2017-01-16 17:46:51 +08:00
|
|
|
if (this.visible) {
|
|
|
|
this.handleClose();
|
|
|
|
} else {
|
|
|
|
this.onFocus();
|
|
|
|
}
|
|
|
|
},
|
2016-11-15 19:17:54 +08:00
|
|
|
onFocus () {
|
|
|
|
this.visible = true;
|
2017-03-06 17:30:39 +08:00
|
|
|
if (!this.currentValue.length) {
|
|
|
|
this.broadcast('Caspanel', 'on-clear');
|
2016-11-16 10:07:03 +08:00
|
|
|
}
|
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) {
|
2017-03-06 17:30:39 +08:00
|
|
|
this.broadcast('Caspanel', 'on-find-selected', {
|
|
|
|
value: this.currentValue
|
|
|
|
});
|
2016-11-16 10:07:03 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
emitValue (val, oldVal) {
|
|
|
|
if (JSON.stringify(val) !== oldVal) {
|
2017-03-06 17:30:39 +08:00
|
|
|
this.$emit('on-change', this.currentValue, JSON.parse(JSON.stringify(this.selected)));
|
2017-03-29 09:33:31 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.dispatch('FormItem', 'on-form-change', {
|
|
|
|
value: this.currentValue,
|
|
|
|
selected: JSON.parse(JSON.stringify(this.selected))
|
|
|
|
});
|
2017-03-09 11:14:40 +08:00
|
|
|
});
|
2016-11-16 10:07:03 +08:00
|
|
|
}
|
2016-11-15 19:17:54 +08:00
|
|
|
}
|
|
|
|
},
|
2017-03-06 17:30:39 +08:00
|
|
|
mounted () {
|
2016-11-16 10:07:03 +08:00
|
|
|
this.updateSelected(true);
|
2017-03-06 17:30:39 +08:00
|
|
|
this.$on('on-result-change', (params) => {
|
|
|
|
// lastValue: is click the final val
|
|
|
|
// fromInit: is this emit from update value
|
|
|
|
const lastValue = params.lastValue;
|
|
|
|
const changeOnSelect = params.changeOnSelect;
|
|
|
|
const fromInit = params.fromInit;
|
|
|
|
|
2016-11-15 23:23:16 +08:00
|
|
|
if (lastValue || changeOnSelect) {
|
2017-03-06 17:30:39 +08:00
|
|
|
const oldVal = JSON.stringify(this.currentValue);
|
2016-11-15 23:23:16 +08:00
|
|
|
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;
|
2017-03-06 17:30:39 +08:00
|
|
|
this.currentValue = newVal;
|
|
|
|
this.emitValue(this.currentValue, oldVal);
|
2016-11-15 23:23:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastValue && !fromInit) {
|
|
|
|
this.handleClose();
|
|
|
|
}
|
2017-03-06 17:30:39 +08:00
|
|
|
});
|
2016-11-15 23:23:16 +08:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
visible (val) {
|
|
|
|
if (val) {
|
2017-03-06 17:30:39 +08:00
|
|
|
if (this.currentValue.length) {
|
2016-11-15 23:23:16 +08:00
|
|
|
this.updateSelected();
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 09:17:45 +08:00
|
|
|
this.$emit('on-visible-change', val);
|
2016-12-15 20:33:16 +08:00
|
|
|
},
|
2017-03-06 17:30:39 +08:00
|
|
|
value (val) {
|
|
|
|
this.currentValue = val;
|
2017-03-27 09:41:08 +08:00
|
|
|
if (!val.length) this.selected = [];
|
2017-03-06 17:30:39 +08:00
|
|
|
},
|
|
|
|
currentValue () {
|
|
|
|
this.$emit('input', this.currentValue);
|
2017-01-10 19:29:59 +08:00
|
|
|
if (this.updatingValue) {
|
|
|
|
this.updatingValue = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.updateSelected(true);
|
2017-04-01 10:22:34 +08:00
|
|
|
},
|
|
|
|
data () {
|
|
|
|
this.$nextTick(() => this.updateSelected());
|
2016-11-15 23:23:16 +08:00
|
|
|
}
|
2016-11-15 10:43:00 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
|
|
|
</script>
|