Skip to content

Commit 3f11d24

Browse files
committed
#93 fixed sharing onOptionClick handler in multiple autocomplete instances
1 parent 94ad89c commit 3f11d24

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

js/autocomplete.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161

162162
// Initialize dropdown
163163
let dropdownOptions = $.extend(
164+
{},
164165
Autocomplete.defaults.dropdownOptions,
165166
this.options.dropdownOptions
166167
);
@@ -305,7 +306,7 @@
305306
//custom filters may return results where the string does not match any part
306307
if (start == -1 || end == -1) {
307308
return [label, '', ''];
308-
}
309+
}
309310
return [label.slice(0, start), label.slice(start, end + 1), label.slice(end + 1)];
310311
}
311312

@@ -389,7 +390,7 @@
389390
const item = document.createElement('li');
390391
if (!!entry.data) {
391392
const img = document.createElement('img');
392-
img.classList.add("right", "circle");
393+
img.classList.add('right', 'circle');
393394
img.src = entry.data;
394395
item.appendChild(img);
395396
}
@@ -399,11 +400,11 @@
399400
if (this.options.allowUnsafeHTML) {
400401
s.innerHTML = parts[0] + '<span class="highlight">' + parts[1] + '</span>' + parts[2];
401402
} else {
402-
s.appendChild(document.createTextNode(parts[0]))
403-
if (!!parts[1]){
403+
s.appendChild(document.createTextNode(parts[0]));
404+
if (!!parts[1]) {
404405
const highlight = document.createElement('span');
405406
highlight.textContent = parts[1];
406-
highlight.classList.add("highlight");
407+
highlight.classList.add('highlight');
407408
s.appendChild(highlight);
408409
s.appendChild(document.createTextNode(parts[2]));
409410
}

js/dropdown.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@
355355
if (!!this.options.container) {
356356
$(this.options.container).append(this.dropdownEl);
357357
} else if (containerEl) {
358-
$(containerEl).append(this.dropdownEl);
358+
if (!containerEl.contains(this.dropdownEl)) {
359+
$(containerEl).append(this.dropdownEl);
360+
}
359361
} else {
360362
this.$el.after(this.dropdownEl);
361363
}

tests/spec/autocomplete/autocompleteSpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,47 @@ describe("Autocomplete Plugin", function () {
9797
done();
9898
}, 200);
9999
});
100+
101+
it("should select option on click", function(done) {
102+
let normal = document.querySelector('#normal-autocomplete');
103+
104+
M.Autocomplete.init(normal, { data: { 'Value A': null }, minLength: 0 });
105+
106+
openDropdownAndSelectFirstOption(normal, () => {
107+
expect(normal.value).toEqual('Value A', 'Value should equal chosen option.');
108+
done();
109+
});
110+
});
111+
112+
it("should select proper options on both autocompletes", function(done) {
113+
let normal = document.querySelector('#normal-autocomplete');
114+
let limited = document.querySelector('#limited-autocomplete');
115+
M.Autocomplete.init(normal, { data: { 'Value A': null }, minLength: 0 });
116+
M.Autocomplete.init(limited, { data: { 'Value B': null }, minLength: 0 });
117+
118+
openDropdownAndSelectFirstOption(normal, () => {
119+
openDropdownAndSelectFirstOption(limited, () => {
120+
expect(normal.value).toEqual('Value A', 'Value should equal chosen option.');
121+
expect(limited.value).toEqual('Value B', 'Value should equal chosen option.');
122+
done();
123+
});
124+
});
125+
});
100126
});
101127

128+
function openDropdownAndSelectFirstOption(autocomplete, onFinish) {
129+
click(autocomplete);
130+
131+
setTimeout(function() {
132+
let firstOption = autocomplete.parentNode.querySelector('.autocomplete-content li');
133+
click(firstOption);
134+
135+
setTimeout(function() {
136+
onFinish();
137+
}, 300);
138+
139+
}, 200);
140+
}
141+
142+
102143
});

0 commit comments

Comments
 (0)