From b4b2c4d4140e72f620f33daf49f9f6ccc9960b20 Mon Sep 17 00:00:00 2001 From: Robert Andreas Kyllo Date: Fri, 20 Nov 2026 23:44:09 +0100 Subject: [PATCH] feat: #101 show more country filters --- client/src/components/FilterPanel.css | 4 +- client/src/components/FilterPanel.test.tsx | 73 ++++++++++++++++++++++ client/src/components/FilterPanel.tsx | 72 ++++++++++++++------- 3 files changed, 124 insertions(+), 25 deletions(-) diff --git a/client/src/components/FilterPanel.css b/client/src/components/FilterPanel.css index 8a9affc..b8de2ff 100644 --- a/client/src/components/FilterPanel.css +++ b/client/src/components/FilterPanel.css @@ -132,12 +132,12 @@ overflow-wrap: anywhere; } .clear-filters, -.show-genres { +.show-filter-options { min-height: 2rem; padding: 0.25rem 0.625rem; font-size: 0.8125rem; } -.show-genres { +.show-filter-options { margin-top: 0.5rem; border: 1px solid var(--border-strong); background: var(--surface); diff --git a/client/src/components/FilterPanel.test.tsx b/client/src/components/FilterPanel.test.tsx index d8b480a..8f95dc3 100644 --- a/client/src/components/FilterPanel.test.tsx +++ b/client/src/components/FilterPanel.test.tsx @@ -162,6 +162,79 @@ describe('FilterPanel', () => { expect(screen.getByRole('checkbox', { name: 'Genre 9 0 albums' })).toBeChecked(); }); + it('shows six countries, retains selected countries, and expands and collapses the list', async () => { + const countryFacets = [ + ['US', 'United States', 10], + ['GB', 'United Kingdom', 9], + ['DE', 'Germany', 8], + ['FR', 'France', 7], + ['JP', 'Japan', 6], + ['SE', 'Sweden', 5], + ['BR', 'Brazil', 4], + ['CA', 'Canada', 3], + ].map(([countryCode, countryName, count]) => ({ + countryCode: String(countryCode), + countryName: String(countryName), + count: Number(count), + })); + const { props } = renderPanel({ countries: ['BR'], countryFacets }); + const group = screen.getByRole('group', { name: 'Artist country' }); + const user = userEvent.setup(); + + expect(within(group).getAllByRole('checkbox')).toHaveLength(7); + expect( + within(group).getByRole('checkbox', { name: 'Brazil 4 albums' }), + ).toBeChecked(); + expect( + within(group).queryByRole('checkbox', { name: 'Canada 3 albums' }), + ).not.toBeInTheDocument(); + + const showMore = within(group).getByRole('button', { name: 'Show more countries' }); + expect(showMore).toHaveAttribute('aria-controls', 'country-options'); + showMore.focus(); + await user.keyboard(' '); + expect(showMore).toHaveAttribute('aria-expanded', 'true'); + expect(within(group).getAllByRole('checkbox')).toHaveLength(8); + expect( + within(group).getByRole('checkbox', { name: 'Canada 3 albums' }), + ).toBeVisible(); + expect(props.onCountriesChange).not.toHaveBeenCalled(); + + await user.keyboard('{Enter}'); + const showMoreAgain = within(group).getByRole('button', { + name: 'Show more countries', + }); + expect(showMoreAgain).toHaveAttribute('aria-expanded', 'false'); + expect(showMoreAgain).toHaveFocus(); + expect(within(group).getAllByRole('checkbox')).toHaveLength(7); + expect( + within(group).getByRole('checkbox', { name: 'Brazil 4 albums' }), + ).toBeChecked(); + }); + + it('returns focus when deselecting a selected country outside the collapsed preview', async () => { + const countryFacets = Array.from({ length: 7 }, (_, index) => ({ + countryCode: `C${index}`, + countryName: `Country ${index + 1}`, + count: 7 - index, + })); + const { props, rerender } = renderPanel({ + countries: ['C6'], + countryFacets, + }); + const selected = screen.getByRole('checkbox', { name: 'Country 7 1 albums' }); + const user = userEvent.setup(); + selected.focus(); + await user.keyboard(' '); + expect(props.onCountriesChange).toHaveBeenCalledWith([]); + + rerender(); + expect( + screen.queryByRole('checkbox', { name: 'Country 7 1 albums' }), + ).not.toBeInTheDocument(); + expect(screen.getByRole('button', { name: 'Filters' })).toHaveFocus(); + }); + it('removes each filter independently and returns focus to the Filters toggle', async () => { const { props } = renderPanel( { genres: ['4', '2'], decades: [1970], reviewed: true }, diff --git a/client/src/components/FilterPanel.tsx b/client/src/components/FilterPanel.tsx index 63d7b04..bcbbbe9 100644 --- a/client/src/components/FilterPanel.tsx +++ b/client/src/components/FilterPanel.tsx @@ -22,6 +22,7 @@ interface Props { } const GENRE_PREVIEW_COUNT = 6; +const COUNTRY_PREVIEW_COUNT = 6; function toggle(list: T[], value: T): T[] { return list.includes(value) ? list.filter((item) => item !== value) : [...list, value]; @@ -104,11 +105,20 @@ export default function FilterPanel(props: Props) { ]; const [open, setOpen] = useState(false); const [showAllGenres, setShowAllGenres] = useState(false); + const [showAllCountries, setShowAllCountries] = useState(false); // Selected genres stay visible even when they fall outside the preview. const visibleGenres = genres.filter( ({ genre }, index) => showAllGenres || index < GENRE_PREVIEW_COUNT || props.genres.includes(genre.id), ); + // Keep the incoming facet order, which is ranked by album count. Selected + // countries outside the six-item preview remain available for deselection. + const visibleCountries = countries.filter( + ({ countryCode }, index) => + showAllCountries || + index < COUNTRY_PREVIEW_COUNT || + props.countries.includes(countryCode), + ); const toggleRef = useRef(null); useEffect(() => { @@ -207,7 +217,7 @@ export default function FilterPanel(props: Props) { {genres.length > GENRE_PREVIEW_COUNT && ( + )} )}