Skip to content

Commit 3630385

Browse files
committed
Merge pull request select2#3051 from select2/issue_3022
Added compatibility with `<input type="text" />` tags
2 parents a954bae + 5ec852e commit 3630385

15 files changed

Lines changed: 627 additions & 24 deletions

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = function (grunt) {
1414

1515
'select2/compat/matcher',
1616
'select2/compat/initSelection',
17+
'select2/compat/inputData',
1718
'select2/compat/query',
1819

1920
'select2/dropdown/attachContainer',

dist/js/select2.amd.full.js

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,7 +2406,6 @@ define('select2/data/select',[
24062406
var val = data.id;
24072407

24082408
this.$element.val(val);
2409-
24102409
this.$element.trigger('change');
24112410
}
24122411
};
@@ -2492,6 +2491,10 @@ define('select2/data/select',[
24922491
});
24932492
};
24942493

2494+
SelectAdapter.prototype.addOptions = function ($options) {
2495+
this.$element.append($options);
2496+
};
2497+
24952498
SelectAdapter.prototype.option = function (data) {
24962499
var option;
24972500

@@ -2632,7 +2635,7 @@ define('select2/data/array',[
26322635

26332636
ArrayAdapter.__super__.constructor.call(this, $element, options);
26342637

2635-
$element.append(this.convertToOptions(data));
2638+
this.addOptions(this.convertToOptions(data));
26362639
}
26372640

26382641
Utils.Extend(ArrayAdapter, SelectAdapter);
@@ -2643,7 +2646,7 @@ define('select2/data/array',[
26432646
if ($option.length === 0) {
26442647
$option = this.option(data);
26452648

2646-
this.$element.append($option);
2649+
this.addOptions([$option]);
26472650
}
26482651

26492652
ArrayAdapter.__super__.select.call(this, data);
@@ -2871,7 +2874,7 @@ define('select2/data/tags',[
28712874
var $option = self.option(tag);
28722875
$option.attr('data-select2-tag', true);
28732876

2874-
self.$element.append($option);
2877+
self.addOptions([$option]);
28752878

28762879
self.insertTag(data, tag);
28772880
}
@@ -4057,6 +4060,15 @@ define('select2/options',[
40574060
}
40584061

40594062
this.options = Defaults.apply(this.options);
4063+
4064+
if ($element && $element.is('input')) {
4065+
var InputCompat = require(this.get('amdBase') + 'compat/inputData');
4066+
4067+
this.options.dataAdapter = Utils.Decorate(
4068+
this.options.dataAdapter,
4069+
InputCompat
4070+
);
4071+
}
40604072
}
40614073

40624074
Options.prototype.fromElement = function ($e) {
@@ -4974,6 +4986,134 @@ define('select2/compat/initSelection',[
49744986
return InitSelection;
49754987
});
49764988

4989+
define('select2/compat/inputData',[
4990+
'jquery'
4991+
], function ($) {
4992+
function InputData (decorated, $element, options) {
4993+
this._currentData = [];
4994+
this._valueSeparator = options.get('valueSeparator') || ',';
4995+
4996+
if ($element.prop('type') === 'hidden') {
4997+
if (console && console.warn) {
4998+
console.warn(
4999+
'Select2: Using a hidden input with Select2 is no longer ' +
5000+
'supported and may stop working in the future. It is recommended ' +
5001+
'to use a `<select>` element instead.'
5002+
);
5003+
}
5004+
}
5005+
5006+
decorated.call(this, $element, options);
5007+
}
5008+
5009+
InputData.prototype.current = function (_, callback) {
5010+
function getSelected (data, selectedIds) {
5011+
var selected = [];
5012+
5013+
if (data.selected || $.inArray(data.id, selectedIds) !== -1) {
5014+
data.selected = true;
5015+
selected.push(data);
5016+
} else {
5017+
data.selected = false;
5018+
}
5019+
5020+
if (data.children) {
5021+
selected.push.apply(selected, getSelected(data.children, selectedIds));
5022+
}
5023+
5024+
return selected;
5025+
}
5026+
5027+
var selected = [];
5028+
5029+
for (var d = 0; d < this._currentData.length; d++) {
5030+
var data = this._currentData[d];
5031+
5032+
selected.push.apply(
5033+
selected,
5034+
getSelected(
5035+
data,
5036+
this.$element.val().split(
5037+
this._valueSeparator
5038+
)
5039+
)
5040+
);
5041+
}
5042+
5043+
callback(selected);
5044+
};
5045+
5046+
InputData.prototype.select = function (_, data) {
5047+
if (!this.options.get('multiple')) {
5048+
this.current(function (allData) {
5049+
$.map(allData, function (data) {
5050+
data.selected = false;
5051+
});
5052+
});
5053+
5054+
this.$element.val(data.id);
5055+
this.$element.trigger('change');
5056+
} else {
5057+
var value = this.$element.val();
5058+
value += this._valueSeparator + data.id;
5059+
5060+
this.$element.val(value);
5061+
this.$element.trigger('change');
5062+
}
5063+
};
5064+
5065+
InputData.prototype.unselect = function (_, data) {
5066+
var self = this;
5067+
5068+
data.selected = false;
5069+
5070+
this.current(function (allData) {
5071+
var values = [];
5072+
5073+
for (var d = 0; d < allData; d++) {
5074+
var item = allData[d];
5075+
5076+
if (data.id == item.id) {
5077+
continue;
5078+
}
5079+
5080+
values.push(data.id);
5081+
}
5082+
5083+
self.$element.val(values.join(self._valueSeparator));
5084+
self.$element.trigger('change');
5085+
});
5086+
};
5087+
5088+
InputData.prototype.query = function (_, params, callback) {
5089+
var results = [];
5090+
5091+
for (var d = 0; d < this._currentData.length; d++) {
5092+
var data = this._currentData[d];
5093+
5094+
var matches = this.matches(params, data);
5095+
5096+
if (matches !== null) {
5097+
results.push(matches);
5098+
}
5099+
}
5100+
5101+
callback({
5102+
results: results
5103+
});
5104+
};
5105+
5106+
InputData.prototype.addOptions = function (_, $options) {
5107+
var options = $.map($options, function ($option) {
5108+
return $.data($option[0], 'data');
5109+
});
5110+
5111+
this._currentData.push.apply(this._currentData, options);
5112+
};
5113+
5114+
return InputData;
5115+
});
5116+
49775117
define('select2/compat/query',[
49785118

49795119
], function () {

dist/js/select2.amd.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,7 +2406,6 @@ define('select2/data/select',[
24062406
var val = data.id;
24072407

24082408
this.$element.val(val);
2409-
24102409
this.$element.trigger('change');
24112410
}
24122411
};
@@ -2492,6 +2491,10 @@ define('select2/data/select',[
24922491
});
24932492
};
24942493

2494+
SelectAdapter.prototype.addOptions = function ($options) {
2495+
this.$element.append($options);
2496+
};
2497+
24952498
SelectAdapter.prototype.option = function (data) {
24962499
var option;
24972500

@@ -2632,7 +2635,7 @@ define('select2/data/array',[
26322635

26332636
ArrayAdapter.__super__.constructor.call(this, $element, options);
26342637

2635-
$element.append(this.convertToOptions(data));
2638+
this.addOptions(this.convertToOptions(data));
26362639
}
26372640

26382641
Utils.Extend(ArrayAdapter, SelectAdapter);
@@ -2643,7 +2646,7 @@ define('select2/data/array',[
26432646
if ($option.length === 0) {
26442647
$option = this.option(data);
26452648

2646-
this.$element.append($option);
2649+
this.addOptions([$option]);
26472650
}
26482651

26492652
ArrayAdapter.__super__.select.call(this, data);
@@ -2871,7 +2874,7 @@ define('select2/data/tags',[
28712874
var $option = self.option(tag);
28722875
$option.attr('data-select2-tag', true);
28732876

2874-
self.$element.append($option);
2877+
self.addOptions([$option]);
28752878

28762879
self.insertTag(data, tag);
28772880
}
@@ -4057,6 +4060,15 @@ define('select2/options',[
40574060
}
40584061

40594062
this.options = Defaults.apply(this.options);
4063+
4064+
if ($element && $element.is('input')) {
4065+
var InputCompat = require(this.get('amdBase') + 'compat/inputData');
4066+
4067+
this.options.dataAdapter = Utils.Decorate(
4068+
this.options.dataAdapter,
4069+
InputCompat
4070+
);
4071+
}
40604072
}
40614073

40624074
Options.prototype.fromElement = function ($e) {

0 commit comments

Comments
 (0)