@quantlab/handsontable
Version:
Spreadsheet-like data grid editor that provides copy/paste functionality compatible with Excel/Google Docs
56 lines (48 loc) • 1.21 kB
JavaScript
;
exports.__esModule = true;
exports.default = autocompleteValidator;
/**
* Autocomplete cell validator.
*
* @private
* @validator AutocompleteValidator
* @param {*} value - Value of edited cell
* @param {Function} callback - Callback called with validation result
*/
function autocompleteValidator(value, callback) {
if (value == null) {
value = '';
}
if (this.allowEmpty && value === '') {
callback(true);
return;
}
if (this.strict && this.source) {
if (typeof this.source === 'function') {
this.source(value, process(value, callback));
} else {
process(value, callback)(this.source);
}
} else {
callback(true);
}
};
/**
* Function responsible for validation of autocomplete value.
*
* @param {*} value - Value of edited cell
* @param {Function} callback - Callback called with validation result
*/
function process(value, callback) {
var originalVal = value;
return function (source) {
var found = false;
for (var s = 0, slen = source.length; s < slen; s++) {
if (originalVal === source[s]) {
found = true; // perfect match
break;
}
}
callback(found);
};
}