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

Allow overriding the default Suggestion class #17177

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var _ = function (input, o) {
container: _.CONTAINER,
item: _.ITEM,
replace: _.REPLACE,
suggestion: Suggestion,
tabSelect: false
}, o);

Expand Down Expand Up @@ -307,7 +308,7 @@ _.prototype = {

this.suggestions = this._list
.map(function(item) {
return new Suggestion(me.data(item, value));
return new me.suggestion(me.data(item, value));
})
.filter(function(item) {
return me.filter(item, value);
Expand Down
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ <h1>Extend</h1>
</td>
<td><code class="language-javascript">Awesomplete.DATA</code>: Identity function which just returns the original list item.</td>
</tr>
<tr>
<td><code>suggestion</code></td>
<td>Controls the class used to create new Suggestions.</td>
<td>A class that responds to the <code>label</code>, <code>value</code>, and <code>length</code> properties and the <code>toString</code> and <code>valueOf</code> methods.</td>
<td><code>Suggestion</code>: A basic class, protoyped from <code>String</code>, which provides the requisite functionality.</td>
</tr>
</tbody>
</table>
</section>
Expand Down
107 changes: 107 additions & 0 deletions test/init/optionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe("Constructor options", function () {
it("replaces input value with REPLACE", function () {
expect(this.subject.replace).toEqual(Awesomplete.REPLACE);
});

it("creates a default Suggestion", function () {
expect(this.subject.suggestion.toString()).toMatch(/function Suggestion\(/);
});
});

describe("with custom options in constructor", function () {
Expand Down Expand Up @@ -77,4 +81,107 @@ describe("Constructor options", function () {
expect(this.subject.autoFirst).toBe(true);
});
});

describe("with a custom suggestion option", function () {
def("element", "#with-data-list");

function CustomSuggestion1(data) {
var o = Array.isArray(data)
? { label: data[0], value: data[1] }
: typeof data === "object" && "label" in data && "value" in data ? data : { label: data, value: data };

this.label = o.label || o.value;
this.value = o.value;
this.custom = o.label.toUpperCase();
}

Object.defineProperty(CustomSuggestion1.prototype = Object.create(String.prototype), "length", {
get: function() { return this.label.length; }
});

CustomSuggestion1.prototype.toString = CustomSuggestion1.prototype.valueOf = function () {
return "" + this.label;
};

function CustomSuggestion2(data) {
var o = Array.isArray(data)
? { label: data[0], value: data[1] }
: typeof data === "object" && "label" in data && "value" in data ? data : { label: data, value: data };

this.label = o.label || o.value;
this.value = o.value;
this.custom = o.label.toLowerCase();
}

Object.defineProperty(CustomSuggestion2.prototype = Object.create(String.prototype), "length", {
get: function() { return this.label.length; }
});

CustomSuggestion2.prototype.toString = CustomSuggestion2.prototype.valueOf = function () {
return "" + this.label;
};

describe("using a class", function () {
def("options", function () {
return {
minChars: 0,
filter: function (item) { return item; },
sort: $.noop,
item: function (suggestion) {
var element = document.createElement("li");
element.textContent = suggestion.custom;

return element;
},
replace: $.noop,
suggestion: CustomSuggestion1
};
});

it("overrides simple default options", function () {
this.subject.evaluate();

this.subject._list.forEach(function (item, index) {
expect(this.subject.ul.children[index].textContent).toEqual(item.label.toUpperCase());
}, this);
});
});

describe("using a function to return different classes", function () {
def("options", function () {
return {
minChars: 0,
filter: function (item) { return item; },
sort: $.noop,
item: function (suggestion) {
var element = document.createElement("li");
element.textContent = suggestion.custom;

return element;
},
replace: $.noop,
suggestion: function (datum) {
if (datum.label === "Data") {
return new CustomSuggestion1(datum);
} else {
return new CustomSuggestion2(datum);
}
}
};
});

it("overrides simple default options", function () {
this.subject.suggestion = this.subject.suggestion.bind(this.subject);
this.subject.evaluate();

this.subject._list.forEach(function (item, index) {
if (item.label === "Data") {
expect(this.subject.ul.children[index].textContent).toEqual(item.label.toUpperCase());
} else {
expect(this.subject.ul.children[index].textContent).toEqual(item.label.toLowerCase());
}
}, this);
});
});
});
});