element/packages/select/src/option.vue

169 lines
4.2 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,
'is-disabled': disabled || groupDisabled || limitReached,
'hover': parent.hoverIndex === index
2016-11-27 12:12:59 +00:00
}">
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';
2017-07-18 03:23:43 +00:00
import { getValueByPath } from 'element-ui/src/utils/util';
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: 'ElOption',
2016-07-27 06:15:02 +00:00
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],
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: {
2017-07-18 03:23:43 +00:00
isObject() {
2017-07-29 13:45:15 +00:00
return Object.prototype.toString.call(this.value).toLowerCase() === '[object object]';
2017-07-18 03:23:43 +00:00
},
2016-10-25 10:00:39 +00:00
currentLabel() {
2017-07-18 03:23:43 +00:00
return this.label || (this.isObject ? '' : this.value);
2016-10-25 10:00:39 +00:00
},
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) {
2017-07-18 03:23:43 +00:00
return this.isEqual(this.value, this.parent.value);
2016-11-27 12:12:59 +00:00
} else {
2017-07-18 03:23:43 +00:00
return this.contains(this.parent.value, this.value);
}
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
}
},
watch: {
currentLabel() {
if (!this.created && !this.parent.remote) this.dispatch('ElSelect', 'setSelected');
},
value() {
if (!this.created && !this.parent.remote) this.dispatch('ElSelect', 'setSelected');
}
},
2016-07-27 06:15:02 +00:00
methods: {
2017-07-18 03:23:43 +00:00
isEqual(a, b) {
if (!this.isObject) {
return a === b;
} else {
const valueKey = this.parent.valueKey;
return getValueByPath(a, valueKey) === getValueByPath(b, valueKey);
}
},
contains(arr = [], target) {
if (!this.isObject) {
return arr.indexOf(target) > -1;
} else {
const valueKey = this.parent.valueKey;
return arr.some(item => {
return getValueByPath(item, valueKey) === getValueByPath(target, valueKey);
});
}
},
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 = String(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>