Skip to content

Commit

Permalink
feat: Add TanStack Lit Table Adapter (#5538)
Browse files Browse the repository at this point in the history
* feat: initial integration for lit

* feat: sorting example working

* feat: add filters example

* chore: removed "unknown" type

* fix: use map instead of repeat for tbody

* feat: add row-sorting example

* configuration and doc templates

* add missing faker deps to examples

* update lit adapter docs

* refactor: change getTable to useLitTable

docs: fix Lit docs

* docs: add twind and some styling to basic example

* docs(lit): add column resizing example

* chore: rename useLitTable to simply "table"

* prettier

* fix state type docs

* docs config

---------

Co-authored-by: Mor Kadosh <mkadosh@paloaltonetworks.com>
Co-authored-by: Kevin Van Cott <kevinvandy656@gmail.com>
  • Loading branch information
3 people committed May 17, 2024
1 parent a6e6c3b commit bc4f3df
Show file tree
Hide file tree
Showing 61 changed files with 2,832 additions and 328 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ stats-hydration.json
stats-react.json
stats.html
.vscode/settings.json
.idea

*.log
.DS_Store
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Try other [TanStack](https://tanstack.com) libraries:
You may know **TanStack Table** by our adapter names, too!

- [Angular Table](https://tanstack.com/table/v8/docs/adapters/angular-table)
- [Lit Table](https://tanstack.com/table/v8/docs/adapters/lit-table)
- [Qwik Table](https://tanstack.com/table/v8/docs/adapters/qwik-table)
- [**React Table**](https://tanstack.com/table/v8/docs/adapters/react-table)
- [Solid Table](https://tanstack.com/table/v8/docs/adapters/solid-table)
Expand Down Expand Up @@ -117,6 +118,7 @@ Install one of the following packages based on your framework of choice:
```bash
# Npm
npm install @tanstack/angular-table
npm install @tanstack/lit-table
npm install @tanstack/qwik-table
npm install @tanstack/react-table
npm install @tanstack/solid-table
Expand Down
43 changes: 43 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
}
]
},
{
"label": "lit",
"children": [
{
"label": "Lit Table Adapter",
"to": "framework/lit/lit-table"
}
]
},
{
"label": "qwik",
"children": [
Expand Down Expand Up @@ -146,6 +155,15 @@
}
]
},
{
"label": "lit",
"children": [
{
"label": "Table State",
"to": "framework/lit/guide/table-state"
}
]
},
{
"label": "qwik",
"children": [
Expand Down Expand Up @@ -423,6 +441,31 @@
}
]
},
{
"label": "lit",
"children": [
{
"to": "framework/lit/examples/basic",
"label": "Basic"
},
{
"to": "framework/lit/examples/column-sizing",
"label": "Column Sizing"
},
{
"to": "framework/lit/examples/filters",
"label": "Filters"
},
{
"to": "framework/lit/examples/row-selection",
"label": "Row Selection"
},
{
"to": "framework/lit/examples/sorting",
"label": "Sorting"
}
]
},
{
"label": "qwik",
"children": [
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/angular/angular-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ state the "angular signals" way, providing types and the rendering implementatio

## Exports

`@tanstack/angular-table` re-exports all of `@tanstack/table-core`'s and the following:
`@tanstack/angular-table` re-exports all of `@tanstack/table-core`'s APIs and the following:

### `createAngularTable`

Expand Down
2 changes: 1 addition & 1 deletion docs/framework/angular/guide/table-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ This is why you will see the `updater instanceof Function ? this.state.update(up
All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling.

```ts
import {createAngularTable, SortingState} from '@tanstack/angular-table'
import {createAngularTable, type SortingState} from '@tanstack/angular-table'

class TableComponent {
readonly sorting = signal<SortingState>([
Expand Down
193 changes: 193 additions & 0 deletions docs/framework/lit/guide/table-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
---
title: Table State (Lit) Guide
---

## Table State (Lit) Guide

TanStack Table has a simple underlying internal state management system to store and manage the state of the table. It also lets you selectively pull out any state that you need to manage in your own state management. This guide will walk you through the different ways in which you can interact with and manage the state of the table.

### Accessing Table State

You do not need to set up anything special in order for the table state to work. If you pass nothing into either `state`, `initialState`, or any of the `on[State]Change` table options, the table will manage its own state internally. You can access any part of this internal state by using the `table.getState()` table instance API.

```ts
private tableController = new TableController<Person>(this);

render() {
const table = this.tableController.table({
columns,
data,
...
})

console.log(table.getState()) //access the entire internal state
console.log(table.getState().rowSelection) //access just the row selection state
// ...
}
```

### Custom Initial State

If all you need to do for certain states is customize their initial default values, you still do not need to manage any of the state yourself. You can simply set values in the `initialState` option of the table instance.

```ts
render() {
const table = this.tableController.table({
columns,
data,
initialState: {
columnOrder: ['age', 'firstName', 'lastName'], //customize the initial column order
columnVisibility: {
id: false //hide the id column by default
},
expanded: true, //expand all rows by default
sorting: [
{
id: 'age',
desc: true //sort by age in descending order by default
}
]
},
})

return html`...`;
}
```

> **Note**: Only specify each particular state in either `initialState` or `state`, but not both. If you pass in a particular state value to both `initialState` and `state`, the initialized state in `state` will take overwrite any corresponding value in `initialState`.
### Controlled State

If you need easy access to the table state in other areas of your application, TanStack Table makes it easy to control and manage any or all of the table state in your own state management system. You can do this by passing in your own state and state management functions to the `state` and `on[State]Change` table options.

#### Individual Controlled State

You can control just the state that you need easy access to. You do NOT have to control all of the table state if you do not need to. It is recommended to only control the state that you need on a case-by-case basis.

In order to control a particular state, you need to both pass in the corresponding `state` value and the `on[State]Change` function to the table instance.

Let's take filtering, sorting, and pagination as an example in a "manual" server-side data fetching scenario. You can store the filtering, sorting, and pagination state in your own state management, but leave out any other state like column order, column visibility, etc. if your API does not care about those values.

```jsx
import {html} from "lit";

@customElement('my-component')
class MyComponent extends LitElement {
@state()
private _sorting: SortingState = []

render() {
const table = this.tableController.table({
columns,
data,
state: {
sorting: this._sorting,
},
onSortingChange: updaterOrValue => {
if (typeof updaterOrValue === 'function') {
this._sorting = updaterOrValue(this._sorting)
} else {
this._sorting = updaterOrValue
}
},
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})

return html`...`
}
}
//...
```

#### Fully Controlled State

Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a component tree, like `columnSizingInfo` state`, might cause bad performance issues.

A couple of more tricks may be needed to make this work. If you use the `onStateChange` table option, the initial values of the `state` must be populated with all of the relevant state values for all of the features that you want to use. You can either manually type out all of the initial state values, or use the `table.setOptions` API in a special way as shown below.

```ts

private tableController = new TableController<Person>(this);

@state()
private _tableState;

render() {
const table = this.tableController.table({
columns,
data,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel()
})
const state = { ...table.initialState, ...this._tableState };
table.setOptions(prev => ({
...prev,
state,
onStateChange: updater => {
this._tableState =
updater instanceof Function ? updater(state) : updater //any state changes will be pushed up to our own state management
},
}))

return html`...`;
}
```

### On State Change Callbacks

So far, we have seen the `on[State]Change` and `onStateChange` table options work to "hoist" the table state changes into our own state management. However, there are a few things about these using these options that you should be aware of.

#### 1. **State Change Callbacks MUST have their corresponding state value in the `state` option**.

Specifying an `on[State]Change` callback tells the table instance that this will be a controlled state. If you do not specify the corresponding `state` value, that state will be "frozen" with its initial value.

```jsx
@state()
private _sorting = [];
//...
render() {
const table = this.tableController.table({
columns,
data,
state: {
sorting: this._sorting,
},
onSortingChange: updaterOrValue => {
if (typeof updaterOrValue === 'function') {
this._sorting = updaterOrValue(this._sorting)
} else {
this._sorting = updaterOrValue
}
},
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})

return html`...`;
}
```

#### 2. **Updaters can either be raw values or callback functions**.

The `on[State]Change` and `onStateChange` callbacks work exactly like the `setState` functions in React. The updater values can either be a new state value or a callback function that takes the previous state value and returns the new state value.

What implications does this have? It means that if you want to add in some extra logic in any of the `on[State]Change` callbacks, you can do so, but you need to check whether or not the new incoming updater value is a function or value.

This is why you will see the `updater instanceof Function ? updater(state.value) : updater` pattern in the examples above. This pattern checks if the updater is a function, and if it is, it calls the function with the previous state value to get the new state value.

### State Types

All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling.

```tsx
import { TableController, type SortingState } from '@tanstack/lit-table'
//...
@state()
private _sorting: SortingState = [
{
id: 'age', //you should get autocomplete for the `id` and `desc` properties
desc: true,
}
]
```
63 changes: 63 additions & 0 deletions docs/framework/lit/lit-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: Lit Table
---

The `@tanstack/lit-table` adapter is a wrapper around the core table logic. Most of it's job is related to managing state the "lit" way, providing types and the rendering implementation of cell/header/footer templates.

## Exports

`@tanstack/lit-table` re-exports all of `@tanstack/table-core`'s APIs and the following:

### `TableController`

Is a reactive controller that provides a `table` API that takes an `options` object and returns a table instance.

```ts
import { TableController } from '@tanstack/lit-table'

@customElement('my-table-element')
class MyTableElement extends LitElement {
private tableController = new TableController<Person>(this)

protected render() {
const table = this.tableController.table(options)
// ...render your table
}
}
```

### `flexRender`

A utility function for rendering cell/header/footer templates with dynamic values.

Example:

```jsx
import { flexRender } from '@tanstack/lit-table'
//...
return html`
<tbody>
${table
.getRowModel()
.rows.slice(0, 10)
.map(
row => html`
<tr>
${row
.getVisibleCells()
.map(
cell => html`
<td>
${flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</td>
`
)}
</tr>
`
)}
</tbody>
`
```
2 changes: 1 addition & 1 deletion docs/framework/qwik/guide/table-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ This is why you will see the `updater instanceof Function ? updater(state.value)
All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling.

```tsx
import { useQwikTable, SortingState } from '@tanstack/qwik-table'
import { useQwikTable, type SortingState } from '@tanstack/qwik-table'
//...
const sorting = Qwik.useSignal<SortingState[]>([
{
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/qwik/qwik-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `@tanstack/qwik-table` adapter is a wrapper around the core table logic. Mos

## Exports

`@tanstack/qwik-table` re-exports all of `@tanstack/table-core`'s and the following:
`@tanstack/qwik-table` re-exports all of `@tanstack/table-core`'s APIs and the following:

### `useQwikTable`

Expand Down
2 changes: 1 addition & 1 deletion docs/framework/react/guide/table-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const table = useReactTable({
All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling.

```tsx
import { useReactTable, SortingState } from '@tanstack/react-table'
import { useReactTable, type SortingState } from '@tanstack/react-table'
//...
const [sorting, setSorting] = React.useState<SortingState[]>([
{
Expand Down
2 changes: 1 addition & 1 deletion docs/framework/solid/guide/table-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ const table = createSolidTable({
All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling.

```tsx
import { createSolidTable, SortingState } from '@tanstack/solid-table'
import { createSolidTable, type SortingState } from '@tanstack/solid-table'
//...
const [sorting, setSorting] = createSignal<SortingState[]>([
{
Expand Down

0 comments on commit bc4f3df

Please sign in to comment.