Merge pull request #15501 from strapi/fix/cm-collection-type-sorting

This commit is contained in:
Jamie Howard 2023-01-23 09:32:52 +00:00 committed by GitHub
commit a7df0940c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 8 deletions

View File

@ -7,10 +7,8 @@
import React, { useMemo, useState } from 'react';
import { useSelector, shallowEqual } from 'react-redux';
import { useIntl } from 'react-intl';
import matchSorter from 'match-sorter';
import sortBy from 'lodash/sortBy';
import toLower from 'lodash/toLower';
import { NavLink } from 'react-router-dom';
import {
SubNav,
SubNavHeader,
@ -18,14 +16,11 @@ import {
SubNavSections,
SubNavLink,
} from '@strapi/design-system/v2/SubNav';
import { matchByTitle } from './utils';
import getTrad from '../../../utils/getTrad';
import { makeSelectModelLinks } from '../selectors';
const matchByTitle = (links, search) =>
search
? matchSorter(links, toLower(search), { keys: [(item) => toLower(item.title)] })
: sortBy(links, (object) => object.title.toLowerCase());
const LeftMenu = () => {
const [search, setSearch] = useState('');
const { formatMessage } = useIntl();

View File

@ -0,0 +1 @@
export { default as matchByTitle } from './matchByTitle';

View File

@ -0,0 +1,24 @@
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;

View File

@ -0,0 +1,43 @@
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',
},
]);
});
});
});