diff --git a/packages/core/src/___internal-do-not-use-will-break-in-patch/admin-ui/pages/ListPage/index.tsx b/packages/core/src/___internal-do-not-use-will-break-in-patch/admin-ui/pages/ListPage/index.tsx index 739eefe2cb1..158699c2b1a 100644 --- a/packages/core/src/___internal-do-not-use-will-break-in-patch/admin-ui/pages/ListPage/index.tsx +++ b/packages/core/src/___internal-do-not-use-will-break-in-patch/admin-ui/pages/ListPage/index.tsx @@ -26,7 +26,7 @@ import { Pagination, PaginationLabel, usePaginationParams } from '../../../../ad import { useList } from '../../../../admin-ui/context' import { GraphQLErrorNotice } from '../../../../admin-ui/components/GraphQLErrorNotice' import { Link, useRouter } from '../../../../admin-ui/router' -import { RelationsSearchFields, useFilter } from '../../../../fields/types/relationship/views/RelationshipSelect' +import { useFilter } from '../../../../fields/types/relationship/views/RelationshipSelect' import { CreateButtonLink } from '../../../../admin-ui/components/CreateButtonLink' import { FieldSelection } from './FieldSelection' import { FilterAdd } from './FilterAdd' @@ -158,27 +158,10 @@ function ListPage ({ listKey }: ListPageProps) { const filters = useFilters(list, filterableFields) const searchFields = Object.keys(list.fields).filter(key => list.fields[key].search) - const relationsSearchFields: RelationsSearchFields[] = Object.keys(list.fields) - .map((key) => { - const field = list.fields[key] - - // @ts-expect-error Wrong types for relationship fields - if (!field.fieldMeta.many !== undefined) return - - return { - field: key, - // @ts-expect-error Wrong types for relationship fields - refSearchFields: field.fieldMeta?.refSearchFields, - // @ts-expect-error Wrong types for relationship fields - many: field.fieldMeta?.many, - } - }) - .filter(Boolean) as RelationsSearchFields[] - const searchLabels = searchFields.map(key => list.fields[key].label) const searchParam = typeof query.search === 'string' ? query.search : '' const [searchString, updateSearchString] = useState(searchParam) - const search = useFilter(searchParam, list, searchFields, relationsSearchFields) + const search = useFilter(searchParam, list, searchFields) const updateSearch = (value: string) => { const { search, ...queries } = query diff --git a/packages/core/src/fields/types/relationship/views/RelationshipSelect.tsx b/packages/core/src/fields/types/relationship/views/RelationshipSelect.tsx index b00e5b6c938..7cb18f85fa5 100644 --- a/packages/core/src/fields/types/relationship/views/RelationshipSelect.tsx +++ b/packages/core/src/fields/types/relationship/views/RelationshipSelect.tsx @@ -72,13 +72,7 @@ function isUuid (x: unknown) { return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(x) } -export type RelationsSearchFields = { - field: string - refSearchFields: string[] - many: boolean -} - -export function useFilter (value: string, list: ListMeta, searchFields: string[], relationsSearchFields: RelationsSearchFields[] = []) { +export function useFilter (value: string, list: ListMeta, searchFields: string[]) { return useMemo(() => { const trimmedSearch = value.trim() if (!trimmedSearch.length) return { OR: [] } @@ -104,6 +98,40 @@ export function useFilter (value: string, list: ListMeta, searchFields: string[] for (const fieldKey of searchFields) { const field = list.fields[fieldKey] + + // @ts-expect-error TODO: fix fieldMeta type for relationship fields + if (field.fieldMeta?.refSearchFields) { + // @ts-expect-error TODO: fix fieldMeta type for relationship fields + for (const fieldKey of field.fieldMeta?.refSearchFields) { + const field = list.fields[fieldKey] + + // @ts-expect-error TODO: fix fieldMeta type for relationship fields + if (field.fieldMeta?.many) { + conditions.push({ + [fieldKey]: { + some: { + [fieldKey]: { + contains: trimmedSearch, + mode: field.search === 'insensitive' ? 'insensitive' : undefined, + }, + }, + }, + }) + + continue + } + + conditions.push({ + [field.path]: { + contains: trimmedSearch, + mode: field.search === 'insensitive' ? 'insensitive' : undefined, + }, + }) + } + + continue + } + conditions.push({ [field.path]: { contains: trimmedSearch, @@ -112,31 +140,6 @@ export function useFilter (value: string, list: ListMeta, searchFields: string[] }) } - for (const { field, refSearchFields, many } of relationsSearchFields) { - conditions.push( - ...refSearchFields.map((refSearchField) => - many - ? { - [field]: { - some: { - [refSearchField]: { - contains: trimmedSearch, - }, - }, - }, - } - : { - [field]: { - [refSearchField]: { - contains: trimmedSearch, - }, - }, - }, - ), - ) - } - - return { OR: conditions } }, [value, list, searchFields]) }