Skip to content

Commit 15c81bb

Browse files
feat: preact adapter (#5858)
* feat: preact adapter * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent f8fde79 commit 15c81bb

42 files changed

Lines changed: 1223 additions & 26 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/preact/basic/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

examples/preact/basic/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `create-preact`
2+
3+
<h2 align="center">
4+
<img height="256" width="256" src="./src/assets/preact.svg">
5+
</h2>
6+
7+
<h3 align="center">Get started using Preact and Vite!</h3>
8+
9+
## Getting Started
10+
11+
- `npm run dev` - Starts a dev server at http://localhost:5173/
12+
13+
- `npm run build` - Builds for production, emitting to `dist/`
14+
15+
- `npm run preview` - Starts a server at http://localhost:4173/ to test production build locally

examples/preact/basic/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<meta name="color-scheme" content="light dark" />
8+
<title>Vite + Preact</title>
9+
</head>
10+
<body>
11+
<div id="root"></div>
12+
<script type="module" src="/src/main.tsx"></script>
13+
</body>
14+
</html>

examples/preact/basic/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "tanstack-table-example-preact-basic",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"serve": "vite preview",
9+
"start": "vite",
10+
"lint": "eslint ./src"
11+
},
12+
"dependencies": {
13+
"@tanstack/preact-table": "^9.0.0-alpha.10",
14+
"preact": "^10.25.3"
15+
},
16+
"devDependencies": {
17+
"@preact/preset-vite": "^2.9.3",
18+
"typescript": "5.6.3",
19+
"vite": "^5.4.11"
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
html {
2+
font-family: sans-serif;
3+
font-size: 14px;
4+
}
5+
6+
table {
7+
border: 1px solid lightgray;
8+
}
9+
10+
tbody {
11+
border-bottom: 1px solid lightgray;
12+
}
13+
14+
th {
15+
border-bottom: 1px solid lightgray;
16+
border-right: 1px solid lightgray;
17+
padding: 2px 4px;
18+
}
19+
20+
tfoot {
21+
color: gray;
22+
}
23+
24+
tfoot th {
25+
font-weight: normal;
26+
}

examples/preact/basic/src/main.tsx

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { render } from 'preact'
2+
import { useReducer, useState } from 'preact/hooks'
3+
import { flexRender, tableFeatures, useTable } from '@tanstack/preact-table'
4+
import type { ColumnDef } from '@tanstack/preact-table'
5+
import './index.css'
6+
7+
// This example uses the classic standalone `useTable` hook to create a table without the new `createTableHelper` util.
8+
9+
// 1. Define what the shape of your data will be for each row
10+
type Person = {
11+
firstName: string
12+
lastName: string
13+
age: number
14+
visits: number
15+
status: string
16+
progress: number
17+
}
18+
19+
// 2. Create some dummy data with a stable reference (this could be an API response stored in useState or similar)
20+
const defaultData: Array<Person> = [
21+
{
22+
firstName: 'tanner',
23+
lastName: 'linsley',
24+
age: 24,
25+
visits: 100,
26+
status: 'In Relationship',
27+
progress: 50,
28+
},
29+
{
30+
firstName: 'tandy',
31+
lastName: 'miller',
32+
age: 40,
33+
visits: 40,
34+
status: 'Single',
35+
progress: 80,
36+
},
37+
{
38+
firstName: 'joe',
39+
lastName: 'dirte',
40+
age: 45,
41+
visits: 20,
42+
status: 'Complicated',
43+
progress: 10,
44+
},
45+
{
46+
firstName: 'kevin',
47+
lastName: 'vandy',
48+
age: 12,
49+
visits: 100,
50+
status: 'Single',
51+
progress: 70,
52+
},
53+
]
54+
55+
// 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features
56+
const _features = tableFeatures({}) // util method to create sharable TFeatures object/type
57+
58+
// 4. Define the columns for your table. This uses the new `ColumnDef` type to define columns. Alternatively, check out the createTableHelper/createColumnHelper util for an even more type-safe way to define columns.
59+
const columns: Array<ColumnDef<typeof _features, Person>> = [
60+
{
61+
accessorKey: 'firstName', // accessorKey method (most common for simple use-cases)
62+
header: 'First Name',
63+
cell: (info) => info.getValue(),
64+
},
65+
{
66+
accessorFn: (row) => row.lastName, // accessorFn used (alternative) along with a custom id
67+
id: 'lastName',
68+
header: () => <span>Last Name</span>,
69+
cell: (info) => <i>{info.getValue<string>()}</i>,
70+
},
71+
{
72+
accessorFn: (row) => Number(row.age), // accessorFn used to transform the data
73+
id: 'age',
74+
header: () => 'Age',
75+
cell: (info) => {
76+
return info.renderValue()
77+
},
78+
},
79+
{
80+
accessorKey: 'visits',
81+
header: () => <span>Visits</span>,
82+
},
83+
{
84+
accessorKey: 'status',
85+
header: 'Status',
86+
},
87+
{
88+
accessorKey: 'progress',
89+
header: 'Profile Progress',
90+
},
91+
]
92+
93+
function App() {
94+
// 5. Store data with a stable reference
95+
const [data, _setData] = useState(() => [...defaultData])
96+
const rerender = useReducer(() => ({}), {})[1]
97+
98+
// 6. Create the table instance with required _features, columns, and data
99+
const table = useTable({
100+
_features, // new required option in V9. Tell the table which features you are importing and using (better tree-shaking)
101+
_rowModels: {}, // `Core` row model is now included by default, but you can still override it here
102+
columns,
103+
data,
104+
// add additional table options here
105+
})
106+
107+
// 7. Render your table markup from the table instance APIs
108+
return (
109+
<div className="p-2">
110+
<table>
111+
<thead>
112+
{table.getHeaderGroups().map((headerGroup) => (
113+
<tr key={headerGroup.id}>
114+
{headerGroup.headers.map((header) => (
115+
<th key={header.id}>
116+
{header.isPlaceholder
117+
? null
118+
: flexRender(
119+
header.column.columnDef.header,
120+
header.getContext(),
121+
)}
122+
</th>
123+
))}
124+
</tr>
125+
))}
126+
</thead>
127+
<tbody>
128+
{table.getRowModel().rows.map((row) => (
129+
<tr key={row.id}>
130+
{row.getAllCells().map((cell) => (
131+
<td key={cell.id}>
132+
{flexRender(cell.column.columnDef.cell, cell.getContext())}
133+
</td>
134+
))}
135+
</tr>
136+
))}
137+
</tbody>
138+
<tfoot>
139+
{table.getFooterGroups().map((footerGroup) => (
140+
<tr key={footerGroup.id}>
141+
{footerGroup.headers.map((header) => (
142+
<th key={header.id}>
143+
{header.isPlaceholder
144+
? null
145+
: flexRender(
146+
header.column.columnDef.footer,
147+
header.getContext(),
148+
)}
149+
</th>
150+
))}
151+
</tr>
152+
))}
153+
</tfoot>
154+
</table>
155+
<div className="h-4" />
156+
<button onClick={() => rerender(0)} className="border p-2">
157+
Rerender
158+
</button>
159+
</div>
160+
)
161+
}
162+
163+
const rootElement = document.getElementById('root')
164+
if (!rootElement) throw new Error('Failed to find the root element')
165+
166+
render(<App />, rootElement)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"strictNullChecks": true,
4+
"target": "ES2020",
5+
"module": "ESNext",
6+
"moduleResolution": "bundler",
7+
"noEmit": true,
8+
"allowJs": true,
9+
"checkJs": true,
10+
11+
/* Preact Config */
12+
"jsx": "react-jsx",
13+
"jsxImportSource": "preact",
14+
"skipLibCheck": true,
15+
"paths": {
16+
"react": ["./node_modules/preact/compat/"],
17+
"react-dom": ["./node_modules/preact/compat/"]
18+
}
19+
},
20+
"include": ["node_modules/vite/client.d.ts", "**/*"]
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import preact from '@preact/preset-vite'
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
plugins: [preact()],
7+
})

examples/preact/sorting/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

0 commit comments

Comments
 (0)