@coreui/vue-pro
Version:
UI Components Library for Vue.js
99 lines (96 loc) • 3.52 kB
JavaScript
;
const filterOptions = (options, search) => {
const result = [];
options.forEach((item) => {
if (typeof item !== 'string' && 'options' in item && Array.isArray(item.options)) {
const filteredOptions = filterOptions(item.options, search);
if (filteredOptions.length > 0) {
result.push({ ...item, options: filteredOptions });
}
}
else if (getOptionLabel(item).toLowerCase().includes(search.toLowerCase())) {
result.push(item);
}
});
return result;
};
const flattenOptionsArray = (options, keepGroupLabel) => {
const optionsList = [];
for (const option of options) {
if (typeof option !== 'string' && 'options' in option && Array.isArray(option.options)) {
optionsList.push(...option.options);
}
else {
optionsList.push(option);
}
}
return optionsList;
};
const isExternalSearch = (search) => {
return ((typeof search === 'string' && search === 'external') ||
(typeof search === 'object' && search.external === true));
};
const isGlobalSearch = (search) => {
return ((typeof search === 'string' && search === 'global') ||
(typeof search === 'object' && search.global === true));
};
const getOptionLabel = (option) => typeof option === 'string' ? option : option.label;
const highlightSubstring = (string, query) => {
if (query) {
const regex = new RegExp(query, 'gi');
return string.replace(regex, (string) => `<strong>${string}</strong>`);
}
return string;
};
const isOptionDisabled = (option) => typeof option === 'string' ? false : option.disabled;
const isOptionSelected = (option, selected) => {
if (!selected) {
return false;
}
if (typeof option === 'string' && typeof selected === 'string') {
return selected === option;
}
if (typeof option !== 'string' && typeof selected !== 'string') {
return selected.label === option.label;
}
return false;
};
const getFirstOptionByLabel = (value, options) => {
for (const option of options) {
if (typeof option !== 'string' && 'options' in option && Array.isArray(option.options)) {
const found = getFirstOptionByLabel(value, option.options);
if (found) {
return found;
}
}
if (getOptionLabel(option).toLowerCase() === value.toLowerCase()) {
return option;
}
}
return null;
};
const getFirstOptionByValue = (value, options) => {
for (const option of options) {
if (typeof option !== 'string' && 'options' in option && Array.isArray(option.options)) {
const found = getFirstOptionByValue(value, option.options);
if (found) {
return found;
}
}
if (typeof option !== 'string' && 'value' in option && option.value === value) {
return option;
}
}
return null;
};
exports.filterOptions = filterOptions;
exports.flattenOptionsArray = flattenOptionsArray;
exports.getFirstOptionByLabel = getFirstOptionByLabel;
exports.getFirstOptionByValue = getFirstOptionByValue;
exports.getOptionLabel = getOptionLabel;
exports.highlightSubstring = highlightSubstring;
exports.isExternalSearch = isExternalSearch;
exports.isGlobalSearch = isGlobalSearch;
exports.isOptionDisabled = isOptionDisabled;
exports.isOptionSelected = isOptionSelected;
//# sourceMappingURL=utils.js.map