{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"paginated-table-with-search-filter","type":"registry:block","title":"Paginated Table With Search Filter","description":"","dependencies":[],"files":[{"path":"components/we-inc/paginated-table-with-search-filter.tsx","type":"registry:component","content":"import { useMemo, useState } from \"react\";\n\n// Stock list for a plant nursery: name, type, pot size, price, stock.\nconst plants = [\n  [\"Japanese maple 'Bloodgood'\", \"Tree\", \"10 L\", 89, 6], [\"Lavender 'Hidcote'\", \"Shrub\", \"2 L\", 9.5, 48], [\"Hellebore 'Ivory Prince'\", \"Perennial\", \"3 L\", 14, 22],\n  [\"Silver birch\", \"Tree\", \"15 L\", 65, 9], [\"Hydrangea 'Annabelle'\", \"Shrub\", \"5 L\", 24, 15], [\"Foxglove 'Pam's Choice'\", \"Perennial\", \"2 L\", 8, 0],\n  [\"Box ball 30 cm\", \"Topiary\", \"7 L\", 39, 12], [\"Rosemary 'Miss Jessopp'\", \"Herb\", \"1 L\", 6.5, 60], [\"Crab apple 'Red Sentinel'\", \"Tree\", \"12 L\", 75, 4],\n  [\"Salvia 'Caradonna'\", \"Perennial\", \"2 L\", 9, 31], [\"Pittosporum 'Tom Thumb'\", \"Shrub\", \"3 L\", 18, 11], [\"Thyme 'Silver Posie'\", \"Herb\", \"9 cm\", 4, 75],\n  [\"Bay tree standard\", \"Topiary\", \"20 L\", 120, 3], [\"Geranium 'Rozanne'\", \"Perennial\", \"2 L\", 10, 40], [\"Olive tree\", \"Tree\", \"25 L\", 149, 2],\n  [\"Mint 'Moroccan'\", \"Herb\", \"1 L\", 5, 0], [\"Viburnum tinus\", \"Shrub\", \"5 L\", 22, 19], [\"Agapanthus 'Navy Blue'\", \"Perennial\", \"3 L\", 16, 27],\n] as const;\nconst PER_PAGE = 5;\nconst gbp = new Intl.NumberFormat(\"en-GB\", { style: \"currency\", currency: \"GBP\" });\n\nexport default function PaginatedTableWithSearchFilter() {\n  const [query, setQuery] = useState(\"\");\n  const [page, setPage] = useState(1);\n  const found = useMemo(() => {\n    const q = query.trim().toLowerCase();\n    return q ? plants.filter((p) => `${p[0]} ${p[1]}`.toLowerCase().includes(q)) : plants;\n  }, [query]);\n  const pages = Math.max(1, Math.ceil(found.length / PER_PAGE));\n  const current = Math.min(page, pages);\n  const shown = found.slice((current - 1) * PER_PAGE, current * PER_PAGE);\n  const first = found.length ? (current - 1) * PER_PAGE + 1 : 0;\n\n  return (\n    <section className=\"bg-sky-50 px-5 py-14 sm:py-20\">\n      <div className=\"mx-auto max-w-4xl rounded-2xl border border-sky-100 bg-white p-5 shadow-sm sm:p-6\">\n        <div className=\"flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between\">\n          <h2 className=\"text-lg font-semibold text-slate-900\">Greenhythe Nursery stock</h2>\n          <div className=\"relative sm:w-72\">\n            <label htmlFor=\"tpg-q\" className=\"sr-only\">Search plants</label>\n            <svg viewBox=\"0 0 20 20\" className=\"pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-500\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" aria-hidden=\"true\"><circle cx=\"9\" cy=\"9\" r=\"6\" /><path d=\"m14 14 4 4\" strokeLinecap=\"round\" /></svg>\n            <input\n              id=\"tpg-q\"\n              type=\"search\"\n              value={query}\n              onChange={(e) => { setQuery(e.target.value); setPage(1); }}\n              placeholder=\"Search by name or type\"\n              className=\"h-10 w-full rounded-lg border border-slate-300 pl-9 pr-3 text-sm text-slate-900 placeholder:text-slate-500 focus:border-sky-600 focus:outline-none focus:ring-2 focus:ring-sky-600/30\"\n            />\n          </div>\n        </div>\n        <div className=\"mt-5 overflow-x-auto\">\n          <table className=\"w-full min-w-[560px] border-collapse text-left text-sm\">\n            <caption className=\"sr-only\">Plants in stock, page {current} of {pages}</caption>\n            <thead>\n              <tr className=\"border-b border-slate-200 text-slate-600\">\n                <th scope=\"col\" className=\"py-2.5 pr-3 font-medium\">Plant</th>\n                <th scope=\"col\" className=\"px-3 py-2.5 font-medium\">Type</th>\n                <th scope=\"col\" className=\"px-3 py-2.5 font-medium\">Pot</th>\n                <th scope=\"col\" className=\"px-3 py-2.5 text-right font-medium\">Price</th>\n                <th scope=\"col\" className=\"py-2.5 pl-3 text-right font-medium\">In stock</th>\n              </tr>\n            </thead>\n            <tbody className=\"divide-y divide-slate-100\">\n              {shown.map(([name, type, pot, price, stock]) => (\n                <tr key={name}>\n                  <th scope=\"row\" className=\"py-3 pr-3 font-medium text-slate-900\">{name}</th>\n                  <td className=\"px-3 py-3 text-slate-600\">{type}</td>\n                  <td className=\"px-3 py-3 text-slate-600\">{pot}</td>\n                  <td className=\"px-3 py-3 text-right tabular-nums text-slate-900\">{gbp.format(price)}</td>\n                  <td className={`py-3 pl-3 text-right tabular-nums ${stock === 0 ? \"font-medium text-rose-700\" : \"text-slate-700\"}`}>{stock === 0 ? \"Sold out\" : stock}</td>\n                </tr>\n              ))}\n              {shown.length === 0 && <tr><td colSpan={5} className=\"py-10 text-center text-slate-600\">No plants match \"{query}\".</td></tr>}\n            </tbody>\n          </table>\n        </div>\n        <nav aria-label=\"Table pages\" className=\"mt-5 flex flex-wrap items-center justify-between gap-3 border-t border-slate-100 pt-4\">\n          <p className=\"text-sm text-slate-600\" aria-live=\"polite\">{first} to {Math.min(current * PER_PAGE, found.length)} of {found.length} plants</p>\n          <div className=\"flex items-center gap-1\">\n            <button type=\"button\" onClick={() => setPage(current - 1)} disabled={current === 1} className=\"h-10 rounded-lg px-3 text-sm font-medium text-slate-700 hover:bg-slate-100 disabled:opacity-40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-600\">Previous</button>\n            {Array.from({ length: pages }, (_, i) => i + 1).map((n) => (\n              <button key={n} type=\"button\" onClick={() => setPage(n)} aria-current={n === current ? \"page\" : undefined} aria-label={`Page ${n}`} className={`h-10 w-10 rounded-lg text-sm font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-600 ${n === current ? \"bg-sky-700 text-white\" : \"text-slate-700 hover:bg-slate-100\"}`}>{n}</button>\n            ))}\n            <button type=\"button\" onClick={() => setPage(current + 1)} disabled={current === pages} className=\"h-10 rounded-lg px-3 text-sm font-medium text-slate-700 hover:bg-slate-100 disabled:opacity-40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-600\">Next</button>\n          </div>\n        </nav>\n      </div>\n    </section>\n  );\n}\n"}]}
