input-pill: Add validation callback for pills.

This adds a validator that when calling `reject` will reject a pill
from being added.
This commit is contained in:
Brock Whittaker 2017-10-24 16:05:24 -07:00 committed by Tim Abbott
parent 5d0aae283f
commit 5a0bba2afc
1 changed files with 15 additions and 9 deletions

View File

@ -14,6 +14,7 @@ var input_pill = function ($parent) {
pills: [],
$parent: $parent,
getKeyFunction: function () {},
validation: function () {},
lastUpdated: null,
lastCreated: {
keys: null,
@ -55,8 +56,18 @@ var input_pill = function ($parent) {
// default `undefined`.
if (typeof optionalKey === "undefined") {
optionalKey = store.getKeyFunction(value, reject);
if (typeof optionalKey === "object" &&
optionalKey.key !== undefined && optionalKey.value !== undefined) {
value = optionalKey.value;
optionalKey = optionalKey.key;
}
}
// now run a separate round of validation, in case they are using
// `getKeyFunction` without `reject`, or not using it at all.
store.validation(value, optionalKey, reject);
// if the `rejected` global is now true, it means that the user's
// created pill was not accepted, and we should no longer proceed.
if (rejected) {
@ -66,15 +77,6 @@ var input_pill = function ($parent) {
var id = Math.random().toString(16);
// the user may provide a function to get a key from a value
// that is entered, so return whatever value is gotten from
// this function.
// the default function is noop, so the return type is by
// default `undefined`.
if (typeof optionalKey === "undefined") {
optionalKey = store.getKeyFunction(value);
}
var payload = {
id: id,
value: value,
@ -308,6 +310,10 @@ var input_pill = function ($parent) {
onPillCreate: function (callback) {
store.getKeyFunction = callback;
},
validate: function (callback) {
store.validation = callback;
},
};
return prototype;