@kelvininc/ui-components
Version:
Kelvin UI Components
147 lines (142 loc) • 6.58 kB
JavaScript
;
var string_helper = require('./string.helper-DxzCWS0Y.js');
var isEmpty = require('./isEmpty-CqcsgK-A.js');
const getSelectableOptions = (options = {}) => {
return Object.values(options).reduce((accumulator, option) => {
if (isEmpty.isEmpty(option.options) && !option.disabled) {
accumulator[option.value] = option;
}
else if (!isEmpty.isEmpty(option.options)) {
const nested = getSelectableOptions(option.options);
for (const key of Object.keys(nested)) {
accumulator[key] = nested[key];
}
}
return accumulator;
}, {});
};
const getSelectableOptionsFromArray = (options) => options.filter(option => isEmpty.isEmpty(option.options) && !option.disabled);
const getSelectedSelectableOptions = (options = {}, selectedOptions = {}) => {
const selectableOptions = getSelectableOptions(options);
return Object.keys(selectedOptions).reduce((accumulator, optionKey) => {
const selectedOption = selectableOptions[optionKey];
const isOptionSelected = selectedOptions[optionKey] === true;
if (selectedOption && isOptionSelected) {
accumulator[optionKey] = selectedOption;
}
return accumulator;
}, {});
};
const getFlattenSelectOptions = (selectOptions = {}) => Object.values(selectOptions).reduce((accumulator, selectOption) => {
accumulator[selectOption.value] = selectOption;
const childrenOpts = getFlattenSelectOptions(selectOption.options);
Object.keys(childrenOpts).forEach(childrenKey => (accumulator[childrenKey] = childrenOpts[childrenKey]));
return accumulator;
}, {});
const flattenOptionsReducer = (accumulator, selectOption) => {
var _a;
accumulator.push(selectOption);
Object.values((_a = selectOption.options) !== null && _a !== void 0 ? _a : {}).reduce(flattenOptionsReducer, accumulator);
return accumulator;
};
const getFlattenSelectOptionsArray = (selectOptions = {}) => Object.values(selectOptions).reduce(flattenOptionsReducer, []);
const flattenSelectOptionsArray = (selectOptions = []) => selectOptions.reduce(flattenOptionsReducer, []);
const buildSelectedOptions = (options = {}, selected) => Object.keys(options).reduce((accumulator, childKey) => {
accumulator[childKey] = selected.includes(childKey);
return accumulator;
}, {});
const buildAllOptionsSelected = (options = {}) => Object.keys(options).reduce((accumulator, childKey) => {
accumulator[childKey] = true;
return accumulator;
}, {});
const getSelectedCount = (selectedOptions = {}) => Object.keys(selectedOptions).filter(key => selectedOptions[key]).length;
const buildPartialOptionsSelected = (options = {}, maxSelectable, currentSelectedCount) => {
const availableSlots = maxSelectable - currentSelectedCount;
if (availableSlots <= 0) {
return undefined;
}
const keysToSelect = Object.keys(options).slice(0, availableSlots);
return keysToSelect.reduce((acc, key) => {
acc[key] = true;
return acc;
}, {});
};
const getPreviousHightlightableOption = (options, highlightedOption) => {
if (options.length === 0) {
return;
}
const optionValues = options.map(o => o.value);
const fallbackOption = optionValues[0];
const highlightedIndex = highlightedOption !== undefined ? optionValues.indexOf(highlightedOption) : -1;
if (highlightedIndex === -1) {
return fallbackOption;
}
if (highlightedIndex === 0) {
return highlightedOption;
}
return optionValues[highlightedIndex - 1];
};
const getNextHightlightableOption = (options, highlightedOption) => {
if (options.length === 0) {
return;
}
const optionValues = options.map(o => o.value);
const fallbackOption = optionValues[0];
const highlightedIndex = highlightedOption !== undefined ? optionValues.indexOf(highlightedOption) : -1;
if (highlightedIndex === -1) {
return fallbackOption;
}
if (highlightedIndex === optionValues.length - 1) {
return highlightedOption;
}
return optionValues[highlightedIndex + 1];
};
const searchDropdownOptions = (term, options = {}) => {
if (isEmpty.isEmpty(term)) {
return options;
}
return Object.keys(options).reduce((accumulator, key) => {
const option = options[key];
if (!isEmpty.isEmpty(option.options)) {
const childrenMatches = searchDropdownOptions(term, option.options);
if (!isEmpty.isEmpty(childrenMatches)) {
accumulator[key] = Object.assign(Object.assign({}, option), { options: childrenMatches });
}
return accumulator;
}
if (string_helper.isSubString(term, option.label) || string_helper.isSubString(term, option.value)) {
accumulator[key] = option;
}
return accumulator;
}, {});
};
var select_helper = /*#__PURE__*/Object.freeze({
__proto__: null,
buildAllOptionsSelected: buildAllOptionsSelected,
buildPartialOptionsSelected: buildPartialOptionsSelected,
buildSelectedOptions: buildSelectedOptions,
flattenSelectOptionsArray: flattenSelectOptionsArray,
getFlattenSelectOptions: getFlattenSelectOptions,
getFlattenSelectOptionsArray: getFlattenSelectOptionsArray,
getNextHightlightableOption: getNextHightlightableOption,
getPreviousHightlightableOption: getPreviousHightlightableOption,
getSelectableOptions: getSelectableOptions,
getSelectableOptionsFromArray: getSelectableOptionsFromArray,
getSelectedCount: getSelectedCount,
getSelectedSelectableOptions: getSelectedSelectableOptions,
searchDropdownOptions: searchDropdownOptions
});
exports.buildAllOptionsSelected = buildAllOptionsSelected;
exports.buildPartialOptionsSelected = buildPartialOptionsSelected;
exports.buildSelectedOptions = buildSelectedOptions;
exports.flattenSelectOptionsArray = flattenSelectOptionsArray;
exports.getFlattenSelectOptions = getFlattenSelectOptions;
exports.getFlattenSelectOptionsArray = getFlattenSelectOptionsArray;
exports.getNextHightlightableOption = getNextHightlightableOption;
exports.getPreviousHightlightableOption = getPreviousHightlightableOption;
exports.getSelectableOptions = getSelectableOptions;
exports.getSelectableOptionsFromArray = getSelectableOptionsFromArray;
exports.getSelectedCount = getSelectedCount;
exports.getSelectedSelectableOptions = getSelectedSelectableOptions;
exports.searchDropdownOptions = searchDropdownOptions;
exports.select_helper = select_helper;