Skip to content

Commit 03af078

Browse files
committed
start cleanup of svelte and lit examples
1 parent 22b932f commit 03af078

File tree

23 files changed

+212
-220
lines changed

23 files changed

+212
-220
lines changed

examples/angular/filters/src/app/app.component.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
signal,
66
} from '@angular/core'
77
import {
8-
type ColumnDef,
9-
type ColumnFiltersState,
108
FlexRenderDirective,
119
createCoreRowModel,
1210
createFacetedMinMaxValues,
@@ -16,11 +14,18 @@ import {
1614
createPaginatedRowModel,
1715
createSortedRowModel,
1816
injectTable,
17+
isFunction,
1918
} from '@tanstack/angular-table'
2019
import { FormsModule } from '@angular/forms'
2120
import { NgClass } from '@angular/common'
2221
import { FilterComponent } from './table-filter.component'
23-
import { type Person, makeData } from './makeData'
22+
import { makeData } from './makeData'
23+
import type {
24+
ColumnDef,
25+
ColumnFiltersState,
26+
Updater,
27+
} from '@tanstack/angular-table'
28+
import type { Person } from './makeData'
2429

2530
@Component({
2631
selector: 'app-root',
@@ -80,8 +85,8 @@ export class AppComponent {
8085
state: {
8186
columnFilters: this.columnFilters(),
8287
},
83-
onColumnFiltersChange: (updater) => {
84-
updater instanceof Function
88+
onColumnFiltersChange: (updater: Updater<ColumnFiltersState>) => {
89+
isFunction(updater)
8590
? this.columnFilters.update(updater)
8691
: this.columnFilters.set(updater)
8792
},

examples/lit/basic/src/main.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import { customElement } from 'lit/decorators.js'
22
import { LitElement, html } from 'lit'
33
import { repeat } from 'lit/directives/repeat.js'
44
import {
5+
ColumnDef,
6+
RowSorting,
57
TableController,
6-
createColumnHelper,
7-
createCoreRowModel,
88
flexRender,
9+
tableFeatures,
910
} from '@tanstack/lit-table'
1011
import install from '@twind/with-web-components'
1112
import config from '../twind.config'
1213

1314
const withTwind = install(config)
1415

16+
// 1. Define what the shape of your data will be for each row
1517
type Person = {
1618
firstName: string
1719
lastName: string
@@ -21,36 +23,44 @@ type Person = {
2123
progress: number
2224
}
2325

24-
const columnHelper = createColumnHelper<any, Person>()
26+
// 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
27+
const _features = tableFeatures({
28+
RowSorting: RowSorting,
29+
})
2530

26-
const columns = [
27-
columnHelper.accessor('firstName', {
31+
// const columnHelper = createColumnHelper<typeof _features, Person>()
32+
33+
// 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.
34+
const columns: Array<ColumnDef<typeof _features, Person>> = [
35+
{
36+
accessorKey: 'firstName', // accessorKey method (most common for simple use-cases)
37+
header: 'First Name',
2838
cell: (info) => info.getValue(),
29-
footer: (info) => info.column.id,
30-
}),
31-
columnHelper.accessor((row) => row.lastName, {
39+
},
40+
{
41+
accessorFn: (row) => row.lastName, // accessorFn used (alternative) along with a custom id
3242
id: 'lastName',
33-
cell: (info) => html`<i>${info.getValue()}</i>`,
3443
header: () => html`<span>Last Name</span>`,
35-
footer: (info) => info.column.id,
36-
}),
37-
columnHelper.accessor('age', {
44+
cell: (info) => html`<i>${info.getValue<string>()}</i>`,
45+
},
46+
{
47+
accessorFn: (row) => Number(row.age), // accessorFn used to transform the data
48+
id: 'age',
3849
header: () => 'Age',
3950
cell: (info) => info.renderValue(),
40-
footer: (info) => info.column.id,
41-
}),
42-
columnHelper.accessor('visits', {
51+
},
52+
{
53+
accessorKey: 'visits',
4354
header: () => html`<span>Visits</span>`,
44-
footer: (info) => info.column.id,
45-
}),
46-
columnHelper.accessor('status', {
55+
},
56+
{
57+
accessorKey: 'status',
4758
header: 'Status',
48-
footer: (info) => info.column.id,
49-
}),
50-
columnHelper.accessor('progress', {
59+
},
60+
{
61+
accessorKey: 'progress',
5162
header: 'Profile Progress',
52-
footer: (info) => info.column.id,
53-
}),
63+
},
5464
]
5565

5666
const data: Array<Person> = [
@@ -91,15 +101,20 @@ const data: Array<Person> = [
91101
@customElement('lit-table-example')
92102
@withTwind
93103
class LitTableExample extends LitElement {
94-
private tableController = new TableController<any, Person>(this)
104+
private tableController = new TableController<typeof _features, Person>(this)
95105

96106
protected render(): unknown {
97107
const table = this.tableController.table({
108+
_features, // new required option in V9. Tell the table which features you are importing and using (better tree-shaking)
109+
_rowModels: {}, // `Core` row model is now included by default, but you can still override it here
98110
columns,
99111
data,
100-
getCoreRowModel: createCoreRowModel(),
112+
// add additional table options here
113+
debugTable: true,
101114
})
102115

116+
console.log(table)
117+
103118
return html`
104119
<table>
105120
<thead>
@@ -129,7 +144,7 @@ class LitTableExample extends LitElement {
129144
(row) => html`
130145
<tr>
131146
${repeat(
132-
row.getVisibleCells(),
147+
row.getAllCells(),
133148
(cell) => cell.id,
134149
(cell) =>
135150
html` <td>

examples/react/basic-table-helper/src/main.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ const defaultData: Array<Person> = [
5555
// 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
5656
const tableHelper = createTableHelper({
5757
_features: {},
58-
_rowModels: {}, // `Core` row model is now included by default, but you can still override it here
58+
_rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here
59+
_processingFns: {}, // client-side processing functions used by the row models (sorting, filtering, etc.). Not needed in this basic example
5960
TData: {} as Person,
6061
})
6162

examples/svelte/column-groups/src/App.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
FlexRender,
55
createTable,
66
tableFeatures,
7-
tableOptions,
87
} from '@tanstack/svelte-table'
98
import './index.css'
109
@@ -98,14 +97,12 @@
9897
},
9998
]
10099
101-
const options = tableOptions({
100+
const table = createTable({
102101
_features,
103102
_rowModels: {},
104103
data: defaultData,
105104
columns: defaultColumns,
106105
})
107-
108-
const table = createTable(options)
109106
</script>
110107

111108
<div class="p-2">

examples/svelte/column-ordering/src/App.svelte

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
ColumnDef,
55
ColumnOrderState,
66
ColumnVisibilityState,
7-
TableOptions,
87
} from '@tanstack/svelte-table'
98
import {
109
ColumnOrdering,
1110
ColumnVisibility,
1211
FlexRender,
1312
createTable,
13+
createTableState,
1414
tableFeatures,
1515
} from '@tanstack/svelte-table'
1616
import './index.css'
1717
import { makeData, type Person } from './makeData'
18-
import { createTableState } from './state.svelte'
1918
2019
const _features = tableFeatures({
2120
ColumnOrdering,
@@ -80,7 +79,13 @@
8079
const [columnVisibility, setColumnVisibility] =
8180
createTableState<ColumnVisibilityState>({})
8281
83-
const options: TableOptions<typeof _features, Person> = {
82+
const randomizeColumns = () => {
83+
table.setColumnOrder((_updater) =>
84+
faker.helpers.shuffle(table.getAllLeafColumns().map((d) => d.id)),
85+
)
86+
}
87+
88+
const table = createTable({
8489
_features,
8590
_rowModels: {},
8691
get data() {
@@ -98,15 +103,7 @@
98103
onColumnOrderChange: setColumnOrder,
99104
onColumnVisibilityChange: setColumnVisibility,
100105
debugTable: true,
101-
}
102-
103-
const randomizeColumns = () => {
104-
table.setColumnOrder((_updater) =>
105-
faker.helpers.shuffle(table.getAllLeafColumns().map((d) => d.id)),
106-
)
107-
}
108-
109-
const table = createTable(options)
106+
})
110107
</script>
111108

112109
<div class="p-2">

examples/svelte/column-pinning/src/App.svelte

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@
66
ColumnPinningState,
77
ColumnVisibilityState,
88
Header,
9-
TableOptions,
109
} from '@tanstack/svelte-table'
1110
import {
1211
ColumnPinning,
1312
ColumnVisibility,
1413
FlexRender,
14+
ColumnOrdering,
1515
RowSorting,
1616
createSortedRowModel,
1717
createTable,
18+
createTableState,
1819
tableFeatures,
1920
} from '@tanstack/svelte-table'
2021
import './index.css'
2122
import { makeData, type Person } from './makeData'
22-
import { createTableState } from './state.svelte'
2323
2424
const _features = tableFeatures({
25-
RowSorting,
26-
ColumnVisibility,
25+
ColumnOrdering,
2726
ColumnPinning,
27+
ColumnVisibility,
28+
RowSorting,
2829
})
2930
3031
const columns: ColumnDef<typeof _features, Person>[] = [
@@ -88,7 +89,13 @@
8889
const [columnVisibility, setColumnVisibility] =
8990
createTableState<ColumnVisibilityState>({})
9091
91-
const options: TableOptions<any, Person> = {
92+
const randomizeColumns = () => {
93+
table.setColumnOrder((_updater) =>
94+
faker.helpers.shuffle(table.getAllLeafColumns().map((d) => d.id)),
95+
)
96+
}
97+
98+
const table = createTable({
9299
_features,
93100
_rowModels: { sortedRowModel: createSortedRowModel() },
94101
get data() {
@@ -110,18 +117,10 @@
110117
onColumnPinningChange: setColumnPinning,
111118
onColumnVisibilityChange: setColumnVisibility,
112119
debugTable: true,
113-
}
114-
115-
const randomizeColumns = () => {
116-
table.setColumnOrder((_updater) =>
117-
faker.helpers.shuffle(table.getAllLeafColumns().map((d) => d.id)),
118-
)
119-
}
120-
121-
const table = createTable(options)
120+
})
122121
</script>
123122

124-
{#snippet headerCell(header: Header<any, Person, unknown>)}
123+
{#snippet headerCell(header: Header<typeof _features, Person, unknown>)}
125124
<th colSpan={header.colSpan}>
126125
<div class="whitespace-nowrap">
127126
{#if !header.isPlaceholder}

examples/svelte/column-pinning/src/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-ignore
21
import { mount } from 'svelte'
32
import App from './App.svelte'
43

examples/svelte/column-pinning/src/state.svelte.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/svelte/column-visibility/src/App.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import type {
33
ColumnDef,
44
ColumnVisibilityState,
5-
TableOptions,
65
Updater,
76
} from '@tanstack/svelte-table'
87
import {
98
ColumnVisibility,
109
FlexRender,
1110
createTable,
11+
isFunction,
1212
tableFeatures,
1313
} from '@tanstack/svelte-table'
1414
import './index.css'
@@ -106,12 +106,12 @@
106106
let columnVisibility = $state<ColumnVisibilityState>({})
107107
108108
function setColumnVisibility(updater: Updater<ColumnVisibilityState>) {
109-
if (updater instanceof Function) {
109+
if (isFunction(updater)) {
110110
columnVisibility = updater(columnVisibility)
111111
} else columnVisibility = updater
112112
}
113113
114-
const options: TableOptions<typeof _features, Person> = {
114+
const table = createTable({
115115
_features,
116116
_rowModels: {},
117117
data: defaultData,
@@ -123,9 +123,7 @@
123123
},
124124
onColumnVisibilityChange: setColumnVisibility,
125125
debugTable: true,
126-
}
127-
128-
const table = createTable(options)
126+
})
129127
</script>
130128

131129
<div class="p-2">

0 commit comments

Comments
 (0)