import type * as React from 'react'; import type { SelectType } from '../components/Select/Select'; import { getTextFromChildren } from './children'; export type Option = { value?: unknown; label?: React.ReactNode; [index: string]: any; }; export type GetOptionLabel = (option: O) => O['label']; export type FilterFn = ( inputValue: string, option: O, getOptionsLabel?: GetOptionLabel, ) => boolean; export type SortFn = (optionA: O, optionB: O, inputValue: string) => number; function getOptionLabelDefault(option: O): O['label'] { return option.label; } export function defaultFilterFn( ...args: Parameters> ): ReturnType> { const [rawSearchQuery = '', option, getOptionLabel] = args; const foundRawLabel = getOptionLabel ? getOptionLabel(option) : getOptionLabelDefault(option); if (foundRawLabel === undefined) { return false; } const searchQuery = rawSearchQuery.toLocaleLowerCase(); const label = getTextFromChildren(foundRawLabel).toLocaleLowerCase(); if (label.startsWith(searchQuery)) { return true; } const findAllIncludes = (target = '', search = '') => { const includes = []; let i = target.indexOf(search); while (i !== -1) { includes.push(i); i = target.indexOf(search, i + 1); } return includes; }; const includes = findAllIncludes(label, searchQuery); if (includes.length) { // Ищем вхождение перед началом которого не буква const letterRegexp = new RegExp('\\p{L}', 'u'); for (const index of includes) { if (!letterRegexp.test(label[index - 1])) { return true; } } } return false; } export const getFormFieldModeFromSelectType = ( selectType: SelectType = 'default', ): 'default' | 'plain' => { return selectType === 'default' ? 'default' : 'plain'; };