@kelvininc/ui-components
Version:
Kelvin UI Components
100 lines (99 loc) • 3.97 kB
JavaScript
import { isEmpty } from "lodash-es";
import { isSubString } from "./string.helper";
const getHightableFallbackOption = (options) => {
const [firstOption] = options;
return firstOption;
};
const getHightableOptionIndex = (options, highlightedOption) => {
if (!highlightedOption) {
return -1;
}
return options.findIndex(name => name === highlightedOption);
};
export const getSelectableOptions = (options = {}) => {
return Object.values(options).reduce((accumulator, option) => {
if (isEmpty(option.options)) {
accumulator[option.value] = option;
}
else {
accumulator = Object.assign(Object.assign({}, accumulator), getSelectableOptions(option.options));
}
return accumulator;
}, {});
};
export 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;
}, {});
};
export 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;
}, {});
export const buildSelectedOptions = (options = {}, selected) => Object.keys(options).reduce((accumulator, childKey) => {
accumulator[childKey] = selected.includes(childKey);
return accumulator;
}, {});
export const buildAllOptionsSelected = (options = {}) => Object.keys(options).reduce((accumulator, childKey) => {
accumulator[childKey] = true;
return accumulator;
}, {});
export const getPreviousHightlightableOption = (options, highlightedOption) => {
if (isEmpty(options)) {
return;
}
const optionsKeys = Object.keys(options);
const fallbackOption = getHightableFallbackOption(optionsKeys);
const highlightedIndex = getHightableOptionIndex(optionsKeys, highlightedOption);
if (highlightedIndex === -1) {
return fallbackOption;
}
// Check if highlighted option is the first
if (highlightedIndex === 0) {
return highlightedOption;
}
return optionsKeys[highlightedIndex - 1];
};
export const getNextHightlightableOption = (options, highlightedOption) => {
if (isEmpty(options)) {
return;
}
const optionsKeys = Object.keys(options);
const fallbackOption = getHightableFallbackOption(optionsKeys);
const highlightedIndex = getHightableOptionIndex(optionsKeys, highlightedOption);
if (highlightedIndex === -1) {
return fallbackOption;
}
// Check if highlighted option is the last
if (highlightedIndex === optionsKeys.length - 1) {
return highlightedOption;
}
return optionsKeys[highlightedIndex + 1];
};
export const searchDropdownOptions = (term, options = {}) => {
if (isEmpty(term)) {
return options;
}
return Object.keys(options).reduce((accumulator, key) => {
const option = options[key];
if (!isEmpty(option.options)) {
const childrenMatches = searchDropdownOptions(term, option.options);
if (!isEmpty(childrenMatches)) {
accumulator[key] = Object.assign(Object.assign({}, option), { options: childrenMatches });
}
return accumulator;
}
if (isSubString(term, option.label) || isSubString(term, option.value)) {
accumulator[key] = option;
}
return accumulator;
}, {});
};