{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sortable-react-table-with-aria-sort","type":"registry:block","title":"Sortable React Table With aria-sort","description":"Free sortable React table in Tailwind with no library: click headers to sort, aria-sort on the active column, a caption, row headers and a plain HTML version.","dependencies":[],"files":[{"path":"components/we-inc/sortable-react-table-with-aria-sort.tsx","type":"registry:component","content":"import { useMemo, useState } from \"react\";\n\ntype Order = { id: string; customer: string; city: string; date: string; total: number; status: \"Paid\" | \"Pending\" | \"Refunded\" };\nconst orders: Order[] = [\n  { id: \"#4821\", customer: \"Hana Osei\", city: \"Leeds\", date: \"2026-10-24\", total: 184.5, status: \"Paid\" },\n  { id: \"#4820\", customer: \"Mateo Ruiz\", city: \"Bristol\", date: \"2026-10-23\", total: 62, status: \"Pending\" },\n  { id: \"#4819\", customer: \"Clara Vos\", city: \"York\", date: \"2026-10-23\", total: 311.2, status: \"Paid\" },\n  { id: \"#4818\", customer: \"Idris Bello\", city: \"Cardiff\", date: \"2026-10-21\", total: 47.9, status: \"Refunded\" },\n  { id: \"#4817\", customer: \"Nell Park\", city: \"Glasgow\", date: \"2026-10-20\", total: 129, status: \"Paid\" },\n  { id: \"#4816\", customer: \"Arjun Mehta\", city: \"Brighton\", date: \"2026-10-18\", total: 238.75, status: \"Pending\" },\n];\nconst columns: { key: keyof Order; label: string; numeric?: boolean }[] = [\n  { key: \"id\", label: \"Order\" },\n  { key: \"customer\", label: \"Customer\" },\n  { key: \"city\", label: \"City\" },\n  { key: \"date\", label: \"Date\" },\n  { key: \"total\", label: \"Total\", numeric: true },\n  { key: \"status\", label: \"Status\" },\n];\nconst badge = { Paid: \"bg-emerald-50 text-emerald-800 ring-emerald-600/20\", Pending: \"bg-amber-50 text-amber-800 ring-amber-600/20\", Refunded: \"bg-slate-100 text-slate-700 ring-slate-500/20\" };\nconst money = new Intl.NumberFormat(\"en-GB\", { style: \"currency\", currency: \"GBP\" });\nconst day = new Intl.DateTimeFormat(\"en-GB\", { day: \"numeric\", month: \"short\" });\n\nexport default function SortableReactTableWithAriaSort() {\n  const [sort, setSort] = useState<{ key: keyof Order; dir: \"ascending\" | \"descending\" }>({ key: \"date\", dir: \"descending\" });\n  const rows = useMemo(() => {\n    const s = [...orders].sort((a, b) => (a[sort.key] < b[sort.key] ? -1 : a[sort.key] > b[sort.key] ? 1 : 0));\n    return sort.dir === \"ascending\" ? s : s.reverse();\n  }, [sort]);\n  const by = (key: keyof Order) =>\n    setSort((s) => ({ key, dir: s.key === key && s.dir === \"ascending\" ? \"descending\" : \"ascending\" }));\n\n  return (\n    <section className=\"bg-slate-50 px-5 py-14 sm:py-20\">\n      <div className=\"mx-auto max-w-4xl\">\n        <div className=\"overflow-x-auto rounded-2xl border border-slate-200 bg-white shadow-sm\">\n          <table className=\"w-full min-w-[640px] border-collapse text-left text-sm\">\n            <caption className=\"px-5 pb-3 pt-5 text-left\">\n              <span className=\"block text-lg font-semibold text-slate-900\">Recent orders</span>\n              <span className=\"block text-sm font-normal text-slate-600\">Wren &amp; Thistle Candles. Click a column heading to sort.</span>\n            </caption>\n            <thead>\n              <tr className=\"border-y border-slate-200 bg-slate-50\">\n                {columns.map((c) => {\n                  const active = sort.key === c.key;\n                  return (\n                    <th key={c.key} scope=\"col\" aria-sort={active ? sort.dir : undefined} className={`px-2 py-1 font-medium text-slate-600 ${c.numeric ? \"text-right\" : \"\"}`}>\n                      <button\n                        type=\"button\"\n                        onClick={() => by(c.key)}\n                        className={`inline-flex h-10 items-center gap-1.5 rounded-md px-3 hover:bg-slate-200/60 hover:text-slate-900 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600 ${active ? \"text-slate-900\" : \"\"} ${c.numeric ? \"flex-row-reverse\" : \"\"}`}\n                      >\n                        {c.label}\n                        <svg viewBox=\"0 0 12 12\" className={`h-3 w-3 ${active ? \"text-indigo-600\" : \"text-slate-400\"}`} aria-hidden=\"true\">\n                          <path d=\"M6 1.5 9 5H3z\" className={active && sort.dir === \"descending\" ? \"opacity-25\" : \"\"} fill=\"currentColor\" />\n                          <path d=\"M6 10.5 3 7h6z\" className={active && sort.dir === \"ascending\" ? \"opacity-25\" : \"\"} fill=\"currentColor\" />\n                        </svg>\n                      </button>\n                    </th>\n                  );\n                })}\n              </tr>\n            </thead>\n            <tbody className=\"divide-y divide-slate-100\">\n              {rows.map((o) => (\n                <tr key={o.id} className=\"hover:bg-slate-50\">\n                  <th scope=\"row\" className=\"px-5 py-3.5 font-medium text-slate-900\">{o.id}</th>\n                  <td className=\"px-5 py-3.5 text-slate-700\">{o.customer}</td>\n                  <td className=\"px-5 py-3.5 text-slate-600\">{o.city}</td>\n                  <td className=\"px-5 py-3.5 text-slate-600\"><time dateTime={o.date}>{day.format(new Date(o.date))}</time></td>\n                  <td className=\"px-5 py-3.5 text-right font-medium text-slate-900 tabular-nums\">{money.format(o.total)}</td>\n                  <td className=\"px-5 py-3.5\"><span className={`rounded-full px-2.5 py-1 text-xs font-medium ring-1 ring-inset ${badge[o.status]}`}>{o.status}</span></td>\n                </tr>\n              ))}\n            </tbody>\n          </table>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
