Skip to content
DevFlash

Junior

Filterable list with empty states

Search as you type, count the results, and say something useful when nothing matches.

FreeJunior~20 min

This challenge is under editorial review.

Why it shows up in interviews

Filtering a list is part of almost every product screen, so interviewers use it to see how you model state. The trap is storing the filtered list as a second state that can drift away from the original.

The other half of the grade is the empty states. "No matches" and "nothing to show yet" are different situations with different messages, and the result count needs a status role so a screen reader announces it.

What you will build

  1. A text input labelled "Filter".
  2. Show only items whose name contains the query, ignoring case and spaces around it.
  3. An empty query shows every item, inside a list labelled "Results".
  4. A role="status" element shows "N results", or "1 result".
  5. When nothing matches, hide the list and show No matches for "query" with the trimmed query.
  6. When there are no items at all, show "Nothing to show yet".

Out of scope: Highlighting the matched part. Debouncing the input.

How you will be graded

Passing the tests is the start. In the follow-up conversation, a good explanation covers:

  • Explains why the filtered list is not a separate state.
  • Distinguishes the two empty states and why each message exists.
  • Mentions when memoization would start to matter.

Starter code

/src/FilterableList.tsx

type Item = { id: string; name: string };
export function FilterableList({ items }: { items: Item[] }) {
// implemente aqui
return (
<ul aria-label="Results">
{items.map((item) => <li key={item.id}>{item.name}</li>)}
</ul>
);
}
See the tests that will run /src/FilterableList.test.tsx
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { FilterableList } from "./FilterableList";
const ITEMS = [
{ id: "1", name: "useState" },
{ id: "2", name: "useEffect" },
{ id: "3", name: "useMemo" },
{ id: "4", name: "Suspense" },
];
const filter = (value: string) =>
fireEvent.change(screen.getByLabelText("Filter"), { target: { value } });
const names = () => screen.queryAllByRole("listitem").map((li) => li.textContent);
describe("FilterableList", () => {
it("shows every item when the filter is empty", () => {
render(<FilterableList items={ITEMS} />);
expect(names()).toEqual(["useState", "useEffect", "useMemo", "Suspense"]);
expect(screen.getByRole("status")).toHaveTextContent("4 results");
});
it("matches ignoring case", () => {
render(<FilterableList items={ITEMS} />);
filter("USE");
expect(names()).toEqual(["useState", "useEffect", "useMemo"]);
});
it("ignores spaces around the query", () => {
render(<FilterableList items={ITEMS} />);
filter(" memo ");
expect(names()).toEqual(["useMemo"]);
});
it("uses the singular for a single result", () => {
render(<FilterableList items={ITEMS} />);
filter("suspense");
expect(screen.getByRole("status")).toHaveTextContent("1 result");
});
it("shows an empty state with the trimmed query", () => {
render(<FilterableList items={ITEMS} />);
filter(" redux ");
expect(screen.getByText('No matches for "redux"')).toBeInTheDocument();
expect(screen.queryByRole("list")).not.toBeInTheDocument();
});
it("has a different message when there are no items at all", () => {
render(<FilterableList items={[]} />);
expect(screen.getByText("Nothing to show yet")).toBeInTheDocument();
});
});

Hints

Open only if you get stuck. Each one gives away a little more.

Hint 1

Only the query needs to be state. The visible list can be computed during render.

Hint 2

There are two different empty states: no items at all, and no items that match.

References