Skip to content

Commit

Permalink
switches to sorted tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dplumlee committed Oct 2, 2020
1 parent 8f139cf commit 1cbc331
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { bucketRulesResponse, showRulesTable } from './helpers';
import { bucketRulesResponse, caseInsensitiveSort, showRulesTable } from './helpers';
import { mockRule, mockRuleError } from './__mocks__/mock';
import uuid from 'uuid';
import { Rule, RuleError } from '../../../../containers/detection_engine/rules';
Expand Down Expand Up @@ -86,4 +86,15 @@ describe('AllRulesTable Helpers', () => {
expect(result).toBeTruthy();
});
});

describe('caseInsensitiveSort', () => {
describe('when an array of differently cased tags is passed', () => {
const unsortedTags = ['atest', 'Ctest', 'Btest', 'ctest', 'btest', 'Atest'];
const result = caseInsensitiveSort(unsortedTags);
it('returns an alphabetically sorted array with no regard for casing', () => {
const expected = ['atest', 'Atest', 'Btest', 'btest', 'Ctest', 'ctest'];
expect(result).toEqual(expected);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ export const showRulesTable = ({
}) =>
(rulesCustomInstalled != null && rulesCustomInstalled > 0) ||
(rulesInstalled != null && rulesInstalled > 0);

export const caseInsensitiveSort = (tags: string[]): string[] => {
return tags.sort((a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase())); // Case insensitive
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import styled from 'styled-components';
import * as i18n from '../../translations';
import { toggleSelectedGroup } from '../../../../../../common/components/ml_popover/jobs_table/filters/toggle_selected_group';
import { caseInsensitiveSort } from '../helpers';

interface TagsFilterPopoverProps {
selectedTags: string[];
Expand Down Expand Up @@ -62,9 +63,7 @@ const TagsFilterPopoverComponent = ({
selectedTags,
onSelectedTagsChanged,
}: TagsFilterPopoverProps) => {
const sortedTags = useMemo(() => {
return tags.sort((a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase())); // Case insensitive
}, [tags]);
const sortedTags = useMemo(() => caseInsensitiveSort(tags), [tags]);
const [isTagPopoverOpen, setIsTagPopoverOpen] = useState(false);
const [searchInput, setSearchInput] = useState('');
const [filterTags, setFilterTags] = useState(sortedTags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import { EuiPopover, EuiBadgeGroup, EuiBadge, EuiButtonEmpty } from '@elastic/eui';
import styled from 'styled-components';
import * as i18n from '../translations';
import { caseInsensitiveSort } from './helpers';

interface TagsDisplayProps {
tags: string[];
Expand All @@ -34,12 +35,13 @@ const TagPopoverButton = styled(EuiButtonEmpty)`
*/
const TagsDisplayComponent = ({ tags }: TagsDisplayProps) => {
const [isTagPopoverOpen, setIsTagPopoverOpen] = useState(false);
const sortedTags = useMemo(() => caseInsensitiveSort(tags), [tags]);

return (
<>
{tags.length <= 3 ? (
{sortedTags.length <= 3 ? (
<TagWrapper data-test-subj="tags">
{tags.map((tag: string, i: number) => (
{sortedTags.map((tag: string, i: number) => (
<EuiBadge
color="hollow"
key={`${tag}-${i}`}
Expand All @@ -51,7 +53,7 @@ const TagsDisplayComponent = ({ tags }: TagsDisplayProps) => {
</TagWrapper>
) : (
<TagWrapper data-test-subj="tags">
{tags.slice(0, 3).map((tag: string, i: number) => (
{sortedTags.slice(0, 3).map((tag: string, i: number) => (
<EuiBadge
color="hollow"
key={`${tag}-${i}`}
Expand Down Expand Up @@ -79,11 +81,12 @@ const TagsDisplayComponent = ({ tags }: TagsDisplayProps) => {
repositionOnScroll
>
<TagPopoverWrapper>
{tags.map((tag: string, i: number) => (
{sortedTags.map((tag: string, i: number) => (
<EuiBadge
color="hollow"
key={`${tag}-${i}`}
data-test-subj={`rules-table-column-popover-tags-${i}`}
title={tag}
>
{tag}
</EuiBadge>
Expand Down

0 comments on commit 1cbc331

Please sign in to comment.