mirror of
https://github.com/strapi/strapi.git
synced 2025-09-21 22:40:24 +00:00
fix: swap out match-sorter with new hooks in CM
This commit is contained in:
parent
28d1786a70
commit
ce6a24ee10
@ -16,32 +16,31 @@ import {
|
|||||||
SubNavSections,
|
SubNavSections,
|
||||||
SubNavLink,
|
SubNavLink,
|
||||||
} from '@strapi/design-system/v2';
|
} from '@strapi/design-system/v2';
|
||||||
|
import { useFilter, useCollator } from '@strapi/helper-plugin';
|
||||||
|
|
||||||
import { matchByTitle } from './utils';
|
|
||||||
import getTrad from '../../../utils/getTrad';
|
import getTrad from '../../../utils/getTrad';
|
||||||
import { makeSelectModelLinks } from '../selectors';
|
import { makeSelectModelLinks } from '../selectors';
|
||||||
|
|
||||||
const LeftMenu = () => {
|
const LeftMenu = () => {
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage, locale } = useIntl();
|
||||||
const modelLinksSelector = useMemo(makeSelectModelLinks, []);
|
const modelLinksSelector = useMemo(makeSelectModelLinks, []);
|
||||||
const { collectionTypeLinks, singleTypeLinks } = useSelector(
|
const { collectionTypeLinks, singleTypeLinks } = useSelector(modelLinksSelector, shallowEqual);
|
||||||
(state) => modelLinksSelector(state),
|
|
||||||
shallowEqual
|
|
||||||
);
|
|
||||||
|
|
||||||
const toIntl = (links) =>
|
const { contains } = useFilter(locale, {
|
||||||
links.map((link) => {
|
sensitivity: 'base',
|
||||||
return {
|
|
||||||
...link,
|
|
||||||
title: formatMessage({ id: link.title, defaultMessage: link.title }),
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const intlCollectionTypeLinks = toIntl(collectionTypeLinks);
|
/**
|
||||||
const intlSingleTypeLinks = toIntl(singleTypeLinks);
|
* @type {Intl.Collator}
|
||||||
|
*/
|
||||||
|
const formatter = useCollator(locale, {
|
||||||
|
sensitivity: 'base',
|
||||||
|
});
|
||||||
|
|
||||||
const menu = [
|
const menu = useMemo(
|
||||||
|
() =>
|
||||||
|
[
|
||||||
{
|
{
|
||||||
id: 'collectionTypes',
|
id: 'collectionTypes',
|
||||||
title: {
|
title: {
|
||||||
@ -49,7 +48,7 @@ const LeftMenu = () => {
|
|||||||
defaultMessage: 'Collection Types',
|
defaultMessage: 'Collection Types',
|
||||||
},
|
},
|
||||||
searchable: true,
|
searchable: true,
|
||||||
links: matchByTitle(intlCollectionTypeLinks, search),
|
links: collectionTypeLinks,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'singleTypes',
|
id: 'singleTypes',
|
||||||
@ -58,9 +57,31 @@ const LeftMenu = () => {
|
|||||||
defaultMessage: 'Single Types',
|
defaultMessage: 'Single Types',
|
||||||
},
|
},
|
||||||
searchable: true,
|
searchable: true,
|
||||||
links: matchByTitle(intlSingleTypeLinks, search),
|
links: singleTypeLinks,
|
||||||
},
|
},
|
||||||
];
|
].map((section) => ({
|
||||||
|
...section,
|
||||||
|
links: section.links
|
||||||
|
/**
|
||||||
|
* Filter by the search value
|
||||||
|
*/
|
||||||
|
.filter((link) => contains(link.title, search))
|
||||||
|
/**
|
||||||
|
* Sort correctly using the language
|
||||||
|
*/
|
||||||
|
.sort((a, b) => formatter.compare(a.title, b.title))
|
||||||
|
/**
|
||||||
|
* Apply the formated strings to the links from react-intl
|
||||||
|
*/
|
||||||
|
.map((link) => {
|
||||||
|
return {
|
||||||
|
...link,
|
||||||
|
title: formatMessage({ id: link.title, defaultMessage: link.title }),
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
})),
|
||||||
|
[collectionTypeLinks, search, singleTypeLinks, contains, formatMessage, formatter]
|
||||||
|
);
|
||||||
|
|
||||||
const handleClear = () => {
|
const handleClear = () => {
|
||||||
setSearch('');
|
setSearch('');
|
||||||
|
@ -1 +0,0 @@
|
|||||||
export { default as matchByTitle } from './matchByTitle';
|
|
@ -1,24 +0,0 @@
|
|||||||
import matchSorter from 'match-sorter';
|
|
||||||
import camelCase from 'lodash/camelCase';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {(links: array, search? : string) => array }
|
|
||||||
*/
|
|
||||||
const matchByTitle = (links, search) =>
|
|
||||||
search
|
|
||||||
? matchSorter(links, search.toLowerCase(), { keys: [(item) => item.title.toLowerCase()] })
|
|
||||||
: links.sort((link, nextLink) => {
|
|
||||||
const title = camelCase(link.title);
|
|
||||||
const nextTitle = camelCase(nextLink.title);
|
|
||||||
|
|
||||||
if (title < nextTitle) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (title > nextTitle) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
export default matchByTitle;
|
|
@ -1,43 +0,0 @@
|
|||||||
import { matchByTitle } from '../index';
|
|
||||||
|
|
||||||
describe('Content Manager | Pages | LeftMenu | Utils', () => {
|
|
||||||
describe('matchByTitle', () => {
|
|
||||||
it('correctly sorts a list of links with special characters', () => {
|
|
||||||
const links = [
|
|
||||||
{
|
|
||||||
title: 'zebra',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'Address',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'dog',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'škola',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'Članky',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
expect(matchByTitle(links)).toEqual([
|
|
||||||
{
|
|
||||||
title: 'Address',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'Članky',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'dog',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'škola',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'zebra',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@ -102,7 +102,6 @@
|
|||||||
"markdown-it-mark": "^3.0.1",
|
"markdown-it-mark": "^3.0.1",
|
||||||
"markdown-it-sub": "^1.0.0",
|
"markdown-it-sub": "^1.0.0",
|
||||||
"markdown-it-sup": "1.0.0",
|
"markdown-it-sup": "1.0.0",
|
||||||
"match-sorter": "^4.0.2",
|
|
||||||
"mini-css-extract-plugin": "2.7.2",
|
"mini-css-extract-plugin": "2.7.2",
|
||||||
"node-schedule": "2.1.0",
|
"node-schedule": "2.1.0",
|
||||||
"p-map": "4.0.0",
|
"p-map": "4.0.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user