@tanstack/react-table 9.0.0-alpha.10 → 9.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-table",
3
- "version": "9.0.0-alpha.10",
3
+ "version": "9.0.0-alpha.11",
4
4
  "description": "Headless UI for building powerful tables & datagrids for React.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -22,40 +22,47 @@
22
22
  ],
23
23
  "type": "module",
24
24
  "types": "dist/esm/index.d.ts",
25
- "main": "dist/cjs/index.cjs",
25
+ "main": "dist/esm/index.js",
26
26
  "module": "dist/esm/index.js",
27
27
  "exports": {
28
28
  ".": {
29
29
  "import": {
30
30
  "types": "./dist/esm/index.d.ts",
31
31
  "default": "./dist/esm/index.js"
32
- },
33
- "require": {
34
- "types": "./dist/cjs/index.d.cts",
35
- "default": "./dist/cjs/index.cjs"
36
32
  }
37
33
  },
38
34
  "./package.json": "./package.json"
39
35
  },
40
36
  "sideEffects": false,
41
37
  "engines": {
42
- "node": ">=12"
38
+ "node": ">=16"
43
39
  },
44
40
  "files": [
45
41
  "dist",
46
42
  "src"
47
43
  ],
48
44
  "dependencies": {
49
- "@tanstack/table-core": "9.0.0-alpha.10"
45
+ "@tanstack/react-store": "^0.8.0",
46
+ "@tanstack/table-core": "9.0.0-alpha.11"
50
47
  },
51
48
  "devDependencies": {
52
- "@types/react": "^18.3.3",
53
- "@vitejs/plugin-react": "^4.3.1",
54
- "react": "^18.3.1"
49
+ "@eslint-react/eslint-plugin": "^2.7.4",
50
+ "@types/react": "^19.2.10",
51
+ "@vitejs/plugin-react": "^5.1.2",
52
+ "eslint-plugin-react-compiler": "19.1.0-rc.2",
53
+ "eslint-plugin-react-hooks": "^7.0.1",
54
+ "react": "^19.2.4"
55
55
  },
56
56
  "peerDependencies": {
57
- "react": ">=16.8",
58
- "react-dom": ">=16.8"
57
+ "react": ">=18"
59
58
  },
60
- "scripts": {}
59
+ "scripts": {
60
+ "clean": "rimraf ./build && rimraf ./dist",
61
+ "test:eslint": "eslint ./src",
62
+ "test:lib": "vitest --passWithNoTests",
63
+ "test:lib:dev": "pnpm test:lib --watch",
64
+ "test:types": "tsc",
65
+ "test:build": "publint --strict",
66
+ "build": "vite build"
67
+ }
61
68
  }
