Skip to content

Commit 9a6f472

Browse files
committed
fix lit adapter and lit examples
1 parent 2aa7380 commit 9a6f472

File tree

6 files changed

+51
-46
lines changed

6 files changed

+51
-46
lines changed

examples/lit/column-sizing/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const data: Array<Person> = makeData(1000)
5959
@customElement('lit-table-example')
6060
class LitTableExample extends LitElement {
6161
@state()
62-
private tableController = new TableController<any, Person>(this)
62+
private tableController = new TableController<typeof _features, Person>(this)
6363

6464
protected render() {
6565
const table = this.tableController.table({
@@ -118,7 +118,7 @@ class LitTableExample extends LitElement {
118118
(row) => html`
119119
<tr>
120120
${row
121-
.getVisibleCells()
121+
.getAllCells()
122122
.map(
123123
(cell) => html`
124124
<td>

examples/lit/filters/src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const data = makeData(50_000)
9494
@customElement('column-filter')
9595
class ColumnFilter extends LitElement {
9696
@property()
97-
private column!: Column<any, {}>
97+
private column!: Column<typeof _features, Person>
9898

9999
private onChange(evt: InputEvent) {
100100
this.column.setFilterValue((evt.target as HTMLInputElement).value)
@@ -153,12 +153,12 @@ class ColumnFilter extends LitElement {
153153

154154
@customElement('lit-table-example')
155155
class LitTableExample extends LitElement {
156-
private tableController = new TableController<any, any>(this)
156+
private tableController = new TableController<typeof _features, Person>(this)
157157

158158
@state()
159159
private _columnFilters: ColumnFiltersState = []
160160

161-
protected render(): unknown {
161+
protected render() {
162162
const table = this.tableController.table({
163163
_features,
164164
_rowModels: {
@@ -242,7 +242,7 @@ class LitTableExample extends LitElement {
242242
(row) => html`
243243
<tr>
244244
${row
245-
.getVisibleCells()
245+
.getAllCells()
246246
.map(
247247
(cell) => html`
248248
<td>

examples/lit/row-selection/src/main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
tableFeatures,
1414
} from '@tanstack/lit-table'
1515
import { makeData } from './makeData'
16-
import type { ColumnDef } from '@tanstack/lit-table'
16+
import type { ColumnDef, RowSelectionState } from '@tanstack/lit-table'
1717
import type { Person } from './makeData'
1818

1919
const _features = tableFeatures({
@@ -81,12 +81,12 @@ const data = makeData(50_000)
8181

8282
@customElement('lit-table-example')
8383
class LitTableExample extends LitElement {
84-
private tableController = new TableController<any, Person>(this)
84+
private tableController = new TableController<typeof _features, Person>(this)
8585

8686
@state()
87-
private _rowSelection: Record<string, boolean> = {}
87+
private _rowSelection: RowSelectionState = {}
8888

89-
protected render(): unknown {
89+
protected render() {
9090
const table = this.tableController.table({
9191
_features,
9292
_rowModels: {
@@ -145,7 +145,7 @@ class LitTableExample extends LitElement {
145145
(row) => html`
146146
<tr>
147147
${row
148-
.getVisibleCells()
148+
.getAllCells()
149149
.map(
150150
(cell) => html`
151151
<td>

examples/lit/sorting/src/main.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@ import {
66
TableController,
77
createSortedRowModel,
88
flexRender,
9+
isFunction,
910
rowSortingFeature,
1011
sortFns,
1112
tableFeatures,
1213
} from '@tanstack/lit-table'
1314
import { Person, makeData } from './makeData'
1415
import type { ColumnDef, SortFn, SortingState } from '@tanstack/lit-table'
1516

16-
const sortStatusFn: SortFn<any, Person> = (rowA, rowB, _columnId) => {
17+
const _features = tableFeatures({
18+
rowSortingFeature,
19+
})
20+
21+
const sortStatusFn: SortFn<any, any> = (rowA, rowB, _columnId) => {
1722
const statusA = rowA.original.status
1823
const statusB = rowB.original.status
1924
const statusOrder = ['single', 'complicated', 'relationship']
2025
return statusOrder.indexOf(statusA) - statusOrder.indexOf(statusB)
2126
}
2227

23-
const _features = tableFeatures({
24-
rowSortingFeature,
25-
})
26-
2728
const columns: Array<ColumnDef<typeof _features, Person>> = [
2829
{
2930
accessorKey: 'firstName',
@@ -77,7 +78,7 @@ class LitTableExample extends LitElement {
7778
@state()
7879
private _sorting: SortingState = []
7980

80-
private tableController = new TableController<any, Person>(this)
81+
private tableController = new TableController<typeof _features, Person>(this)
8182

8283
protected render() {
8384
const table = this.tableController.table({
@@ -91,7 +92,7 @@ class LitTableExample extends LitElement {
9192
sorting: this._sorting,
9293
},
9394
onSortingChange: (updaterOrValue) => {
94-
if (typeof updaterOrValue === 'function') {
95+
if (isFunction(updaterOrValue)) {
9596
this._sorting = updaterOrValue(this._sorting)
9697
} else {
9798
this._sorting = updaterOrValue
@@ -148,7 +149,7 @@ class LitTableExample extends LitElement {
148149
(row) => html`
149150
<tr>
150151
${row
151-
.getVisibleCells()
152+
.getAllCells()
152153
.map(
153154
(cell) => html`
154155
<td>

examples/lit/virtualized-rows/src/main.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
columnSizingFeature,
77
createSortedRowModel,
88
flexRender,
9+
rowSortingFeature,
910
sortFns,
1011
tableFeatures,
1112
} from '@tanstack/lit-table'
@@ -17,6 +18,7 @@ import type { ColumnDef } from '@tanstack/lit-table'
1718

1819
const _features = tableFeatures({
1920
columnSizingFeature,
21+
rowSortingFeature,
2022
})
2123

2224
const columns: Array<ColumnDef<typeof _features, Person>> = [
@@ -65,7 +67,7 @@ const data = makeData(50_000)
6567

6668
@customElement('lit-table-example')
6769
class LitTableExample extends LitElement {
68-
private tableController = new TableController<any, Person>(this)
70+
private tableController = new TableController<typeof _features, Person>(this)
6971

7072
private tableContainerRef: Ref = createRef()
7173

@@ -81,7 +83,7 @@ class LitTableExample extends LitElement {
8183
super.connectedCallback()
8284
}
8385

84-
protected render(): unknown {
86+
protected render() {
8587
const table = this.tableController.table({
8688
_features,
8789
_rowModels: {
@@ -173,7 +175,7 @@ class LitTableExample extends LitElement {
173175
)}
174176
>
175177
${repeat(
176-
row.getVisibleCells(),
178+
row.getAllCells(),
177179
(cell) => cell.id,
178180
(cell) => html`
179181
<td

packages/lit-table/src/TableController.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
TableFeatures,
1111
TableOptions,
1212
TableState,
13+
Updater,
1314
} from '@tanstack/table-core'
1415
import type { ReactiveController, ReactiveControllerHost } from 'lit'
1516

@@ -20,42 +21,43 @@ export class TableController<
2021
{
2122
host: ReactiveControllerHost
2223

23-
private tableInstance: Table<TFeatures, TData> | null = null
24-
25-
private state: TableState<TFeatures> = {} as TableState<TFeatures>
24+
private _features: TableFeatures | null = null
25+
private _state: TableState<TFeatures> | null = null
26+
private _table: Table<TFeatures, TData> | null = null
2627

2728
constructor(host: ReactiveControllerHost) {
2829
;(this.host = host).addController(this)
2930
}
3031

31-
public table(tableOptions: TableOptions<TFeatures, TData>) {
32-
if (!this.tableInstance) {
33-
const _features = { ...coreFeatures, ...tableOptions._features }
34-
35-
this.state = {
36-
...getInitialTableState(_features, tableOptions.initialState),
37-
...tableOptions.state,
38-
}
32+
public table(
33+
tableOptions: TableOptions<TFeatures, TData>,
34+
): Table<TFeatures, TData> {
35+
if (!this._table) {
36+
this._features = { ...coreFeatures, ...tableOptions._features }
37+
this._state = getInitialTableState(
38+
this._features,
39+
tableOptions.initialState,
40+
)
3941

40-
const statefulOptions: TableOptions<TFeatures, TData> = {
42+
const initialOptions: TableOptions<TFeatures, TData> = {
4143
...tableOptions,
42-
state: { ...this.state, ...tableOptions.state },
43-
onStateChange: (updater) => {
44-
this.state = isFunction(updater) ? updater(this.state) : updater
45-
this.host.requestUpdate()
46-
tableOptions.onStateChange?.(updater)
47-
},
44+
_features: this._features,
4845
}
4946

50-
this.tableInstance = constructTable(statefulOptions)
47+
this._table = constructTable(initialOptions)
5148
}
5249

53-
// this.tableInstance.setOptions((prev) => ({
54-
// ...prev,
55-
// state: { ...this.state, ...tableOptions.state },
56-
// }))
50+
this._table.setOptions((prev) => ({
51+
...prev,
52+
state: { ...this._state, ...tableOptions.state },
53+
onStateChange: (updater: Updater<TableState<TFeatures>>) => {
54+
this._state = isFunction(updater) ? updater(this._state!) : updater
55+
this.host.requestUpdate()
56+
tableOptions.onStateChange?.(updater)
57+
},
58+
}))
5759

58-
return this.tableInstance
60+
return this._table
5961
}
6062

6163
hostDisconnected() {}

0 commit comments

Comments
 (0)