@@ -10,6 +10,7 @@ import type {
1010 TableFeatures ,
1111 TableOptions ,
1212 TableState ,
13+ Updater ,
1314} from '@tanstack/table-core'
1415import 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