Skip to content

Commit

Permalink
rename construct functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Sep 16, 2024
1 parent 3338718 commit 3ce2287
Show file tree
Hide file tree
Showing 56 changed files with 232 additions and 220 deletions.
64 changes: 32 additions & 32 deletions docs/guide/custom-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ All of the functionality of a feature object can be described with the `TableFea

```ts
export interface TableFeature<TFeatures extends TableFeatures, TData extends RowData = any> {
_createCell?: (
constructCell?: (
cell: Cell<TFeatures, TData, unknown>,
column: Column<TFeatures, TData>,
row: Row<TFeatures, TData>,
table: Table<TFeatures, TData>
) => void
_createColumn?: (column: Column<TFeatures, TData, unknown>, table: Table<TFeatures, TData>) => void
_createHeader?: (header: Header<TFeatures, TData, unknown>, table: Table<TFeatures, TData>) => void
_createRow?: (row: Row<TFeatures, TData>, table: Table<TFeatures, TData>) => void
_createTable?: (table: Table<TFeatures, TData>) => void
_getDefaultColumnDef?: () => Partial<ColumnDef<TFeatures, TData, unknown>>
_getDefaultOptions?: (
constructColumn?: (column: Column<TFeatures, TData, unknown>, table: Table<TFeatures, TData>) => void
constructHeader?: (header: Header<TFeatures, TData, unknown>, table: Table<TFeatures, TData>) => void
constructRow?: (row: Row<TFeatures, TData>, table: Table<TFeatures, TData>) => void
constructTable?: (table: Table<TFeatures, TData>) => void
getDefaultColumnDef?: () => Partial<ColumnDef<TFeatures, TData, unknown>>
getDefaultOptions?: (
table: Table<TFeatures, TData>
) => Partial<TableOptions<TFeatures, TData>>
_getInitialState?: (initialState?: InitialTableState) => Partial<TableState>
getInitialState?: (initialState?: InitialTableState) => Partial<TableState>
}
```

Expand All @@ -56,53 +56,53 @@ This might be a bit confusing, so let's break down what each of these methods do

<br />

##### _getDefaultOptions
##### getDefaultOptions

The `_getDefaultOptions` method in a table feature is responsible for setting the default table options for that feature. For example, in the [Column Sizing](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnSizing.ts) feature, the `_getDefaultOptions` method sets the default `columnResizeMode` option with a default value of `"onEnd"`.
The `getDefaultOptions` method in a table feature is responsible for setting the default table options for that feature. For example, in the [Column Sizing](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnSizing.ts) feature, the `getDefaultOptions` method sets the default `columnResizeMode` option with a default value of `"onEnd"`.

<br />

##### _getDefaultColumnDef
##### getDefaultColumnDef

The `_getDefaultColumnDef` method in a table feature is responsible for setting the default column options for that feature. For example, in the [Sorting](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSorting.ts) feature, the `_getDefaultColumnDef` method sets the default `sortUndefined` column option with a default value of `1`.
The `getDefaultColumnDef` method in a table feature is responsible for setting the default column options for that feature. For example, in the [Sorting](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSorting.ts) feature, the `getDefaultColumnDef` method sets the default `sortUndefined` column option with a default value of `1`.

<br />

##### _getInitialState
##### getInitialState

The `_getInitialState` method in a table feature is responsible for setting the default state for that feature. For example, in the [Pagination](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowPagination.ts) feature, the `_getInitialState` method sets the default `pageSize` state with a value of `10` and the default `pageIndex` state with a value of `0`.
The `getInitialState` method in a table feature is responsible for setting the default state for that feature. For example, in the [Pagination](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowPagination.ts) feature, the `getInitialState` method sets the default `pageSize` state with a value of `10` and the default `pageIndex` state with a value of `0`.

#### API Creators

<br />

##### _createTable
##### constructTable

The `_createTable` method in a table feature is responsible for adding methods to the `table` instance. For example, in the [Row Selection](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSelection.ts) feature, the `_createTable` method adds many table instance API methods such as `toggleAllRowsSelected`, `getIsAllRowsSelected`, `getIsSomeRowsSelected`, etc. So then, when you call `table.toggleAllRowsSelected()`, you are calling a method that was added to the table instance by the `RowSelection` feature.
The `constructTable` method in a table feature is responsible for adding methods to the `table` instance. For example, in the [Row Selection](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSelection.ts) feature, the `constructTable` method adds many table instance API methods such as `toggleAllRowsSelected`, `getIsAllRowsSelected`, `getIsSomeRowsSelected`, etc. So then, when you call `table.toggleAllRowsSelected()`, you are calling a method that was added to the table instance by the `RowSelection` feature.

<br />

##### _createHeader
##### constructHeader

The `_createHeader` method in a table feature is responsible for adding methods to the `header` instance. For example, in the [Column Sizing](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnSizing.ts) feature, the `_createHeader` method adds many header instance API methods such as `getStart`, and many others. So then, when you call `header.getStart()`, you are calling a method that was added to the header instance by the `ColumnSizing` feature.
The `constructHeader` method in a table feature is responsible for adding methods to the `header` instance. For example, in the [Column Sizing](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnSizing.ts) feature, the `constructHeader` method adds many header instance API methods such as `getStart`, and many others. So then, when you call `header.getStart()`, you are calling a method that was added to the header instance by the `ColumnSizing` feature.

<br />

##### _createColumn
##### constructColumn

The `_createColumn` method in a table feature is responsible for adding methods to the `column` instance. For example, in the [Sorting](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSorting.ts) feature, the `_createColumn` method adds many column instance API methods such as `getNextSortingOrder`, `toggleSorting`, etc. So then, when you call `column.toggleSorting()`, you are calling a method that was added to the column instance by the `RowSorting` feature.
The `constructColumn` method in a table feature is responsible for adding methods to the `column` instance. For example, in the [Sorting](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSorting.ts) feature, the `constructColumn` method adds many column instance API methods such as `getNextSortingOrder`, `toggleSorting`, etc. So then, when you call `column.toggleSorting()`, you are calling a method that was added to the column instance by the `RowSorting` feature.

<br />

##### _createRow
##### constructRow

The `_createRow` method in a table feature is responsible for adding methods to the `row` instance. For example, in the [Row Selection](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSelection.ts) feature, the `_createRow` method adds many row instance API methods such as `toggleSelected`, `getIsSelected`, etc. So then, when you call `row.toggleSelected()`, you are calling a method that was added to the row instance by the `RowSelection` feature.
The `constructRow` method in a table feature is responsible for adding methods to the `row` instance. For example, in the [Row Selection](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/RowSelection.ts) feature, the `constructRow` method adds many row instance API methods such as `toggleSelected`, `getIsSelected`, etc. So then, when you call `row.toggleSelected()`, you are calling a method that was added to the row instance by the `RowSelection` feature.

<br />

##### _createCell
##### constructCell

The `_createCell` method in a table feature is responsible for adding methods to the `cell` instance. For example, in the [Column Grouping](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnGrouping.ts) feature, the `_createCell` method adds many cell instance API methods such as `getIsGrouped`, `getIsAggregated`, etc. So then, when you call `cell.getIsGrouped()`, you are calling a method that was added to the cell instance by the `ColumnGrouping` feature.
The `constructCell` method in a table feature is responsible for adding methods to the `cell` instance. For example, in the [Column Grouping](https://github.com/TanStack/table/blob/main/packages/table-core/src/features/ColumnGrouping.ts) feature, the `constructCell` method adds many cell instance API methods such as `getIsGrouped`, `getIsAggregated`, etc. So then, when you call `cell.getIsGrouped()`, you are calling a method that was added to the cell instance by the `ColumnGrouping` feature.

### Adding a Custom Feature

Expand Down Expand Up @@ -179,15 +179,15 @@ Use the `TableFeature` type to ensure that you are creating the feature object c
```ts
export const DensityFeature: TableFeature<any> = { //Use the TableFeature type!!
// define the new feature's initial state
_getInitialState: (state): DensityTableState => {
getInitialState: (state): DensityTableState => {
return {
density: 'md',
...state,
}
},

// define the new feature's default options
_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Partial<Table<TFeatures, TData>>
): DensityOptions => {
return {
Expand All @@ -196,12 +196,12 @@ export const DensityFeature: TableFeature<any> = { //Use the TableFeature type!!
} as DensityOptions
},
// if you need to add a default column definition...
// _getDefaultColumnDef: <TFeatures extends TableFeatures, TData extends RowData>(): Partial<ColumnDef<TFeatures, TData>> => {
// getDefaultColumnDef: <TFeatures extends TableFeatures, TData extends RowData>(): Partial<ColumnDef<TFeatures, TData>> => {
// return { meta: {} } //use meta instead of directly adding to the columnDef to avoid typescript stuff that's hard to workaround
// },

// define the new feature's table instance methods
_createTable: <TFeatures extends TableFeatures, TData extends RowData>(table: Table<TFeatures, TData>): void => {
constructTable: <TFeatures extends TableFeatures, TData extends RowData>(table: Table<TFeatures, TData>): void => {
table.setDensity = updater => {
const safeUpdater: Updater<DensityState> = old => {
let newState = functionalUpdate(updater, old)
Expand All @@ -218,13 +218,13 @@ export const DensityFeature: TableFeature<any> = { //Use the TableFeature type!!
},

// if you need to add row instance APIs...
// _createRow: <TFeatures extends TableFeatures, TData extends RowData>(row, table): void => {},
// constructRow: <TFeatures extends TableFeatures, TData extends RowData>(row, table): void => {},
// if you need to add cell instance APIs...
// _createCell: <TFeatures extends TableFeatures, TData extends RowData>(cell, column, row, table): void => {},
// constructCell: <TFeatures extends TableFeatures, TData extends RowData>(cell, column, row, table): void => {},
// if you need to add column instance APIs...
// _createColumn: <TFeatures extends TableFeatures, TData extends RowData>(column, table): void => {},
// constructColumn: <TFeatures extends TableFeatures, TData extends RowData>(column, table): void => {},
// if you need to add header instance APIs...
// _createHeader: <TFeatures extends TableFeatures, TData extends RowData>(header, table): void => {},
// constructHeader: <TFeatures extends TableFeatures, TData extends RowData>(header, table): void => {},
}
```

Expand Down
16 changes: 8 additions & 8 deletions examples/react/custom-features/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ declare module '@tanstack/react-table' {
// Here is all of the actual javascript code for our new feature
export const DensityFeature: TableFeature = {
// define the new feature's initial state
_getInitialState: (state): DensityTableState => {
getInitialState: (state): DensityTableState => {
return {
density: 'md',
...state,
}
},

// define the new feature's default options
_getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
getDefaultOptions: <TFeatures extends TableFeatures, TData extends RowData>(
table: Partial<Table<TFeatures, TData>>,
): DensityOptions => {
return {
Expand All @@ -92,12 +92,12 @@ export const DensityFeature: TableFeature = {
} as DensityOptions
},
// if you need to add a default column definition...
// _getDefaultColumnDef: <TFeatures extends TableFeatures, TData extends RowData>(): Partial<ColumnDef<TFeatures, TData>> => {
// getDefaultColumnDef: <TFeatures extends TableFeatures, TData extends RowData>(): Partial<ColumnDef<TFeatures, TData>> => {
// return { meta: {} } //use meta instead of directly adding to the columnDef to avoid typescript stuff that's hard to workaround
// },

// define the new feature's table instance methods
_createTable: <TFeatures extends TableFeatures, TData extends RowData>(
constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData>,
): void => {
table.setDensity = (updater) => {
Expand All @@ -116,13 +116,13 @@ export const DensityFeature: TableFeature = {
},

// if you need to add row instance APIs...
// _createRow: <TFeatures extends TableFeatures, TData extends RowData>(row, table): void => {},
// constructRow: <TFeatures extends TableFeatures, TData extends RowData>(row, table): void => {},
// if you need to add cell instance APIs...
// _createCell: <TFeatures extends TableFeatures, TData extends RowData>(cell, column, row, table): void => {},
// constructCell: <TFeatures extends TableFeatures, TData extends RowData>(cell, column, row, table): void => {},
// if you need to add column instance APIs...
// _createColumn: <TFeatures extends TableFeatures, TData extends RowData>(column, table): void => {},
// constructColumn: <TFeatures extends TableFeatures, TData extends RowData>(column, table): void => {},
// if you need to add header instance APIs...
// _createHeader: <TFeatures extends TableFeatures, TData extends RowData>(header, table): void => {},
// constructHeader: <TFeatures extends TableFeatures, TData extends RowData>(header, table): void => {},
}
//end of custom feature code

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "module",
"scripts": {
"clean": "pnpm --filter \"./packages/**\" run clean",
"clean:node_modules": "find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +",
"preinstall": "node -e \"if(process.env.CI == 'true') {console.log('Skipping preinstall...'); process.exit(1)}\" || npx -y only-allow pnpm",
"test": "pnpm run test:ci",
"test:pr": "nx affected --targets=test:format,test:eslint,test:sherif,test:knip,test:lib,test:types,test:build,build",
Expand Down
4 changes: 2 additions & 2 deletions packages/angular-table/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Signal, computed, signal } from '@angular/core'
import { _createTable } from '@tanstack/table-core'
import { constructTable } from '@tanstack/table-core'
import { lazyInit } from './lazy-signal-initializer'
import { proxifyTable } from './proxy'
import type {
Expand Down Expand Up @@ -33,7 +33,7 @@ export function injectTable<
...options(),
}

const table = _createTable(resolvedOptions)
const table = constructTable(resolvedOptions)

// By default, manage table state here using the table's initial state
const state = signal<TableState<TFeatures>>(table.initialState as any) // TODO: fix type
Expand Down
4 changes: 2 additions & 2 deletions packages/lit-table/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _createTable } from '@tanstack/table-core'
import { constructTable } from '@tanstack/table-core'
import type {
RowData,
Table,
Expand Down Expand Up @@ -51,7 +51,7 @@ export class TableController<
...options,
}

this.tableInstance = _createTable(resolvedOptions)
this.tableInstance = constructTable(resolvedOptions)
this._tableState = {
...this.tableInstance.initialState,
...options.state,
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik-table/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Qwik from '@builder.io/qwik'
import { _createTable } from '@tanstack/table-core'
import { constructTable } from '@tanstack/table-core'
import type {
RowData,
Table,
Expand Down Expand Up @@ -46,7 +46,7 @@ export function useTable<
const table = Qwik.useStore<{
instance: Qwik.NoSerialize<Table<TFeatures, TData>>
}>({
instance: Qwik.noSerialize(_createTable(resolvedOptions)),
instance: Qwik.noSerialize(constructTable(resolvedOptions)),
})

// By default, manage table state here using the table's initial state
Expand Down
4 changes: 2 additions & 2 deletions packages/react-table/src/tableHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _createTableHelper } from '@tanstack/table-core'
import { constructTableHelper } from '@tanstack/table-core'
import { useTable } from './useTable'
import type {
RowData,
Expand Down Expand Up @@ -27,7 +27,7 @@ export function createTableHelper<
>(
tableHelperOptions: TableHelperOptions<TFeatures, TData>,
): TableHelper<TFeatures, TData> {
const tableHelper = _createTableHelper(useTable, tableHelperOptions)
const tableHelper = constructTableHelper(useTable, tableHelperOptions)
return {
...tableHelper,
useTable: tableHelper.tableCreator,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-table/src/useTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'

import {
_createTable,
constructTable,
builtInFeatures,
getInitialTableState,
} from '@tanstack/table-core'
Expand All @@ -19,7 +19,7 @@ function useTableRef<TFeatures extends TableFeatures, TData extends RowData>(
const tableRef = React.useRef<Table<TFeatures, TData>>()

if (!tableRef.current) {
tableRef.current = _createTable(options)
tableRef.current = constructTable(options)
}

return tableRef.current
Expand Down
4 changes: 2 additions & 2 deletions packages/solid-table/src/createTable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
_createTable,
builtInFeatures,
constructTable,
getInitialTableState,
} from '@tanstack/table-core'
import { createComputed, mergeProps } from 'solid-js'
Expand All @@ -24,7 +24,7 @@ export function createTable<
},
}

const table = _createTable<TFeatures, TData>(statefulOptions)
const table = constructTable<TFeatures, TData>(statefulOptions)

createComputed(() => {
table.setOptions((prev) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/solid-table/src/tableHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _createTableHelper } from '@tanstack/table-core'
import { constructTableHelper } from '@tanstack/table-core'
import { createTable } from './createTable'
import type {
RowData,
Expand Down Expand Up @@ -27,7 +27,7 @@ export function createTableHelper<
>(
tableHelperOptions: TableHelperOptions<TFeatures, TData>,
): TableHelper<TFeatures, TData> {
const tableHelper = _createTableHelper(createTable, tableHelperOptions)
const tableHelper = constructTableHelper(createTable, tableHelperOptions)
return {
...tableHelper,
createTable: tableHelper.tableCreator,
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte-table/src/table.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _createTable } from '@tanstack/table-core'
import { constructTable } from '@tanstack/table-core'
import type {
RowData,
TableFeatures,
Expand Down Expand Up @@ -51,7 +51,7 @@ export function createTable<
options,
)

const table = _createTable(resolvedOptions)
const table = constructTable(resolvedOptions)
let state = $state<Partial<TableState<TFeatures>>>(table.initialState)

function updateOptions() {
Expand Down
2 changes: 1 addition & 1 deletion packages/table-core/src/core/cells/Cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Table } from '../../types/Table'
import type { Cell } from '../../types/Cell'

export const Cells: TableFeature = {
_createCell: <
constructCell: <
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
Expand Down
Loading

0 comments on commit 3ce2287

Please sign in to comment.