2020-10-03 19:07:02 +08:00
|
|
|
<template>
|
|
|
|
<div
|
|
|
|
ref="selectWrapper"
|
2020-12-31 10:41:50 +08:00
|
|
|
v-click-outside:[popperPaneRef]="handleClose"
|
2020-10-03 19:07:02 +08:00
|
|
|
class="el-select"
|
|
|
|
:class="[selectSize ? 'el-select--' + selectSize : '']"
|
|
|
|
@click.stop="toggleMenu"
|
|
|
|
>
|
|
|
|
<el-popper
|
|
|
|
ref="popper"
|
|
|
|
v-model:visible="dropMenuVisible"
|
|
|
|
placement="bottom-start"
|
|
|
|
:append-to-body="popperAppendToBody"
|
2020-12-02 22:33:26 +08:00
|
|
|
:popper-class="`el-select__popper ${popperClass}`"
|
2020-10-03 19:07:02 +08:00
|
|
|
manual-mode
|
|
|
|
effect="light"
|
2020-12-02 22:33:26 +08:00
|
|
|
pure
|
2020-10-03 19:07:02 +08:00
|
|
|
trigger="click"
|
2020-12-02 22:33:26 +08:00
|
|
|
transition="el-zoom-in-top"
|
2020-12-31 10:41:50 +08:00
|
|
|
:stop-popper-mouse-event="false"
|
2020-12-02 22:33:26 +08:00
|
|
|
:gpu-acceleration="false"
|
|
|
|
@before-enter="handleMenuEnter"
|
2020-10-03 19:07:02 +08:00
|
|
|
>
|
|
|
|
<template #trigger>
|
|
|
|
<div class="select-trigger">
|
|
|
|
<div
|
|
|
|
v-if="multiple"
|
|
|
|
ref="tags"
|
|
|
|
class="el-select__tags"
|
|
|
|
:style="{ 'max-width': inputWidth - 32 + 'px', width: '100%' }"
|
|
|
|
>
|
|
|
|
<span v-if="collapseTags && selected.length">
|
|
|
|
<el-tag
|
|
|
|
:closable="!selectDisabled"
|
|
|
|
:size="collapseTagSize"
|
|
|
|
:hit="selected[0].hitState"
|
|
|
|
type="info"
|
|
|
|
disable-transitions
|
|
|
|
@close="deleteTag($event, selected[0])"
|
|
|
|
>
|
2020-12-17 16:51:59 +08:00
|
|
|
<span class="el-select__tags-text" :style="{ 'max-width': inputWidth - 123 + 'px' }">{{ selected[0].currentLabel }}</span>
|
2020-10-03 19:07:02 +08:00
|
|
|
</el-tag>
|
|
|
|
<el-tag
|
|
|
|
v-if="selected.length > 1"
|
|
|
|
:closable="false"
|
|
|
|
:size="collapseTagSize"
|
|
|
|
type="info"
|
|
|
|
disable-transitions
|
|
|
|
>
|
|
|
|
<span class="el-select__tags-text">+ {{ selected.length - 1 }}</span>
|
|
|
|
</el-tag>
|
|
|
|
</span>
|
|
|
|
<!-- <div> -->
|
|
|
|
<transition v-if="!collapseTags" @after-leave="resetInputHeight">
|
|
|
|
<span>
|
|
|
|
<el-tag
|
|
|
|
v-for="item in selected"
|
|
|
|
:key="getValueKey(item)"
|
|
|
|
:closable="!selectDisabled"
|
|
|
|
:size="collapseTagSize"
|
|
|
|
:hit="item.hitState"
|
|
|
|
type="info"
|
|
|
|
disable-transitions
|
|
|
|
@close="deleteTag($event, item)"
|
|
|
|
>
|
2020-12-17 16:51:59 +08:00
|
|
|
<span class="el-select__tags-text" :style="{ 'max-width': inputWidth - 75 + 'px' }">{{ item.currentLabel }}</span>
|
2020-10-03 19:07:02 +08:00
|
|
|
</el-tag>
|
|
|
|
</span>
|
|
|
|
</transition>
|
|
|
|
<!-- </div> -->
|
|
|
|
<input
|
|
|
|
v-if="filterable"
|
|
|
|
ref="input"
|
|
|
|
v-model="query"
|
|
|
|
type="text"
|
|
|
|
class="el-select__input"
|
|
|
|
:class="[selectSize ? `is-${ selectSize }` : '']"
|
|
|
|
:disabled="selectDisabled"
|
|
|
|
:autocomplete="autocomplete"
|
|
|
|
:style="{ 'flex-grow': '1', width: inputLength / (inputWidth - 32) + '%', 'max-width': inputWidth - 42 + 'px' }"
|
|
|
|
@focus="handleFocus"
|
|
|
|
@blur="softFocus = false"
|
|
|
|
@keyup="managePlaceholder"
|
|
|
|
@keydown="resetInputState"
|
|
|
|
@keydown.down.prevent="navigateOptions('next')"
|
|
|
|
@keydown.up.prevent="navigateOptions('prev')"
|
|
|
|
@keydown.esc.stop.prevent="visible = false"
|
|
|
|
@keydown.enter.stop.prevent="selectOption"
|
|
|
|
@keydown.delete="deletePrevTag"
|
|
|
|
@keydown.tab="visible = false"
|
|
|
|
@compositionstart="handleComposition"
|
|
|
|
@compositionupdate="handleComposition"
|
|
|
|
@compositionend="handleComposition"
|
|
|
|
@input="debouncedQueryChange"
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
<el-input
|
|
|
|
:id="id"
|
|
|
|
ref="reference"
|
|
|
|
v-model="selectedLabel"
|
|
|
|
type="text"
|
|
|
|
:placeholder="currentPlaceholder"
|
|
|
|
:name="name"
|
|
|
|
:autocomplete="autocomplete"
|
|
|
|
:size="selectSize"
|
|
|
|
:disabled="selectDisabled"
|
|
|
|
:readonly="readonly"
|
|
|
|
:validate-event="false"
|
|
|
|
:class="{ 'is-focus': visible }"
|
|
|
|
:tabindex="(multiple && filterable) ? '-1' : null"
|
|
|
|
@focus="handleFocus"
|
|
|
|
@blur="handleBlur"
|
|
|
|
@input="debouncedOnInputChange"
|
|
|
|
@paste="debouncedOnInputChange"
|
|
|
|
@keydown.down.stop.prevent="navigateOptions('next')"
|
|
|
|
@keydown.up.stop.prevent="navigateOptions('prev')"
|
|
|
|
@keydown.enter.stop.prevent="selectOption"
|
|
|
|
@keydown.esc.stop.prevent="visible = false"
|
|
|
|
@keydown.tab="visible = false"
|
|
|
|
@mouseenter="inputHovering = true"
|
|
|
|
@mouseleave="inputHovering = false"
|
|
|
|
>
|
|
|
|
<template v-if="$slots.prefix" #prefix>
|
|
|
|
<slot name="prefix"></slot>
|
|
|
|
</template>
|
|
|
|
<template #suffix>
|
|
|
|
<i v-show="!showClose" :class="['el-select__caret', 'el-input__icon', 'el-icon-' + iconClass]"></i>
|
2020-10-04 19:57:49 +08:00
|
|
|
<i
|
|
|
|
v-if="showClose"
|
|
|
|
:class="`el-select__caret el-input__icon ${clearIcon}`"
|
|
|
|
@click="handleClearClick"
|
|
|
|
></i>
|
2020-10-03 19:07:02 +08:00
|
|
|
</template>
|
|
|
|
</el-input>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template #default>
|
2020-12-02 22:33:26 +08:00
|
|
|
<el-select-menu>
|
|
|
|
<el-scrollbar
|
|
|
|
v-show="options.length > 0 && !loading"
|
|
|
|
ref="scrollbar"
|
|
|
|
tag="ul"
|
|
|
|
wrap-class="el-select-dropdown__wrap"
|
|
|
|
view-class="el-select-dropdown__list"
|
|
|
|
:class="{ 'is-empty': !allowCreate && query && filteredOptionsCount === 0 }"
|
2020-10-03 19:07:02 +08:00
|
|
|
>
|
2020-12-02 22:33:26 +08:00
|
|
|
<el-option
|
|
|
|
v-if="showNewOption"
|
|
|
|
:value="query"
|
|
|
|
:created="true"
|
|
|
|
/>
|
|
|
|
<slot></slot>
|
|
|
|
</el-scrollbar>
|
|
|
|
<template v-if="emptyText && (!allowCreate || loading || (allowCreate && options.length === 0 ))">
|
|
|
|
<slot v-if="$slots.empty" name="empty"></slot>
|
|
|
|
<p v-else class="el-select-dropdown__empty">
|
|
|
|
{{ emptyText }}
|
|
|
|
</p>
|
|
|
|
</template>
|
|
|
|
</el-select-menu>
|
2020-10-03 19:07:02 +08:00
|
|
|
</template>
|
|
|
|
</el-popper>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-11-02 11:05:08 +08:00
|
|
|
import {
|
|
|
|
toRefs,
|
|
|
|
defineComponent,
|
|
|
|
onMounted,
|
|
|
|
onBeforeUnmount,
|
|
|
|
nextTick,
|
|
|
|
reactive,
|
|
|
|
provide,
|
2020-12-31 10:41:50 +08:00
|
|
|
computed,
|
2020-11-02 11:05:08 +08:00
|
|
|
} from 'vue'
|
2020-11-20 20:24:16 +08:00
|
|
|
import ElInput from '@element-plus/input'
|
2020-10-03 19:07:02 +08:00
|
|
|
import ElOption from './option.vue'
|
|
|
|
import ElSelectMenu from './select-dropdown.vue'
|
2020-11-20 20:24:16 +08:00
|
|
|
import ElTag from '@element-plus/tag'
|
|
|
|
import ElPopper from '@element-plus/popper'
|
|
|
|
import ElScrollbar from '@element-plus/scrollbar'
|
2020-10-29 16:13:29 +08:00
|
|
|
import { ClickOutside } from '@element-plus/directives'
|
2020-10-03 19:07:02 +08:00
|
|
|
import { addResizeListener, removeResizeListener } from '@element-plus/utils/resize-event'
|
|
|
|
import { t } from '@element-plus/locale'
|
2020-11-02 11:05:08 +08:00
|
|
|
import { UPDATE_MODEL_EVENT, CHANGE_EVENT } from '@element-plus/utils/constants'
|
|
|
|
import { isValidComponentSize } from '@element-plus/utils/validators'
|
2020-10-03 19:07:02 +08:00
|
|
|
import { useSelect, useSelectStates } from './useSelect'
|
|
|
|
import { selectKey } from './token'
|
2020-12-07 11:29:08 +08:00
|
|
|
import { useFocus } from '@element-plus/hooks'
|
2020-10-03 19:07:02 +08:00
|
|
|
|
2020-11-02 11:05:08 +08:00
|
|
|
import type { PropType } from 'vue'
|
2020-10-03 19:07:02 +08:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'ElSelect',
|
|
|
|
componentName: 'ElSelect',
|
|
|
|
components: {
|
|
|
|
ElInput,
|
|
|
|
ElSelectMenu,
|
|
|
|
ElOption,
|
|
|
|
ElTag,
|
|
|
|
ElScrollbar,
|
|
|
|
ElPopper,
|
|
|
|
},
|
|
|
|
directives: { ClickOutside },
|
|
|
|
props: {
|
|
|
|
name: String,
|
|
|
|
id: String,
|
2020-12-31 14:11:35 +08:00
|
|
|
modelValue: [Array, String, Number, Boolean, Object],
|
2020-10-03 19:07:02 +08:00
|
|
|
autocomplete: {
|
|
|
|
type: String,
|
|
|
|
default: 'off',
|
|
|
|
},
|
|
|
|
automaticDropdown: Boolean,
|
2020-11-02 11:05:08 +08:00
|
|
|
size: {
|
|
|
|
type: String as PropType<ComponentSize>,
|
|
|
|
validator: isValidComponentSize,
|
|
|
|
},
|
2020-10-03 19:07:02 +08:00
|
|
|
disabled: Boolean,
|
|
|
|
clearable: Boolean,
|
|
|
|
filterable: Boolean,
|
|
|
|
allowCreate: Boolean,
|
|
|
|
loading: Boolean,
|
2020-12-02 22:33:26 +08:00
|
|
|
popperClass: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
2020-10-03 19:07:02 +08:00
|
|
|
remote: Boolean,
|
|
|
|
loadingText: String,
|
|
|
|
noMatchText: String,
|
|
|
|
noDataText: String,
|
|
|
|
remoteMethod: Function,
|
|
|
|
filterMethod: Function,
|
|
|
|
multiple: Boolean,
|
|
|
|
multipleLimit: {
|
|
|
|
type: Number,
|
|
|
|
default: 0,
|
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
defaultFirstOption: Boolean,
|
|
|
|
reserveKeyword: Boolean,
|
|
|
|
valueKey: {
|
|
|
|
type: String,
|
|
|
|
default: 'value',
|
|
|
|
},
|
|
|
|
collapseTags: Boolean,
|
2020-12-05 12:30:15 +08:00
|
|
|
popperAppendToBody: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-10-04 19:57:49 +08:00
|
|
|
clearIcon: {
|
|
|
|
type: String,
|
|
|
|
default: 'el-icon-circle-close',
|
|
|
|
},
|
2020-10-03 19:07:02 +08:00
|
|
|
},
|
2020-11-02 11:05:08 +08:00
|
|
|
emits: [UPDATE_MODEL_EVENT, CHANGE_EVENT, 'remove-tag', 'clear', 'visible-change', 'focus', 'blur'],
|
2020-10-03 19:07:02 +08:00
|
|
|
|
|
|
|
setup(props, ctx) {
|
|
|
|
const states = useSelectStates(props)
|
|
|
|
const {
|
|
|
|
selectSize,
|
|
|
|
readonly,
|
|
|
|
handleResize,
|
|
|
|
collapseTagSize,
|
|
|
|
debouncedOnInputChange,
|
|
|
|
debouncedQueryChange,
|
|
|
|
deletePrevTag,
|
|
|
|
deleteTag,
|
|
|
|
deleteSelected,
|
|
|
|
handleOptionSelect,
|
|
|
|
scrollToOption,
|
|
|
|
setSelected,
|
|
|
|
resetInputHeight,
|
|
|
|
managePlaceholder,
|
|
|
|
showClose,
|
|
|
|
selectDisabled,
|
|
|
|
iconClass,
|
|
|
|
showNewOption,
|
|
|
|
emptyText,
|
|
|
|
toggleLastOptionHitState,
|
|
|
|
resetInputState,
|
|
|
|
handleComposition,
|
2020-12-04 13:39:01 +08:00
|
|
|
onOptionCreate,
|
2020-10-03 19:07:02 +08:00
|
|
|
onOptionDestroy,
|
|
|
|
handleMenuEnter,
|
|
|
|
handleFocus,
|
|
|
|
blur,
|
|
|
|
handleBlur,
|
|
|
|
handleClearClick,
|
|
|
|
handleClose,
|
|
|
|
toggleMenu,
|
|
|
|
selectOption,
|
|
|
|
getValueKey,
|
|
|
|
navigateOptions,
|
|
|
|
dropMenuVisible,
|
|
|
|
|
|
|
|
reference,
|
|
|
|
input,
|
|
|
|
popper,
|
|
|
|
tags,
|
|
|
|
selectWrapper,
|
|
|
|
scrollbar,
|
|
|
|
} = useSelect(props, states, ctx)
|
|
|
|
|
2020-12-07 11:29:08 +08:00
|
|
|
const { focus } = useFocus(reference)
|
|
|
|
|
2020-10-03 19:07:02 +08:00
|
|
|
const {
|
|
|
|
inputWidth,
|
|
|
|
selected,
|
|
|
|
inputLength,
|
|
|
|
filteredOptionsCount,
|
|
|
|
visible,
|
|
|
|
softFocus,
|
|
|
|
selectedLabel,
|
|
|
|
hoverIndex,
|
|
|
|
query,
|
|
|
|
inputHovering,
|
|
|
|
currentPlaceholder,
|
|
|
|
menuVisibleOnFocus,
|
|
|
|
isOnComposition,
|
|
|
|
isSilentBlur,
|
|
|
|
options,
|
|
|
|
cachedOptions,
|
|
|
|
optionsCount,
|
|
|
|
} = toRefs(states)
|
|
|
|
|
|
|
|
provide(selectKey, reactive({
|
2020-11-02 11:05:08 +08:00
|
|
|
props,
|
2020-10-03 19:07:02 +08:00
|
|
|
options,
|
|
|
|
cachedOptions,
|
|
|
|
optionsCount,
|
|
|
|
filteredOptionsCount,
|
|
|
|
hoverIndex,
|
|
|
|
handleOptionSelect,
|
|
|
|
selectEmitter: states.selectEmitter,
|
2020-12-04 13:39:01 +08:00
|
|
|
onOptionCreate,
|
2020-10-03 19:07:02 +08:00
|
|
|
onOptionDestroy,
|
|
|
|
selectWrapper,
|
2020-11-02 11:05:08 +08:00
|
|
|
selected,
|
|
|
|
setSelected,
|
2020-10-03 19:07:02 +08:00
|
|
|
}))
|
|
|
|
|
|
|
|
onMounted(() => {
|
2020-11-18 22:20:36 +08:00
|
|
|
states.cachedPlaceHolder = currentPlaceholder.value = (props.placeholder || t('el.select.placeholder'))
|
2020-10-03 19:07:02 +08:00
|
|
|
if (props.multiple && Array.isArray(props.modelValue) && props.modelValue.length > 0) {
|
|
|
|
currentPlaceholder.value = ''
|
|
|
|
}
|
2020-11-02 11:05:08 +08:00
|
|
|
addResizeListener(selectWrapper.value as any, handleResize)
|
2020-10-03 19:07:02 +08:00
|
|
|
if (reference.value && reference.value.$el) {
|
|
|
|
const sizeMap = {
|
|
|
|
medium: 36,
|
|
|
|
small: 32,
|
|
|
|
mini: 28,
|
|
|
|
}
|
2020-12-04 16:58:39 +08:00
|
|
|
const input = reference.value.input
|
2020-10-03 19:07:02 +08:00
|
|
|
states.initialInputHeight = input.getBoundingClientRect().height || sizeMap[selectSize.value]
|
|
|
|
}
|
|
|
|
if (props.remote && props.multiple) {
|
|
|
|
resetInputHeight()
|
|
|
|
}
|
|
|
|
nextTick(() => {
|
|
|
|
if (reference.value.$el) {
|
|
|
|
inputWidth.value = reference.value.$el.getBoundingClientRect().width
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setSelected()
|
|
|
|
})
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
2020-11-02 11:05:08 +08:00
|
|
|
removeResizeListener(selectWrapper.value as any, handleResize)
|
2020-10-03 19:07:02 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if (props.multiple && !Array.isArray(props.modelValue)) {
|
|
|
|
ctx.emit(UPDATE_MODEL_EVENT, [])
|
|
|
|
}
|
|
|
|
if (!props.multiple && Array.isArray(props.modelValue)) {
|
|
|
|
ctx.emit(UPDATE_MODEL_EVENT, '')
|
|
|
|
}
|
2020-12-04 13:39:01 +08:00
|
|
|
|
2020-12-31 10:41:50 +08:00
|
|
|
const popperPaneRef = computed(() => {
|
|
|
|
return popper.value?.popperRef
|
|
|
|
})
|
|
|
|
|
2020-10-03 19:07:02 +08:00
|
|
|
return {
|
|
|
|
selectSize,
|
|
|
|
readonly,
|
|
|
|
handleResize,
|
|
|
|
collapseTagSize,
|
|
|
|
debouncedOnInputChange,
|
|
|
|
debouncedQueryChange,
|
|
|
|
deletePrevTag,
|
|
|
|
deleteTag,
|
|
|
|
deleteSelected,
|
|
|
|
handleOptionSelect,
|
|
|
|
scrollToOption,
|
|
|
|
inputWidth,
|
|
|
|
selected,
|
|
|
|
inputLength,
|
|
|
|
filteredOptionsCount,
|
|
|
|
visible,
|
|
|
|
softFocus,
|
|
|
|
selectedLabel,
|
|
|
|
hoverIndex,
|
|
|
|
query,
|
|
|
|
inputHovering,
|
|
|
|
currentPlaceholder,
|
|
|
|
menuVisibleOnFocus,
|
|
|
|
isOnComposition,
|
|
|
|
isSilentBlur,
|
|
|
|
options,
|
|
|
|
resetInputHeight,
|
|
|
|
managePlaceholder,
|
|
|
|
showClose,
|
|
|
|
selectDisabled,
|
|
|
|
iconClass,
|
|
|
|
showNewOption,
|
|
|
|
emptyText,
|
|
|
|
toggleLastOptionHitState,
|
|
|
|
resetInputState,
|
|
|
|
handleComposition,
|
|
|
|
handleMenuEnter,
|
|
|
|
handleFocus,
|
|
|
|
blur,
|
|
|
|
handleBlur,
|
|
|
|
handleClearClick,
|
|
|
|
handleClose,
|
|
|
|
toggleMenu,
|
|
|
|
selectOption,
|
|
|
|
getValueKey,
|
|
|
|
navigateOptions,
|
|
|
|
dropMenuVisible,
|
2020-12-07 11:29:08 +08:00
|
|
|
focus,
|
2020-10-03 19:07:02 +08:00
|
|
|
|
|
|
|
reference,
|
|
|
|
input,
|
|
|
|
popper,
|
2020-12-31 10:41:50 +08:00
|
|
|
popperPaneRef,
|
2020-10-03 19:07:02 +08:00
|
|
|
tags,
|
|
|
|
selectWrapper,
|
|
|
|
scrollbar,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
</script>
|