UNPKG

@totalsoft/rocket-ui

Version:

A set of reusable and composable React components built on top of Material UI core for developing fast and friendly web applications interfaces.

57 lines 2.93 kB
import { createFilterOptions } from '@mui/material/Autocomplete'; import { prop, map, innerJoin, find, propEq, all, includes, is, isEmpty, isNil, props, omit, equals, any } from 'ramda'; export const findFirstNotNil = (propNames, option) => find(x => !isNil(x), props(propNames, option)); export const isStringOrNumber = (option) => is(String, option) || is(Number, option); const hasStringOptions = (options) => all(is(String), options) && !isEmpty(options); const filter = createFilterOptions(); export const filterOptions = (labelKey, valueKey, creatable) => (options, params) => { const filtered = filter(options, params); const { inputValue } = params; // Suggest the creation of a new value if it's not empty and it doesn't already exist const exists = any((option) => is(Object, option) ? equals(inputValue, is(String, option?.[labelKey]) ? option?.[labelKey] : JSON.stringify(option?.[labelKey])) : equals(inputValue, is(String, option) ? option : JSON.stringify(option)), options); if (creatable && !(isEmpty(inputValue) || isNil(inputValue)) && !exists) { filtered.push(hasStringOptions(options) ? { _primitiveValue: inputValue, _createdOption: true } : { // TODO: The valueKey should be different from the inputValue [valueKey]: inputValue, [labelKey]: inputValue, _createdOption: true }); } return filtered; }; export const getSimpleValue = (readonlyOptions, value, valueKey, isMultiSelection) => { const options = readonlyOptions; if (isMultiSelection && (!is(Array, value) || isEmpty(options))) return []; if (!all(is(Object), options)) return value; // Add new options if the DeprecatedAutocomplete is multiSelection and creatable if (is(Array, value)) { const optionsSimpleValues = map(prop(valueKey), options); value?.map(v => { if (!includes(v, optionsSimpleValues)) options.push({ [valueKey]: v }); }); } const result = isMultiSelection ? innerJoin((o, v) => o[valueKey] === v, options, value) : find(propEq(value, valueKey), options); return result || null; }; export const computeChangedMultiValue = (input, simpleValue, valueKey, labelKey) => simpleValue ? input.map((a) => (!is(Object, a) ? a : findFirstNotNil([valueKey, labelKey, '_primitiveValue'], a))) : input.map((a) => (!is(Object, a) ? a : prop('_primitiveValue', a) || omit(['_createdOption'], a))); export const computeChangedSingleValue = (input, simpleValue, valueKey, labelKey) => simpleValue ? findFirstNotNil([valueKey, labelKey], input) : (prop('_primitiveValue', input) ?? omit(['_createdOption'], input)); export const stopPropagation = (event) => { event.stopPropagation(); }; //# sourceMappingURL=utils.js.map