Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow free input #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions js/bootstrap-combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
this.options = $.extend({}, $.fn.combobox.defaults, options);
this.template = this.options.template || this.template
this.$source = $(element);
this.freeInput = this.options.freeInput || null
this.$container = this.setup();
this.$element = this.$container.find('input[type=text]');
this.$target = this.$container.find('input[type=hidden]');
Expand Down Expand Up @@ -80,7 +81,7 @@
}
map[option.text()] = option.val();
source.push(option.text());
if (option.prop('selected')) {
if (option.attr('selected')) {
selected = option.text();
selectedValue = option.val();
}
Expand All @@ -98,11 +99,13 @@
, transferAttributes: function() {
this.options.placeholder = this.$source.attr('data-placeholder') || this.options.placeholder
if(this.options.appendId !== "undefined") {
this.$element.attr('id', this.$source.attr('id') + this.options.appendId);
this.$element.attr('id', this.$source.attr('id') + this.options.appendId);
}
this.$element.attr('placeholder', this.options.placeholder)
this.$target.prop('name', this.$source.prop('name'))
this.$target.val(this.$source.val())
if (!this.freeInput) {
this.$target.val(this.$source.val())
}
this.$source.removeAttr('name') // Remove from source otherwise form will pass parameter twice.
this.$element.attr('required', this.$source.attr('required'))
this.$element.attr('rel', this.$source.attr('rel'))
Expand Down Expand Up @@ -177,10 +180,15 @@
}

, template: function() {
var freeInputAttributes = '';
if (this.freeInput) {
freeInputAttributes = 'name="' + this.freeInput.name + '" value="' + this.freeInput.value + '" '
}

if (this.options.bsVersion == '2') {
return '<div class="combobox-container"><input type="hidden" /> <div class="input-append"> <input type="text" autocomplete="false" /> <span class="add-on dropdown-toggle" data-dropdown="dropdown"> <span class="caret"/> <i class="icon-remove"/> </span> </div> </div>'
return '<div class="combobox-container"><input type="hidden" /> <div class="input-append"> <input ' + freeInputAttributes + 'type="text" autocomplete="off" /> <span class="add-on dropdown-toggle" data-dropdown="dropdown"> <span class="caret"/> <i class="icon-remove"/> </span> </div> </div>'
} else {
return '<div class="combobox-container"> <input type="hidden" /> <div class="input-group"> <input type="text" autocomplete="false" /> <span class="input-group-addon dropdown-toggle" data-dropdown="dropdown"> <span class="caret" /> <span class="glyphicon glyphicon-remove" /> </span> </div> </div>'
return '<div class="combobox-container"> <input type="hidden" /> <div class="input-group"> <input ' + freeInputAttributes + 'type="text" autocomplete="off" /> <span class="input-group-addon dropdown-toggle" data-dropdown="dropdown"> <span class="caret" /> <span class="glyphicon glyphicon-remove" /> </span> </div> </div>'
}
}

Expand Down Expand Up @@ -413,7 +421,7 @@
var that = this;
this.focused = false;
var val = this.$element.val();
if (!this.selected && val !== '' ) {
if (!this.freeInput && !this.selected && val !== '' ) {
this.$element.val('');
this.$source.val('').trigger('change');
this.$target.val('').trigger('change');
Expand Down
40 changes: 40 additions & 0 deletions js/tests/unit/bootstrap-combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,44 @@ $(function () {

combobox.$menu.remove()
})

test("should not set as selected if no select was selected before load with free input", function () {
var $select = $('<select><option>aa</option><option>ab</option><option>ac</option></select>')
, $input = $select.combobox({freeInput: {name:'foo', value:''}}).data('combobox').$element
, $target = $select.combobox({freeInput: {name:'foo', value:''}}).data('combobox').$target
, combobox = $select.data('combobox')

equal($input.val(), '', 'input value was correctly set')
equal($target.val(), '', 'hidden input value was correctly set')
equal($select.val(), 'aa', 'select value was correctly set')
})

test("should not clear input on blur when value does not exist with free input", function() {
var $select = $('<select><option>aa</option></select>')
, $input = $select.combobox({freeInput: {name:'foo', value:'bar'}}).data('combobox').$element
, combobox = $select.data('combobox')

$input.val('DOES NOT EXIST')
$input.trigger('keyup')
$input.trigger('blur')

equal($input.val(), 'DOES NOT EXIST', 'input value was correctly set')
equal($select.val(), 'aa', 'select value was correctly set')

combobox.$menu.remove()
})

test("should give input name attribute with free input", function () {
var $select = $('<select><option>aa</option></select>')
, $input = $select.combobox({freeInput: {name:'foo', value:'bar'}}).data('combobox').$element

equal($input.attr('name'), 'foo', 'input name was correctly set')
})

test("should copy value from data attribute to input with free input", function () {
var $select = $('<select><option>aa</option></select>')
, $input = $select.combobox({freeInput: {name:'foo', value:'bar'}}).data('combobox').$element

equal($input.val(), 'bar', 'input value was correctly set')
})
})