Skip to content

Commit

Permalink
fix: update filter-badge implementation so filters can be removed on …
Browse files Browse the repository at this point in the history
…small screen
  • Loading branch information
HendrikThePendric committed Dec 16, 2024
1 parent 43d0d92 commit af974e9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
99 changes: 61 additions & 38 deletions src/pages/view/FilterBar/FilterBadge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ const getFilterValuesText = (values) =>
? values[0].name
: i18n.t('{{count}} selected', { count: values.length })

const FilterBadge = ({ filter, openFilterModal, onRemove }) => {
const { isConnected } = useDhis2ConnectionStatus()
const { width } = useWindowDimensions()
const notAllowed = !isConnected || width <= 480
const tooltipContent = !isConnected
? i18n.t('Cannot edit filters while offline')
: i18n.t('Cannot edit filters on a small screen')
const EditFilterButton = ({ tooltipContent, filter, openFilterModal }) => {
const buttonText = `${filter.name}: ${getFilterValuesText(filter.values)}`

if (!tooltipContent) {
return (
<button
data-test="filter-badge-button"
className={cx(classes.button, classes.filterButton)}
onClick={() =>
openFilterModal({
id: filter.id,
name: filter.name,
})
}
>
{buttonText}
</button>
)
}

return (
<Tooltip
Expand All @@ -30,42 +42,53 @@ const FilterBadge = ({ filter, openFilterModal, onRemove }) => {
closeDelay={100}
className={classes.tooltip}
>
{({ onFocus, onBlur, onMouseOver, onMouseOut, ref }) => (
<div
className={classes.container}
data-test="dashboard-filter-badge"
onFocus={() => notAllowed && onFocus()}
onBlur={() => notAllowed && onBlur()}
onMouseOver={() => notAllowed && onMouseOver()}
onMouseOut={() => notAllowed && onMouseOut()}
ref={ref}
{(props) => (
<button
disabled
data-test="filter-badge-button"
className={cx(classes.button, classes.filterButton)}
{...props}
>
<button
data-test="filter-badge-button"
className={cx(classes.button, classes.filterButton)}
disabled={notAllowed}
onClick={() =>
openFilterModal({
id: filter.id,
name: filter.name,
})
}
>
{filter.name}: {getFilterValuesText(filter.values)}
</button>
<button
data-test="filter-badge-clear-button"
className={cx(classes.button, classes.removeButton)}
disabled={notAllowed}
onClick={() => onRemove(filter.id)}
>
<IconCross16 />
</button>
</div>
{buttonText}
</button>
)}
</Tooltip>
)
}
EditFilterButton.propTypes = {
filter: PropTypes.object,
openFilterModal: PropTypes.func,
tooltipContent: PropTypes.string,
}

const FilterBadge = ({ filter, openFilterModal, onRemove }) => {
const { isConnected } = useDhis2ConnectionStatus()
const { width } = useWindowDimensions()
const isEditDisabled = !isConnected || width <= 480
const tooltipContent = !isConnected
? i18n.t('Cannot edit filters while offline')
: i18n.t('Cannot edit filters on a small screen')

return (
<div className={classes.container} data-test="dashboard-filter-badge">
<EditFilterButton
filter={filter}
openFilterModal={openFilterModal}
tooltipContent={isEditDisabled ? tooltipContent : null}
/>
{isConnected && (
<button
data-test="filter-badge-clear-button"
className={cx(classes.button, classes.removeButton)}
disabled={!isConnected}
onClick={() => onRemove(filter.id)}
>
<IconCross16 />
</button>
)}
</div>
)
}

FilterBadge.propTypes = {
filter: PropTypes.object.isRequired,
Expand Down
19 changes: 12 additions & 7 deletions src/pages/view/FilterBar/__tests__/FilterBadge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ test('Has enabled buttons when online', () => {
expect(getByTestId('filter-badge-clear-button')).toBeEnabled()
})

test('Has disabled buttons when offline', () => {
useDhis2ConnectionStatus.mockImplementation(() => ({ isConnected: false }))
test('Shows only a disabled edit-filter button when offline', () => {
useDhis2ConnectionStatus.mockImplementationOnce(() => ({
isConnected: false,
}))
const filter = {
id: 'ponies',
name: 'Ponies',
values: [{ name: 'Twilight Sparkle' }],
}
const { getByTestId } = render(
const { getByTestId, queryByTestId } = render(
<Provider store={createMockStore()}>
<FilterBadge
filter={filter}
Expand All @@ -97,11 +99,14 @@ test('Has disabled buttons when offline', () => {
</Provider>
)
expect(getByTestId('filter-badge-button')).toBeDisabled()
expect(getByTestId('filter-badge-clear-button')).toBeDisabled()
expect(queryByTestId('filter-badge-clear-button')).not.toBeInTheDocument()
})

test('Has disabled buttons when on small screen', () => {
useWindowDimensions.mockImplementation(() => ({ width: 440, height: 780 }))
test('Shows a disabled edit-filter button and enabled clear-filter when on small screen', () => {
useWindowDimensions.mockImplementationOnce(() => ({
width: 440,
height: 780,
}))
const filter = {
id: 'ponies',
name: 'Ponies',
Expand All @@ -118,5 +123,5 @@ test('Has disabled buttons when on small screen', () => {
</Provider>
)
expect(getByTestId('filter-badge-button')).toBeDisabled()
expect(getByTestId('filter-badge-clear-button')).toBeDisabled()
expect(getByTestId('filter-badge-clear-button')).toBeEnabled()
})
6 changes: 6 additions & 0 deletions src/pages/view/FilterBar/styles/FilterBadge.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
border-end-start-radius: 4px;
}

.filterButton:only-child {
padding-inline-end: 6px;
border-start-end-radius: 4px;
border-end-end-radius: 4px;
}

.removeButton {
padding-inline: 4px 6px;
border-start-end-radius: 4px;
Expand Down

0 comments on commit af974e9

Please sign in to comment.