mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 19:04:38 +00:00
test(content-manager): leftMenu title sorting
chore(content-manager)
This commit is contained in:
parent
88b2d2a5fa
commit
2ae9cd3a45
@ -7,13 +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 { NavLink } from 'react-router-dom';
|
import { NavLink } from 'react-router-dom';
|
||||||
|
|
||||||
import sortBy from 'lodash/sortBy';
|
|
||||||
import toLower from 'lodash/toLower';
|
|
||||||
import camelCase from 'lodash/camelCase';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
SubNav,
|
SubNav,
|
||||||
SubNavHeader,
|
SubNavHeader,
|
||||||
@ -22,14 +17,10 @@ import {
|
|||||||
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) => camelCase(object.title));
|
|
||||||
|
|
||||||
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, linkNext) => {
|
||||||
|
const title = camelCase(link.title);
|
||||||
|
const nextTitle = camelCase(linkNext.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