Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/src/components/FilterPanel.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
73 changes: 73 additions & 0 deletions client/src/components/FilterPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<FilterPanel {...props} countries={[]} />);
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 },
Expand Down
72 changes: 49 additions & 23 deletions client/src/components/FilterPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface Props {
}

const GENRE_PREVIEW_COUNT = 6;
const COUNTRY_PREVIEW_COUNT = 6;

function toggle<T>(list: T[], value: T): T[] {
return list.includes(value) ? list.filter((item) => item !== value) : [...list, value];
Expand Down Expand Up @@ -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<HTMLButtonElement>(null);

useEffect(() => {
Expand Down Expand Up @@ -207,7 +217,7 @@ export default function FilterPanel(props: Props) {
{genres.length > GENRE_PREVIEW_COUNT && (
<button
type="button"
className="show-genres"
className="show-filter-options"
aria-expanded={showAllGenres}
aria-controls="genre-options"
onClick={() => setShowAllGenres(!showAllGenres)}
Expand Down Expand Up @@ -247,29 +257,45 @@ export default function FilterPanel(props: Props) {
{countries.length > 0 && (
<fieldset className="filter-group--countries">
<legend>Artist country</legend>
<div className="filter-list">
{countries.map(({ countryCode, countryName, count }) => (
<label key={countryCode}>
<input
type="checkbox"
checked={props.countries.includes(countryCode)}
onChange={(event) => {
if (
document.activeElement === event.currentTarget &&
!event.currentTarget.checked &&
count === 0
)
toggleRef.current?.focus();
props.onCountriesChange(toggle(props.countries, countryCode));
}}
/>
<span>{countryName}</span>{' '}
<span className="filter-count">
{count} <span className="visually-hidden">albums</span>
</span>
</label>
))}
<div id="country-options" className="filter-list">
{visibleCountries.map(
({ countryCode, countryName, count }, index) => (
<label key={countryCode}>
<input
type="checkbox"
checked={props.countries.includes(countryCode)}
onChange={(event) => {
if (
document.activeElement === event.currentTarget &&
!event.currentTarget.checked &&
(count === 0 ||
(!showAllCountries && index >= COUNTRY_PREVIEW_COUNT))
)
toggleRef.current?.focus();
props.onCountriesChange(
toggle(props.countries, countryCode),
);
}}
/>
<span>{countryName}</span>{' '}
<span className="filter-count">
{count} <span className="visually-hidden">albums</span>
</span>
</label>
),
)}
</div>
{countries.length > COUNTRY_PREVIEW_COUNT && (
<button
type="button"
className="show-filter-options"
aria-expanded={showAllCountries}
aria-controls="country-options"
onClick={() => setShowAllCountries(!showAllCountries)}
>
{showAllCountries ? 'Show fewer countries' : 'Show more countries'}
</button>
)}
</fieldset>
)}
</div>
Expand Down