@@ -0,0 +1,118 @@
1
+ import React from 'react'
2
+ import type {
3
+ Cell,
4
+ CellData,
5
+ Header,
6
+ RowData,
7
+ TableFeatures,
8
+ } from '@tanstack/table-core'
9
+ import type { ComponentType, JSX, ReactNode } from 'react'
10
+
11
+ export type Renderable<TProps> = ReactNode | ComponentType<TProps>
12
+
13
+ function isReactComponent<TProps>(
14
+ component: unknown,
15
+ ): component is ComponentType<TProps> {
16
+ return (
17
+ isClassComponent(component) ||
18
+ typeof component === 'function' ||
19
+ isExoticComponent(component)
20
+ )
21
+ }
22
+
23
+ function isClassComponent(component: any) {
24
+ return (
25
+ typeof component === 'function' &&
26
+ (() => {
27
+ const proto = Object.getPrototypeOf(component)
28
+ return proto.prototype && proto.prototype.isReactComponent
29
+ })()
30
+ )
31
+ }
32
+
33
+ function isExoticComponent(component: any) {
34
+ return (
35
+ typeof component === 'object' &&
36
+ typeof component.$$typeof === 'symbol' &&
37
+ ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description)
38
+ )
39
+ }
40
+
41
+ /**
42
+ * If rendering headers, cells, or footers with custom markup, use flexRender instead of `cell.getValue()` or `cell.renderValue()`.
43
+ * @example flexRender(cell.column.columnDef.cell, cell.getContext())
44
+ */
45
+ export function flexRender<TProps extends object>(
46
+ Comp: Renderable<TProps>,
47
+ props: TProps,
48
+ ): ReactNode | JSX.Element {
49
+ return !Comp ? null : isReactComponent<TProps>(Comp) ? (
50
+ <Comp {...props} />
51
+ ) : (
52
+ Comp
53
+ )
54
+ }
55
+
56
+ /**
57
+ * Simplified component wrapper of `flexRender`. Use this utility component to render headers, cells, or footers with custom markup.
58
+ * Only one prop (`cell`, `header`, or `footer`) may be passed.
59
+ * @example <FlexRender cell={cell} />
60
+ * @example <FlexRender header={header} />
61
+ * @example <FlexRender footer={footer} />
62
+ */
63
+ export type FlexRenderProps<
64
+ TFeatures extends TableFeatures,
65
+ TData extends RowData,
66
+ TValue extends CellData = CellData,
67
+ > =
68
+ | { cell: Cell<TFeatures, TData, TValue>; header?: never; footer?: never }
69
+ | {
70
+ header: Header<TFeatures, TData, TValue>
71
+ cell?: never
72
+ footer?: never
73
+ }
74
+ | {
75
+ footer: Header<TFeatures, TData, TValue>
76
+ cell?: never
77
+ header?: never
78
+ }
79
+
80
+ /**
81
+ * Simplified component wrapper of `flexRender`. Use this utility component to render headers, cells, or footers with custom markup.
82
+ * Only one prop (`cell`, `header`, or `footer`) may be passed.
83
+ * @example
84
+ * ```tsx
85
+ * <FlexRender cell={cell} />
86
+ * <FlexRender header={header} />
87
+ * <FlexRender footer={footer} />
88
+ * ```
89
+ *
90
+ * This replaces calling `flexRender` directly like this:
91
+ * ```tsx
92
+ * flexRender(cell.column.columnDef.cell, cell.getContext())
93
+ * flexRender(header.column.columnDef.header, header.getContext())
94
+ * flexRender(footer.column.columnDef.footer, footer.getContext())
95
+ * ```
96
+ */
97
+ export function FlexRender<
98
+ TFeatures extends TableFeatures,
99
+ TData extends RowData,
100
+ TValue extends CellData = CellData,
101
+ >(props: FlexRenderProps<TFeatures, TData, TValue>) {
102
+ if ('cell' in props && props.cell) {
103
+ return flexRender(props.cell.column.columnDef.cell, props.cell.getContext())
104
+ }
105
+ if ('header' in props && props.header) {
106
+ return flexRender(
107
+ props.header.column.columnDef.header,
108
+ props.header.getContext(),
109
+ )
110
+ }
111
+ if ('footer' in props && props.footer) {
112
+ return flexRender(
113
+ props.footer.column.columnDef.footer,
114
+ props.footer.getContext(),
115
+ )
116
+ }
117
+ return null
118
+ }
@@ -0,0 +1,70 @@
1
+ 'use client'
2
+ import { useStore } from '@tanstack/react-store'
3
+ import type {
4
+ NoInfer,
5
+ RowData,
6
+ Table,
7
+ TableFeatures,
8
+ TableState,
9
+ } from '@tanstack/table-core'
10
+ import type { FunctionComponent, ReactNode } from 'react'
11
+
12
+ export type SubscribeProps<
13
+ TFeatures extends TableFeatures,
14
+ TData extends RowData,
15
+ TSelected = {},
16
+ > = {
17
+ /**
18
+ * The table instance to subscribe to. Required when using as a standalone component.
19
+ * Not needed when using as `table.Subscribe`.
20
+ */
21
+ table: Table<TFeatures, TData>
22
+ /**
23
+ * A selector function that selects the part of the table state to subscribe to.
24
+ * This allows for fine-grained reactivity by only re-rendering when the selected state changes.
25
+ */
26
+ selector: (state: NoInfer<TableState<TFeatures>>) => TSelected
27
+ /**
28
+ * The children to render. Can be a function that receives the selected state, or a React node.
29
+ */
30
+ children: ((state: TSelected) => ReactNode) | ReactNode
31
+ }
32
+
33
+ /**
34
+ * A React component that allows you to subscribe to the table state.
35
+ *
36
+ * This is useful for opting into state re-renders for specific parts of the table state.
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ * // As a standalone component
41
+ * <Subscribe table={table} selector={(state) => ({ rowSelection: state.rowSelection })}>
42
+ * {({ rowSelection }) => (
43
+ * <div>Selected rows: {Object.keys(rowSelection).length}</div>
44
+ * )}
45
+ * </Subscribe>
46
+ * ```
47
+ *
48
+ * @example
49
+ * ```tsx
50
+ * // As table.Subscribe (table instance method)
51
+ * <table.Subscribe selector={(state) => ({ rowSelection: state.rowSelection })}>
52
+ * {({ rowSelection }) => (
53
+ * <div>Selected rows: {Object.keys(rowSelection).length}</div>
54
+ * )}
55
+ * </table.Subscribe>
56
+ * ```
57
+ */
58
+ export function Subscribe<
59
+ TFeatures extends TableFeatures,
60
+ TData extends RowData,
61
+ TSelected = {},
62
+ >(
63
+ props: SubscribeProps<TFeatures, TData, TSelected>,
64
+ ): ReturnType<FunctionComponent> {
65
+ const selected = useStore(props.table.store, props.selector)
66
+
67
+ return typeof props.children === 'function'
68
+ ? props.children(selected)
69
+ : props.children
70
+ }