Improved ko.extenders.limitedList

This commit is contained in:
the-djmaze 2022-02-25 17:18:45 +01:00
parent 1d63683cf9
commit 0bf891ba9d

34
dev/External/ko.js vendored
View file

@ -154,28 +154,20 @@ Object.assign(ko.bindingHandlers, {
ko.extenders.limitedList = (target, limitedList) => { ko.extenders.limitedList = (target, limitedList) => {
const result = ko const result = ko
.computed({ .computed({
read: target, read: target,
write: (newValue) => { write: newValue => {
const currentValue = ko.unwrap(target), let currentValue = target(),
list = ko.unwrap(limitedList); list = ko.unwrap(limitedList);
list = arrayLength(list) ? list : [''];
if (arrayLength(list)) { if (!list.includes(newValue)) {
if (list.includes(newValue)) { newValue = list.includes(currentValue, list) ? currentValue : list[0];
target(newValue); target(newValue + ' ');
} else if (list.includes(currentValue, list)) {
target(currentValue + ' ');
target(currentValue);
} else {
target(list[0] + ' ');
target(list[0]);
}
} else {
target('');
}
} }
}) target(newValue);
.extend({ notify: 'always' }); }
})
.extend({ notify: 'always' });
result(target()); result(target());