Dev Helpers
← Back

Searchable list

Import and render <SearchableList /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/SearchableList.tsx

import { useState } from "react";

export default function SearchableList({
  items = ["Oak", "Pine", "Maple"],
}: { items?: string[] } = {}) {
  const [query, set] = useState("");
  const matches = items.filter((v) => v.toLowerCase().includes(query.toLowerCase()));
  return (
    <section>
      <label>
        Search <input value={query} onChange={(e) => set(e.target.value)} />
      </label>
      <p role="status">{matches.length} matches</p>
      <ul>
        {matches.map((v, i) => (
          <li key={i}>{v}</li>
        ))}
      </ul>
    </section>
  );
}