Skip to content

Commit

Permalink
Merge pull request #481 from yandex-ui/suggest-prevent-add-value-to-i…
Browse files Browse the repository at this point in the history
…nput

Add preventing to insert data to input of suggest
  • Loading branch information
basvasilich committed Apr 6, 2015
2 parents 6cdaf8a + aeb190b commit 4c0f4bd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
47 changes: 38 additions & 9 deletions blocks/suggest/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@
}
},

_value: function(value) {
if (value) {
this._trigger('_setvalue', null, value);
return;
}

return this._super.apply(this, arguments);
},

search: function(value, event) {
this._trigger('_search');

Expand Down Expand Up @@ -229,19 +238,39 @@
this.trigger('nb-type', this, this.getValue());
}.bind(this));

this.$jUI.on('suggestselect.nb-suggest', function(e, item) {
this.$selected = item.item;
if (this.input) {
this.input.setValue(item.item.value);
} else {
this.$control.val(item.item.value);
}
this.trigger('nb-select', this, item.item);
}.bind(this));
this.$jUI.on('suggest_setvalue.nb-suggest', this.onSetValueToInput.bind(this));

this.$jUI.on('suggestselect.nb-suggest', this.onSelectValue.bind(this));

this.trigger('nb-inited', this);
},

/**
* Callback of a `suggestselect` event
* @param {$.Event} e
* @param {Object} item data of selected item
* @private
*/
onSelectValue: function(e, item) {
this.$selected = item.item;

this.trigger('nb-select', this, item.item);
},

/**
* Callback of a `suggest_setvalue` event
* @param {$.Event} e
* @param {string} value
* @private
*/
onSetValueToInput: function(e, value) {
if (this.input) {
this.input.setValue(value);
} else {
this.$control.val(value);
}
},

/**
* Get selected item from suggest
* @return {Object}
Expand Down
59 changes: 59 additions & 0 deletions unittests/spec/suggest/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("suggest Tests", function() {

itShouldBindEvent('keydown.nb-suggest');
itShouldBindEvent('suggest_search.nb-suggest');
itShouldBindEvent('suggest_setvalue.nb-suggest');
itShouldBindEvent('suggestselect.nb-suggest');

it('should trigger event \'nb-inited\'', function() {
Expand All @@ -85,6 +86,64 @@ describe("suggest Tests", function() {
});
});

describe('onSetValueToInput', function() {

beforeEach(function() {
this.event = $.Event('suggest_setvalue');
this._input = this.suggest.input;

sinon.stub(this.suggest.$control, 'val');
this.suggest.input = {
setValue: sinon.stub()
}
});

afterEach(function() {
this.suggest.input = this._input;
this.suggest.$control.val.restore();
});

it('should add the value to nb-input, if it was created', function() {
this.suggest.onSetValueToInput(this.event, 'test-value');

expect(this.suggest.input.setValue.calledWithExactly('test-value')).to.be.equal(true);
expect(this.suggest.$control.val.notCalled).to.be.equal(true);
});

it('should add the value to input control, if nb-input was not created', function() {
this.suggest.input = null;

this.suggest.onSetValueToInput(this.event, 'test-value');

expect(this.suggest.$control.val.called).to.be.equal(true);
});

});

describe('onSelectValue', function() {

beforeEach(function() {
this.event = $.Event('suggestselect');
this.itemData = {
item: {
value: 'test'
}
};
sinon.stub(this.suggest, 'trigger');
});

afterEach(function() {
this.suggest.trigger.restore();
});

it('should be trigger `nb-select` event', function() {
this.suggest.onSelectValue(this.event, this.itemData);

expect(this.suggest.trigger.calledWithExactly('nb-select', this.suggest, this.itemData.item)).to.be.equal(true);
});

});

describe('#destroy', function() {
it('should destroy the Suggest', function() {
sinon.spy(jQuery.fn, 'suggest');
Expand Down

0 comments on commit 4c0f4bd

Please sign in to comment.