element/packages/select/src/option.vue

127 lines
3.0 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="queryPassed"
:class="{ 'selected': itemSelected(), 'is-disabled': disabled, 'hover': parent.hoverIndex === index }">
2016-07-28 10:37:14 +00:00
<slot>
<span>{{ label }}</span>
</slot>
2016-07-27 06:15:02 +00:00
</li>
</template>
<script type="text/babel">
import emitter from 'main/mixins/emitter';
const toString = Object.prototype.toString;
export default {
mixins: [emitter],
name: 'el-option',
componentName: 'option',
props: {
value: {
required: true
},
label: [String, Number],
selected: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
}
},
data() {
return {
parent: null,
index: -1,
queryPassed: true,
hitState: false
};
},
2016-07-28 10:37:14 +00:00
computed: {
currentSelected() {
return this.selected || (this.$parent.multiple ? this.$parent.value.indexOf(this.value) > -1 : this.$parent.value === this.value);
}
},
2016-07-27 06:15:02 +00:00
watch: {
2016-07-28 10:37:14 +00:00
currentSelected(val) {
2016-07-27 06:15:02 +00:00
if (val === true) {
this.dispatch('select', 'addOptionToValue', this);
}
}
},
methods: {
disableOptions() {
this.disabled = true;
},
hoverItem() {
if (!this.disabled) {
this.parent.hoverIndex = this.parent.options.indexOf(this);
}
},
selectOptionClick() {
if (this.disabled !== true) {
this.dispatch('select', 'handleOptionClick', this);
}
},
itemSelected() {
if (toString.call(this.parent.selected) === '[object Object]') {
return this === this.parent.selected;
} else if (toString.call(this.parent.selected) === '[object Array]') {
2016-08-20 07:00:01 +00:00
return this.parent.value.indexOf(this.value) > -1;
2016-07-27 06:15:02 +00:00
}
},
queryChange(query) {
this.queryPassed = new RegExp(query, 'i').test(this.label);
if (!this.queryPassed) {
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 = this.$parent;
while (!this.parent.isSelect) {
this.parent = this.parent.$parent;
}
this.label = this.label || ((typeof this.value === 'string' || typeof this.value === 'number') ? this.value : '');
this.parent.options.push(this);
this.parent.optionsCount++;
this.parent.filteredOptionsCount++;
this.index = this.parent.options.indexOf(this);
2016-07-28 10:37:14 +00:00
if (this.currentSelected === true) {
2016-07-27 06:15:02 +00:00
this.dispatch('select', 'addOptionToValue', this);
}
this.$on('queryChange', this.queryChange);
this.$on('disableOptions', this.disableOptions);
2016-08-20 07:00:01 +00:00
this.$on('resetIndex', this.resetIndex);
},
beforeDestroy() {
this.dispatch('select', 'onOptionDestroy', this);
2016-07-27 06:15:02 +00:00
}
};
</script>