Skip to content

Commit 466bb15

Browse files
meliteleLeaVerou
authored andcommitted
container function to generate container element (#17143)
I've added a container function to options that will allow client to provide a different container element. The default version of container function creates container element as before. I believe this is in line with the item function that customizes creating item elements. This addresses my use case where container element cannot reuse the input element parent because the input parent clips container's content. It also addresses a case when input element has to be next to it's original siblings. It also solves #17086 & #16718 Thank you for consideration.
1 parent f1440f2 commit 466bb15

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

awesomplete.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ var _ = function (input, o) {
2323
this.input.setAttribute("aria-owns", "awesomplete_list_" + this.count);
2424
this.input.setAttribute("role", "combobox");
2525

26-
o = o || {};
26+
// store constructor options in case we need to distinguish
27+
// between default and customized behavior later on
28+
this.options = o = o || {};
2729

2830
configure(this, {
2931
minChars: 2,
@@ -32,6 +34,7 @@ var _ = function (input, o) {
3234
data: _.DATA,
3335
filter: _.FILTER_CONTAINS,
3436
sort: o.sort === false ? false : _.SORT_BYLENGTH,
37+
container: _.CONTAINER,
3538
item: _.ITEM,
3639
replace: _.REPLACE
3740
}, o);
@@ -40,10 +43,7 @@ var _ = function (input, o) {
4043

4144
// Create necessary elements
4245

43-
this.container = $.create("div", {
44-
className: "awesomplete",
45-
around: input
46-
});
46+
this.container = this.container(input);
4747

4848
this.ul = $.create("ul", {
4949
hidden: "hidden",
@@ -203,11 +203,14 @@ _.prototype = {
203203
$.unbind(this.input, this._events.input);
204204
$.unbind(this.input.form, this._events.form);
205205

206-
//move the input out of the awesomplete container and remove the container and its children
207-
var parentNode = this.container.parentNode;
206+
// cleanup container if it was created by Awesomplete but leave it alone otherwise
207+
if (!this.options.container) {
208+
//move the input out of the awesomplete container and remove the container and its children
209+
var parentNode = this.container.parentNode;
208210

209-
parentNode.insertBefore(this.input, this.container);
210-
parentNode.removeChild(this.container);
211+
parentNode.insertBefore(this.input, this.container);
212+
parentNode.removeChild(this.container);
213+
}
211214

212215
//remove autocomplete and aria-autocomplete attributes
213216
this.input.removeAttribute("autocomplete");
@@ -351,6 +354,13 @@ _.SORT_BYLENGTH = function (a, b) {
351354
return a < b? -1 : 1;
352355
};
353356

357+
_.CONTAINER = function (input) {
358+
return $.create("div", {
359+
className: "awesomplete",
360+
around: input
361+
});
362+
}
363+
354364
_.ITEM = function (text, input, item_id) {
355365
var html = input.trim() === "" ? text : text.replace(RegExp($.regExpEscape(input.trim()), "gi"), "<mark>$&</mark>");
356366
return $.create("li", {

index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ <h1>Extend</h1>
241241
<td>Sort function (will be passed directly to <code>Array.prototype.sort()</code>) to sort the items after they have been filtered and before they are truncated and converted to HTML elements. If value is <code>false</code>, sorting will be disabled.</td>
242242
<td>Sorted by length first, order second.</td>
243243
</tr>
244+
<tr>
245+
<td><code>container</code></td>
246+
<td>Controls how list container element is generated.</td>
247+
<td>Function that takes one parameter, the user’s input and returns an element.</td>
248+
<td>Generates <code>&lt;div></code> with class <code>awesomplete</code></td>
249+
</tr>
244250
<tr>
245251
<td><code>item</code></td>
246252
<td>Controls how list items are generated.</td>

0 commit comments

Comments
 (0)