Skip to content

Commit ac0a08d

Browse files
author
Pablo Henrique Penha Silva
committed
enhancements on autocomplete
1 parent 4633432 commit ac0a08d

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/components/mdInputContainer/mdAutocomplete.vue

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
@blur="onBlur">
55
<md-menu :md-offset-x="8"
66
md-offset-y="45"
7-
ref="menu">
7+
ref="menu"
8+
class="md-autocomplete-menu">
89
<span md-menu-trigger></span>
910
<input class="md-input"
1011
ref="input"
@@ -46,6 +47,7 @@
4647
selected: null,
4748
timeout: 0,
4849
parentContainer: null,
50+
searchButton: null,
4951
};
5052
},
5153
computed: {
@@ -89,6 +91,23 @@
8991
this.onInput();
9092
this.$emit('selected', this.selected, this.$refs.input.value);
9193
},
94+
makeFetchRequest(queryObject) {
95+
return this.fetch(queryObject)
96+
.then((response) => {
97+
let data = response || response.data || response.body;
98+
99+
data = this.prepareResponseData ?
100+
this.prepareResponseData(data) :
101+
data;
102+
this.items = this.limit ?
103+
data.slice(0, this.limit) :
104+
data;
105+
106+
this.loading = false;
107+
108+
this.toggleMenu();
109+
});
110+
},
92111
onFocus() {
93112
if (this.parentContainer) {
94113
this.parentContainer.isFocused = true;
@@ -127,6 +146,13 @@
127146
this.setParentValue(value);
128147
this.updateValues(value);
129148
},
149+
setSearchButton() {
150+
this.searchButton = this.parentContainer.$el.querySelector('[md-autocomplete-search]');
151+
152+
if (this.searchButton) {
153+
this.searchButton.addEventListener('click', this.makeFetchRequest);
154+
}
155+
},
130156
update() {
131157
if (!this.query && !this.list.length) {
132158
return this.reset();
@@ -140,23 +166,7 @@
140166
141167
const queryObject = { [this.queryParam]: this.query };
142168
143-
this.fetch(queryObject)
144-
.then((response) => {
145-
if (this.query) {
146-
let data = response || response.data || response.body;
147-
148-
data = this.prepareResponseData ?
149-
this.prepareResponseData(data) :
150-
data;
151-
this.items = this.limit ?
152-
data.slice(0, this.limit) :
153-
data;
154-
155-
this.loading = false;
156-
157-
this.toggleMenu();
158-
}
159-
});
169+
return this.makeFetchRequest(queryObject);
160170
},
161171
toggleMenu() {
162172
if (this.items.length) {
@@ -170,6 +180,11 @@
170180
this.parentContainer.inputLength = newValue ? newValue.length : 0;
171181
}
172182
},
183+
beforeDestroy() {
184+
if (this.searchButton) {
185+
this.searchButton.removeEventListener('click', this.makeFetchRequest);
186+
}
187+
},
173188
mounted() {
174189
this.$nextTick(() => {
175190
this.parentContainer = getClosestVueParent(this.$parent, 'md-input-container');
@@ -181,6 +196,7 @@
181196
this.query = this.value;
182197
183198
this.verifyProps();
199+
this.setSearchButton();
184200
185201
this.setParentDisabled();
186202
this.setParentRequired();

src/components/mdInputContainer/mdInputContainer.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ $input-size: 32px;
135135
}
136136
}
137137

138+
.md-theme-default.md-input-container .md-autocomplete .md-icon:not(.md-icon-search):after {
139+
height: 0;
140+
}
141+
138142
.md-input-container {
139143
&.md-input-placeholder {
140144
label {

0 commit comments

Comments
 (0)