Highlight Search Matches

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

The Library component gets a books prop, a list of titles (the preview uses "The Hobbit", "Brave New World", "The Road", "Dr. No", "Rebecca" and "Emma"). Add a text input labelled "Search". Show the titles that contain the search text, ignoring upper and lower case, as list items in their original order, and wrap each matching part of a title in a mark element, keeping the title's own capitals: searching "the" shows "The Hobbit" with "The" marked. Mark every match in a title, not just the first. The search is plain text, not a pattern: searching "." matches only titles with a full stop. With an empty search, show every title with nothing marked; when nothing matches, show "No matches".

Your code must pass

  • Shows every title, unmarked, at first
  • Marks the matching part, keeping its capitals
  • Marks every match in a title
  • Treats the search as plain text
  • Shows "No matches" when nothing matches