Skip to content

Commit

Permalink
Merge pull request #21 from KazanExpress/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kaskar2008 authored Mar 30, 2018
2 parents ae0ff34 + 654b902 commit be49d92
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 65 deletions.
2 changes: 1 addition & 1 deletion docs/main.js

Large diffs are not rendered by default.

144 changes: 82 additions & 62 deletions lib/vue-simple-suggest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export default {
type: Boolean,
default: false
},
// TODO: Document this!
filter: {
type: Function,
default: el => value ? ~this.displayProperty(el).toLowerCase().indexOf(value.toLowerCase()) : true
},
//
debounce: {
type: Number,
default: 0
Expand Down Expand Up @@ -207,10 +215,8 @@ export default {
if (this.suggestions.length > 0
&& hasKeyCode([this.controlScheme.selectionUp, this.controlScheme.selectionDown], event)
) {
event.preventDefault();
if (!this.listShown) {
this.showList()
}
event.preventDefault()
this.showList()
const isArrowDown = hasKeyCode(this.controlScheme.selectionDown, event)
const direction = isArrowDown * 2 - 1
Expand Down Expand Up @@ -279,82 +285,96 @@ export default {
}
},
async research () {
if (this.canSend) {
this.canSend = false
var result = await this.getSuggestions(this.text)
this.canSend = true
} else {
result = this.suggestions;
try {
if (this.canSend) {
this.canSend = false
var result = await this.getSuggestions(this.text)
this.canSend = true
} else {
result = this.suggestions;
}
}
return result;
catch (e) {
result = [];
throw e;
}
finally {
return result;
}
},
async getSuggestions (value = '') {
if (this.listShown && !value) {
this.hideList()
this.suggestions.splice(0)
return this.suggestions
}
if ((this.minLength > 0) && value.length < this.minLength) {
return this.suggestions
}
this.selected = null
let res;
if ((this.minLength === 0) || value.length >= this.minLength) {
this.listIsRequest && this.$emit('request-start', value)
// Start request if can
if (this.listIsRequest) {
this.$emit('request-start', value)
// TODO: Deprecated, remove in the next minor update
this.listIsRequest && this.$emit('requestStart', value)
try {
if (this.listIsRequest) {
res = (await this.list(value)) || []
} else {
res = this.list;
}
if (!Array.isArray(res)) {
res = [res]
}
if (typeof res[0] === 'object' && !Array.isArray(res[0])) {
this.isSuggestionConverted = false;
} else {
res = res.map((el, i) => ({
[this.valueAttribute]: i,
[this.displayAttribute]: el
}));
this.isSuggestionConverted = true;
}
if (this.filterByQuery) {
res = res.filter(el => value ? ~this.displayProperty(el).toLowerCase().indexOf(value.toLowerCase()) : true);
}
this.listIsRequest && this.$emit('request-done', res)
this.$emit('requestStart', value)
}
// TODO: Deprecated, remove in the next minor update
this.listIsRequest && this.$emit('requestDone', res)
} catch (e) {
if (this.listIsRequest) {
this.$emit('request-failed', e)
// TODO: Deprecated, remove in the next minor update
this.$emit('requestFailed', e)
} else {
throw e;
}
let result = [];
try {
result = this.listIsRequest ? (await this.list(value)) || [] : this.list;
// IFF the result is not an array (just in case!) - make it an array
if (!Array.isArray(result)) { result = [result] }
if (typeof result[0] === 'object' && !Array.isArray(result[0])) {
this.isSuggestionConverted = false;
} else {
result = result.map((el, i) => ({
[this.valueAttribute]: i,
[this.displayAttribute]: el
}));
this.isSuggestionConverted = true;
}
if (this.maxSuggestions) {
res.splice(this.maxSuggestions);
if (this.filterByQuery) {
result = result.filter(this.filter);
}
this.$set(this, 'suggestions', res);
if (this.listIsRequest) {
this.$emit('request-done', result)
if (!this.listShown) {
this.showList()
// TODO: Deprecated, remove in the next minor update
this.$emit('requestDone', result)
}
}
/* hide only if 0 text left, mthrfckr */
else if (this.listShown && !value) {
this.hideList()
this.suggestions.splice(0)
catch (e) {
if (this.listIsRequest) {
this.$emit('request-failed', e)
// TODO: Deprecated, remove in the next minor update
this.$emit('requestFailed', e)
} else {
throw e;
}
}
return this.suggestions;
finally {
if (this.maxSuggestions) {
result.splice(this.maxSuggestions);
}
this.$set(this, 'suggestions', result);
this.showList()
return this.suggestions;
}
},
clearSuggestions () {
this.suggestions.splice(0)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vue-simple-suggest",
"description": "Feature-rich autocomplete component for Vue.js",
"version": "1.2.5",
"version": "1.2.6",
"author": "KazanExpress",
"license": "MIT",
"repository": "KazanExpress/vue-simple-suggest",
Expand Down

0 comments on commit be49d92

Please sign in to comment.