Skip to content

Commit d42c4ee

Browse files
author
Natalia Kowalczyk
committed
container function to generate container element
addresses a use case where container element cannot reuse input element parent because input parent clips container content also solves LeaVerou#17086
1 parent f1440f2 commit d42c4ee

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

awesomplete.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var _ = function (input, o) {
3232
data: _.DATA,
3333
filter: _.FILTER_CONTAINS,
3434
sort: o.sort === false ? false : _.SORT_BYLENGTH,
35+
container: _.CONTAINER,
3536
item: _.ITEM,
3637
replace: _.REPLACE
3738
}, o);
@@ -40,10 +41,7 @@ var _ = function (input, o) {
4041

4142
// Create necessary elements
4243

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

4846
this.ul = $.create("ul", {
4947
hidden: "hidden",
@@ -203,11 +201,13 @@ _.prototype = {
203201
$.unbind(this.input, this._events.input);
204202
$.unbind(this.input.form, this._events.form);
205203

206-
//move the input out of the awesomplete container and remove the container and its children
207-
var parentNode = this.container.parentNode;
204+
if (this.input.parentNode === this.container) {
205+
//move the input out of the awesomplete container and remove the container and its children
206+
var parentNode = this.container.parentNode;
208207

209-
parentNode.insertBefore(this.input, this.container);
210-
parentNode.removeChild(this.container);
208+
parentNode.insertBefore(this.input, this.container);
209+
parentNode.removeChild(this.container);
210+
}
211211

212212
//remove autocomplete and aria-autocomplete attributes
213213
this.input.removeAttribute("autocomplete");
@@ -351,6 +351,13 @@ _.SORT_BYLENGTH = function (a, b) {
351351
return a < b? -1 : 1;
352352
};
353353

354+
_.CONTAINER = function (input) {
355+
return $.create("div", {
356+
className: "awesomplete",
357+
around: input
358+
});
359+
}
360+
354361
_.ITEM = function (text, input, item_id) {
355362
var html = input.trim() === "" ? text : text.replace(RegExp($.regExpEscape(input.trim()), "gi"), "<mark>$&</mark>");
356363
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 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)