element/packages/select/src/option.vue

164 lines
4.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="visible"
2016-11-27 12:12:59 +00:00
:class="{
'selected': itemSelected,
'is-disabled': disabled || groupDisabled || limitReached,
2017-07-29 10:24:07 +00:00
'hover': hover
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';
import { getValueByPath, escapeRegexpString } 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
2017-07-29 10:24:07 +00:00
inject: ['select'],
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,
2017-07-29 10:24:07 +00:00
hitState: false,
hover: 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 || '';
},
itemSelected() {
2017-07-29 10:24:07 +00:00
if (!this.select.multiple) {
return this.isEqual(this.value, this.select.value);
2016-11-27 12:12:59 +00:00
} else {
2017-07-29 10:24:07 +00:00
return this.contains(this.select.value, this.value);
}
2016-11-28 14:34:03 +00:00
},
limitReached() {
2017-07-29 10:24:07 +00:00
if (this.select.multiple) {
2016-11-28 14:34:03 +00:00
return !this.itemSelected &&
(this.select.value || []).length >= this.select.multipleLimit &&
2017-07-29 10:24:07 +00:00
this.select.multipleLimit > 0;
2016-11-28 14:34:03 +00:00
} else {
return false;
}
2016-07-27 06:15:02 +00:00
}
},
watch: {
currentLabel() {
2017-07-29 10:24:07 +00:00
if (!this.created && !this.select.remote) this.dispatch('ElSelect', 'setSelected');
},
value(val, oldVal) {
const { remote, valueKey } = this.select;
if (!this.created && !remote) {
if (valueKey && typeof val === 'object' && typeof oldVal === 'object' && val[valueKey] === oldVal[valueKey]) {
return;
}
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 {
2017-07-29 10:24:07 +00:00
const valueKey = this.select.valueKey;
2017-07-18 03:23:43 +00:00
return getValueByPath(a, valueKey) === getValueByPath(b, valueKey);
}
},
contains(arr = [], target) {
if (!this.isObject) {
return arr && arr.indexOf(target) > -1;
2017-07-18 03:23:43 +00:00
} else {
2017-07-29 10:24:07 +00:00
const valueKey = this.select.valueKey;
return arr && arr.some(item => {
2017-07-18 03:23:43 +00:00
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) {
2017-07-29 10:24:07 +00:00
this.select.hoverIndex = this.select.options.indexOf(this);
2016-07-27 06:15:02 +00:00
}
},
selectOptionClick() {
2016-10-25 10:00:39 +00:00
if (this.disabled !== true && this.groupDisabled !== true) {
this.dispatch('ElSelect', 'handleOptionClick', [this, true]);
2016-07-27 06:15:02 +00:00
}
},
queryChange(query) {
this.visible = new RegExp(escapeRegexpString(query), 'i').test(this.currentLabel) || this.created;
if (!this.visible) {
2017-07-29 10:24:07 +00:00
this.select.filteredOptionsCount--;
2016-07-27 06:15:02 +00:00
}
}
},
created() {
2017-07-29 10:24:07 +00:00
this.select.options.push(this);
this.select.cachedOptions.push(this);
this.select.optionsCount++;
this.select.filteredOptionsCount++;
2016-07-27 06:15:02 +00:00
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
},
beforeDestroy() {
2019-07-22 06:16:13 +00:00
let index = this.select.cachedOptions.indexOf(this);
if (index > -1) {
this.select.cachedOptions.splice(index, 1);
}
2017-08-18 07:32:29 +00:00
this.select.onOptionDestroy(this.select.options.indexOf(this));
2016-07-27 06:15:02 +00:00
}
};
</script>