From 184ec448aff65ad1a248f5a669b00d7da5769c62 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Tue, 3 Dec 2019 13:25:51 -0700 Subject: [PATCH 1/8] Update README.md --- examples/kitchen-sink-controlled/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/kitchen-sink-controlled/README.md b/examples/kitchen-sink-controlled/README.md index 5ce3ae7..48a41e5 100644 --- a/examples/kitchen-sink-controlled/README.md +++ b/examples/kitchen-sink-controlled/README.md @@ -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 From 1bbc89cedb0dbd42c72e54d283a9e55e9e6fea70 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Tue, 3 Dec 2019 13:28:16 -0700 Subject: [PATCH 2/8] Update App.js --- examples/kitchen-sink-controlled/src/App.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/kitchen-sink-controlled/src/App.js b/examples/kitchen-sink-controlled/src/App.js index a3a98e4..290c18c 100644 --- a/examples/kitchen-sink-controlled/src/App.js +++ b/examples/kitchen-sink-controlled/src/App.js @@ -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 From 5c2b8302c02843860db172fa90af9b475e693d6d Mon Sep 17 00:00:00 2001 From: Aaron Agarunov Date: Wed, 4 Dec 2019 14:26:28 -0500 Subject: [PATCH 3/8] Remove getTableHeaderProps destructured from useTable (#1706) --- examples/material-UI-components/src/App.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/material-UI-components/src/App.js b/examples/material-UI-components/src/App.js index c11912f..af7a48f 100644 --- a/examples/material-UI-components/src/App.js +++ b/examples/material-UI-components/src/App.js @@ -15,7 +15,6 @@ function Table({ columns, data }) { // Use the state and functions returned from useTable to build your UI const { getTableProps, - getTableHeaderProps, headerGroups, rows, prepareRow, From b687cc01f0edc203d4f437218e3034c9f48ae8c8 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 4 Dec 2019 22:46:53 -0500 Subject: [PATCH 4/8] 7.0.0-beta.27 --- .size-snapshot.json | 12 ++++++------ CHANGELOG.md | 4 ++++ README.md | 13 +++++++------ docs/api.md | 6 ++++++ docs/faq.md | 29 +++++++++++++++++++++++++++++ package.json | 2 +- src/hooks/useTable.js | 10 ++++++++-- 7 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 docs/faq.md diff --git a/.size-snapshot.json b/.size-snapshot.json index 931f944..7bc4247 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -1,13 +1,13 @@ { "dist/index.js": { - "bundled": 96781, - "minified": 46762, - "gzipped": 12303 + "bundled": 97093, + "minified": 46881, + "gzipped": 12344 }, "dist/index.es.js": { - "bundled": 96216, - "minified": 46270, - "gzipped": 12187, + "bundled": 96528, + "minified": 46389, + "gzipped": 12228, "treeshaked": { "rollup": { "code": 78, diff --git a/CHANGELOG.md b/CHANGELOG.md index 79663a9..c8ec2e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 99d426a..d2d5d66 100644 --- a/README.md +++ b/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. diff --git a/docs/api.md b/docs/api.md index 6d87fa6..8b84ecf 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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 `{}` diff --git a/docs/faq.md b/docs/faq.md new file mode 100644 index 0000000..c04d23f --- /dev/null +++ b/docs/faq.md @@ -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 + +
+ +## 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.** diff --git a/package.json b/package.json index 3f3a7a9..1bdefe6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/hooks/useTable.js b/src/hooks/useTable.js index a0cf22a..a3b35da 100755 --- a/src/hooks/useTable.js +++ b/src/hooks/useTable.js @@ -25,6 +25,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 @@ -36,6 +37,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({}) From b65c853d09c8509ea6bf05692d1fc267c971bcce Mon Sep 17 00:00:00 2001 From: "size-plugin[bot]" <52573255+size-plugin[bot]@users.noreply.github.com> Date: Thu, 5 Dec 2019 03:48:57 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=A4=B5=F0=9F=8F=BF=20=F0=9F=A7=8E?= =?UTF-8?q?=F0=9F=8F=BB=20update=20sizes-cjs.json=20=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sizes-cjs.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sizes-cjs.json b/sizes-cjs.json index eb048b7..1384690 100644 --- a/sizes-cjs.json +++ b/sizes-cjs.json @@ -1,4 +1,14 @@ [ + { + "timestamp": 1575517726476, + "files": [ + { + "filename": "index.js", + "size": 20569, + "delta": 58 + } + ] + }, { "timestamp": 1575391465436, "files": [ From 2ddd7731fadfc64bbb5cf51c5ed2d4ebb219d063 Mon Sep 17 00:00:00 2001 From: "size-plugin[bot]" <52573255+size-plugin[bot]@users.noreply.github.com> Date: Thu, 5 Dec 2019 03:48:58 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=91=A0=20=F0=9F=A4=8F=20update=20size?= =?UTF-8?q?s-es.json=20=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sizes-es.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sizes-es.json b/sizes-es.json index d0444d6..6273f4b 100644 --- a/sizes-es.json +++ b/sizes-es.json @@ -1,4 +1,14 @@ [ + { + "timestamp": 1575517715056, + "files": [ + { + "filename": "index.es.js", + "size": 20427, + "delta": 59 + } + ] + }, { "timestamp": 1575391457318, "files": [ From 57cc5dc7d0c74010f73131ccdb499da06bc0c727 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 4 Dec 2019 22:52:14 -0500 Subject: [PATCH 7/8] Update selectedRowPaths in examples --- examples/kitchen-sink-controlled/src/App.js | 2 +- examples/kitchen-sink/src/App.js | 2 +- examples/row-selection/src/App.js | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/kitchen-sink-controlled/src/App.js b/examples/kitchen-sink-controlled/src/App.js index 290c18c..5bf1b0a 100644 --- a/examples/kitchen-sink-controlled/src/App.js +++ b/examples/kitchen-sink-controlled/src/App.js @@ -442,7 +442,7 @@ function Table({ columns, data, updateMyData, skipPageReset }) { groupBy, expanded, filters, - selectedRowPaths, + selectedRowPaths: [...selectedRowPaths.values()], }, null, 2 diff --git a/examples/kitchen-sink/src/App.js b/examples/kitchen-sink/src/App.js index 3e3034a..c8b7229 100644 --- a/examples/kitchen-sink/src/App.js +++ b/examples/kitchen-sink/src/App.js @@ -441,7 +441,7 @@ function Table({ columns, data, updateMyData, skipReset }) { groupBy, expanded, filters, - selectedRowPaths, + selectedRowPaths: [...selectedRowPaths.values()], }, null, 2 diff --git a/examples/row-selection/src/App.js b/examples/row-selection/src/App.js index 144fcf8..99f2d4b 100644 --- a/examples/row-selection/src/App.js +++ b/examples/row-selection/src/App.js @@ -78,8 +78,8 @@ function Table({ columns, data }) { })} -

Selected Rows: {selectedRowPaths.length}

- {/*
+      

Selected Rows: {selectedRowPaths.size}

+
         
           {JSON.stringify(
             {
@@ -92,7 +92,7 @@ function Table({ columns, data }) {
             2
           )}
         
-      
*/} +
) } From 9829927fab8f300dbceb83fd58393a263a41a7f7 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Wed, 4 Dec 2019 22:54:31 -0500 Subject: [PATCH 8/8] Update installation.md --- docs/installation.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/installation.md b/docs/installation.md index fff1895..9b2be06 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -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