Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #52 from adrian-enspired/fixings
Browse files Browse the repository at this point in the history
Minor Fixes
  • Loading branch information
Mobius1 committed Nov 14, 2017
2 parents bcb6c90 + 8a70581 commit a07dd5f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
64 changes: 37 additions & 27 deletions src/selectr.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,13 @@
}, this);
}

// highlight first selected option if any; first option otherwise
if (f.childElementCount) {
util.removeClass(this.items[this.navIndex], "active");
this.navIndex = f.querySelector(".selectr-option").idx;
this.navIndex = (
f.querySelector(".selectr-option.selected") ||
f.querySelector(".selectr-option")
).idx;
util.addClass(this.items[this.navIndex], "active");
}

Expand Down Expand Up @@ -1155,6 +1159,7 @@
that.toggle();
}

e.stopPropagation();
e.preventDefault();
});

Expand All @@ -1178,14 +1183,17 @@
(! that.opened && ["Enter", "ArrowUp", "ArrowDown"].indexOf(e.key) > -1)
) {
that.toggle();
e.preventDefault();
e.stopPropagation();
return;
}

// Type to search if multiple; type to select otherwise
// make sure e.key is a single, printable character
// .length check is a short-circut to skip checking keys like "ArrowDown", etc.
// prefer "codePoint" methods; they work with the full range of unicode
if (
e.key.length <= 2 &&
String[String.fromCodePoint ? "fromCodePoint" : "fromCharCode"](
e.key[String.codePointAt ? "codePointAt" : "charCodeAt"]( 0 )
) === e.key
Expand All @@ -1203,12 +1211,13 @@
}
typing += e.key;
var found = that.search( typing, true );
if ( found.length ) {
if ( found && found.length ) {
that.clear();
that.setValue( found[0].value );
}
setTimeout(function () { typing = ''; }, 1000);
}
e.preventDefault();
e.stopPropagation();
return;
}
Expand Down Expand Up @@ -1264,6 +1273,9 @@
}
}
}

e.preventDefault();
e.stopPropagation();
});

// Mouseover list items
Expand Down Expand Up @@ -1866,44 +1878,45 @@
return;
}

// we're only going to alter the DOM for "live" searches
var live = false;
if ( ! string ) {
string = this.input.value;
live = true;
}
string = string.toLowerCase();
var f = document.createDocumentFragment();
var results = [];
var f = document.createDocumentFragment();

// Remove message and clear dropdown
this.removeMessage();
util.truncate(this.tree);
string = string.trim().toLowerCase();

if ( string.length > 0 ) {
var compare = anchor ? util.startsWith : util.includes;

// Check the options for the matching string
util.each(this.options, function(i, option) {
util.each( this.options, function ( i, option ) {
var item = this.items[option.idx];
var matches = compare( option.textContent.trim().toLowerCase(), string );

if ( matches && !option.disabled ) {

appendItem( item, f, this.customOption );

util.removeClass( item, "excluded" );

// Underline the matching results
if ( !this.customOption ) {
item.innerHTML = match( string, option );
results.push( { text: option.textContent, value: option.value } );
if ( live ) {
appendItem( item, f, this.customOption );
util.removeClass( item, "excluded" );

// Underline the matching results
if ( !this.customOption ) {
item.innerHTML = match( string, option );
}
}
} else {
} else if ( live ) {
util.addClass( item, "excluded" );
}
}, this);

// Only change Selectr if this is a "live" search (#42)
if ( live ) {
// Remove message and clear dropdown
this.removeMessage();
util.truncate(this.tree);

// Append results
if ( !f.childElementCount ) {
if ( !this.config.taggable ) {
Expand All @@ -1918,17 +1931,13 @@
this.navIndex = firstEl.idx;
util.addClass( firstEl, "active" );
}
}

// Compile and return search results object (#42)
util.each( f.childNodes, function ( i, option ) {
var opt = this.options[option.idx];
results.push( { text: opt.textContent, value: opt.value } );
}, this );
this.tree.appendChild( f );
}
} else {
render.call( this );
render.call(this);
}
this.tree.appendChild( f );

return results;
};

Expand Down Expand Up @@ -2013,6 +2022,7 @@
}

this.opened = false;
this.navigating = false;

if (this.mobileDevice || this.config.nativeDropdown) {
util.removeClass(this.container, "native-open");
Expand Down
3 changes: 3 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@

assert.equal( typeof selectr, "object", "can create new instance" );
assert.equal( selectr.el, s, "instance has reference to <select> element" );

selectr.destroy();
document.body.removeChild(s);
});

QUnit.test( "api methods", function( assert ) {
Expand Down

0 comments on commit a07dd5f

Please sign in to comment.