From 7887560165d511471586be00bb54da9babae46c8 Mon Sep 17 00:00:00 2001 From: Sergio Clebal Date: Tue, 22 Feb 2022 13:09:08 +0100 Subject: [PATCH] Add searchable prop in CategoryWidget to optionally use the search functionality (#338) --- CHANGELOG.md | 1 + .../widgets/CategoryWidgetUI.test.js | 6 +++++ .../react-ui/src/widgets/CategoryWidgetUI.js | 23 +++++++++++-------- .../src/widgets/CategoryWidget.js | 5 ++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d6cf1e41..c0efa8086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add groupByDate tests [#346](https://github.com/CartoDB/carto-react/pull/346) - Refactor PieWidgetUI [#341](https://github.com/CartoDB/carto-react/pull/341) +- Add searchable prop in CategoryWidget to optionally use the search functionality [#338](https://github.com/CartoDB/carto-react/pull/338) - Implement stringSearch filter [#331](https://github.com/CartoDB/carto-react/pull/331) - Add filters tests [#331](https://github.com/CartoDB/carto-react/pull/331) diff --git a/packages/react-ui/__tests__/widgets/CategoryWidgetUI.test.js b/packages/react-ui/__tests__/widgets/CategoryWidgetUI.test.js index a3c4f116e..373d25fbc 100644 --- a/packages/react-ui/__tests__/widgets/CategoryWidgetUI.test.js +++ b/packages/react-ui/__tests__/widgets/CategoryWidgetUI.test.js @@ -161,5 +161,11 @@ describe('CategoryWidgetUI', () => { fireEvent.click(screen.getByText(/Search in 4 elements/)); fireEvent.click(screen.getByText(/Cancel/)); }); + + test('searchable prop', () => { + render(); + expect(screen.queryByText('Search in 4 elements')).not.toBeInTheDocument(); + expect(screen.getByText('Others (4)')).toBeInTheDocument(); + }); }); }); diff --git a/packages/react-ui/src/widgets/CategoryWidgetUI.js b/packages/react-ui/src/widgets/CategoryWidgetUI.js index 522526e88..e867e69a8 100644 --- a/packages/react-ui/src/widgets/CategoryWidgetUI.js +++ b/packages/react-ui/src/widgets/CategoryWidgetUI.js @@ -141,7 +141,8 @@ function CategoryWidgetUI(props) { order, selectedCategories, animation, - filterable + filterable, + searchable } = props; const [sortedData, setSortedData] = useState([]); const [maxValue, setMaxValue] = useState(1); @@ -283,15 +284,20 @@ function CategoryWidgetUI(props) { [blockedCategories, labels, maxItems, searchValue, showAll] ); + const getCategoriesCount = useCallback(() => { + const blocked = blockedCategories.length; + return blocked ? data.length - blocked : data.length - maxItems; + }, [data, maxItems, blockedCategories]); + const getCategoryLabel = useCallback( (name) => { if (name === REST_CATEGORY) { - return 'Others'; + return `Others ${searchable ? '' : `(${getCategoriesCount()})`}`; } else { return labels[name] || name; } }, - [labels] + [getCategoriesCount, labels, searchable] ); const getProgressbarLength = useCallback( @@ -363,11 +369,6 @@ function CategoryWidgetUI(props) { } }, [animation, sortedData]); - const getCategoriesCount = useCallback(() => { - const blocked = blockedCategories.length; - return blocked ? data.length - blocked : data.length - maxItems; - }, [data, maxItems, blockedCategories]); - // Separated to simplify the widget layout but inside the main component to avoid passing all dependencies const CategoryItem = (props) => { const { data, onCategoryClick } = props; @@ -569,7 +570,7 @@ function CategoryWidgetUI(props) { )} - {data.length > maxItems ? ( + {data.length > maxItems && searchable ? ( showAll ? (