Skip to content

Commit

Permalink
feat(ui): FilterGroup - add search when there are more than 10 items
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Jan 9, 2024
1 parent 1ff1ee3 commit 5f40560
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/ui/src/ui/filters/filters.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@
text-transform: lowercase;
}

.filterGroupSearchWrapper {
padding-bottom: var(--space-xxxsmall);
margin-bottom: var(--space-xxxsmall);
border-bottom: 1px solid var(--color-outline);
}

.filterGroupSearchNotFound {
padding: var(--space-xxxsmall);
text-align: center;
font-size: var(--size-small);
color: var(--color-text-light);
}

.filterGroupItems {
max-height: calc(10 * (var(--space-small) + 2 * var(--space-xxxsmall)));
overflow: auto;
Expand Down
30 changes: 28 additions & 2 deletions packages/ui/src/ui/filters/filters.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useCallback } from 'react';
import React, { ChangeEvent, useCallback, useMemo, useState } from 'react';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import ChangeEvent.
import cx from 'classnames';

import type { FilterFieldsData, FilterGroupFieldData } from '../../types';
import { FlexStack } from '../../layout/flex-stack';
import { Dropdown } from '../dropdown';
import { Icon } from '../icon';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import Icon.
import { InputSearch } from '../input-search';
import * as I18N from './filters.i18n';
import { getGroupFiltersLabelSuffix, LABELS } from './filters.utils';
import css from './filters.module.css';
Expand Down Expand Up @@ -75,6 +77,8 @@ const FilterGroup = (props: FilterGroupProps) => {
toggleFilters,
} = props;

const [search, setSearch] = useState('');

const { label: groupLabel, children: groupItems } = data;

const areAllGroupItemsChecked = groupItems
Expand Down Expand Up @@ -108,6 +112,14 @@ const FilterGroup = (props: FilterGroupProps) => {
});
};

const filteredGroupItems = useMemo(() => {
if (!search) {
return groupItems;
}

return groupItems.filter((item) => item.label.match(new RegExp(`${search}`, 'i')));
}, [search, groupItems]);

return (
<Dropdown
className={className}
Expand All @@ -118,8 +130,22 @@ const FilterGroup = (props: FilterGroupProps) => {
{({ MenuItem, menuItemClassName }) => {
return (
<>
{groupItems.length > 10 && (
<div className={css.filterGroupSearchWrapper}>
<InputSearch
defaultValue={search}
onChange={setSearch}
placeholder="Chunk name"
debounceWait={0}
size="small"
/>
</div>
)}
<div className={css.filterGroupItems}>
{groupItems.map(({ key: itemKey, ...itemData }) => {
{filteredGroupItems.length === 0 && (
<p className={css.filterGroupSearchNotFound}>No chunks found</p>
)}
{filteredGroupItems.map(({ key: itemKey, ...itemData }) => {
const id = [groupKey, itemKey].join('.');
const getOnOnlyClick = () => getOnGroupCheck(false, { [id]: true });

Expand Down

0 comments on commit 5f40560

Please sign in to comment.