2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
|
|
|
<li
|
|
|
|
@mouseenter="hoverItem"
|
|
|
|
@click.stop="selectOptionClick"
|
|
|
|
class="el-select-dropdown__item"
|
2016-11-04 08:47:11 +00:00
|
|
|
v-show="visible"
|
2016-11-27 12:12:59 +00:00
|
|
|
:class="{
|
|
|
|
'selected': itemSelected,
|
2017-05-08 06:06:23 +00:00
|
|
|
'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
|
|
|
|
2016-12-31 15:33:51 +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,
|
2016-11-04 08:47:11 +00:00
|
|
|
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() {
|
|
|
|
const type = typeof this.value;
|
2017-07-20 04:27:55 +00:00
|
|
|
return type !== 'string' && type !== 'number' && type !== 'boolean';
|
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
|
|
|
},
|
|
|
|
|
2016-11-25 06:06:53 +00:00
|
|
|
currentValue() {
|
|
|
|
return this.value || this.label || '';
|
|
|
|
},
|
|
|
|
|
2016-08-21 03:04:29 +00:00
|
|
|
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-08-21 03:04:29 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-18 13:24:02 +00:00
|
|
|
watch: {
|
|
|
|
currentLabel() {
|
2017-03-29 10:57:10 +00:00
|
|
|
if (!this.created && !this.parent.remote) this.dispatch('ElSelect', 'setSelected');
|
2016-12-18 13:24:02 +00:00
|
|
|
},
|
|
|
|
value() {
|
2017-03-29 10:57:10 +00:00
|
|
|
if (!this.created && !this.parent.remote) this.dispatch('ElSelect', 'setSelected');
|
2016-12-18 13:24:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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) {
|
2016-11-04 08:47:11 +00:00
|
|
|
// query 里如果有正则中的特殊字符,需要先将这些字符转义
|
2017-01-13 04:38:54 +00:00
|
|
|
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;
|
2016-11-04 08:47:11 +00:00
|
|
|
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);
|
2016-12-08 10:21:09 +00:00
|
|
|
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>
|