diff --git a/demo/index.html b/demo/index.html
index d3f0186..b8fb4f2 100644
--- a/demo/index.html
+++ b/demo/index.html
@@ -13,7 +13,7 @@
overflow-y: auto;
display: block;
}
- ul .is-selected {
+ ul .is-focused {
color: blue;
font-weight: bold;
}
diff --git a/demo/script.js b/demo/script.js
index f215e16..eda7c83 100644
--- a/demo/script.js
+++ b/demo/script.js
@@ -1,12 +1,13 @@
-import { Suggestions, Source, AjaxSource } from '../src/suggestions.js';
+import { Suggestions, AjaxSuggestions } from '../src/suggestions.js';
const datalistInput = document.getElementById('input-datalist');
-const source = Source.createFromElement(document.getElementById('colors'));
-const ajax = new AjaxSource('data.json', datalistInput.closest('fieldset'));
-const suggestions = new Suggestions(datalistInput, source);
+const suggestions = Suggestions.createFromElement(document.getElementById('colors'));
+//const suggestions = new AjaxSuggestions('data.json', datalistInput.closest('fieldset'));
-datalistInput.addEventListener('suggestion:choosen', e => {
+suggestions.attachInput(datalistInput);
+
+datalistInput.addEventListener('suggestions:select', e => {
console.log(e.detail);
});
diff --git a/src/suggestions.js b/src/suggestions.js
index 8842790..e724f36 100644
--- a/src/suggestions.js
+++ b/src/suggestions.js
@@ -28,8 +28,8 @@ export class Suggestion {
});
}
- get selected() {
- return this.element.classList.contains('is-selected');
+ get focused() {
+ return this.element.classList.contains('is-focused');
}
render(element) {
@@ -37,7 +37,7 @@ export class Suggestion {
}
refresh(result, filter) {
- this.unselect();
+ this.blur();
if (filter(this)) {
this.parent.element.appendChild(this.element);
@@ -47,41 +47,70 @@ export class Suggestion {
}
}
- select() {
- this.element.classList.add('is-selected');
+ focus() {
+ this.element.classList.add('is-focused');
this.element.dispatchEvent(
- new CustomEvent('suggestion:select', {
+ new CustomEvent('suggestion:focus', {
detail: this,
bubbles: true
})
);
}
- unselect() {
- this.element.classList.remove('is-selected');
+ blur() {
+ this.element.classList.remove('is-focused');
this.element.dispatchEvent(
- new CustomEvent('suggestion:unselect', {
+ new CustomEvent('suggestion:blur', {
detail: this,
bubbles: true
})
);
}
- scroll(scrollGroup) {
- let rect = this.element.getBoundingClientRect();
- const parentRect = this.parent.element.getBoundingClientRect();
+ scroll() {
+ let scroll;
- if (parentRect.top - rect.top > 0) {
- this.parent.element.scrollTop -= parentRect.top - rect.top;
- } else if (parentRect.bottom < rect.bottom) {
- this.element.scrollIntoView(false);
- }
+ //Is in a group
+ if (this.parent.wrapper) {
+ let rectTop, rectBottom;
+ const viewbox = this.parent.parent.element.getBoundingClientRect();
- if (scrollGroup && this.group && !this.element.previousElementSibling) {
- rect = this.group.wrapperElement.getBoundingClientRect();
+ //Is the first element of the group
+ if (!this.element.previousElementSibling) {
+ rectTop = this.parent.wrapper.getBoundingClientRect();
+ rectBottom = this.element.getBoundingClientRect();
+ } else {
+ rectBottom = rectTop = this.element.getBoundingClientRect();
+ }
- if (parentRect.top - rect.top > 0) {
- this.parent.element.scrollTop -= parentRect.top - rect.top;
+ if (viewbox.top - rectTop.top > 0) {
+ scroll = this.element.previousElementSibling
+ ? 'start'
+ : 'center';
+ } else if (viewbox.bottom < rectBottom.bottom) {
+ scroll = this.element.nextElementSibling ? 'end' : 'center';
+ }
+ } else {
+ const viewbox = this.parent.element.getBoundingClientRect();
+ const rect = this.element.getBoundingClientRect();
+
+ if (viewbox.top - rect.top > 0) {
+ scroll = this.element.previousElementSibling
+ ? 'start'
+ : 'center';
+ } else if (viewbox.bottom < rect.bottom) {
+ scroll = this.element.previousElementSibling ? 'end' : 'center';
+ }
+ }
+
+ if (scroll) {
+ try {
+ this.element.scrollIntoView({
+ behavior: 'smooth',
+ block: scroll
+ });
+ } catch (err) {
+ this.element.scrollIntoView();
}
}
}
@@ -144,14 +173,13 @@ export class Group {
}
/**
- * Manage a data source
- * (groups and suggestions)
- * ------------------------
+ * Suggestions
+ * -----------
*/
-export class Source {
- //Create a source from a