import Alpine from 'alpinejs'
import {
FlexRender,
columnResizingFeature,
columnSizingFeature,
createTable,
tableFeatures,
} from '@tanstack/alpine-table'
import { makeData } from './makeData'
import './index.css'
import type { ColumnDef } from '@tanstack/alpine-table'
import type { Person } from './makeData'
const features = tableFeatures({
columnSizingFeature,
columnResizingFeature,
})
const columns: Array<ColumnDef<typeof features, Person>> = [
{
header: 'Name',
footer: (props) => props.column.id,
columns: [
{
accessorKey: 'firstName',
cell: (info) => info.getValue(),
footer: (props) => props.column.id,
},
{
accessorFn: (row) => row.lastName,
id: 'lastName',
cell: (info) => info.getValue(),
header: () => '<span>Last Name</span>',
footer: (props) => props.column.id,
},
],
},
{
header: 'Info',
footer: (props) => props.column.id,
columns: [
{
accessorKey: 'age',
header: () => 'Age',
footer: (props) => props.column.id,
},
{
accessorKey: 'visits',
header: () => '<span>Visits</span>',
footer: (props) => props.column.id,
},
{
accessorKey: 'status',
header: 'Status',
footer: (props) => props.column.id,
},
{
accessorKey: 'progress',
header: 'Profile Progress',
footer: (props) => props.column.id,
},
],
},
]
Alpine.data('table', () => {
const local = Alpine.reactive({ data: makeData(200) })
const table = createTable(
{
features,
columns,
get data() {
return local.data
},
defaultColumn: { minSize: 60, maxSize: 800 },
columnResizeMode: 'onChange',
debugTable: true,
debugHeaders: true,
debugColumns: true,
},
() => ({}),
)
let subscriptions: Array<{ unsubscribe: () => void }> = []
return {
table,
FlexRender,
local,
init(this: { $refs: Record<string, HTMLElement> }) {
const tableEl = this.$refs.tableEl
const stateDump = this.$refs.stateDump
const writeColumnSizeVars = () => {
for (const header of table.getFlatHeaders()) {
tableEl.style.setProperty(
`--header-${header.id}-size`,
String(header.getSize()),
)
tableEl.style.setProperty(
`--col-${header.column.id}-size`,
String(header.column.getSize()),
)
}
tableEl.style.width = `${table.getTotalSize()}px`
}
const writeResizingClass = () => {
const resizingId = table.atoms.columnResizing.get().isResizingColumn
tableEl
.querySelectorAll('.resizer.isResizing')
.forEach((el) => el.classList.remove('isResizing'))
if (resizingId) {
tableEl
.querySelector(`.resizer[data-col-id="${CSS.escape(resizingId)}"]`)
?.classList.add('isResizing')
}
}
const writeStateDump = () => {
stateDump.textContent = JSON.stringify(table.store.get(), null, 2)
}
writeColumnSizeVars()
writeStateDump()
subscriptions = [
table.atoms.columnSizing.subscribe(writeColumnSizeVars),
table.atoms.columnResizing.subscribe(writeResizingClass),
table.store.subscribe(writeStateDump),
]
},
destroy() {
subscriptions.forEach((subscription) => subscription.unsubscribe())
subscriptions = []
},
refreshData() {
local.data = makeData(200)
},
stressTest() {
local.data = makeData(5_000)
},
}
})
window.Alpine = Alpine
Alpine.start()