Commit 1e780910 authored by baiyaaaaa's avatar baiyaaaaa

fix autocomplete

parent de8c3b8b
This diff is collapsed.
...@@ -12,5 +12,6 @@ ...@@ -12,5 +12,6 @@
"author": "haiping.zeng<haiping.zeng@ele.me>", "author": "haiping.zeng<haiping.zeng@ele.me>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"vue-clickoutside": "^0.1.0"
} }
} }
<template> <template>
<div class="el-autocomplete"> <div class="el-autocomplete" v-clickoutside="handleBlur">
<el-input <el-input
:value="value" :value="value"
:disabled="disabled" :disabled="disabled"
:placeholder="placeholder" :placeholder="placeholder"
:name = 'name' :name='name'
@onchange="handleChange" @onchange="handleChange"
@onfocus="handleFocus()" @onfocus="handleFocus"
@onblur="handleBlur()" @keydown.up.native="highlight(highlightedIndex - 1)"
@keydown.up="highlight(highlightedIndex - 1)" @keydown.down.native="highlight(highlightedIndex + 1)"
@keydown.down="highlight(highlightedIndex + 1)" @keydown.enter.native="select(highlightedIndex)"
@keydown.enter="select(highlightedIndex)"
></el-input> ></el-input>
<ul <transition name="md-fade-bottom">
v-show="showSuggestions && !loading && suggestions.length > 0" <ul
class="el-autocomplete__suggestions" v-show="suggestionVisible && !loading && suggestions.length > 0"
:class="[partial ? partial.name : '']" class="el-autocomplete__suggestions"
transition="md-fade-bottom" ref="suggestions"
v-el:suggestions >
> <li
<li :class="{'highlighted': highlightedIndex === $index}" @click="select($index)" v-for="item in suggestions">{{item.display}}</li> v-if="!customItem"
</ul> :class="{'highlighted': highlightedIndex === index}"
<div @click="select(index)"
v-show="showSuggestions && loading" v-for="(item, index) in suggestions">
class="el-autocomplete__suggestions is-loading" {{item.display}}
> </li>
<i class="el-icon-loading"></i> <component
</div> v-else
:is="customItem"
@click.native="select(index)"
v-for="(item, index) in suggestions"
:item="item"
:index="index">
</component>
</ul>
</transition>
<transition name="md-fade-bottom">
<div
v-show="suggestionVisible && loading"
class="el-autocomplete__suggestions is-loading"
>
<i class="el-icon-loading"></i>
</div>
</transition>
</div> </div>
</template> </template>
<script> <script>
import ElInput from 'packages/input/index.js'; import ElInput from 'packages/input/index.js';
import Vue from 'vue';
import VueClickoutside from 'main/utils/clickoutside';
Vue.use(VueClickoutside);
export default { export default {
name: 'ElAutocomplete', name: 'ElAutocomplete',
...@@ -42,61 +60,58 @@ ...@@ -42,61 +60,58 @@
placeholder: String, placeholder: String,
disabled: Boolean, disabled: Boolean,
name: String, name: String,
suggestions: [Array, Object],
value: String, value: String,
showOnUpDown: Boolean, fetchSuggestions: Function,
partial: Object triggerOnfocus: {
type: Boolean,
default: true
},
customItem: String
}, },
data() { data() {
return { return {
showSuggestions: false, suggestions: [],
suggestionVisible: false,
inputFocusing: false, inputFocusing: false,
loading: false, loading: false,
highlightedIndex: -1 highlightedIndex: -1
}; };
}, },
created() {
if (this.partial) {
this.$options.template = this.$options.template.replace(/(item\sin\ssuggestions">)(?:.|\s)*?(<)/, '$1' + this.partial.template + '$2');
}
},
watch: {
'suggestions'(val) {
if (val && val.then) {
this.loading = true;
this.suggestions.then((res) => {
this.loading = false;
this.suggestions = res;
});
}
}
},
methods: { methods: {
handleChange(value) { handleChange(value) {
this.value = value; this.$emit('input', value);
this.showSuggestions = true; this.showSuggestions(value);
}, },
handleFocus() { handleFocus() {
if (!this.showOnUpDown) { if (this.triggerOnfocus) {
this.showSuggestions = true; this.showSuggestions(this.value);
} }
}, },
handleBlur() { handleBlur() {
this.showSuggestions = false; this.suggestionVisible = false;
}, },
select(index) { select(index) {
debugger;
if (this.suggestions && this.suggestions[index]) { if (this.suggestions && this.suggestions[index]) {
this.value = this.suggestions[index].value; this.$emit('input', this.suggestions[index].value);
this.$nextTick(() => { this.$nextTick(() => {
this.showSuggestions = false; this.suggestionVisible = false;
}); });
} }
}, },
showSuggestions(value) {
this.suggestionVisible = true;
this.loading = true;
this.fetchSuggestions(value, (suggestions) => {
this.loading = false;
this.suggestions = suggestions;
});
},
getSuggestionElement(index) { getSuggestionElement(index) {
if (!this.suggestions || !this.suggestions[index]) { if (!this.suggestions || !this.suggestions[index]) {
return null; return null;
} else { } else {
return this.$els.suggestions.children[index]; return this.$refs.suggestions.children[index];
} }
}, },
highlight(index) { highlight(index) {
...@@ -107,7 +122,7 @@ ...@@ -107,7 +122,7 @@
} }
var elSelect = this.getSuggestionElement(index); var elSelect = this.getSuggestionElement(index);
var elSuggestions = this.$els.suggestions; var elSuggestions = this.$refs.suggestions;
var scrollTop = elSuggestions.scrollTop; var scrollTop = elSuggestions.scrollTop;
var offsetTop = elSelect.offsetTop; var offsetTop = elSelect.offsetTop;
...@@ -121,7 +136,7 @@ ...@@ -121,7 +136,7 @@
this.highlightedIndex = index; this.highlightedIndex = index;
if (this.showOnUpDown) { if (this.showOnUpDown) {
this.showSuggestions = true; this.suggestionVisible = true;
} }
} }
} }
......
...@@ -28,28 +28,28 @@ ...@@ -28,28 +28,28 @@
transition: all .3s cubic-bezier(.55,0,.1,1); transition: all .3s cubic-bezier(.55,0,.1,1);
} }
.md-fade-center-enter, .md-fade-center-enter,
.md-fade-center-leave { .md-fade-center-leave,
opacity: 0;
transform: scaleY(0);
}
.md-fade-center-leave-active { .md-fade-center-leave-active {
opacity: 0; opacity: 0;
transform: scaleY(0); transform: scaleY(0);
} }
.md-fade-bottom-transition { .md-fade-bottom-enter-active,
.md-fade-bottom-leave-active {
opacity: 1; opacity: 1;
transform: scaleY(1); transform: scaleY(1);
transition: var(--md-fade-transition); transition: var(--md-fade-transition);
transform-origin: center top; transform-origin: center top;
} }
.md-fade-bottom-enter, .md-fade-bottom-enter,
.md-fade-bottom-leave { .md-fade-bottom-leave,
.md-fade-bottom-leave-active {
opacity: 0; opacity: 0;
transform: scaleY(0); transform: scaleY(0);
} }
.md-fade-top-transition { .md-fade-top-enter-active,
.md-fade-top-leave-active {
opacity: 1; opacity: 1;
transform: scaleY(1); transform: scaleY(1);
transition: var(--md-fade-transition); transition: var(--md-fade-transition);
...@@ -57,40 +57,47 @@ ...@@ -57,40 +57,47 @@
} }
.md-fade-top-enter, .md-fade-top-enter,
.md-fade-top-leave { .md-fade-top-leave,
.md-fade-top-leave-active {
opacity: 0; opacity: 0;
transform: scaleY(0); transform: scaleY(0);
} }
.md-fade-left-transition { .md-fade-left-enter-active,
.md-fade-left-leave-active {
opacity: 1; opacity: 1;
transform: scaleX(1); transform: scaleX(1);
transition: var(--md-fade-transition); transition: var(--md-fade-transition);
transform-origin: right center; transform-origin: right center;
} }
.md-fade-left-enter, .md-fade-left-enter,
.md-fade-left-leave { .md-fade-left-leave,
.md-fade-left-leave-active {
opacity: 0; opacity: 0;
transform: scaleX(0); transform: scaleX(0);
} }
.md-fade-right-transition { .md-fade-right-enter-active,
.md-fade-right-leave-active {
opacity: 1; opacity: 1;
transform: scaleX(1); transform: scaleX(1);
transition: var(--md-fade-transition); transition: var(--md-fade-transition);
transform-origin: left center; transform-origin: left center;
} }
.md-fade-right-enter, .md-fade-right-enter,
.md-fade-right-leave { .md-fade-right-leave,
.md-fade-right-leave-active {
opacity: 0; opacity: 0;
transform: scaleX(0); transform: scaleX(0);
} }
.fade-enter-active, .fade-leave-active { .fade-enter-active,
.fade-leave-active {
transition: opacity .3s cubic-bezier(.645,.045,.355,1); transition: opacity .3s cubic-bezier(.645,.045,.355,1);
} }
.fade-enter, .fade-enter,
.fade-leave { .fade-leave,
.fade-leave-active {
opacity: 0; opacity: 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment