mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 02:16:03 +00:00
Merge pull request #15501 from strapi/fix/cm-collection-type-sorting
This commit is contained in:
commit
a7df0940c0
@ -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();
|
||||
|
||||
@ -0,0 +1 @@
|
||||
export { default as matchByTitle } from './matchByTitle';
|
||||
@ -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;
|
||||
@ -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',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user