|
@@ -1,19 +1,26 @@
|
|
|
<template>
|
|
|
<div
|
|
|
class="el-select"
|
|
|
- v-clickoutside="handleClose"
|
|
|
- :class="{ 'is-multiple': multiple, 'is-small': size === 'small' }">
|
|
|
- <div class="el-select__tags" v-if="multiple" @click.stop="toggleMenu" ref="tags" :style="{ 'max-width': inputWidth - 32 + 'px' }">
|
|
|
+ v-clickoutside="handleClose">
|
|
|
+ <div
|
|
|
+ class="el-select__tags"
|
|
|
+ v-if="multiple"
|
|
|
+ @click.stop="toggleMenu"
|
|
|
+ ref="tags"
|
|
|
+ :style="{ 'max-width': inputWidth - 32 + 'px' }">
|
|
|
<transition-group @after-leave="resetInputHeight">
|
|
|
<el-tag
|
|
|
v-for="item in selected"
|
|
|
- :key="item"
|
|
|
+ :key="item.value"
|
|
|
closable
|
|
|
:hit="item.hitState"
|
|
|
type="primary"
|
|
|
@close="deleteTag($event, item)"
|
|
|
- close-transition>{{ item.currentLabel }}</el-tag>
|
|
|
+ close-transition>
|
|
|
+ {{ item.currentLabel }}
|
|
|
+ </el-tag>
|
|
|
</transition-group>
|
|
|
+
|
|
|
<input
|
|
|
type="text"
|
|
|
class="el-select__input"
|
|
@@ -40,7 +47,7 @@
|
|
|
:disabled="disabled"
|
|
|
:readonly="!filterable || multiple"
|
|
|
@focus="toggleMenu"
|
|
|
- @click="toggleMenu"
|
|
|
+ @click="handleIconClick"
|
|
|
@mousedown.native="handleMouseDown"
|
|
|
@keyup.native="debouncedOnInputChange"
|
|
|
@keydown.native.down.prevent="navigateOptions('next')"
|
|
@@ -56,10 +63,18 @@
|
|
|
<el-select-menu
|
|
|
ref="popper"
|
|
|
v-show="visible && emptyText !== false">
|
|
|
- <ul class="el-select-dropdown__list" v-show="options.length > 0 && filteredOptionsCount > 0 && !loading">
|
|
|
+ <ul
|
|
|
+ class="el-select-dropdown__list"
|
|
|
+ :class="{ 'is-empty': !allowCreate && filteredOptionsCount === 0 }"
|
|
|
+ v-show="options.length > 0 && !loading">
|
|
|
+ <el-option
|
|
|
+ :value="query"
|
|
|
+ created
|
|
|
+ v-if="showNewOption">
|
|
|
+ </el-option>
|
|
|
<slot></slot>
|
|
|
</ul>
|
|
|
- <p class="el-select-dropdown__empty" v-if="emptyText">{{ emptyText }}</p>
|
|
|
+ <p class="el-select-dropdown__empty" v-if="emptyText && !allowCreate">{{ emptyText }}</p>
|
|
|
</el-select-menu>
|
|
|
</transition>
|
|
|
</div>
|
|
@@ -70,6 +85,7 @@
|
|
|
import Locale from 'element-ui/src/mixins/locale';
|
|
|
import ElInput from 'element-ui/packages/input';
|
|
|
import ElSelectMenu from './select-dropdown.vue';
|
|
|
+ import ElOption from './option.vue';
|
|
|
import ElTag from 'element-ui/packages/tag';
|
|
|
import debounce from 'throttle-debounce/debounce';
|
|
|
import Clickoutside from 'element-ui/src/utils/clickoutside';
|
|
@@ -94,22 +110,18 @@
|
|
|
},
|
|
|
|
|
|
showCloseIcon() {
|
|
|
- let criteria = this.clearable && this.inputHovering && !this.multiple && this.options.indexOf(this.selected) > -1;
|
|
|
+ let criteria = this.clearable &&
|
|
|
+ this.inputHovering &&
|
|
|
+ !this.multiple &&
|
|
|
+ this.value !== undefined &&
|
|
|
+ this.value !== '';
|
|
|
if (!this.$el) return false;
|
|
|
-
|
|
|
this.$nextTick(() => {
|
|
|
let icon = this.$el.querySelector('.el-input__icon');
|
|
|
if (icon) {
|
|
|
- if (criteria) {
|
|
|
- icon.addEventListener('click', this.deleteSelected);
|
|
|
- addClass(icon, 'is-show-close');
|
|
|
- } else {
|
|
|
- icon.removeEventListener('click', this.deleteSelected);
|
|
|
- removeClass(icon, 'is-show-close');
|
|
|
- }
|
|
|
+ criteria ? addClass(icon, 'is-show-close') : removeClass(icon, 'is-show-close');
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
return criteria;
|
|
|
},
|
|
|
|
|
@@ -129,12 +141,19 @@
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
+ },
|
|
|
+
|
|
|
+ showNewOption() {
|
|
|
+ let hasExistingOption = this.options.filter(option => !option.created)
|
|
|
+ .some(option => option.currentLabel === this.query);
|
|
|
+ return this.filterable && this.allowCreate && this.query !== '' && !hasExistingOption;
|
|
|
}
|
|
|
},
|
|
|
|
|
|
components: {
|
|
|
ElInput,
|
|
|
ElSelectMenu,
|
|
|
+ ElOption,
|
|
|
ElTag
|
|
|
},
|
|
|
|
|
@@ -143,15 +162,19 @@
|
|
|
props: {
|
|
|
name: String,
|
|
|
value: {},
|
|
|
- size: String,
|
|
|
disabled: Boolean,
|
|
|
clearable: Boolean,
|
|
|
filterable: Boolean,
|
|
|
+ allowCreate: Boolean,
|
|
|
loading: Boolean,
|
|
|
remote: Boolean,
|
|
|
remoteMethod: Function,
|
|
|
filterMethod: Function,
|
|
|
multiple: Boolean,
|
|
|
+ multipleLimit: {
|
|
|
+ type: Number,
|
|
|
+ default: 0
|
|
|
+ },
|
|
|
placeholder: {
|
|
|
type: String,
|
|
|
default() {
|
|
@@ -163,22 +186,21 @@
|
|
|
data() {
|
|
|
return {
|
|
|
options: [],
|
|
|
- selected: {},
|
|
|
+ selected: this.multiple ? [] : {},
|
|
|
isSelect: true,
|
|
|
inputLength: 20,
|
|
|
inputWidth: 0,
|
|
|
- valueChangeBySelected: false,
|
|
|
cachedPlaceHolder: '',
|
|
|
optionsCount: 0,
|
|
|
filteredOptionsCount: 0,
|
|
|
dropdownUl: null,
|
|
|
visible: false,
|
|
|
selectedLabel: '',
|
|
|
- selectInit: false,
|
|
|
hoverIndex: -1,
|
|
|
query: '',
|
|
|
voidRemoteQuery: false,
|
|
|
bottomOverflowBeforeHidden: 0,
|
|
|
+ topOverflowBeforeHidden: 0,
|
|
|
optionsAllDisabled: false,
|
|
|
inputHovering: false,
|
|
|
currentPlaceholder: ''
|
|
@@ -191,79 +213,25 @@
|
|
|
},
|
|
|
|
|
|
value(val) {
|
|
|
- if (this.valueChangeBySelected) {
|
|
|
- this.valueChangeBySelected = false;
|
|
|
- return;
|
|
|
- }
|
|
|
- this.$nextTick(() => {
|
|
|
- if (this.multiple && Array.isArray(val)) {
|
|
|
- this.$nextTick(() => {
|
|
|
- this.resetInputHeight();
|
|
|
- });
|
|
|
- this.selectedInit = true;
|
|
|
- this.selected = [];
|
|
|
- this.currentPlaceholder = this.cachedPlaceHolder;
|
|
|
- val.forEach(item => {
|
|
|
- let option = this.options.filter(option => option.value === item)[0];
|
|
|
- if (option) {
|
|
|
- this.$emit('addOptionToValue', option);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- if (!this.multiple) {
|
|
|
- let option = this.options.filter(option => option.value === val)[0];
|
|
|
- if (option) {
|
|
|
- this.$emit('addOptionToValue', option);
|
|
|
- } else {
|
|
|
- this.selected = {};
|
|
|
- this.selectedLabel = '';
|
|
|
- }
|
|
|
- }
|
|
|
- this.resetHoverIndex();
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- selected(val, oldVal) {
|
|
|
if (this.multiple) {
|
|
|
- if (this.selected.length > 0) {
|
|
|
+ this.resetInputHeight();
|
|
|
+ if (val.length > 0) {
|
|
|
this.currentPlaceholder = '';
|
|
|
} else {
|
|
|
this.currentPlaceholder = this.cachedPlaceHolder;
|
|
|
}
|
|
|
- this.$nextTick(() => {
|
|
|
- this.resetInputHeight();
|
|
|
- });
|
|
|
- if (this.selectedInit) {
|
|
|
- this.selectedInit = false;
|
|
|
- return;
|
|
|
- }
|
|
|
- this.valueChangeBySelected = true;
|
|
|
- const result = val.map(item => item.value);
|
|
|
-
|
|
|
- this.$emit('input', result);
|
|
|
- this.$emit('change', result);
|
|
|
- this.dispatch('ElFormItem', 'el.form.change', val);
|
|
|
- if (this.filterable) {
|
|
|
- this.query = '';
|
|
|
- this.hoverIndex = -1;
|
|
|
- this.$refs.input.focus();
|
|
|
- this.inputLength = 20;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (this.selectedInit) {
|
|
|
- this.selectedInit = false;
|
|
|
- return;
|
|
|
- }
|
|
|
- if (val.value === oldVal.value) return;
|
|
|
- this.$emit('input', val.value);
|
|
|
- this.$emit('change', val.value);
|
|
|
}
|
|
|
+ this.selected = this.getSelected();
|
|
|
+ if (this.filterable && !this.multiple) {
|
|
|
+ this.inputLength = 20;
|
|
|
+ }
|
|
|
+ this.$emit('change', val);
|
|
|
+ this.dispatch('ElFormItem', 'el.form.change', val);
|
|
|
},
|
|
|
|
|
|
query(val) {
|
|
|
- this.$nextTick(() => {
|
|
|
- this.broadcast('ElSelectDropdown', 'updatePopper');
|
|
|
- });
|
|
|
+ this.broadcast('ElSelectDropdown', 'updatePopper');
|
|
|
+ this.hoverIndex = -1;
|
|
|
if (this.multiple && this.filterable) {
|
|
|
this.resetInputHeight();
|
|
|
}
|
|
@@ -283,27 +251,22 @@
|
|
|
visible(val) {
|
|
|
if (!val) {
|
|
|
this.$refs.reference.$el.querySelector('input').blur();
|
|
|
- if (this.$el.querySelector('.el-input__icon')) {
|
|
|
- removeClass(this.$el.querySelector('.el-input__icon'), 'is-reverse');
|
|
|
- }
|
|
|
+ this.handleIconHide();
|
|
|
this.broadcast('ElSelectDropdown', 'destroyPopper');
|
|
|
if (this.$refs.input) {
|
|
|
this.$refs.input.blur();
|
|
|
}
|
|
|
+ this.query = '';
|
|
|
+ this.selectedLabel = '';
|
|
|
this.resetHoverIndex();
|
|
|
if (!this.multiple) {
|
|
|
- if (this.dropdownUl && this.selected.$el) {
|
|
|
- this.bottomOverflowBeforeHidden = this.selected.$el.getBoundingClientRect().bottom - this.$refs.popper.$el.getBoundingClientRect().bottom;
|
|
|
- }
|
|
|
+ this.getOverflows();
|
|
|
if (this.selected && this.selected.value) {
|
|
|
this.selectedLabel = this.selected.currentLabel;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- let icon = this.$el.querySelector('.el-input__icon');
|
|
|
- if (icon && !hasClass(icon, 'el-icon-circle-close')) {
|
|
|
- addClass(this.$el.querySelector('.el-input__icon'), 'is-reverse');
|
|
|
- }
|
|
|
+ this.handleIconShow();
|
|
|
this.broadcast('ElSelectDropdown', 'updatePopper');
|
|
|
if (this.filterable) {
|
|
|
this.query = this.selectedLabel;
|
|
@@ -318,19 +281,88 @@
|
|
|
this.dropdownUl = [].filter.call(dropdownChildNodes, item => item.tagName === 'UL')[0];
|
|
|
}
|
|
|
if (!this.multiple && this.dropdownUl) {
|
|
|
- if (this.bottomOverflowBeforeHidden > 0) {
|
|
|
- this.dropdownUl.scrollTop += this.bottomOverflowBeforeHidden;
|
|
|
- }
|
|
|
+ this.setOverflow();
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
options(val) {
|
|
|
this.optionsAllDisabled = val.length === val.filter(item => item.disabled === true).length;
|
|
|
+ if (this.multiple) {
|
|
|
+ this.resetInputHeight();
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
+ handleIconHide() {
|
|
|
+ let icon = this.$el.querySelector('.el-input__icon');
|
|
|
+ if (icon) {
|
|
|
+ removeClass(icon, 'is-reverse');
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ handleIconShow() {
|
|
|
+ let icon = this.$el.querySelector('.el-input__icon');
|
|
|
+ if (icon && !hasClass(icon, 'el-icon-circle-close')) {
|
|
|
+ addClass(icon, 'is-reverse');
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getOverflows() {
|
|
|
+ if (this.dropdownUl && this.selected && this.selected.$el) {
|
|
|
+ let selectedRect = this.selected.$el.getBoundingClientRect();
|
|
|
+ let popperRect = this.$refs.popper.$el.getBoundingClientRect();
|
|
|
+ this.bottomOverflowBeforeHidden = selectedRect.bottom - popperRect.bottom;
|
|
|
+ this.topOverflowBeforeHidden = selectedRect.top - popperRect.top;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ setOverflow() {
|
|
|
+ if (this.bottomOverflowBeforeHidden > 0) {
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.dropdownUl.scrollTop += this.bottomOverflowBeforeHidden;
|
|
|
+ });
|
|
|
+ } else if (this.topOverflowBeforeHidden < 0) {
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.dropdownUl.scrollTop += this.topOverflowBeforeHidden;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getSelected() {
|
|
|
+ if (!this.multiple) {
|
|
|
+ let option = this.options.filter(option => option.value === this.value)[0] ||
|
|
|
+ { value: this.value, currentLabel: this.value };
|
|
|
+ this.selectedLabel = option.currentLabel;
|
|
|
+ return option;
|
|
|
+ }
|
|
|
+ let result = [];
|
|
|
+ if (Array.isArray(this.value)) {
|
|
|
+ this.value.forEach(value => {
|
|
|
+ let option = this.options.filter(option => option.value === value)[0];
|
|
|
+ if (option) {
|
|
|
+ result.push(option);
|
|
|
+ } else {
|
|
|
+ result.push({
|
|
|
+ value: this.value,
|
|
|
+ currentLabel: value,
|
|
|
+ hitState: false
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+
|
|
|
+ handleIconClick(event) {
|
|
|
+ if (this.iconClass === 'circle-close') {
|
|
|
+ this.deleteSelected(event);
|
|
|
+ } else {
|
|
|
+ this.toggleMenu();
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
handleMouseDown(event) {
|
|
|
if (event.target.tagName !== 'INPUT') return;
|
|
|
if (this.visible) {
|
|
@@ -363,22 +395,7 @@
|
|
|
|
|
|
deletePrevTag(e) {
|
|
|
if (e.target.value.length <= 0 && !this.toggleLastOptionHitState()) {
|
|
|
- this.selected.pop();
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
- addOptionToValue(option, init) {
|
|
|
- if (this.multiple) {
|
|
|
- if (this.selected.indexOf(option) === -1 && (this.remote ? this.value.indexOf(option.value) === -1 : true)) {
|
|
|
- this.selectedInit = !!init;
|
|
|
- this.selected.push(option);
|
|
|
- this.resetHoverIndex();
|
|
|
- }
|
|
|
- } else {
|
|
|
- this.selectedInit = !!init;
|
|
|
- this.selected = option;
|
|
|
- this.selectedLabel = option.currentLabel;
|
|
|
- this.hoverIndex = option.index;
|
|
|
+ this.value.pop();
|
|
|
}
|
|
|
},
|
|
|
|
|
@@ -418,20 +435,24 @@
|
|
|
|
|
|
handleOptionSelect(option) {
|
|
|
if (!this.multiple) {
|
|
|
- this.selected = option;
|
|
|
- this.selectedLabel = option.currentLabel;
|
|
|
+ this.$emit('input', option.value);
|
|
|
this.visible = false;
|
|
|
} else {
|
|
|
let optionIndex = -1;
|
|
|
- this.selected.forEach((item, index) => {
|
|
|
- if (item === option || item.currentValue === option.currentValue) {
|
|
|
+ this.value.forEach((item, index) => {
|
|
|
+ if (item === option.value) {
|
|
|
optionIndex = index;
|
|
|
}
|
|
|
});
|
|
|
if (optionIndex > -1) {
|
|
|
- this.selected.splice(optionIndex, 1);
|
|
|
- } else {
|
|
|
- this.selected.push(option);
|
|
|
+ this.value.splice(optionIndex, 1);
|
|
|
+ } else if (this.multipleLimit <= 0 || this.value.length < this.multipleLimit) {
|
|
|
+ this.value.push(option.value);
|
|
|
+ }
|
|
|
+ if (option.created) {
|
|
|
+ this.query = '';
|
|
|
+ this.inputLength = 20;
|
|
|
+ this.$refs.input.focus();
|
|
|
}
|
|
|
}
|
|
|
},
|
|
@@ -450,6 +471,7 @@
|
|
|
this.visible = true;
|
|
|
return;
|
|
|
}
|
|
|
+ if (this.options.length === 0 || this.filteredOptionsCount === 0) return;
|
|
|
if (!this.optionsAllDisabled) {
|
|
|
if (direction === 'next') {
|
|
|
this.hoverIndex++;
|
|
@@ -479,8 +501,10 @@
|
|
|
},
|
|
|
|
|
|
resetScrollTop() {
|
|
|
- let bottomOverflowDistance = this.options[this.hoverIndex].$el.getBoundingClientRect().bottom - this.$refs.popper.$el.getBoundingClientRect().bottom;
|
|
|
- let topOverflowDistance = this.options[this.hoverIndex].$el.getBoundingClientRect().top - this.$refs.popper.$el.getBoundingClientRect().top;
|
|
|
+ let bottomOverflowDistance = this.options[this.hoverIndex].$el.getBoundingClientRect().bottom -
|
|
|
+ this.$refs.popper.$el.getBoundingClientRect().bottom;
|
|
|
+ let topOverflowDistance = this.options[this.hoverIndex].$el.getBoundingClientRect().top -
|
|
|
+ this.$refs.popper.$el.getBoundingClientRect().top;
|
|
|
if (bottomOverflowDistance > 0) {
|
|
|
this.dropdownUl.scrollTop += bottomOverflowDistance;
|
|
|
}
|
|
@@ -497,8 +521,6 @@
|
|
|
|
|
|
deleteSelected(event) {
|
|
|
event.stopPropagation();
|
|
|
- this.selected = {};
|
|
|
- this.selectedLabel = '';
|
|
|
this.$emit('input', '');
|
|
|
this.$emit('change', '');
|
|
|
this.visible = false;
|
|
@@ -507,13 +529,13 @@
|
|
|
deleteTag(event, tag) {
|
|
|
let index = this.selected.indexOf(tag);
|
|
|
if (index > -1) {
|
|
|
- this.selected.splice(index, 1);
|
|
|
+ this.value.splice(index, 1);
|
|
|
}
|
|
|
event.stopPropagation();
|
|
|
},
|
|
|
|
|
|
onInputChange() {
|
|
|
- if (this.filterable && this.selectedLabel !== this.value) {
|
|
|
+ if (this.filterable) {
|
|
|
this.query = this.selectedLabel;
|
|
|
}
|
|
|
},
|
|
@@ -535,9 +557,11 @@
|
|
|
|
|
|
created() {
|
|
|
this.cachedPlaceHolder = this.currentPlaceholder = this.placeholder;
|
|
|
- if (this.multiple) {
|
|
|
- this.selectedInit = true;
|
|
|
- this.selected = [];
|
|
|
+ if (this.multiple && !Array.isArray(this.value)) {
|
|
|
+ this.$emit('input', []);
|
|
|
+ }
|
|
|
+ if (!this.multiple && (!this.value || Array.isArray(this.value))) {
|
|
|
+ this.$emit('input', '');
|
|
|
}
|
|
|
if (this.remote) {
|
|
|
this.voidRemoteQuery = true;
|
|
@@ -547,20 +571,18 @@
|
|
|
this.onInputChange();
|
|
|
});
|
|
|
|
|
|
- this.$on('addOptionToValue', this.addOptionToValue);
|
|
|
this.$on('handleOptionClick', this.handleOptionSelect);
|
|
|
this.$on('onOptionDestroy', this.onOptionDestroy);
|
|
|
},
|
|
|
|
|
|
mounted() {
|
|
|
+ if (this.multiple && Array.isArray(this.value) && this.value.length > 0) {
|
|
|
+ this.currentPlaceholder = '';
|
|
|
+ }
|
|
|
addResizeListener(this.$el, this.resetInputWidth);
|
|
|
- if (this.remote && this.multiple && Array.isArray(this.value)) {
|
|
|
- this.selected = this.options.reduce((prev, curr) => {
|
|
|
- return this.value.indexOf(curr.value) > -1 ? prev.concat(curr) : prev;
|
|
|
- }, []);
|
|
|
- this.$nextTick(() => {
|
|
|
- this.resetInputHeight();
|
|
|
- });
|
|
|
+ this.selected = this.getSelected();
|
|
|
+ if (this.remote && this.multiple) {
|
|
|
+ this.resetInputHeight();
|
|
|
}
|
|
|
this.$nextTick(() => {
|
|
|
if (this.$refs.reference.$el) {
|