Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#93 fixed sharing onOptionClick handler in multiple autocomplete instances #94

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@

// Initialize dropdown
let dropdownOptions = $.extend(
{},
Autocomplete.defaults.dropdownOptions,
this.options.dropdownOptions
);
Expand Down Expand Up @@ -305,7 +306,7 @@
//custom filters may return results where the string does not match any part
if (start == -1 || end == -1) {
return [label, '', ''];
}
}
return [label.slice(0, start), label.slice(start, end + 1), label.slice(end + 1)];
}

Expand Down Expand Up @@ -389,7 +390,7 @@
const item = document.createElement('li');
if (!!entry.data) {
const img = document.createElement('img');
img.classList.add("right", "circle");
img.classList.add('right', 'circle');
img.src = entry.data;
item.appendChild(img);
}
Expand All @@ -399,11 +400,11 @@
if (this.options.allowUnsafeHTML) {
s.innerHTML = parts[0] + '<span class="highlight">' + parts[1] + '</span>' + parts[2];
} else {
s.appendChild(document.createTextNode(parts[0]))
if (!!parts[1]){
s.appendChild(document.createTextNode(parts[0]));
if (!!parts[1]) {
const highlight = document.createElement('span');
highlight.textContent = parts[1];
highlight.classList.add("highlight");
highlight.classList.add('highlight');
s.appendChild(highlight);
s.appendChild(document.createTextNode(parts[2]));
}
Expand Down
4 changes: 3 additions & 1 deletion js/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@
if (!!this.options.container) {
$(this.options.container).append(this.dropdownEl);
} else if (containerEl) {
$(containerEl).append(this.dropdownEl);
if (!containerEl.contains(this.dropdownEl)) {
$(containerEl).append(this.dropdownEl);
}
} else {
this.$el.after(this.dropdownEl);
}
Expand Down
41 changes: 41 additions & 0 deletions tests/spec/autocomplete/autocompleteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,47 @@ describe("Autocomplete Plugin", function () {
done();
}, 200);
});

it("should select option on click", function(done) {
let normal = document.querySelector('#normal-autocomplete');

M.Autocomplete.init(normal, { data: { 'Value A': null }, minLength: 0 });

openDropdownAndSelectFirstOption(normal, () => {
expect(normal.value).toEqual('Value A', 'Value should equal chosen option.');
done();
});
});

it("should select proper options on both autocompletes", function(done) {
let normal = document.querySelector('#normal-autocomplete');
let limited = document.querySelector('#limited-autocomplete');
M.Autocomplete.init(normal, { data: { 'Value A': null }, minLength: 0 });
M.Autocomplete.init(limited, { data: { 'Value B': null }, minLength: 0 });

openDropdownAndSelectFirstOption(normal, () => {
openDropdownAndSelectFirstOption(limited, () => {
expect(normal.value).toEqual('Value A', 'Value should equal chosen option.');
expect(limited.value).toEqual('Value B', 'Value should equal chosen option.');
done();
});
});
});
});

function openDropdownAndSelectFirstOption(autocomplete, onFinish) {
click(autocomplete);

setTimeout(function() {
let firstOption = autocomplete.parentNode.querySelector('.autocomplete-content li');
click(firstOption);

setTimeout(function() {
onFinish();
}, 300);

}, 200);
}


});