UNPKG

@kelvininc/ui-components

Version:
115 lines (111 loc) 4.71 kB
import { i as isSubString } from './p-BVsEG3aJ.js'; import { i as isEmpty } from './p-BZNGlO8m.js'; const getHightableFallbackOption = (options) => { const [firstOption] = options; return firstOption; }; const getHightableOptionIndex = (options, highlightedOption) => { if (!highlightedOption) { return -1; } return options.findIndex(name => name === highlightedOption); }; 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; }, {}); }; 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 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 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]; }; 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]; }; 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; }, {}); }; var select_helper = /*#__PURE__*/Object.freeze({ __proto__: null, buildAllOptionsSelected: buildAllOptionsSelected, buildSelectedOptions: buildSelectedOptions, getFlattenSelectOptions: getFlattenSelectOptions, getNextHightlightableOption: getNextHightlightableOption, getPreviousHightlightableOption: getPreviousHightlightableOption, getSelectableOptions: getSelectableOptions, getSelectedSelectableOptions: getSelectedSelectableOptions, searchDropdownOptions: searchDropdownOptions }); export { getNextHightlightableOption as a, getPreviousHightlightableOption as b, buildAllOptionsSelected as c, getSelectableOptions as d, getFlattenSelectOptions as e, searchDropdownOptions as f, getSelectedSelectableOptions as g, select_helper as s };