Skip to content

Commit

Permalink
feat: Add clear filter btn to filter widget
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Jul 3, 2024
1 parent dfd3190 commit 3400f71
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/search-manager/FilterBy.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
width: 100%;
}
}

.clear-filter-button:hover {
color: $info-900 !important;
}
3 changes: 2 additions & 1 deletion src/search-manager/FilterByBlockType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ const FilterByBlockType: React.FC<Record<never, never>> = () => {
<SearchFilterWidget
appliedFilters={blockTypesFilter.map(blockType => ({ label: <BlockTypeLabel type={blockType} /> }))}
label={<FormattedMessage {...messages.blockTypeFilter} />}
clearFilter={() => setBlockTypesFilter([])}
icon={FilterList}
>
<Form.Group>
<Form.Group className="mb-0">
<Form.CheckboxSet
name="block-type-filter"
defaultValue={blockTypesFilter}
Expand Down
5 changes: 3 additions & 2 deletions src/search-manager/FilterByTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const TagOptions: React.FC<{

const FilterByTags: React.FC<Record<never, never>> = () => {
const intl = useIntl();
const { tagsFilter } = useSearchContext();
const { tagsFilter, setTagsFilter } = useSearchContext();
const [tagSearchKeywords, setTagSearchKeywords] = React.useState('');

// e.g. {"Location", "Location > North America"} if those two paths of the tag tree are expanded
Expand All @@ -186,9 +186,10 @@ const FilterByTags: React.FC<Record<never, never>> = () => {
<SearchFilterWidget
appliedFilters={tagsFilter.map(tf => ({ label: tf.split(TAG_SEP).pop() }))}
label={<FormattedMessage {...messages.blockTagsFilter} />}
clearFilter={() => setTagsFilter([])}
icon={Tag}
>
<Form.Group className="pt-3">
<Form.Group className="pt-3 mb-0">
<SearchField
onSubmit={setTagSearchKeywords}
onChange={setTagSearchKeywords}
Expand Down
25 changes: 25 additions & 0 deletions src/search-manager/SearchFilterWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
ModalPopup,
useToggle,
} from '@openedx/paragon';
import { useIntl } from '@edx/frontend-platform/i18n';

import messages from './messages';

/**
* A button that represents a filter on the search.
Expand All @@ -22,11 +25,18 @@ const SearchFilterWidget: React.FC<{
appliedFilters: { label: React.ReactNode }[];
label: React.ReactNode;
children: React.ReactNode;
clearFilter: () => void,
icon?: React.ReactNode; // eslint-disable-line react/require-default-props
}> = ({ appliedFilters, ...props }) => {
const intl = useIntl();
const [isOpen, open, close] = useToggle(false);
const [target, setTarget] = React.useState(null);

const clearAndClose = React.useCallback(() => {
props.clearFilter();
close();
}, [props.clearFilter]);

return (
<>
<div className="d-flex mr-3">
Expand All @@ -53,6 +63,21 @@ const SearchFilterWidget: React.FC<{
style={{ textAlign: 'start' }}
>
{props.children}

{
!!appliedFilters.length
&& (
<div className="d-flex justify-content-end">
<Button
onClick={clearAndClose}
variant="link"
className="text-info-500 text-decoration-none clear-filter-button"
>
{ intl.formatMessage(messages.clearFilter) }
</Button>
</div>
)
}
</div>
</ModalPopup>
</>
Expand Down
5 changes: 5 additions & 0 deletions src/search-manager/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const messages = defineMessages({
defaultMessage: '{numResults, plural, one {# result} other {# results}} found',
description: 'This count displays how many matching results were found from the user\'s search',
},
clearFilter: {
id: 'course-authoring.search-manager.searchFilterWidget.clearFilter',
defaultMessage: 'Clear Filter',
description: 'Label for the button that removes applied search filters in a specific widget',
},
});

export default messages;

0 comments on commit 3400f71

Please sign in to comment.