element/packages/select/src/option.vue

139 lines
3.3 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<li
@mouseenter="hoverItem"
@click.stop="selectOptionClick"
class="el-select-dropdown__item"
v-show="visible"
2016-11-27 12:12:59 +00:00
:class="{
'selected': itemSelected,
2016-11-28 14:34:03 +00:00
'is-disabled': disabled || groupDisabled || limitReached,
2016-11-27 12:12:59 +00:00
'hover': parent.hoverIndex === index
}">
2016-07-28 10:37:14 +00:00
<slot>
2016-08-24 10:52:07 +00:00
<span>{{ currentLabel }}</span>
2016-07-28 10:37:14 +00:00
</slot>
2016-07-27 06:15:02 +00:00
</li>
</template>
<script type="text/babel">
2016-10-27 09:31:22 +00:00
import Emitter from 'element-ui/src/mixins/emitter';
2016-07-27 06:15:02 +00:00
export default {
2016-10-27 09:31:22 +00:00
mixins: [Emitter],
2016-07-27 06:15:02 +00:00
name: 'el-option',
2016-11-26 07:18:38 +00:00
componentName: 'ElOption',
2016-07-27 06:15:02 +00:00
props: {
value: {
required: true
},
label: [String, Number],
selected: {
type: Boolean,
default: false
},
2016-11-29 15:11:33 +00:00
created: Boolean,
2016-07-27 06:15:02 +00:00
disabled: {
type: Boolean,
default: false
}
},
data() {
return {
index: -1,
2016-10-25 10:00:39 +00:00
groupDisabled: false,
visible: true,
2016-10-25 10:00:39 +00:00
hitState: false
2016-07-27 06:15:02 +00:00
};
},
2016-07-28 10:37:14 +00:00
computed: {
2016-10-25 10:00:39 +00:00
currentLabel() {
return this.label || ((typeof this.value === 'string' || typeof this.value === 'number') ? this.value : '');
},
currentValue() {
return this.value || this.label || '';
},
parent() {
let result = this.$parent;
while (!result.isSelect) {
result = result.$parent;
}
return result;
},
itemSelected() {
2016-11-27 12:12:59 +00:00
if (!this.parent.multiple) {
return this.value === this.parent.value;
} else {
return this.parent.value.indexOf(this.value) > -1;
}
2016-11-28 14:34:03 +00:00
},
limitReached() {
if (this.parent.multiple) {
return !this.itemSelected &&
this.parent.value.length >= this.parent.multipleLimit &&
this.parent.multipleLimit > 0;
} else {
return false;
}
2016-07-27 06:15:02 +00:00
}
},
methods: {
2016-10-25 10:00:39 +00:00
handleGroupDisabled(val) {
this.groupDisabled = val;
2016-07-27 06:15:02 +00:00
},
hoverItem() {
2016-10-25 10:00:39 +00:00
if (!this.disabled && !this.groupDisabled) {
2016-07-27 06:15:02 +00:00
this.parent.hoverIndex = this.parent.options.indexOf(this);
}
},
selectOptionClick() {
2016-10-25 10:00:39 +00:00
if (this.disabled !== true && this.groupDisabled !== true) {
2016-11-26 07:18:38 +00:00
this.dispatch('ElSelect', 'handleOptionClick', this);
2016-07-27 06:15:02 +00:00
}
},
queryChange(query) {
// query 里如果有正则中的特殊字符,需要先将这些字符转义
let parsedQuery = query.replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
2016-11-29 15:11:33 +00:00
this.visible = new RegExp(parsedQuery, 'i').test(this.currentLabel) || this.created;
if (!this.visible) {
2016-07-27 06:15:02 +00:00
this.parent.filteredOptionsCount--;
}
2016-08-20 07:00:01 +00:00
},
resetIndex() {
this.$nextTick(() => {
this.index = this.parent.options.indexOf(this);
});
2016-07-27 06:15:02 +00:00
}
},
created() {
this.parent.options.push(this);
this.parent.cachedOptions.push(this);
2016-07-27 06:15:02 +00:00
this.parent.optionsCount++;
this.parent.filteredOptionsCount++;
this.index = this.parent.options.indexOf(this);
this.$on('queryChange', this.queryChange);
2016-10-25 10:00:39 +00:00
this.$on('handleGroupDisabled', this.handleGroupDisabled);
2016-08-20 07:00:01 +00:00
this.$on('resetIndex', this.resetIndex);
},
beforeDestroy() {
2016-11-26 07:18:38 +00:00
this.dispatch('ElSelect', 'onOptionDestroy', this);
2016-07-27 06:15:02 +00:00
}
};
</script>