mirror of
https://github.com/gosticks/react-table.git
synced 2026-06-29 01:20:02 +00:00
Merge branch 'master' into pr/1634
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"dist/index.js": {
|
||||
"bundled": 98780,
|
||||
"minified": 48107,
|
||||
"gzipped": 12496
|
||||
"bundled": 97093,
|
||||
"minified": 46881,
|
||||
"gzipped": 12344
|
||||
},
|
||||
"dist/index.es.js": {
|
||||
"bundled": 97724,
|
||||
"minified": 47164,
|
||||
"gzipped": 12312,
|
||||
"bundled": 96528,
|
||||
"minified": 46389,
|
||||
"gzipped": 12228,
|
||||
"treeshaked": {
|
||||
"rollup": {
|
||||
"code": 78,
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
## 7.0.0-beta.27
|
||||
|
||||
- Added the `useControlledState` option, which allows for hook-context control of the resolved internal table state
|
||||
|
||||
## 7.0.0-beta.26
|
||||
|
||||
- Fixed an issue where the table would crash if useSortBy was reset via the resetSortBy action
|
||||
|
||||
13
README.md
13
README.md
@@ -42,8 +42,9 @@ Enjoy this library? Try them all! [React Query](https://github.com/tannerlinsley
|
||||
- [Installation](./docs/installation.md) - Walk through how to install React Table
|
||||
- [Concepts](./docs/concepts.md) - Read how React Table works and how you can use it better
|
||||
- [Examples](./docs/examples.md) - Experience and learn from some examples and guides of how to use React Table and implement common patterns
|
||||
- [API](./docs/api.md) - Get to know React Table's API, how to use it and how to extend its functionality
|
||||
- [Using V6 Still?](#v6-info)
|
||||
- [API](./docs/api.md) - Get to know React Table's API
|
||||
- [FAQ](./docs/faq.md) - Learn how to use React Table for specific challenges and tasks
|
||||
- [Previous Versions](#previous-versions)
|
||||
- [Contributing](./CONTRIBUTING.md) - Become familiar with how to contribute back to React Table
|
||||
- [Code of Conduct](./CODE_OF_CONDUCT.md) - Be a good React Table citizen by following these repository rules
|
||||
|
||||
@@ -194,11 +195,11 @@ This library is being built and maintained by me, @tannerlinsley and I am always
|
||||
|
||||
### [Become a Sponsor](https://github.com/sponsors/tannerlinsley/)
|
||||
|
||||
## v6 Info
|
||||
## Previous Versions
|
||||
|
||||
#### I'm still using v6, what should I do?
|
||||
### Version 6
|
||||
|
||||
v6 is a great library and while it is still available for use, I do not intend on offering any long-term support for it. If you intend to keep using v6, I recommend maintaining your own fork of the library and keeping it up to date for your version of React.
|
||||
v6 is a great library and while it is still available to install and use, I am no longer offering any long-term support for it. If you intend to keep using v6, I recommend maintaining your own fork of the library and keeping it up to date for your version of React.
|
||||
|
||||
#### Where are the docs for the older v6 version?
|
||||
|
||||
@@ -208,4 +209,4 @@ Please [visit the v6 branch](https://github.com/tannerlinsley/react-table/tree/v
|
||||
|
||||
The differences between the 2 versions are incredibly massive. Unfortunately, I cannot write a one-to-one upgrade guide for any of v6's API, simply because much of it is irrelevant with v7's headless approach. The best approach for migrating to v7 is to learn its API by reading the documentation and then following some of the examples to begin building your own table component.
|
||||
|
||||
In case you would need to have both v6 and v7 in one app during the migration process (large codebase, complex use cases), you can install an official [`react-table-6` package](https://www.npmjs.com/package/react-table-6) alongside the `react-table`.
|
||||
In case you would need to have both v6 and v7 in one app during the migration process (large codebase, complex use cases), you can either (1) fork and maintain your own local version of React Table v6 or (2) install the [`react-table-6` alias package](https://www.npmjs.com/package/react-table-6) for use alongside the `react-table` package.
|
||||
|
||||
@@ -92,6 +92,12 @@ The following options are supported via the main options object passed to `useTa
|
||||
- With every action that is dispatched to the table's internal `React.useReducer` instance, this reducer is called and is allowed to modify the final state object for updating.
|
||||
- It is passed the `newState`, `action`, and `prevState` and is expected to either return the `newState` or a modified version of the `newState`
|
||||
- May also be used to "control" the state of the table, by overriding certain pieces of state regardless of the action.
|
||||
- `useControlledState: HookFunction(state) => controlledState`
|
||||
- Optional
|
||||
- If you need to control part of the table state, this is the place to do it.
|
||||
- This function is run on every single render, just like a hook and allows you to alter the final state of the table if necessary.
|
||||
- You can use hooks inside of this function, but most of the time, we just suggest using `React.useMemo` to memoize your state overrides.
|
||||
- See the FAQ ["How can I manually control the table state?"](./faq.md#how-can-i-manually-control-the-table-state) for a an example.
|
||||
- `defaultColumn: Object`
|
||||
- Optional
|
||||
- Defaults to `{}`
|
||||
|
||||
29
docs/faq.md
Normal file
29
docs/faq.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# FAQ
|
||||
|
||||
Below are some of the most frequently asked questions on how to use the React Table API to solve various table challenges you may encounter
|
||||
|
||||
<hr/>
|
||||
|
||||
## How can I manually control the table state?
|
||||
|
||||
Occasionally, you may need to override some of the table state from a parent component or from somewhere above the usage of `useTable`. In this case, you can turn to `useTable`'s `useControlledState` option. This hook function is run on every render and allows you an opportunity to override or change the final table state in a safe way.
|
||||
|
||||
For example, to control a table's `pageIndex` from a parent component:
|
||||
|
||||
```js
|
||||
const [controlledPageIndex, setControlledPage] = React.useState(0)
|
||||
|
||||
useTable({
|
||||
useControlledState: state => {
|
||||
return React.useMemo(
|
||||
() => ({
|
||||
...state,
|
||||
pageIndex: controlledPageIndex,
|
||||
}),
|
||||
[controlledPageIndex]
|
||||
)
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
**It's important that the state override is done within a `useMemo` call to prevent the state variable from changing on every render.**
|
||||
@@ -1,17 +1,15 @@
|
||||
# Installation
|
||||
|
||||
Install the React Table v7 Alpha as a dependency using `npm` or `yarn`
|
||||
Install React Table as a dependency using `npm` or `yarn`
|
||||
|
||||
```bash
|
||||
# NPM
|
||||
$ npm install react-table@next
|
||||
$ npm install react-table
|
||||
|
||||
# Yarn
|
||||
$ yarn add react-table@next
|
||||
$ yarn add react-table
|
||||
```
|
||||
|
||||
> NOTE: `@next` is only required temporarily while work on the alpha and beta versions are still shifting.
|
||||
|
||||
To import React Table:
|
||||
|
||||
```js
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Kitchen Sink
|
||||
|
||||
- [Open this example in a new CodeSandbox](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/kitchen-sink)
|
||||
- [Open this example in a new CodeSandbox](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/kitchen-sink-controlled)
|
||||
- `yarn` and `yarn start` to run and edit the example
|
||||
|
||||
@@ -308,9 +308,9 @@ function Table({ columns, data, updateMyData, skipPageReset }) {
|
||||
// when we edit the data, undefined means using the default
|
||||
getResetPageDeps: skipPageReset ? false : undefined,
|
||||
},
|
||||
useGroupBy,
|
||||
useFilters,
|
||||
useSortBy,
|
||||
useGroupBy,
|
||||
useExpanded,
|
||||
usePagination,
|
||||
useRowSelect
|
||||
@@ -442,7 +442,7 @@ function Table({ columns, data, updateMyData, skipPageReset }) {
|
||||
groupBy,
|
||||
expanded,
|
||||
filters,
|
||||
selectedRowPaths,
|
||||
selectedRowPaths: [...selectedRowPaths.values()],
|
||||
},
|
||||
null,
|
||||
2
|
||||
|
||||
@@ -441,7 +441,7 @@ function Table({ columns, data, updateMyData, skipReset }) {
|
||||
groupBy,
|
||||
expanded,
|
||||
filters,
|
||||
selectedRowPaths,
|
||||
selectedRowPaths: [...selectedRowPaths.values()],
|
||||
},
|
||||
null,
|
||||
2
|
||||
|
||||
@@ -13,13 +13,7 @@ import makeData from './makeData'
|
||||
|
||||
function Table({ columns, data }) {
|
||||
// Use the state and functions returned from useTable to build your UI
|
||||
const {
|
||||
getTableProps,
|
||||
getTableHeaderProps,
|
||||
headerGroups,
|
||||
rows,
|
||||
prepareRow,
|
||||
} = useTable({
|
||||
const { getTableProps, headerGroups, rows, prepareRow } = useTable({
|
||||
columns,
|
||||
data,
|
||||
})
|
||||
@@ -39,21 +33,20 @@ function Table({ columns, data }) {
|
||||
))}
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{rows.map(
|
||||
(row, i) => {
|
||||
prepareRow(row);
|
||||
return (
|
||||
<TableRow {...row.getRowProps()}>
|
||||
{row.cells.map(cell => {
|
||||
return (
|
||||
<TableCell {...cell.getCellProps()}>
|
||||
{cell.render('Cell')}
|
||||
</TableCell>
|
||||
)
|
||||
})}
|
||||
</TableRow>
|
||||
)}
|
||||
)}
|
||||
{rows.map((row, i) => {
|
||||
prepareRow(row)
|
||||
return (
|
||||
<TableRow {...row.getRowProps()}>
|
||||
{row.cells.map(cell => {
|
||||
return (
|
||||
<TableCell {...cell.getCellProps()}>
|
||||
{cell.render('Cell')}
|
||||
</TableCell>
|
||||
)
|
||||
})}
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</MaUTable>
|
||||
)
|
||||
|
||||
@@ -78,8 +78,8 @@ function Table({ columns, data }) {
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
<p>Selected Rows: {selectedRowPaths.length}</p>
|
||||
{/* <pre>
|
||||
<p>Selected Rows: {selectedRowPaths.size}</p>
|
||||
<pre>
|
||||
<code>
|
||||
{JSON.stringify(
|
||||
{
|
||||
@@ -92,7 +92,7 @@ function Table({ columns, data }) {
|
||||
2
|
||||
)}
|
||||
</code>
|
||||
</pre> */}
|
||||
</pre>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-table",
|
||||
"version": "7.0.0-beta.26",
|
||||
"version": "7.0.0-beta.27",
|
||||
"description": "A fast, lightweight, opinionated table and datagrid built on React",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/tannerlinsley/react-table#readme",
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
[
|
||||
{
|
||||
"timestamp": 1575517726476,
|
||||
"files": [
|
||||
{
|
||||
"filename": "index.js",
|
||||
"size": 20569,
|
||||
"delta": 58
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"timestamp": 1575391465436,
|
||||
"files": [
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
[
|
||||
{
|
||||
"timestamp": 1575517715056,
|
||||
"files": [
|
||||
{
|
||||
"filename": "index.es.js",
|
||||
"size": 20427,
|
||||
"delta": 59
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"timestamp": 1575391457318,
|
||||
"files": [
|
||||
|
||||
@@ -23,6 +23,7 @@ const defaultColumnInstance = {}
|
||||
const defaultReducer = (state, action, prevState) => state
|
||||
const defaultGetSubRows = (row, index) => row.subRows || []
|
||||
const defaultGetRowId = (row, index) => index
|
||||
const defaultUseControlledState = d => d
|
||||
|
||||
export const useTable = (props, ...plugins) => {
|
||||
// Destructure props
|
||||
@@ -34,6 +35,7 @@ export const useTable = (props, ...plugins) => {
|
||||
getSubRows = defaultGetSubRows,
|
||||
getRowId = defaultGetRowId,
|
||||
reducer: userReducer = defaultReducer,
|
||||
useControlledState = defaultUseControlledState,
|
||||
debug,
|
||||
} = props
|
||||
|
||||
@@ -57,10 +59,14 @@ export const useTable = (props, ...plugins) => {
|
||||
}
|
||||
|
||||
// But use the users table state if provided
|
||||
const [state, originalDispatch] = React.useReducer(reducer, undefined, () =>
|
||||
reducer(initialState, { type: actions.init })
|
||||
const [reducerState, originalDispatch] = React.useReducer(
|
||||
reducer,
|
||||
undefined,
|
||||
() => reducer(initialState, { type: actions.init })
|
||||
)
|
||||
|
||||
const state = useControlledState(reducerState)
|
||||
|
||||
// The table instance ref
|
||||
let instanceRef = React.useRef({})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user