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

#270 Selectize Modifications For Pasting Delimited Values #425

Merged
merged 10 commits into from Jan 27, 2015
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Selectize.count = 0;
Selectize.defaults = {
plugins: [],
delimiter: ',',
splitOn: null, // Regex or string for splitting up values from a paste command

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this option missing from documentation? I couldn't find it

persist: true,
diacritics: true,
create: false,
Expand Down
28 changes: 26 additions & 2 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ $.extend(Selectize.prototype, {
$control_input.attr('placeholder', settings.placeholder);
}

// if splitOn was not passed in, construct it from the delimiter to allow pasting universally
if (!self.settings.splitOn && self.settings.delimiter) {
var delimiterEscaped = self.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
self.settings.splitOn = new RegExp('\\s*' + delimiterEscaped + '+\\s*');
}

if ($input.attr('autocorrect')) {
$control_input.attr('autocorrect', $input.attr('autocorrect'));
}
Expand Down Expand Up @@ -389,6 +395,16 @@ $.extend(Selectize.prototype, {
var self = this;
if (self.isFull() || self.isInputHidden || self.isLocked) {
e.preventDefault();
} else {
// If a regex or string is included, this will split the pasted input and create Items for each separate value
if (self.settings.splitOn) {
setTimeout(function() {
var splitInput = $.trim(self.$control_input.val() || '').split(self.settings.splitOn);
splitInput.forEach(function(input) {
self.createItem(input);
});
}, 0);
}
}
},

Expand Down Expand Up @@ -1408,13 +1424,21 @@ $.extend(Selectize.prototype, {
var self = this;
var input = $.trim(self.$control_input.val() || '');
var caret = self.caretPos;
if (!self.canCreate(input)) return false;
self.lock();

if (typeof triggerDropdown === 'undefined') {
triggerDropdown = true;

// allow a string to be passed in, like the API docs say
} else if (typeof triggerDropdown === 'string') {
input = triggerDropdown;
}

if (!input.length) return false;

if (!self.canCreate(input)) return false;

self.lock();

var setup = (typeof self.settings.create === 'function') ? this.settings.create : function(input) {
var data = {};
data[self.settings.labelField] = input;
Expand Down