Skip to content

Commit

Permalink
add item_select event
Browse files Browse the repository at this point in the history
  • Loading branch information
oyejorge committed Jun 8, 2021
1 parent f17d382 commit e53dfcc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
7 changes: 6 additions & 1 deletion doc_src/pages/docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ select.off('event_name', handler);
<tr>
<td><code>"item_add"</code></td>
<td><code>value</code>, <code>item</code></td>
<td>Invoked when an item is selected.</td>
<td>Invoked when an item is added (i.e., when an option is selected)</td>
</tr>
<tr>
<td><code>"item_remove"</code></td>
<td><code>value</code>, <code>$item</code></td>
<td>Invoked when an item is deselected.</td>
</tr>
<tr>
<td><code>"item_select"</code></td>
<td><code>item</code></td>
<td>Invoked when an item is selected.</td>
</tr>
<tr>
<td><code>"clear"</code></td>
<td></td>
Expand Down
10 changes: 6 additions & 4 deletions src/tom-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ export default class TomSelect extends MicroPlugin(MicroEvent){
'change' : 'onChange',
'item_add' : 'onItemAdd',
'item_remove' : 'onItemRemove',
'item_select' : 'onItemSelect',
'clear' : 'onClear',
'option_add' : 'onOptionAdd',
'option_remove' : 'onOptionRemove',
Expand Down Expand Up @@ -1013,13 +1014,14 @@ export default class TomSelect extends MicroPlugin(MicroEvent){
*
*/
setActiveItemClass( item:TomItem ){

var last_active = this.control.querySelector('.last-active');
const self = this;
const last_active = self.control.querySelector('.last-active');
if( last_active ) removeClasses(last_active as HTMLElement,'last-active');

addClasses(item,'active last-active');
if( this.activeItems.indexOf(item) == -1 ){
this.activeItems.push( item );
self.trigger('item_select', item);
if( self.activeItems.indexOf(item) == -1 ){
self.activeItems.push( item );
}
}

Expand Down
53 changes: 13 additions & 40 deletions test/tests/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ describe('Events', function() {
});

describe('item_add', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select><option value="a"></option><option value="b"></option><option value="c"></option></select>', {});
test.instance.on('item_add', function() {
done();
});
test.instance.addItem('b');
});
it_n('should contain item\'s value and element', function(done) {
var test = setup_test('<select><option value="a"></option><option value="b"></option><option value="c"></option></select>', {});
test.instance.on('item_add', function(value, item) {
Expand All @@ -177,13 +170,6 @@ describe('Events', function() {
});

describe('item_remove', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select multiple><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('item_remove', function() {
done();
});
test.instance.removeItem('a');
});
it_n('should contain item\'s value and element', function(done) {
var test = setup_test('<select multiple><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('item_remove', function(value, item) {
Expand All @@ -195,6 +181,19 @@ describe('Events', function() {
});
});

describe('item_select', function() {
it_n('should contain item\'s element', function(done) {
var test = setup_test('AB_Multi');
test.instance.on('item_select', function(item) {
expect(item.dataset.value).to.be.equal('b');
done();
});
test.instance.addItem('b');
var item = test.instance.getItem('b');
click(item);
});
});

describe('clear', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
Expand All @@ -206,11 +205,6 @@ describe('Events', function() {
});

describe('optgroup_add', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('optgroup_add', function() { done(); });
test.instance.addOptionGroup('id', {label: 'Group'});
});
it_n('should contain optgroup id', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('optgroup_add', function(id, data) {
Expand Down Expand Up @@ -254,13 +248,6 @@ describe('Events', function() {
});

describe('option_add', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('option_add', function() {
done();
});
test.instance.addOption({value: 'e'});
});
it_n('should contain option value', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('option_add', function(value, data) {
Expand All @@ -281,13 +268,6 @@ describe('Events', function() {
});

describe('option_remove', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('option_remove', function() {
done();
});
test.instance.removeOption('a');
});
it_n('should contain option value', function(done) {
var test = setup_test('<select><option value="a" selected></option><option value="b" selected></option><option value="c"></option></select>', {});
test.instance.on('option_remove', function(value) {
Expand Down Expand Up @@ -340,13 +320,6 @@ describe('Events', function() {
});

describe('type', function() {
it_n('should be triggered', function(done) {
var test = setup_test('<select></select>', {create: true});
test.instance.on('type', function() {
done();
});
syn.click(test.instance.control).type('a', test.instance.control_input);
});
it_n('should contain current value', function(done) {
var test = setup_test('<select></select>', {create: true});
test.instance.on('type', function(value) {
Expand Down

0 comments on commit e53dfcc

Please sign in to comment.