2025-09-06 19:11:39 +03:00
|
|
|
import { useEffect, useRef } from "preact/hooks";
|
2025-09-06 19:19:52 +03:00
|
|
|
import { ColumnDefinition, Module, Tabulator as VanillaTabulator } from "tabulator-tables";
|
2025-09-06 19:11:39 +03:00
|
|
|
import "tabulator-tables/dist/css/tabulator.css";
|
|
|
|
|
import "../../../../src/stylesheets/table.css";
|
|
|
|
|
|
|
|
|
|
interface TableProps<T> {
|
|
|
|
|
className?: string;
|
|
|
|
|
columns: ColumnDefinition[];
|
|
|
|
|
data?: T[];
|
2025-09-06 19:19:52 +03:00
|
|
|
modules?: (new (table: VanillaTabulator) => Module)[];
|
2025-09-06 19:11:39 +03:00
|
|
|
}
|
|
|
|
|
|
2025-09-06 19:19:52 +03:00
|
|
|
export default function Tabulator<T>({ className, columns, data, modules }: TableProps<T>) {
|
2025-09-06 19:11:39 +03:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const tabulatorRef = useRef<VanillaTabulator>(null);
|
|
|
|
|
|
2025-09-06 19:19:52 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!modules) return;
|
|
|
|
|
for (const module of modules) {
|
|
|
|
|
VanillaTabulator.registerModule(module);
|
|
|
|
|
}
|
|
|
|
|
}, [modules]);
|
|
|
|
|
|
2025-09-06 19:11:39 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!containerRef.current) return;
|
|
|
|
|
|
|
|
|
|
const tabulator = new VanillaTabulator(containerRef.current, {
|
|
|
|
|
columns,
|
|
|
|
|
data
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tabulatorRef.current = tabulator;
|
|
|
|
|
|
|
|
|
|
return () => tabulator.destroy();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div ref={containerRef} className={className} />
|
|
|
|
|
);
|
|
|
|
|
}
|