Skip to content

Commit ab1d74d

Browse files
committed
feat: add support for hiding the remove button in filter chips
1 parent 1af39f1 commit ab1d74d

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

src/Shared/Components/FilterChips/FilterChips.component.tsx

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const FilterChip = ({
2424
handleRemoveFilter,
2525
getFormattedLabel = noop,
2626
getFormattedValue = noop,
27+
showRemoveIcon,
2728
}: FilterChipProps) => {
2829
const removeFilter = () => {
2930
handleRemoveFilter(label, value)
@@ -40,14 +41,16 @@ const FilterChip = ({
4041
<span className="fw-6 dc__capitalize">{labelToDisplay}</span>
4142
<span className="dc__divider h-24" />
4243
<span className="dc__ellipsis-right">{valueToDisplay}</span>
43-
<button
44-
type="button"
45-
className="flex p-0 dc__transparent"
46-
onClick={removeFilter}
47-
aria-label="Remove filter"
48-
>
49-
<CloseIcon className="icon-dim-12 icon-use-fill-n6" />
50-
</button>
44+
{showRemoveIcon && (
45+
<button
46+
type="button"
47+
className="flex p-0 dc__transparent dc__hover-n50 br-4"
48+
onClick={removeFilter}
49+
aria-label="Remove filter"
50+
>
51+
<CloseIcon className="icon-dim-12 icon-use-fill-n6" />
52+
</button>
53+
)}
5154
</div>
5255
)
5356
)
@@ -64,6 +67,7 @@ const FilterChips = <T = Record<string, unknown>,>({
6467
getFormattedValue,
6568
className = '',
6669
clearButtonClassName = '',
70+
showClearAndRemove = true,
6771
}: FilterChipsProps<T>) => {
6872
const handleRemoveFilter = (filterKey, valueToRemove) => {
6973
const updatedFilterConfig = JSON.parse(JSON.stringify(filterConfig))
@@ -95,6 +99,7 @@ const FilterChips = <T = Record<string, unknown>,>({
9599
handleRemoveFilter={handleRemoveFilter}
96100
getFormattedLabel={getFormattedLabel}
97101
getFormattedValue={getFormattedValue}
102+
showRemoveIcon={showClearAndRemove}
98103
/>
99104
))
100105
) : (
@@ -105,18 +110,21 @@ const FilterChips = <T = Record<string, unknown>,>({
105110
handleRemoveFilter={handleRemoveFilter}
106111
getFormattedLabel={getFormattedLabel}
107112
getFormattedValue={getFormattedValue}
113+
showRemoveIcon={showClearAndRemove}
108114
/>
109115
),
110116
)}
111-
<div className="flex">
112-
<button
113-
type="button"
114-
className={`cta text fs-13-imp lh-20-imp h-20 p-0-imp ${clearButtonClassName}`}
115-
onClick={clearFilters}
116-
>
117-
Clear All Filters
118-
</button>
119-
</div>
117+
{showClearAndRemove && (
118+
<div className="flex">
119+
<button
120+
type="button"
121+
className={`cta text fs-13-imp lh-20-imp h-20 p-0-imp ${clearButtonClassName}`}
122+
onClick={clearFilters}
123+
>
124+
Clear All Filters
125+
</button>
126+
</div>
127+
)}
120128
</div>
121129
)
122130
)

src/Shared/Components/FilterChips/types.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,45 @@ export interface FilterChipProps {
3737
* If passed, the label will be formatted accordingly
3838
*/
3939
getFormattedValue?: (filterKey: string, filterValue: unknown) => ReactNode
40+
showRemoveIcon: boolean
4041
}
4142

42-
export interface FilterChipsProps<T = Record<string, unknown>>
43-
extends Pick<FilterChipProps, 'getFormattedLabel' | 'getFormattedValue'> {
43+
export type FilterChipsProps<T = Record<string, unknown>> = Pick<
44+
FilterChipProps,
45+
'getFormattedLabel' | 'getFormattedValue'
46+
> & {
4447
/**
4548
* Current filter configuration
4649
*/
4750
filterConfig: T
48-
/**
49-
* Callback handler for removing the filters
50-
*/
51-
clearFilters: () => void
52-
/**
53-
* Handler for removing a applied filter
54-
*/
55-
onRemoveFilter: (filterConfig: T) => void
5651
/**
5752
* Class name for the container
5853
*/
5954
className?: string
60-
/**
61-
* Class name for the clear filter button
62-
*/
63-
clearButtonClassName?: string
64-
}
55+
} & (
56+
| {
57+
/**
58+
* If false, anything related to removing filters is not shown
59+
* @default 'true'
60+
*/
61+
showClearAndRemove: false
62+
clearFilters?: never
63+
onRemoveFilter?: never
64+
clearButtonClassName?: never
65+
}
66+
| {
67+
showClearAndRemove?: true
68+
/**
69+
* Callback handler for removing the filters
70+
*/
71+
clearFilters: () => void
72+
/**
73+
* Handler for removing a applied filter
74+
*/
75+
onRemoveFilter: (filterConfig: T) => void
76+
/**
77+
* Class name for the clear filter button
78+
*/
79+
clearButtonClassName?: string
80+
}
81+
)

0 commit comments

Comments
 (0)