@trail-ui/react
Version:
111 lines (108 loc) • 3.67 kB
JavaScript
import {
_Checkbox
} from "./chunk-26XCDXWQ.mjs";
// src/table/table.tsx
import {
useReactTable,
getCoreRowModel,
getPaginationRowModel,
getFilteredRowModel,
getExpandedRowModel,
flexRender
} from "@tanstack/react-table";
import { useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
function Table(props) {
let { columns, data } = props;
let [expanded, setExpanded] = useState({});
let table = useReactTable({
data,
columns,
state: {
expanded
},
onExpandedChange: setExpanded,
getSubRows: (row) => row.subIssues,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getExpandedRowModel: getExpandedRowModel(),
debugTable: true
});
return /* @__PURE__ */ jsx("div", { className: "rounded", children: /* @__PURE__ */ jsxs("table", { children: [
/* @__PURE__ */ jsx("thead", { children: table.getHeaderGroups().map((headerGroup) => /* @__PURE__ */ jsx("tr", { className: "bg-neutral-200 text-neutral-100", children: headerGroup.headers.map((header) => {
return /* @__PURE__ */ jsx(
"th",
{
colSpan: header.colSpan,
className: "border-r p-2 text-lg font-semibold text-neutral-950",
children: header.isPlaceholder ? null : /* @__PURE__ */ jsx("div", { className: "flex", children: flexRender(header.column.columnDef.header, header.getContext()) })
},
header.id
);
}) }, headerGroup.id)) }),
/* @__PURE__ */ jsx("tbody", { children: table.getRowModel().rows.map((row) => {
return /* @__PURE__ */ jsx("tr", { className: "border", children: row.getVisibleCells().map((cell) => {
return /* @__PURE__ */ jsx("td", { className: "border-r p-2", children: flexRender(cell.column.columnDef.cell, cell.getContext()) }, cell.id);
}) }, row.id);
}) })
] }) });
}
function Filter({ column, table }) {
var _a, _b, _c;
const firstValue = (_a = table.getPreFilteredRowModel().flatRows[0]) == null ? void 0 : _a.getValue(column.id);
const columnFilterValue = column.getFilterValue();
return typeof firstValue === "number" ? /* @__PURE__ */ jsxs("div", { className: "flex space-x-2", children: [
/* @__PURE__ */ jsx(
"input",
{
type: "number",
value: (_b = columnFilterValue == null ? void 0 : columnFilterValue[0]) != null ? _b : "",
onChange: (e) => column.setFilterValue((old) => [e.target.value, old == null ? void 0 : old[1]]),
placeholder: `Min`,
className: "w-24 rounded border shadow"
}
),
/* @__PURE__ */ jsx(
"input",
{
type: "number",
value: (_c = columnFilterValue == null ? void 0 : columnFilterValue[1]) != null ? _c : "",
onChange: (e) => column.setFilterValue((old) => [old == null ? void 0 : old[0], e.target.value]),
placeholder: `Max`,
className: "w-24 rounded border shadow"
}
)
] }) : /* @__PURE__ */ jsx(
"input",
{
type: "text",
value: columnFilterValue != null ? columnFilterValue : "",
onChange: (e) => column.setFilterValue(e.target.value),
placeholder: `Search...`,
className: "w-36 rounded border shadow"
}
);
}
var IndeterminateCheckbox = ({
isIndeterminate,
isSelected,
className = "",
...rest
}) => {
return /* @__PURE__ */ jsx(
_Checkbox,
{
isIndeterminate,
isSelected,
className: className + " cursor-pointer",
...rest
}
);
};
IndeterminateCheckbox.displayName = "IndeterminateCheckbox";
export {
Table,
Filter,
IndeterminateCheckbox
};