Skip to content

Commit

Permalink
fix: searches on correct useAsTitle field in polymorphic list drawers #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed May 30, 2023
1 parent c76dc77 commit 9ec2a40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/admin/components/elements/ListControls/index.tsx
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import AnimateHeight from 'react-animate-height';
import { useTranslation } from 'react-i18next';
import { useWindowInfo } from '@faceless-ui/window-info';
Expand All @@ -20,11 +20,24 @@ import EditMany from '../EditMany';
import DeleteMany from '../DeleteMany';
import PublishMany from '../PublishMany';
import UnpublishMany from '../UnpublishMany';
import { SanitizedCollectionConfig } from '../../../../collections/config/types';

import './index.scss';

const baseClass = 'list-controls';

const getUseAsTitle = (collection: SanitizedCollectionConfig) => {
const {
admin: {
useAsTitle,
},
fields,
} = collection;

const topLevelFields = flattenFields(fields);
return topLevelFields.find((field) => fieldAffectsData(field) && field.name === useAsTitle);
};

const ListControls: React.FC<Props> = (props) => {
const {
collection,
Expand All @@ -37,7 +50,6 @@ const ListControls: React.FC<Props> = (props) => {
collection: {
fields,
admin: {
useAsTitle,
listSearchableFields,
},
},
Expand All @@ -46,10 +58,11 @@ const ListControls: React.FC<Props> = (props) => {
const params = useSearchParams();
const shouldInitializeWhereOpened = validateWhereQuery(params?.where);

const [titleField] = useState(() => {
const topLevelFields = flattenFields(fields);
return topLevelFields.find((field) => fieldAffectsData(field) && field.name === useAsTitle);
});
const [titleField, setTitleField] = useState(getUseAsTitle(collection));
useEffect(() => {
setTitleField(getUseAsTitle(collection));
}, [collection]);

const [textFieldsToBeSearched] = useState(getTextFieldsToBeSearched(listSearchableFields, fields));
const [visibleDrawer, setVisibleDrawer] = useState<'where' | 'sort' | 'columns'>(shouldInitializeWhereOpened ? 'where' : undefined);
const { t, i18n } = useTranslation('general');
Expand Down
4 changes: 3 additions & 1 deletion src/admin/components/elements/SearchFilter/index.tsx
Expand Up @@ -85,8 +85,10 @@ const SearchFilter: React.FC<Props> = (props) => {
}
return `${prev}, ${getTranslation(curr.label || curr.name, i18n)}`;
}, placeholder.current);
} else {
placeholder.current = t('searchBy', { label: getTranslation(fieldLabel, i18n) });
}
}, [t, listSearchableFields, i18n]);
}, [t, listSearchableFields, i18n, fieldLabel]);

return (
<div className={baseClass}>
Expand Down

0 comments on commit 9ec2a40

Please sign in to comment.