Skip to content

Commit

Permalink
Add searchable prop in CategoryWidget to optionally use the search fu…
Browse files Browse the repository at this point in the history
…nctionality (#338)
  • Loading branch information
Sergio Clebal authored Feb 22, 2022
1 parent fad5780 commit 7887560
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions packages/react-ui/__tests__/widgets/CategoryWidgetUI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,11 @@ describe('CategoryWidgetUI', () => {
fireEvent.click(screen.getByText(/Search in 4 elements/));
fireEvent.click(screen.getByText(/Cancel/));
});

test('searchable prop', () => {
render(<CategoryWidgetUI data={DATA} maxItems={1} searchable={false} />);
expect(screen.queryByText('Search in 4 elements')).not.toBeInTheDocument();
expect(screen.getByText('Others (4)')).toBeInTheDocument();
});
});
});
23 changes: 13 additions & 10 deletions packages/react-ui/src/widgets/CategoryWidgetUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ function CategoryWidgetUI(props) {
order,
selectedCategories,
animation,
filterable
filterable,
searchable
} = props;
const [sortedData, setSortedData] = useState([]);
const [maxValue, setMaxValue] = useState(1);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -569,7 +570,7 @@ function CategoryWidgetUI(props) {
</>
)}
</Grid>
{data.length > maxItems ? (
{data.length > maxItems && searchable ? (
showAll ? (
<Button size='small' color='primary' onClick={handleCancelClicked}>
Cancel
Expand Down Expand Up @@ -606,7 +607,8 @@ CategoryWidgetUI.defaultProps = {
order: CategoryWidgetUI.ORDER_TYPES.RANKING,
selectedCategories: [],
animation: true,
filterable: true
filterable: true,
searchable: true
};

CategoryWidgetUI.propTypes = {
Expand All @@ -624,6 +626,7 @@ CategoryWidgetUI.propTypes = {
order: PropTypes.oneOf(Object.values(CategoryWidgetUI.ORDER_TYPES)),
animation: PropTypes.bool,
filterable: PropTypes.bool,
searchable: PropTypes.bool
};

export default CategoryWidgetUI;
5 changes: 5 additions & 0 deletions packages/react-widgets/src/widgets/CategoryWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const EMPTY_ARRAY = [];
* @param {Object} [props.labels] - Overwrite category labels.
* @param {boolean} [props.animation] - Enable/disable widget animations on data updates. Enabled by default.
* @param {boolean} [props.filterable] - Enable/disable widget filtering capabilities. Enabled by default.
* @param {boolean} [props.searchable] - Enable/disable widget searching capabilities. Enabled by default.
* @param {Function} [props.onError] - Function to handle error messages from the widget.
* @param {Object} [props.wrapperProps] - Extra props to pass to [WrapperWidgetUI](https://storybook-react.carto.com/?path=/docs/widgets-wrapperwidgetui--default)
* @param {Object} [props.noDataAlertProps] - Extra props to pass to [NoDataAlert]()
Expand All @@ -40,6 +41,7 @@ function CategoryWidget(props) {
labels,
animation,
filterable,
searchable,
onError,
wrapperProps,
noDataAlertProps
Expand Down Expand Up @@ -128,6 +130,7 @@ function CategoryWidget(props) {
onSelectedCategoriesChange={handleSelectedCategoriesChange}
animation={animation}
filterable={filterable}
searchable={searchable}
/>
) : (
<NoDataAlert {...noDataAlertProps} />
Expand All @@ -147,6 +150,7 @@ CategoryWidget.propTypes = {
labels: PropTypes.object,
animation: PropTypes.bool,
filterable: PropTypes.bool,
searchable: PropTypes.bool,
onError: PropTypes.func,
wrapperProps: PropTypes.object,
noDataAlertProps: PropTypes.object
Expand All @@ -156,6 +160,7 @@ CategoryWidget.defaultProps = {
labels: {},
animation: true,
filterable: true,
searchable: true,
wrapperProps: {},
noDataAlertProps: {}
};
Expand Down

0 comments on commit 7887560

Please sign in to comment.