UNPKG

@spaced-out/ui-design-system

Version:
97 lines (93 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useArbitraryOptionAddition = useArbitraryOptionAddition; var _useFilteredOptions = require("../useFilteredOptions"); function useArbitraryOptionAddition(_ref) { let { searchTerm, options = [], excludedKeys = [], allowArbitraryValues = true, allowMultiArbitraryValues = false, validateArbitraryValue = value => value.trim() !== '', // @ts-ignore - TS2322 - Type '{ key: string; label: string; arbitrary: boolean; }' is not assignable to type 'V'. makeArbitraryValue = searchTerm => ({ key: searchTerm, label: searchTerm, arbitrary: true }), // @ts-ignore - TS2366 - Function lacks ending return statement and return type does not include 'undefined'. makeMultiArbitraryValue = searchTerm => { const regex = /^(?=.*[,\n]).+$/s; if (regex.test(searchTerm)) { // $FlowFixMe - Object literal is compatible with V due to type constraint // @ts-ignore - TS2322 - Type '{ key: string; label: string; multiArbitrary: boolean; }' is not assignable to type 'V'. return { key: searchTerm, label: searchTerm, multiArbitrary: true }; } }, groupTitleOptions = [], arbitraryGroup = { groupTitle: '', showLineDivider: false, options: [] }, searchOptionsBy = (option, searchTerm) => { // $FlowFixMe // @ts-ignore - TS2322 - Type 'V' is not assignable to type '{ label: string; key: string; }'. const { label, key } = option; return key.toLowerCase().includes(searchTerm) || label.toLowerCase().includes(searchTerm); } } = _ref; const trimmedSearchTerm = (searchTerm || '').trim().toLowerCase(); const { filteredOptions, filteredGroupTitleOptions } = (0, _useFilteredOptions.useFilteredOptions)({ searchTerm: trimmedSearchTerm, options, groupTitleOptions, searchOptionsBy }); // Find if an arbitrary option should be added const arbitraryOption = allowArbitraryValues && trimmedSearchTerm && // @ts-ignore - TS2532 - Object is possibly 'undefined'. | TS2339 - Property 'key' does not exist on type 'V'. !filteredOptions.some(option => option.key === trimmedSearchTerm) && // @ts-ignore - TS2532 - Object is possibly 'undefined'. !filteredGroupTitleOptions.some(group => Array.isArray(group.options) && // @ts-ignore - TS2339 - Property 'key' does not exist on type 'V'. group.options.some(option => option.key === trimmedSearchTerm)) && !excludedKeys.includes(trimmedSearchTerm) && validateArbitraryValue(trimmedSearchTerm) && makeArbitraryValue(trimmedSearchTerm); // Find if a multi-arbitrary option should be added const multiArbitraryOption = allowMultiArbitraryValues && trimmedSearchTerm && makeMultiArbitraryValue(trimmedSearchTerm); // Compose the results let optionsWithArbitrary = filteredOptions; let groupTitleOptionsWithArbitrary = groupTitleOptions; if (arbitraryOption) { // @ts-ignore - TS2488 - Type 'V[] | undefined' must have a '[Symbol.iterator]()' method that returns an iterator. optionsWithArbitrary = [arbitraryOption, ...optionsWithArbitrary]; groupTitleOptionsWithArbitrary = [{ ...arbitraryGroup, options: [arbitraryOption] }, ...groupTitleOptionsWithArbitrary]; } if (multiArbitraryOption) { optionsWithArbitrary = [multiArbitraryOption, ...optionsWithArbitrary]; groupTitleOptionsWithArbitrary = [{ ...arbitraryGroup, options: [multiArbitraryOption] }, ...groupTitleOptionsWithArbitrary]; } // @ts-ignore - TS2322 - Type 'V[] | undefined' is not assignable to type 'V[]'. return { optionsWithArbitrary, groupTitleOptionsWithArbitrary }; }