UNPKG

@kelvininc/ui-components

Version:
132 lines (128 loc) 6.07 kB
import { a as isSubString } from './p-YzSFobpQ.js'; import { i as isEmpty } from './p-BZNGlO8m.js'; const getSelectableOptions = (options = {}) => { return Object.values(options).reduce((accumulator, option) => { if (isEmpty(option.options) && !option.disabled) { accumulator[option.value] = option; } else if (!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(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(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, buildPartialOptionsSelected: buildPartialOptionsSelected, buildSelectedOptions: buildSelectedOptions, flattenSelectOptionsArray: flattenSelectOptionsArray, getFlattenSelectOptions: getFlattenSelectOptions, getFlattenSelectOptionsArray: getFlattenSelectOptionsArray, getNextHightlightableOption: getNextHightlightableOption, getPreviousHightlightableOption: getPreviousHightlightableOption, getSelectableOptions: getSelectableOptions, getSelectableOptionsFromArray: getSelectableOptionsFromArray, getSelectedCount: getSelectedCount, getSelectedSelectableOptions: getSelectedSelectableOptions, searchDropdownOptions: searchDropdownOptions }); export { getSelectableOptionsFromArray as a, getSelectedSelectableOptions as b, getFlattenSelectOptions as c, getFlattenSelectOptionsArray as d, buildSelectedOptions as e, flattenSelectOptionsArray as f, getSelectableOptions as g, buildAllOptionsSelected as h, getSelectedCount as i, buildPartialOptionsSelected as j, getPreviousHightlightableOption as k, getNextHightlightableOption as l, searchDropdownOptions as m, select_helper as s };