Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
select elements on mouseover #1
Browse files Browse the repository at this point in the history
oscarotero committed Jul 20, 2017
1 parent c511e9e commit e7beeaf
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion demo/script.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,13 @@ const datalistInput = document.getElementById('input-datalist');

const suggestions = new Suggestions(
datalistInput,
new DatalistSource(datalistInput)
new DatalistSource(datalistInput, {
suggestions: {
render: option => {
return `<strong>${option.label}</strong>`;
}
}
})
);

suggestions.on('select', value => {
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -36,6 +36,6 @@
"webpack": "^3.0.0"
},
"dependencies": {
"d_js": "^1.9.0"
"d_js": "^1.10.0"
}
}
18 changes: 18 additions & 0 deletions src/Source.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,12 @@ export default class Source {

this.element = d.parse('<ul></ul>');
(this.settings.parent || document.body).appendChild(this.element);

var self = this;

d.delegate('mouseenter', this.element, 'li', function(e, target) {
self.selectByElement(target);
});
}

getSuggestion(item) {
@@ -81,6 +87,18 @@ export default class Source {
}
}

selectByElement(element) {
if (this.result[this.current]) {
const key = this.result.findIndex(item => item.element === element);

if (key !== -1) {
this.result[this.current].unselect();
this.current = key;
this.result[this.current].select(this.element);
}
}
}

getByElement(element) {
const item = this.result.find(item => item.element === element);

0 comments on commit e7beeaf

Please sign in to comment.