Skip to content

Commit

Permalink
container function to generate container element
Browse files Browse the repository at this point in the history
addresses a use case where container element cannot reuse input element parent
because input parent clips container content

also solves LeaVerou#17086
  • Loading branch information
Natalia Kowalczyk committed May 27, 2018
1 parent f1440f2 commit d42c4ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
23 changes: 15 additions & 8 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var _ = function (input, o) {
data: _.DATA,
filter: _.FILTER_CONTAINS,
sort: o.sort === false ? false : _.SORT_BYLENGTH,
container: _.CONTAINER,
item: _.ITEM,
replace: _.REPLACE
}, o);
Expand All @@ -40,10 +41,7 @@ var _ = function (input, o) {

// Create necessary elements

this.container = $.create("div", {
className: "awesomplete",
around: input
});
this.container = this.container(input);

this.ul = $.create("ul", {
hidden: "hidden",
Expand Down Expand Up @@ -203,11 +201,13 @@ _.prototype = {
$.unbind(this.input, this._events.input);
$.unbind(this.input.form, this._events.form);

//move the input out of the awesomplete container and remove the container and its children
var parentNode = this.container.parentNode;
if (this.input.parentNode === this.container) {
//move the input out of the awesomplete container and remove the container and its children
var parentNode = this.container.parentNode;

parentNode.insertBefore(this.input, this.container);
parentNode.removeChild(this.container);
parentNode.insertBefore(this.input, this.container);
parentNode.removeChild(this.container);
}

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

_.CONTAINER = function (input) {
return $.create("div", {
className: "awesomplete",
around: input
});
}

_.ITEM = function (text, input, item_id) {
var html = input.trim() === "" ? text : text.replace(RegExp($.regExpEscape(input.trim()), "gi"), "<mark>$&</mark>");
return $.create("li", {
Expand Down
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ <h1>Extend</h1>
<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>
<td>Sorted by length first, order second.</td>
</tr>
<tr>
<td><code>container</code></td>
<td>Controls how list container element generated.</td>
<td>Function that takes one parameter, the user’s input and returns an element.</td>
<td>Generates <code>&lt;div></code> with class <code>awesomplete</code></td>
</tr>
<tr>
<td><code>item</code></td>
<td>Controls how list items are generated.</td>
Expand Down

0 comments on commit d42c4ee

Please sign in to comment.