Feat: blocks should only search on singular labels (#2479)
* Blocks now only search on singular labels * Add types to getBlockLabel params --------- Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com>
This commit is contained in:
@@ -7,11 +7,21 @@ import { Drawer } from '../../../../elements/Drawer';
|
||||
import { getTranslation } from '../../../../../../utilities/getTranslation';
|
||||
import { ThumbnailCard } from '../../../../elements/ThumbnailCard';
|
||||
import DefaultBlockImage from '../../../../graphics/DefaultBlockImage';
|
||||
import { i18n } from 'i18next';
|
||||
import { Block } from '../../../../../../fields/config/types';
|
||||
|
||||
import './index.scss';
|
||||
|
||||
const baseClass = 'blocks-drawer';
|
||||
|
||||
const getBlockLabel = (block: Block, i18n: i18n) => {
|
||||
if (typeof block.labels.singular === 'string') return block.labels.singular.toLowerCase();
|
||||
if (typeof block.labels.singular === 'object') {
|
||||
return getTranslation(block.labels.singular, i18n).toLowerCase();
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
export const BlocksDrawer: React.FC<Props> = (props) => {
|
||||
const {
|
||||
blocks,
|
||||
@@ -33,13 +43,15 @@ export const BlocksDrawer: React.FC<Props> = (props) => {
|
||||
}, [isModalOpen]);
|
||||
|
||||
useEffect(() => {
|
||||
const searchTermToUse = searchTerm.toLowerCase();
|
||||
const matchingBlocks = blocks.reduce((matchedBlocks, block) => {
|
||||
if (block.slug.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1) matchedBlocks.push(block);
|
||||
const blockLabel = getBlockLabel(block, i18n);
|
||||
if (blockLabel.includes(searchTermToUse)) matchedBlocks.push(block);
|
||||
return matchedBlocks;
|
||||
}, []);
|
||||
|
||||
setFilteredBlocks(matchingBlocks);
|
||||
}, [searchTerm, blocks]);
|
||||
}, [searchTerm, blocks, i18n]);
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
|
||||
Reference in New Issue
Block a user