iview/src/components/cascader/cascader.vue

399 lines
15 KiB
Vue
Raw Normal View History

<template>
2016-11-15 19:17:54 +08:00
<div :class="classes" v-clickoutside="handleClose">
2017-07-20 16:00:03 +08:00
<div :class="[prefixCls + '-rel']" @click="toggleOpen" ref="reference">
2017-09-19 16:45:02 +08:00
<input type="hidden" :name="name" :value="currentValue">
<slot>
<i-input
2017-09-22 15:29:50 +08:00
:element-id="elementId"
2017-05-11 17:17:44 +08:00
ref="input"
2017-05-10 09:18:54 +08:00
:readonly="!filterable"
:disabled="disabled"
2017-05-11 17:17:44 +08:00
:value="displayInputRender"
2017-05-10 18:24:30 +08:00
@on-change="handleInput"
:size="size"
2017-05-11 17:17:44 +08:00
:placeholder="inputPlaceholder"></i-input>
<div
:class="[prefixCls + '-label']"
v-show="filterable && query === ''"
@click="handleFocus">{{ displayRender }}</div>
2017-03-06 17:30:39 +08:00
<Icon type="ios-close" :class="[prefixCls + '-arrow']" v-show="showCloseIcon" @click.native.stop="clearSelect"></Icon>
<Icon type="arrow-down-b" :class="[prefixCls + '-arrow']"></Icon>
</slot>
</div>
2017-03-06 17:30:39 +08:00
<transition name="slide-up">
2017-07-20 16:00:03 +08:00
<Drop
v-show="visible"
:class="{ [prefixCls + '-transfer']: transfer }"
ref="drop"
:data-transfer="transfer"
v-transfer-dom>
2017-03-06 17:30:39 +08:00
<div>
<Caspanel
2017-05-10 18:24:30 +08:00
v-show="!filterable || (filterable && query === '')"
2017-03-06 17:30:39 +08:00
ref="caspanel"
:prefix-cls="prefixCls"
:data="data"
:disabled="disabled"
:change-on-select="changeOnSelect"
:trigger="trigger"></Caspanel>
2017-05-12 10:23:11 +08:00
<div :class="[prefixCls + '-dropdown']" v-show="filterable && query !== '' && querySelections.length">
2017-05-10 18:24:30 +08:00
<ul :class="[selectPrefixCls + '-dropdown-list']">
<li
:class="[selectPrefixCls + '-item', {
[selectPrefixCls + '-item-disabled']: item.disabled
}]"
v-for="(item, index) in querySelections"
2017-05-11 17:29:44 +08:00
@click="handleSelectItem(index)" v-html="item.display"></li>
2017-05-10 18:24:30 +08:00
</ul>
</div>
2017-05-12 10:23:11 +08:00
<ul v-show="filterable && query !== '' && !querySelections.length" :class="[prefixCls + '-not-found-tip']"><li>{{ localeNotFoundText }}</li></ul>
2017-03-06 17:30:39 +08:00
</div>
</Drop>
</transition>
</div>
</template>
<script>
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';
import clickoutside from '../../directives/clickoutside';
2017-07-20 16:00:03 +08:00
import TransferDom from '../../directives/transfer-dom';
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';
2017-05-11 17:17:44 +08:00
import Locale from '../../mixins/locale';
const prefixCls = 'ivu-cascader';
2017-05-10 18:24:30 +08:00
const selectPrefixCls = 'ivu-select';
export default {
name: 'Cascader',
2017-05-11 17:17:44 +08:00
mixins: [ Emitter, Locale ],
2017-03-06 17:30:39 +08:00
components: { iInput, Drop, Icon, Caspanel },
2017-07-20 16:00:03 +08:00
directives: { clickoutside, TransferDom },
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: {
2017-05-11 17:17:44 +08:00
type: String
},
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(' / ');
}
2017-05-09 15:46:08 +08:00
},
loadData: {
type: Function
2017-05-10 09:18:54 +08:00
},
filterable: {
type: Boolean,
default: false
2017-05-12 10:23:11 +08:00
},
notFoundText: {
type: String
2017-07-20 16:00:03 +08:00
},
transfer: {
type: Boolean,
default: false
2017-09-19 16:45:02 +08:00
},
name: {
type: String
2017-09-22 15:29:50 +08:00
},
elementId: {
type: String
}
},
data () {
return {
prefixCls: prefixCls,
2017-05-10 18:24:30 +08:00
selectPrefixCls: selectPrefixCls,
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
2017-05-10 18:24:30 +08:00
currentValue: this.value,
2017-05-18 18:23:39 +08:00
query: '',
validDataStr: '',
isLoadedChildren: false // #950
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,
2017-05-11 17:17:44 +08:00
[`${prefixCls}-size-${this.size}`]: !!this.size,
2016-11-16 12:44:47 +08:00
[`${prefixCls}-visible`]: this.visible,
2017-05-12 10:23:11 +08:00
[`${prefixCls}-disabled`]: this.disabled,
[`${prefixCls}-not-found`]: this.filterable && this.query !== '' && !this.querySelections.length
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
},
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);
2017-05-10 18:24:30 +08:00
},
2017-05-11 17:17:44 +08:00
displayInputRender () {
return this.filterable ? '' : this.displayRender;
},
localePlaceholder () {
if (this.placeholder === undefined) {
return this.t('i.select.placeholder');
} else {
return this.placeholder;
}
},
inputPlaceholder () {
return this.filterable && this.currentValue.length ? null : this.localePlaceholder;
},
2017-05-12 10:23:11 +08:00
localeNotFoundText () {
if (this.notFoundText === undefined) {
return this.t('i.select.noMatch');
} else {
return this.notFoundText;
}
},
2017-05-10 18:24:30 +08:00
querySelections () {
let selections = [];
function getSelections (arr, label, value) {
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
item.__label = label ? label + ' / ' + item.label : item.label;
item.__value = value ? value + ',' + item.value : item.value;
if (item.children && item.children.length) {
getSelections(item.children, item.__label, item.__value);
delete item.__label;
delete item.__value;
} else {
selections.push({
label: item.__label,
value: item.__value,
2017-05-11 17:29:44 +08:00
display: item.__label,
2017-05-10 18:24:30 +08:00
item: item,
disabled: !!item.disabled
});
}
}
}
getSelections(this.data);
2017-12-29 22:44:53 +08:00
selections = selections.filter(item => {
2018-01-15 18:11:41 +08:00
return item.label ? item.label.indexOf(this.query) > -1 : false;
2017-12-29 22:44:53 +08:00
}).map(item => {
2017-05-11 17:29:44 +08:00
item.display = item.display.replace(new RegExp(this.query, 'g'), `<span>${this.query}</span>`);
return item;
});
2017-05-10 18:24:30 +08:00
return selections;
}
},
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;
},
toggleOpen () {
if (this.disabled) return false;
if (this.visible) {
2017-05-10 18:24:30 +08:00
if (!this.filterable) 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;
},
2018-01-23 11:38:02 +08:00
updateSelected (init = false, changeOnSelectDataChange = false) {
// #2793 changeOnSelectDataChange used for changeOnSelect when data changed and set value
if (!this.changeOnSelect || init || changeOnSelectDataChange) {
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))
});
});
2016-11-16 10:07:03 +08:00
}
2017-05-10 18:24:30 +08:00
},
handleInput (event) {
this.query = event.target.value;
},
handleSelectItem (index) {
const item = this.querySelections[index];
if (item.item.disabled) return false;
this.query = '';
2017-05-11 17:17:44 +08:00
this.$refs.input.currentValue = '';
2017-05-10 18:24:30 +08:00
const oldVal = JSON.stringify(this.currentValue);
this.currentValue = item.value.split(',');
this.emitValue(this.currentValue, oldVal);
this.handleClose();
2017-05-11 17:17:44 +08:00
},
handleFocus () {
this.$refs.input.focus();
2017-05-18 18:23:39 +08:00
},
// 排除 loading 后的 data避免重复触发 updateSelect
getValidData (data) {
function deleteData (item) {
const new_item = Object.assign({}, item);
if ('loading' in new_item) {
delete new_item.loading;
}
if ('__value' in new_item) {
delete new_item.__value;
}
if ('__label' in new_item) {
delete new_item.__label;
}
if ('children' in new_item && new_item.children.length) {
new_item.children = new_item.children.map(i => deleteData(i));
}
return new_item;
}
return data.map(item => deleteData(item));
2016-11-15 19:17:54 +08:00
}
},
2017-05-04 10:33:50 +08:00
created () {
2017-05-18 18:23:39 +08:00
this.validDataStr = JSON.stringify(this.getValidData(this.data));
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
},
2017-05-04 10:33:50 +08:00
mounted () {
this.updateSelected(true);
},
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-07-20 16:00:03 +08:00
if (this.transfer) {
this.$refs.drop.update();
}
2017-05-11 17:17:44 +08:00
} else {
if (this.filterable) {
this.query = '';
this.$refs.input.currentValue = '';
}
2017-07-20 16:00:03 +08:00
if (this.transfer) {
this.$refs.drop.destroy();
}
2016-11-15 23:23:16 +08:00
}
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
},
2017-05-09 15:46:08 +08:00
data: {
deep: true,
handler () {
2017-05-18 18:23:39 +08:00
const validDataStr = JSON.stringify(this.getValidData(this.data));
if (validDataStr !== this.validDataStr) {
this.validDataStr = validDataStr;
if (!this.isLoadedChildren) {
2018-01-23 11:38:02 +08:00
this.$nextTick(() => this.updateSelected(false, this.changeOnSelect));
2017-05-18 18:23:39 +08:00
}
this.isLoadedChildren = false;
}
2017-05-09 15:46:08 +08:00
}
2016-11-15 23:23:16 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>