mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 19:04:38 +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 React, { useMemo, useState } from 'react';
|
||||||
import { useSelector, shallowEqual } from 'react-redux';
|
import { useSelector, shallowEqual } from 'react-redux';
|
||||||
import { useIntl } from 'react-intl';
|
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 { NavLink } from 'react-router-dom';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
SubNav,
|
SubNav,
|
||||||
SubNavHeader,
|
SubNavHeader,
|
||||||
@ -18,14 +16,11 @@ import {
|
|||||||
SubNavSections,
|
SubNavSections,
|
||||||
SubNavLink,
|
SubNavLink,
|
||||||
} from '@strapi/design-system/v2/SubNav';
|
} from '@strapi/design-system/v2/SubNav';
|
||||||
|
|
||||||
|
import { matchByTitle } from './utils';
|
||||||
import getTrad from '../../../utils/getTrad';
|
import getTrad from '../../../utils/getTrad';
|
||||||
import { makeSelectModelLinks } from '../selectors';
|
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 LeftMenu = () => {
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
const { formatMessage } = useIntl();
|
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