UNPKG

@kelvininc/ui-components

Version:
112 lines (111 loc) 4.89 kB
import { isEmpty } from "lodash-es"; import { isSubString } from "./string.helper"; export 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; }, {}); }; export const getSelectableOptionsFromArray = (options) => options.filter(option => isEmpty(option.options) && !option.disabled); 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; }, {}); const flattenOptionsReducer = (accumulator, selectOption) => { var _a; accumulator.push(selectOption); Object.values((_a = selectOption.options) !== null && _a !== void 0 ? _a : {}).reduce(flattenOptionsReducer, accumulator); return accumulator; }; export const getFlattenSelectOptionsArray = (selectOptions = {}) => Object.values(selectOptions).reduce(flattenOptionsReducer, []); export const flattenSelectOptionsArray = (selectOptions = []) => selectOptions.reduce(flattenOptionsReducer, []); 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 getSelectedCount = (selectedOptions = {}) => Object.keys(selectedOptions).filter(key => selectedOptions[key]).length; export 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; }, {}); }; export 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]; }; export 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]; }; 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; }, {}); };