Skip to content

Commit b2866d7

Browse files
author
Damian Dulisz
authored
Merge pull request shentao#164 from pczarn/refactor-and-fix
Refactor and fix logic of multiple vs single select
2 parents ab136c8 + 102a1e2 commit b2866d7

File tree

5 files changed

+64
-76
lines changed

5 files changed

+64
-76
lines changed

lib/multiselectMixin.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = {
7474
return {
7575
search: '',
7676
isOpen: false,
77-
internalValue: this.value || this.value === 0 ? (0, _utils2.default)(this.value) : this.multiple ? [] : null
77+
internalValue: this.value || this.value === 0 ? this.multiple ? (0, _utils2.default)(this.value) : (0, _utils2.default)([this.value]) : []
7878
};
7979
},
8080

@@ -213,9 +213,9 @@ module.exports = {
213213
var _this = this;
214214

215215
if (this.trackBy) {
216-
return this.multiple ? this.internalValue.map(function (element) {
216+
return this.internalValue.map(function (element) {
217217
return element[_this.trackBy];
218-
}) : this.internalValue[this.trackBy];
218+
});
219219
} else {
220220
return this.internalValue;
221221
}
@@ -231,13 +231,13 @@ module.exports = {
231231
});
232232
},
233233
currentOptionLabel: function currentOptionLabel() {
234-
return this.getOptionLabel(this.internalValue) + '';
234+
return this.getOptionLabel(this.internalValue[0]) + '';
235235
}
236236
},
237237
watch: {
238238
'internalValue': function internalValue() {
239239
if (this.resetAfter) {
240-
this.internalValue = null;
240+
this.internalValue = [];
241241
this.search = '';
242242
}
243243
this.adjustSearch();
@@ -248,7 +248,7 @@ module.exports = {
248248
this.$emit('search-change', this.search, this.id);
249249
},
250250
'value': function value() {
251-
this.internalValue = (0, _utils2.default)(this.value);
251+
this.internalValue = (0, _utils2.default)(this.multiple ? this.value : [this.value]);
252252
}
253253
},
254254
methods: {
@@ -265,14 +265,8 @@ module.exports = {
265265
return !this.options ? false : this.optionKeys.indexOf(query) > -1;
266266
},
267267
isSelected: function isSelected(option) {
268-
if (!this.internalValue) return false;
269268
var opt = this.trackBy ? option[this.trackBy] : option;
270-
271-
if (this.multiple) {
272-
return this.valueKeys.indexOf(opt) > -1;
273-
} else {
274-
return this.valueKeys === opt;
275-
}
269+
return this.valueKeys.indexOf(opt) > -1;
276270
},
277271
isNotSelected: function isNotSelected(option) {
278272
return !this.isSelected(option);
@@ -290,22 +284,18 @@ module.exports = {
290284
this.$emit('tag', option.label, this.id);
291285
this.search = '';
292286
} else {
293-
if (this.multiple) {
294-
if (this.isSelected(option)) {
295-
if (key !== 'Tab') this.removeElement(option);
296-
return;
297-
} else {
298-
this.internalValue.push(option);
299-
}
287+
var isSelected = this.isSelected(option);
288+
if (isSelected) {
289+
if (key !== 'Tab') this.removeElement(option);
290+
return;
291+
} else if (this.multiple) {
292+
this.internalValue.push(option);
300293
} else {
301-
var isSelected = this.isSelected(option);
302-
303-
if (isSelected && (!this.allowEmpty || key === 'Tab')) return;
304-
305-
this.internalValue = isSelected ? null : option;
294+
this.internalValue = [option];
306295
}
307296
this.$emit('select', (0, _utils2.default)(option), this.id);
308-
this.$emit('input', (0, _utils2.default)(this.internalValue), this.id);
297+
var value = this.multiple ? this.internalValue : this.internalValue[0];
298+
this.$emit('input', (0, _utils2.default)(value), this.id);
309299

310300
if (this.closeOnSelect) this.deactivate();
311301
}
@@ -314,11 +304,12 @@ module.exports = {
314304
if (this.disabled) return;
315305
if (!this.allowEmpty && this.internalValue.length <= 1) return;
316306

317-
var index = this.multiple && (typeof option === 'undefined' ? 'undefined' : _typeof(option)) === 'object' ? this.valueKeys.indexOf(option[this.trackBy]) : this.valueKeys.indexOf(option);
307+
var index = (typeof option === 'undefined' ? 'undefined' : _typeof(option)) === 'object' ? this.valueKeys.indexOf(option[this.trackBy]) : this.valueKeys.indexOf(option);
318308

319309
this.internalValue.splice(index, 1);
320310
this.$emit('remove', (0, _utils2.default)(option), this.id);
321-
this.$emit('input', (0, _utils2.default)(this.internalValue), this.id);
311+
var value = this.multiple ? this.internalValue : this.internalValue[0];
312+
this.$emit('input', (0, _utils2.default)(value), this.id);
322313
},
323314
removeLastElement: function removeLastElement() {
324315
if (this.blockKeys.indexOf('Delete') !== -1) return;
@@ -351,7 +342,8 @@ module.exports = {
351342
} else {
352343
this.$el.blur();
353344
}
354-
this.$emit('close', (0, _utils2.default)(this.internalValue), this.id);
345+
var value = this.multiple ? this.internalValue : this.internalValue[0];
346+
this.$emit('close', (0, _utils2.default)(value), this.id);
355347
},
356348
adjustSearch: function adjustSearch() {
357349
if (!this.searchable || !this.clearOnSelect) return;

0 commit comments

Comments
 (0)