From 0bf891ba9d252e7d26f780adbb01e57b48a83390 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 25 Feb 2022 17:18:45 +0100 Subject: [PATCH] Improved ko.extenders.limitedList --- dev/External/ko.js | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/dev/External/ko.js b/dev/External/ko.js index 69abed526..ab87619f4 100644 --- a/dev/External/ko.js +++ b/dev/External/ko.js @@ -154,28 +154,20 @@ Object.assign(ko.bindingHandlers, { ko.extenders.limitedList = (target, limitedList) => { const result = ko - .computed({ - read: target, - write: (newValue) => { - const currentValue = ko.unwrap(target), - list = ko.unwrap(limitedList); - - if (arrayLength(list)) { - if (list.includes(newValue)) { - target(newValue); - } else if (list.includes(currentValue, list)) { - target(currentValue + ' '); - target(currentValue); - } else { - target(list[0] + ' '); - target(list[0]); - } - } else { - target(''); - } + .computed({ + read: target, + write: newValue => { + let currentValue = target(), + list = ko.unwrap(limitedList); + list = arrayLength(list) ? list : ['']; + if (!list.includes(newValue)) { + newValue = list.includes(currentValue, list) ? currentValue : list[0]; + target(newValue + ' '); } - }) - .extend({ notify: 'always' }); + target(newValue); + } + }) + .extend({ notify: 'always' }); result(target());