mirror of
https://github.com/gosticks/react-table.git
synced 2026-08-19 08:20:30 +00:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca23da0342 | ||
|
|
072967b771 | ||
|
|
da9e94091f | ||
|
|
a98ec48b20 | ||
|
|
1e2ca74f96 | ||
|
|
aaa9459389 | ||
|
|
7884f33486 | ||
|
|
6193a33c89 | ||
|
|
0e54ef0005 | ||
|
|
b10446b8c4 |
@@ -44,6 +44,28 @@ Hooks for building **lightweight, fast and extendable datagrids** for React
|
||||
- Extensible via hook-based plugin system
|
||||
- <a href="https://medium.com/@tannerlinsley/why-i-wrote-react-table-and-the-problems-it-has-solved-for-nozzle-others-445c4e93d4a8#.axza4ixba" target="\_parent">"Why I wrote React Table and the problems it has solved for Nozzle.io"</a> by Tanner Linsley
|
||||
|
||||
## News / Q&A
|
||||
|
||||
#### What is the current state of React Table?
|
||||
|
||||
React Table v7 is still under active development, and its API is still changing, albeit only slightly. For this reason, it is currently in an **alpha** release state. It's estimated that it will be out of alpha and into **beta** sometime during September 2019. It will then remain in beta for a short time before being released sometime in Q4 of 2019.
|
||||
|
||||
#### Should I use v7@alpha in production?
|
||||
|
||||
You can use it production as long as you lock in the alpha version in your package.json and also accept that the API will likely change before it's official release.
|
||||
|
||||
#### I'm still using v6, what should I do?
|
||||
|
||||
v6 is a great library and is still the default install for `react-table`, however, 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.
|
||||
|
||||
#### Where are the docs for the older v6 version?
|
||||
|
||||
Please [visit the v6 branch](https://github.com/tannerlinsley/react-table/tree/v6)
|
||||
|
||||
#### I want to migrate from v6 to v7. How do I do that?
|
||||
|
||||
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.
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Installation](./docs/installation.md) - Walk through how to install React Table
|
||||
@@ -149,6 +171,7 @@ Hooks for building **lightweight, fast and extendable datagrids** for React
|
||||
<div>Thomas Funk</div>
|
||||
<div>Dan Houle</div>
|
||||
<div>David Pickut</div>
|
||||
<div>Jordan Soltman</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
+11
-11
@@ -147,7 +147,7 @@ The following options are supported on any column object you can pass to `column
|
||||
- If a function/component is passed, it will be used for formatting the header value, eg. You can use a `Header` function to dynamically format the header using any table or column state.
|
||||
- `Cell: Function | React.Component => JSX`
|
||||
- Optional
|
||||
- Defaults to `({ cell: { value } }) => value`
|
||||
- Defaults to `({ cell: { value } }) => String(value)`
|
||||
- Receives the table instance and cell model as props
|
||||
- Must return valid JSX
|
||||
- This function (or component) is primarily used for formatting the column value, eg. If your column accessor returns a date object, you can use a `Cell` function to format that date to a readable format.
|
||||
@@ -390,7 +390,7 @@ The following options are supported via the main options object passed to `useTa
|
||||
- `disableMultiSort: Bool`
|
||||
- Disables multi-sorting for the entire table.
|
||||
- `isMultiSortEvent: Function`
|
||||
- Allows to override default multisort behaviour(i.e. multisort applies when shift key is presssed), if this function is provided then returned boolean value from this function will make decision whether newly applied sort action will be considered as multisort or not.
|
||||
- Allows to override default multisort behaviour(i.e. multisort applies when shift key is pressed), if this function is provided then returned boolean value from this function will make decision whether newly applied sort action will be considered as multisort or not.
|
||||
- Receives `event` as argument.
|
||||
- `maxMultiSortColCount: Number`
|
||||
- Limit on max number of columns for multisort, e.g. if set to 3, and suppose table is sorted by `[A, B, C]` and then clicking `D` for sorting should result in table sorted by `[B, C , D]`
|
||||
@@ -399,7 +399,7 @@ The following options are supported via the main options object passed to `useTa
|
||||
- `disableMultiRemove: Bool`
|
||||
- If true, the un-sorted state will not be available to multi-sorted columns.
|
||||
- `orderByFn: Function`
|
||||
- Must be **memoizd**
|
||||
- Must be **memoized**
|
||||
- Defaults to the built-in [default orderBy function](TODO)
|
||||
- This function is responsible for composing multiple sorting functions together for multi-sorting, and also handles both the directional sorting and stable-sorting tie breaking. Rarely would you want to override this function unless you have a very advanced use-case that requires it.
|
||||
- `sortTypes: Object<sortKey: sortType>`
|
||||
@@ -413,7 +413,7 @@ The following options are supported on any `Column` object passed to the `column
|
||||
|
||||
- `disableSorting: Bool`
|
||||
- Optional
|
||||
- Defualts to `false`
|
||||
- Defaults to `false`
|
||||
- If set to `true`, the sorting for this column will be disabled
|
||||
- `sortDescFirst: Bool`
|
||||
- Optional
|
||||
@@ -427,9 +427,9 @@ The following options are supported on any `Column` object passed to the `column
|
||||
- `sortType: String | Function`
|
||||
- Used to compare 2 rows of data and order them correctly.
|
||||
- If a **function** is passed, it must be **memoized**
|
||||
- Defaults to [`alphanumeric`](TODO)
|
||||
- String options: `basic`, `datettime`, `alphanumeric`. Defaults to [`alphanumeric`](TODO).
|
||||
- The resolved function from the this string/function will be used to sort the this column's data.
|
||||
- If a `string` is passed, the function with that name located on either the custom `sortTypes` option or the built-in sorting types object will be used. If
|
||||
- If a `string` is passed, the function with that name located on either the custom `sortTypes` option or the built-in sorting types object will be used.
|
||||
- If a `function` is passed, it will be used.
|
||||
- For more information on sort types, see [Sorting](TODO)
|
||||
|
||||
@@ -934,18 +934,18 @@ function App() {
|
||||
- Plugin Hook
|
||||
- Optional
|
||||
|
||||
`useExpanded` is the hook that implements **row expanding**. It is most often used with `useGroupBy` to expand grouped rows or on its own with nested `subRows` in tree-li,ke `data` sets, but is not limited to these use-cases. It supports expanding rows both via internal table state and also via a hard-coded key on the raw row model.
|
||||
`useExpanded` is the hook that implements **row expanding**. It is most often used with `useGroupBy` to expand grouped rows or on its own with nested `subRows` in tree-like `data` sets, but is not limited to these use-cases. It supports expanding rows both via internal table state and also via a hard-coded key on the raw row model.
|
||||
|
||||
### Table Options
|
||||
|
||||
The following options are supported via the main options object passed to `useTable(options)`
|
||||
|
||||
- `state[0].expanded: Object<[pathIndex]: Boolean | ExpandedStateObject>`
|
||||
- `state[0].expanded: Array<pathKey: String>`
|
||||
- Optional
|
||||
- Must be **memoized**
|
||||
- An nested object of expanded paths.
|
||||
- A `pathIndex` can be set as the key and its value set to `true` to expand that row's subRows into view. For example, if `{ '3': true }` was passed as the `expanded` state, the **4th row in the original data array** would be expanded.
|
||||
- For nested expansion, you may **use another object** instead of a Boolean to expand sub rows. For example, if `{ '3': { '5' : true }}` was passed as the `expanded` state, then the **6th subRow of the 4th row and the 4th row of the original data array** would be expanded.
|
||||
- An array of expanded path keys.
|
||||
- If a row's path key (`row.path.join('.')`) is present in this array, that row will have an expanded state. For example, if `['3']` was passed as the `expanded` state, the **4th row in the original data array** would be expanded.
|
||||
- For nested expansion, you may **join the row path with a `.`** to expand sub rows. For example, if `['3', '3.5']` was passed as the `expanded` state, then the **6th subRow of the 4th row and also the 4th row of the original data array** would be expanded.
|
||||
- This information is stored in state since the table is allowed to manipulate the filter through user interaction.
|
||||
- `getSubRows: Function(row, relativeIndex) => Rows[]`
|
||||
- Optional
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"presets": ["react-app"],
|
||||
"plugins": ["styled-components"]
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
SKIP_PREFLIGHT_CHECK=true
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": ["react-app", "prettier"],
|
||||
"rules": {
|
||||
// "eqeqeq": 0,
|
||||
// "jsx-a11y/anchor-is-valid": 0
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
@@ -1,29 +0,0 @@
|
||||
const path = require('path')
|
||||
const resolveFrom = require('resolve-from')
|
||||
|
||||
const fixLinkedDependencies = config => {
|
||||
config.resolve = {
|
||||
...config.resolve,
|
||||
alias: {
|
||||
...config.resolve.alias,
|
||||
react$: resolveFrom(path.resolve('node_modules'), 'react'),
|
||||
'react-dom$': resolveFrom(path.resolve('node_modules'), 'react-dom'),
|
||||
},
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
const includeSrcDirectory = config => {
|
||||
config.resolve = {
|
||||
...config.resolve,
|
||||
modules: [path.resolve('src'), ...config.resolve.modules],
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
['use-babel-config', '.babelrc'],
|
||||
['use-eslint-config', '.eslintrc'],
|
||||
fixLinkedDependencies,
|
||||
// includeSrcDirectory,
|
||||
]
|
||||
@@ -1,6 +0,0 @@
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app) and Rescripts.
|
||||
|
||||
You can:
|
||||
|
||||
- [Open this example in a new CodeSandbox](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/basic)
|
||||
- `yarn` and `yarn start` to run and edit the example
|
||||
@@ -1,38 +0,0 @@
|
||||
{
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "rescripts start",
|
||||
"build": "rescripts build",
|
||||
"test": "rescripts test",
|
||||
"eject": "rescripts eject"
|
||||
},
|
||||
"dependencies": {
|
||||
"framer-motion": "^1.6.3",
|
||||
"match-sorter": "^4.0.1",
|
||||
"namor": "^1.1.2",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-scripts": "3.0.1",
|
||||
"react-spring": "^8.0.27",
|
||||
"react-table": "next",
|
||||
"styled-components": "^4.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rescripts/cli": "^0.0.11",
|
||||
"@rescripts/rescript-use-babel-config": "^0.0.8",
|
||||
"@rescripts/rescript-use-eslint-config": "^0.0.9",
|
||||
"babel-eslint": "10.0.1"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 3.8 KiB |
@@ -1,38 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#000000" />
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is installed on a
|
||||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
Only files inside the `public` folder can be referenced from the HTML.
|
||||
|
||||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||
work correctly both with client-side routing and a non-root public URL.
|
||||
Learn how to configure a non-root public URL by running `npm run build`.
|
||||
-->
|
||||
<title>React App</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||
<div id="root"></div>
|
||||
<!--
|
||||
This HTML file is a template.
|
||||
If you open it directly in the browser, you will see an empty page.
|
||||
|
||||
You can add webfonts, meta tags, or analytics to this file.
|
||||
The build step will place the bundled scripts into the <body> tag.
|
||||
|
||||
To begin the development, run `npm start` or `yarn start`.
|
||||
To create a production bundle, use `npm run build` or `yarn build`.
|
||||
-->
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"short_name": "React App",
|
||||
"name": "Create React App Sample",
|
||||
"icons": [
|
||||
{
|
||||
"src": "favicon.ico",
|
||||
"sizes": "64x64 32x32 24x24 16x16",
|
||||
"type": "image/x-icon"
|
||||
}
|
||||
],
|
||||
"start_url": ".",
|
||||
"display": "standalone",
|
||||
"theme_color": "#000000",
|
||||
"background_color": "#ffffff"
|
||||
}
|
||||
@@ -1,382 +0,0 @@
|
||||
import React from 'react'
|
||||
import styled from 'styled-components'
|
||||
import { useTable, useSortBy, useFilters, useColumnOrder } from 'react-table'
|
||||
import { motion, AnimatePresence } from 'framer-motion'
|
||||
import matchSorter from 'match-sorter'
|
||||
|
||||
import makeData from './makeData'
|
||||
|
||||
const Styles = styled.div`
|
||||
padding: 1rem;
|
||||
|
||||
table {
|
||||
border-spacing: 0;
|
||||
border: 1px solid black;
|
||||
|
||||
tr {
|
||||
:last-child {
|
||||
td {
|
||||
border-bottom: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
th,
|
||||
td {
|
||||
margin: 0;
|
||||
padding: 0.5rem;
|
||||
border-bottom: 1px solid black;
|
||||
border-right: 1px solid black;
|
||||
background: white;
|
||||
|
||||
:last-child {
|
||||
border-right: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
// Define a default UI for filtering
|
||||
function DefaultColumnFilter({
|
||||
column: { filterValue, preFilteredRows, setFilter },
|
||||
}) {
|
||||
const count = preFilteredRows.length
|
||||
|
||||
return (
|
||||
<input
|
||||
value={filterValue || ''}
|
||||
onChange={e => {
|
||||
setFilter(e.target.value || undefined) // Set undefined to remove the filter entirely
|
||||
}}
|
||||
placeholder={`Search ${count} records...`}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// This is a custom filter UI for selecting
|
||||
// a unique option from a list
|
||||
function SelectColumnFilter({
|
||||
column: { filterValue, setFilter, preFilteredRows, id },
|
||||
}) {
|
||||
// Calculate the options for filtering
|
||||
// using the preFilteredRows
|
||||
const options = React.useMemo(() => {
|
||||
const options = new Set()
|
||||
preFilteredRows.forEach(row => {
|
||||
options.add(row.values[id])
|
||||
})
|
||||
return [...options.values()]
|
||||
}, [id, preFilteredRows])
|
||||
|
||||
// Render a multi-select box
|
||||
return (
|
||||
<select
|
||||
value={filterValue}
|
||||
onChange={e => {
|
||||
setFilter(e.target.value || undefined)
|
||||
}}
|
||||
>
|
||||
<option value="">All</option>
|
||||
{options.map((option, i) => (
|
||||
<option key={i} value={option}>
|
||||
{option}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)
|
||||
}
|
||||
|
||||
// This is a custom filter UI that uses a
|
||||
// slider to set the filter value between a column's
|
||||
// min and max values
|
||||
function SliderColumnFilter({
|
||||
column: { filterValue, setFilter, preFilteredRows, id },
|
||||
}) {
|
||||
// Calculate the min and max
|
||||
// using the preFilteredRows
|
||||
|
||||
const [min, max] = React.useMemo(() => {
|
||||
let min = preFilteredRows.length ? preFilteredRows[0].values[id] : 0
|
||||
let max = preFilteredRows.length ? preFilteredRows[0].values[id] : 0
|
||||
preFilteredRows.forEach(row => {
|
||||
min = Math.min(row.values[id], min)
|
||||
max = Math.max(row.values[id], max)
|
||||
})
|
||||
return [min, max]
|
||||
}, [id, preFilteredRows])
|
||||
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
type="range"
|
||||
min={min}
|
||||
max={max}
|
||||
value={filterValue || min}
|
||||
onChange={e => {
|
||||
setFilter(parseInt(e.target.value, 10))
|
||||
}}
|
||||
/>
|
||||
<button onClick={() => setFilter(undefined)}>Off</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
// This is a custom UI for our 'between' or number range
|
||||
// filter. It uses two number boxes and filters rows to
|
||||
// ones that have values between the two
|
||||
function NumberRangeColumnFilter({
|
||||
column: { filterValue = [], preFilteredRows, setFilter, id },
|
||||
}) {
|
||||
const [min, max] = React.useMemo(() => {
|
||||
let min = preFilteredRows.length ? preFilteredRows[0].values[id] : 0
|
||||
let max = preFilteredRows.length ? preFilteredRows[0].values[id] : 0
|
||||
preFilteredRows.forEach(row => {
|
||||
min = Math.min(row.values[id], min)
|
||||
max = Math.max(row.values[id], max)
|
||||
})
|
||||
return [min, max]
|
||||
}, [id, preFilteredRows])
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
}}
|
||||
>
|
||||
<input
|
||||
value={filterValue[0] || ''}
|
||||
type="number"
|
||||
onChange={e => {
|
||||
const val = e.target.value
|
||||
setFilter((old = []) => [val ? parseInt(val, 10) : undefined, old[1]])
|
||||
}}
|
||||
placeholder={`Min (${min})`}
|
||||
style={{
|
||||
width: '70px',
|
||||
marginRight: '0.5rem',
|
||||
}}
|
||||
/>
|
||||
to
|
||||
<input
|
||||
value={filterValue[1] || ''}
|
||||
type="number"
|
||||
onChange={e => {
|
||||
const val = e.target.value
|
||||
setFilter((old = []) => [old[0], val ? parseInt(val, 10) : undefined])
|
||||
}}
|
||||
placeholder={`Max (${max})`}
|
||||
style={{
|
||||
width: '70px',
|
||||
marginLeft: '0.5rem',
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function fuzzyTextFilterFn(rows, id, filterValue) {
|
||||
return matchSorter(rows, filterValue, { keys: [row => row.values[id]] })
|
||||
}
|
||||
|
||||
// Let the table remove the filter if the string is empty
|
||||
fuzzyTextFilterFn.autoRemove = val => !val
|
||||
|
||||
function shuffle(arr) {
|
||||
arr = [...arr]
|
||||
const shuffled = []
|
||||
while (arr.length) {
|
||||
const rand = Math.floor(Math.random() * arr.length)
|
||||
shuffled.push(arr.splice(rand, 1)[0])
|
||||
}
|
||||
return shuffled
|
||||
}
|
||||
|
||||
function Table({ columns, data }) {
|
||||
const defaultColumn = React.useMemo(
|
||||
() => ({
|
||||
// Let's set up our default Filter UI
|
||||
Filter: DefaultColumnFilter,
|
||||
}),
|
||||
[]
|
||||
)
|
||||
|
||||
const {
|
||||
getTableProps,
|
||||
headerGroups,
|
||||
rows,
|
||||
flatColumns,
|
||||
prepareRow,
|
||||
setColumnOrder,
|
||||
state,
|
||||
} = useTable(
|
||||
{
|
||||
columns,
|
||||
data,
|
||||
defaultColumn,
|
||||
},
|
||||
useColumnOrder,
|
||||
useFilters,
|
||||
useSortBy
|
||||
)
|
||||
|
||||
const spring = React.useMemo(
|
||||
() => ({
|
||||
type: 'spring',
|
||||
damping: 50,
|
||||
stiffness: 100,
|
||||
}),
|
||||
[]
|
||||
)
|
||||
|
||||
const randomizeColumns = () => {
|
||||
setColumnOrder(shuffle(flatColumns.map(d => d.id)))
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button onClick={() => randomizeColumns({})}>Randomize Columns</button>
|
||||
<table {...getTableProps()}>
|
||||
<thead>
|
||||
{headerGroups.map((headerGroup, i) => (
|
||||
<tr {...headerGroup.getHeaderGroupProps()}>
|
||||
{headerGroup.headers.map(column => (
|
||||
<motion.th
|
||||
{...column.getHeaderProps({
|
||||
layoutTransition: spring,
|
||||
style: {
|
||||
minWidth: column.minWidth,
|
||||
},
|
||||
})}
|
||||
>
|
||||
<div {...column.getSortByToggleProps()}>
|
||||
{column.render('Header')}
|
||||
<span>
|
||||
{column.isSorted
|
||||
? column.isSortedDesc
|
||||
? ' 🔽'
|
||||
: ' 🔼'
|
||||
: ''}
|
||||
</span>
|
||||
</div>
|
||||
<div>{column.canFilter ? column.render('Filter') : null}</div>
|
||||
</motion.th>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</thead>
|
||||
<tbody>
|
||||
<AnimatePresence>
|
||||
{rows.slice(0, 10).map(
|
||||
(row, i) =>
|
||||
prepareRow(row) || (
|
||||
<motion.tr
|
||||
{...row.getRowProps({
|
||||
layoutTransition: spring,
|
||||
exit: { opacity: 0, maxHeight: 0 },
|
||||
})}
|
||||
>
|
||||
{row.cells.map((cell, i) => {
|
||||
return (
|
||||
<motion.td
|
||||
{...cell.getCellProps({
|
||||
layoutTransition: spring,
|
||||
})}
|
||||
>
|
||||
{cell.render('Cell')}
|
||||
</motion.td>
|
||||
)
|
||||
})}
|
||||
</motion.tr>
|
||||
)
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</tbody>
|
||||
</table>
|
||||
<pre>
|
||||
<code>{JSON.stringify(state[0], null, 2)}</code>
|
||||
</pre>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
// Define a custom filter filter function!
|
||||
function filterGreaterThan(rows, id, filterValue) {
|
||||
return rows.filter(row => {
|
||||
const rowValue = row.values[id]
|
||||
return rowValue >= filterValue
|
||||
})
|
||||
}
|
||||
|
||||
// This is an autoRemove method on the filter function that
|
||||
// when given the new filter value and returns true, the filter
|
||||
// will be automatically removed. Normally this is just an undefined
|
||||
// check, but here, we want to remove the filter if it's not a number
|
||||
filterGreaterThan.autoRemove = val => typeof val !== 'number'
|
||||
|
||||
function App() {
|
||||
const columns = React.useMemo(
|
||||
() => [
|
||||
{
|
||||
Header: 'Name',
|
||||
columns: [
|
||||
{
|
||||
Header: 'First Name',
|
||||
accessor: 'firstName',
|
||||
minWidth: 150,
|
||||
},
|
||||
{
|
||||
Header: 'Last Name',
|
||||
accessor: 'lastName',
|
||||
minWidth: 150,
|
||||
// Use our custom `fuzzyText` filter on this column
|
||||
filter: 'fuzzyText',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
Header: 'Info',
|
||||
columns: [
|
||||
{
|
||||
Header: 'Age',
|
||||
accessor: 'age',
|
||||
minWidth: 150,
|
||||
Filter: SliderColumnFilter,
|
||||
filter: 'equals',
|
||||
},
|
||||
{
|
||||
Header: 'Visits',
|
||||
accessor: 'visits',
|
||||
minWidth: 150,
|
||||
Filter: NumberRangeColumnFilter,
|
||||
filter: 'between',
|
||||
},
|
||||
{
|
||||
Header: 'Status',
|
||||
accessor: 'status',
|
||||
minWidth: 150,
|
||||
Filter: SelectColumnFilter,
|
||||
filter: 'includes',
|
||||
},
|
||||
{
|
||||
Header: 'Profile Progress',
|
||||
accessor: 'progress',
|
||||
minWidth: 150,
|
||||
Filter: SliderColumnFilter,
|
||||
filter: filterGreaterThan,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
[]
|
||||
)
|
||||
|
||||
const data = React.useMemo(() => makeData(100), [])
|
||||
|
||||
return (
|
||||
<Styles>
|
||||
<Table columns={columns} data={data} />
|
||||
</Styles>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
||||
@@ -1,9 +0,0 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import App from './App'
|
||||
|
||||
it('renders without crashing', () => {
|
||||
const div = document.createElement('div')
|
||||
ReactDOM.render(<App />, div)
|
||||
ReactDOM.unmountComponentAtNode(div)
|
||||
})
|
||||
@@ -1,13 +0,0 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
monospace;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import './index.css'
|
||||
import App from './App'
|
||||
import * as serviceWorker from './serviceWorker'
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'))
|
||||
|
||||
// If you want your app to work offline and load faster, you can change
|
||||
// unregister() to register() below. Note this comes with some pitfalls.
|
||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||
serviceWorker.unregister()
|
||||
@@ -1,40 +0,0 @@
|
||||
import namor from 'namor'
|
||||
|
||||
const range = len => {
|
||||
const arr = []
|
||||
for (let i = 0; i < len; i++) {
|
||||
arr.push(i)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
const newPerson = () => {
|
||||
const statusChance = Math.random()
|
||||
return {
|
||||
firstName: namor.generate({ words: 1, numbers: 0 }),
|
||||
lastName: namor.generate({ words: 1, numbers: 0 }),
|
||||
age: Math.floor(Math.random() * 30),
|
||||
visits: Math.floor(Math.random() * 100),
|
||||
progress: Math.floor(Math.random() * 100),
|
||||
status:
|
||||
statusChance > 0.66
|
||||
? 'relationship'
|
||||
: statusChance > 0.33
|
||||
? 'complicated'
|
||||
: 'single',
|
||||
}
|
||||
}
|
||||
|
||||
export default function makeData(...lens) {
|
||||
const makeDataLevel = (depth = 0) => {
|
||||
const len = lens[depth]
|
||||
return range(len).map(d => {
|
||||
return {
|
||||
...newPerson(),
|
||||
subRows: lens[depth + 1] ? makeDataLevel(depth + 1) : undefined,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return makeDataLevel()
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
// This optional code is used to register a service worker.
|
||||
// register() is not called by default.
|
||||
|
||||
// This lets the app load faster on subsequent visits in production, and gives
|
||||
// it offline capabilities. However, it also means that developers (and users)
|
||||
// will only see deployed updates on subsequent visits to a page, after all the
|
||||
// existing tabs open on the page have been closed, since previously cached
|
||||
// resources are updated in the background.
|
||||
|
||||
// To learn more about the benefits of this model and instructions on how to
|
||||
// opt-in, read https://bit.ly/CRA-PWA
|
||||
|
||||
const isLocalhost = Boolean(
|
||||
window.location.hostname === 'localhost' ||
|
||||
// [::1] is the IPv6 localhost address.
|
||||
window.location.hostname === '[::1]' ||
|
||||
// 127.0.0.1/8 is considered localhost for IPv4.
|
||||
window.location.hostname.match(
|
||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||
)
|
||||
)
|
||||
|
||||
export function register(config) {
|
||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||
// The URL constructor is available in all browsers that support SW.
|
||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href)
|
||||
if (publicUrl.origin !== window.location.origin) {
|
||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||
// from what our page is served on. This might happen if a CDN is used to
|
||||
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
||||
return
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`
|
||||
|
||||
if (isLocalhost) {
|
||||
// This is running on localhost. Let's check if a service worker still exists or not.
|
||||
checkValidServiceWorker(swUrl, config)
|
||||
|
||||
// Add some additional logging to localhost, pointing developers to the
|
||||
// service worker/PWA documentation.
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log(
|
||||
'This web app is being served cache-first by a service ' +
|
||||
'worker. To learn more, visit https://bit.ly/CRA-PWA'
|
||||
)
|
||||
})
|
||||
} else {
|
||||
// Is not localhost. Just register service worker
|
||||
registerValidSW(swUrl, config)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function registerValidSW(swUrl, config) {
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(registration => {
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing
|
||||
if (installingWorker == null) {
|
||||
return
|
||||
}
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the updated precached content has been fetched,
|
||||
// but the previous service worker will still serve the older
|
||||
// content until all client tabs are closed.
|
||||
console.log(
|
||||
'New content is available and will be used when all ' +
|
||||
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
|
||||
)
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onUpdate) {
|
||||
config.onUpdate(registration)
|
||||
}
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a
|
||||
// "Content is cached for offline use." message.
|
||||
console.log('Content is cached for offline use.')
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onSuccess) {
|
||||
config.onSuccess(registration)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during service worker registration:', error)
|
||||
})
|
||||
}
|
||||
|
||||
function checkValidServiceWorker(swUrl, config) {
|
||||
// Check if the service worker can be found. If it can't reload the page.
|
||||
fetch(swUrl)
|
||||
.then(response => {
|
||||
// Ensure service worker exists, and that we really are getting a JS file.
|
||||
const contentType = response.headers.get('content-type')
|
||||
if (
|
||||
response.status === 404 ||
|
||||
(contentType != null && contentType.indexOf('javascript') === -1)
|
||||
) {
|
||||
// No service worker found. Probably a different app. Reload the page.
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister().then(() => {
|
||||
window.location.reload()
|
||||
})
|
||||
})
|
||||
} else {
|
||||
// Service worker found. Proceed as normal.
|
||||
registerValidSW(swUrl, config)
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.log(
|
||||
'No internet connection found. App is running in offline mode.'
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
export function unregister() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister()
|
||||
})
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -50,9 +50,6 @@ function Table({ columns: userColumns, data }) {
|
||||
|
||||
return (
|
||||
<>
|
||||
<pre>
|
||||
<code>{JSON.stringify({ expanded }, null, 2)}</code>
|
||||
</pre>
|
||||
<table {...getTableProps()}>
|
||||
<thead>
|
||||
{headerGroups.map(headerGroup => (
|
||||
@@ -80,6 +77,9 @@ function Table({ columns: userColumns, data }) {
|
||||
</table>
|
||||
<br />
|
||||
<div>Showing the first 20 results of {rows.length} rows</div>
|
||||
<pre>
|
||||
<code>{JSON.stringify({ expanded }, null, 2)}</code>
|
||||
</pre>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ function MyTable() {
|
||||
data,
|
||||
columns,
|
||||
},
|
||||
- useSortBy
|
||||
+ useSortBy
|
||||
)
|
||||
|
||||
@@ -60,3 +59,54 @@ function MyTable() {
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
By default, the sorting will be `alphanumeric`. This can be changed in your `column` object.
|
||||
Other options include `basic` and `datettime`.
|
||||
Note that if you're planning on sorting numbers between 0 and 1, `basic` sorting will be more accurate.
|
||||
|
||||
More information can be found in the [API Docs](/docs/api.md#useSortBy)
|
||||
|
||||
````diff
|
||||
const columns = React.useMemo(
|
||||
() => [
|
||||
{
|
||||
Header: 'Name',
|
||||
columns: [
|
||||
{
|
||||
Header: 'First Name',
|
||||
accessor: 'firstName',
|
||||
},
|
||||
{
|
||||
Header: 'Last Name',
|
||||
accessor: 'lastName',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
Header: 'Info',
|
||||
columns: [
|
||||
{
|
||||
Header: 'Age',
|
||||
accessor: 'age',
|
||||
+ sortType: 'basic'
|
||||
},
|
||||
{
|
||||
Header: 'Visits',
|
||||
accessor: 'visits',
|
||||
+ sortType: 'basic'
|
||||
},
|
||||
{
|
||||
Header: 'Status',
|
||||
accessor: 'status',
|
||||
},
|
||||
{
|
||||
Header: 'Profile Progress',
|
||||
accessor: 'progress',
|
||||
+ sortType: 'basic'
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
[]
|
||||
)```
|
||||
````
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-table",
|
||||
"version": "7.0.0-alpha.32",
|
||||
"version": "7.0.0-alpha.34",
|
||||
"description": "A fast, lightweight, opinionated table and datagrid built on React",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/tannerlinsley/react-table#readme",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as utils from './utils'
|
||||
export { utils }
|
||||
export { defaultColumn } from './utils'
|
||||
export { useTable } from './hooks/useTable'
|
||||
export { useTableState, defaultState } from './hooks/useTableState'
|
||||
export { useExpanded } from './plugin-hooks/useExpanded'
|
||||
|
||||
@@ -10,10 +10,10 @@ Snapshot Diff:
|
||||
<pre>
|
||||
<code>
|
||||
{
|
||||
- "expanded": {}
|
||||
+ "expanded": {
|
||||
+ "0": true
|
||||
+ }
|
||||
- "expanded": []
|
||||
+ "expanded": [
|
||||
+ "0"
|
||||
+ ]
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
@@ -220,22 +220,21 @@ Snapshot Diff:
|
||||
- First value
|
||||
+ Second value
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
@@ -1,11 +1,12 @@
|
||||
<DocumentFragment>
|
||||
<pre>
|
||||
<code>
|
||||
{
|
||||
"expanded": {
|
||||
- "0": true
|
||||
+ "0": {
|
||||
+ "0": true
|
||||
+ }
|
||||
}
|
||||
"expanded": [
|
||||
- "0"
|
||||
+ "0",
|
||||
+ "0.0"
|
||||
]
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
<table
|
||||
@@ -127,10 +129,53 @@
|
||||
@@ -127,11 +128,54 @@
|
||||
<td
|
||||
class=""
|
||||
>
|
||||
@@ -281,15 +280,16 @@ Snapshot Diff:
|
||||
+ >
|
||||
+ <td
|
||||
+ class=""
|
||||
+ >
|
||||
>
|
||||
+ <span
|
||||
+ style="cursor: pointer; padding-left: 4rem;"
|
||||
>
|
||||
+ >
|
||||
👉
|
||||
</span>
|
||||
</td>
|
||||
<td
|
||||
@@ -160,10 +205,139 @@
|
||||
class=""
|
||||
@@ -160,10 +204,139 @@
|
||||
</td>
|
||||
<td
|
||||
class=""
|
||||
@@ -436,22 +436,21 @@ Snapshot Diff:
|
||||
- First value
|
||||
+ Second value
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
@@ -2,11 +2,12 @@
|
||||
<pre>
|
||||
<code>
|
||||
{
|
||||
"expanded": {
|
||||
"0": {
|
||||
- "0": true
|
||||
+ "0": {
|
||||
+ "0": true
|
||||
+ }
|
||||
}
|
||||
}
|
||||
"expanded": [
|
||||
"0",
|
||||
- "0.0"
|
||||
+ "0.0",
|
||||
+ "0.0.0"
|
||||
]
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
@@ -172,10 +174,53 @@
|
||||
<table
|
||||
@@ -171,11 +172,54 @@
|
||||
<td
|
||||
class=""
|
||||
>
|
||||
@@ -497,15 +496,16 @@ Snapshot Diff:
|
||||
+ >
|
||||
+ <td
|
||||
+ class=""
|
||||
+ >
|
||||
>
|
||||
+ <span
|
||||
+ style="cursor: pointer; padding-left: 6rem;"
|
||||
>
|
||||
+ >
|
||||
👉
|
||||
</span>
|
||||
</td>
|
||||
<td
|
||||
@@ -205,10 +250,139 @@
|
||||
class=""
|
||||
@@ -204,10 +248,139 @@
|
||||
</td>
|
||||
<td
|
||||
class=""
|
||||
@@ -652,22 +652,21 @@ Snapshot Diff:
|
||||
- First value
|
||||
+ Second value
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
@@ -3,11 +3,12 @@
|
||||
<code>
|
||||
{
|
||||
"expanded": {
|
||||
"0": {
|
||||
"0": {
|
||||
- "0": true
|
||||
+ "0": {
|
||||
+ "0": true
|
||||
+ }
|
||||
}
|
||||
}
|
||||
}
|
||||
"expanded": [
|
||||
"0",
|
||||
"0.0",
|
||||
- "0.0.0"
|
||||
+ "0.0.0",
|
||||
+ "0.0.0.0"
|
||||
]
|
||||
}
|
||||
</code>
|
||||
@@ -218,11 +220,11 @@
|
||||
</pre>
|
||||
<table
|
||||
@@ -216,11 +217,11 @@
|
||||
class=""
|
||||
>
|
||||
<span
|
||||
@@ -680,7 +679,7 @@ Snapshot Diff:
|
||||
<td
|
||||
class=""
|
||||
>
|
||||
@@ -250,10 +252,30 @@
|
||||
@@ -248,10 +249,30 @@
|
||||
</td>
|
||||
<td
|
||||
class=""
|
||||
@@ -712,3 +711,587 @@ Snapshot Diff:
|
||||
class=""
|
||||
>
|
||||
`;
|
||||
|
||||
exports[`renders an expandable table 5`] = `
|
||||
Snapshot Diff:
|
||||
- First value
|
||||
+ Second value
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
<DocumentFragment>
|
||||
<pre>
|
||||
<code>
|
||||
{
|
||||
- "expanded": [
|
||||
- "0",
|
||||
- "0.0",
|
||||
- "0.0.0",
|
||||
- "0.0.0.0"
|
||||
- ]
|
||||
+ "expanded": []
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
<table
|
||||
class=""
|
||||
@@ -88,140 +83,11 @@
|
||||
class=""
|
||||
>
|
||||
<span
|
||||
style="cursor: pointer; padding-left: 0rem;"
|
||||
>
|
||||
- 👇
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- tanner
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- linsley
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 29
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 100
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- In Relationship
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 50
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 2rem;"
|
||||
- >
|
||||
- 👇
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- tanner
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- linsley
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 29
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 100
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- In Relationship
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 50
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 4rem;"
|
||||
- >
|
||||
- 👇
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- tanner
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- linsley
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 29
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 100
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- In Relationship
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 50
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 6rem;"
|
||||
- >
|
||||
- 👇
|
||||
+ 👉
|
||||
</span>
|
||||
</td>
|
||||
<td
|
||||
class=""
|
||||
>
|
||||
@@ -249,417 +115,10 @@
|
||||
</td>
|
||||
<td
|
||||
class=""
|
||||
>
|
||||
50
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr>
|
||||
- <td
|
||||
- colspan="7"
|
||||
- >
|
||||
- <pre>
|
||||
- <code>
|
||||
- {
|
||||
- "values": {
|
||||
- "firstName": "tanner",
|
||||
- "lastName": "linsley",
|
||||
- "age": 29,
|
||||
- "visits": 100,
|
||||
- "status": "In Relationship",
|
||||
- "progress": 50
|
||||
- }
|
||||
- }
|
||||
- </code>
|
||||
- </pre>
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 6rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- derek
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- perkins
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 40
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 40
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- Single
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 80
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 6rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- joe
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- bergevin
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 45
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 20
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- Complicated
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 10
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 6rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- jaylen
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- linsley
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 26
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 99
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- In Relationship
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 70
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 4rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- derek
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- perkins
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 40
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 40
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- Single
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 80
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 4rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- joe
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- bergevin
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 45
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 20
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- Complicated
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 10
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 4rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- jaylen
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- linsley
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 26
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 99
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- In Relationship
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 70
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 2rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- derek
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- perkins
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 40
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 40
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- Single
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 80
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 2rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- joe
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- bergevin
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 45
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 20
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- Complicated
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 10
|
||||
- </td>
|
||||
- </tr>
|
||||
- <tr
|
||||
- class=""
|
||||
- >
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- <span
|
||||
- style="cursor: pointer; padding-left: 2rem;"
|
||||
- >
|
||||
- 👉
|
||||
- </span>
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- jaylen
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- linsley
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 26
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 99
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- In Relationship
|
||||
- </td>
|
||||
- <td
|
||||
- class=""
|
||||
- >
|
||||
- 70
|
||||
</td>
|
||||
</tr>
|
||||
<tr
|
||||
class=""
|
||||
>
|
||||
`;
|
||||
|
||||
@@ -178,29 +178,37 @@ test('renders an expandable table', () => {
|
||||
|
||||
let expandButtons = getAllByText('👉')
|
||||
|
||||
const beforeGrouping = asFragment()
|
||||
const before = asFragment()
|
||||
|
||||
fireEvent.click(expandButtons[0])
|
||||
|
||||
const afterGrouping1 = asFragment()
|
||||
const after1 = asFragment()
|
||||
|
||||
expandButtons = getAllByText('👉')
|
||||
fireEvent.click(expandButtons[0])
|
||||
|
||||
const afterGrouping2 = asFragment()
|
||||
const after2 = asFragment()
|
||||
|
||||
expandButtons = getAllByText('👉')
|
||||
fireEvent.click(expandButtons[0])
|
||||
|
||||
const afterGrouping3 = asFragment()
|
||||
const after3 = asFragment()
|
||||
|
||||
expandButtons = getAllByText('👉')
|
||||
fireEvent.click(expandButtons[0])
|
||||
|
||||
const afterGrouping4 = asFragment()
|
||||
const after4 = asFragment()
|
||||
|
||||
expect(beforeGrouping).toMatchDiffSnapshot(afterGrouping1)
|
||||
expect(afterGrouping1).toMatchDiffSnapshot(afterGrouping2)
|
||||
expect(afterGrouping2).toMatchDiffSnapshot(afterGrouping3)
|
||||
expect(afterGrouping3).toMatchDiffSnapshot(afterGrouping4)
|
||||
expandButtons = getAllByText('👇')
|
||||
expandButtons.reverse().forEach(button => {
|
||||
fireEvent.click(button)
|
||||
})
|
||||
|
||||
const after5 = asFragment()
|
||||
|
||||
expect(before).toMatchDiffSnapshot(after1)
|
||||
expect(after1).toMatchDiffSnapshot(after2)
|
||||
expect(after2).toMatchDiffSnapshot(after3)
|
||||
expect(after3).toMatchDiffSnapshot(after4)
|
||||
expect(after4).toMatchDiffSnapshot(after5)
|
||||
})
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
import { useMemo } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import {
|
||||
getBy,
|
||||
getFirstDefined,
|
||||
setBy,
|
||||
mergeProps,
|
||||
applyPropHooks,
|
||||
} from '../utils'
|
||||
import { mergeProps, applyPropHooks } from '../utils'
|
||||
import { addActions, actions } from '../actions'
|
||||
import { defaultState } from '../hooks/useTableState'
|
||||
|
||||
defaultState.expanded = {}
|
||||
defaultState.expanded = []
|
||||
|
||||
addActions('toggleExpanded', 'useExpanded')
|
||||
|
||||
@@ -40,13 +34,24 @@ function useMain(instance) {
|
||||
} = instance
|
||||
|
||||
const toggleExpandedByPath = (path, set) => {
|
||||
const key = path.join('.')
|
||||
|
||||
return setState(old => {
|
||||
const { expanded } = old
|
||||
const existing = getBy(expanded, path)
|
||||
set = getFirstDefined(set, existing ? undefined : true)
|
||||
const exists = old.expanded.includes(key)
|
||||
const shouldExist = typeof set !== 'undefined' ? set : !exists
|
||||
let newExpanded = new Set(old.expanded)
|
||||
|
||||
if (!exists && shouldExist) {
|
||||
newExpanded.add(key)
|
||||
} else if (exists && !shouldExist) {
|
||||
newExpanded.delete(key)
|
||||
} else {
|
||||
return old
|
||||
}
|
||||
|
||||
return {
|
||||
...old,
|
||||
expanded: setBy(expanded, path, set),
|
||||
expanded: [...newExpanded.values()],
|
||||
}
|
||||
}, actions.toggleExpanded)
|
||||
}
|
||||
@@ -81,9 +86,10 @@ function useMain(instance) {
|
||||
// Here we do some mutation, but it's the last stage in the
|
||||
// immutable process so this is safe
|
||||
const handleRow = row => {
|
||||
const key = row.path.join('.')
|
||||
row.isExpanded =
|
||||
(row.original && row.original[manualExpandedKey]) ||
|
||||
getBy(expanded, row.path)
|
||||
expanded.includes(key)
|
||||
|
||||
expandedRows.push(row)
|
||||
|
||||
@@ -116,11 +122,13 @@ function useMain(instance) {
|
||||
}
|
||||
}
|
||||
|
||||
function findExpandedDepth(obj, depth = 1) {
|
||||
return Object.values(obj).reduce((prev, curr) => {
|
||||
if (typeof curr === 'object') {
|
||||
return Math.max(prev, findExpandedDepth(curr, depth + 1))
|
||||
}
|
||||
return depth
|
||||
}, 0)
|
||||
function findExpandedDepth(expanded) {
|
||||
let maxDepth = 0
|
||||
|
||||
expanded.forEach(key => {
|
||||
const path = key.split('.')
|
||||
maxDepth = Math.max(maxDepth, path.length)
|
||||
})
|
||||
|
||||
return maxDepth
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ function useMain(instance) {
|
||||
state: [{ groupBy }, setState],
|
||||
} = instance
|
||||
|
||||
ensurePluginOrder(plugins, [], 'useGroupBy', ['useExpanded'])
|
||||
ensurePluginOrder(plugins, [], 'useGroupBy', ['useSortBy', 'useExpanded'])
|
||||
|
||||
flatColumns.forEach(column => {
|
||||
const { id, accessor, disableGrouping: columnDisableGrouping } = column
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import { ensurePluginOrder } from '../utils'
|
||||
import { ensurePluginOrder, defaultColumn } from '../utils'
|
||||
import { addActions, actions } from '../actions'
|
||||
import { defaultState } from '../hooks/useTableState'
|
||||
import * as sortTypes from '../sortTypes'
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
} from '../utils'
|
||||
|
||||
defaultState.sortBy = []
|
||||
defaultColumn.sortType = 'alphanumeric'
|
||||
defaultColumn.sortDescFirst = false
|
||||
|
||||
addActions('sortByChange')
|
||||
|
||||
@@ -173,18 +175,18 @@ function useMain(instance) {
|
||||
|
||||
if (column.canSort) {
|
||||
column.toggleSortBy = (desc, multi) =>
|
||||
toggleSortBy(column.id, desc, multi);
|
||||
|
||||
toggleSortBy(column.id, desc, multi)
|
||||
|
||||
column.clearSorting = () => {
|
||||
return setState(old => {
|
||||
const { sortBy } = old;
|
||||
const newSortBy = sortBy.filter(d => d.id !== column.id);
|
||||
const { sortBy } = old
|
||||
const newSortBy = sortBy.filter(d => d.id !== column.id)
|
||||
return {
|
||||
...old,
|
||||
sortBy: newSortBy,
|
||||
}
|
||||
}, actions.sortByChange);
|
||||
};
|
||||
}, actions.sortByChange)
|
||||
}
|
||||
}
|
||||
|
||||
column.getSortByToggleProps = props => {
|
||||
@@ -255,8 +257,13 @@ function useMain(instance) {
|
||||
const sortMethod =
|
||||
isFunction(sortType) ||
|
||||
(userSortTypes || {})[sortType] ||
|
||||
sortTypes[sortType] ||
|
||||
sortTypes.alphanumeric
|
||||
sortTypes[sortType]
|
||||
|
||||
if (!sortMethod) {
|
||||
throw new Error(
|
||||
`React-Table: Could not find a valid sortType of '${sortType}' for column '${sort.id}'.`
|
||||
)
|
||||
}
|
||||
|
||||
// Return the correct sortFn.
|
||||
// This function should always return in ascending order
|
||||
|
||||
+13
-22
@@ -1,7 +1,6 @@
|
||||
import React from 'react'
|
||||
|
||||
const columnFallbacks = {
|
||||
Header: () => null,
|
||||
export const defaultColumn = {
|
||||
Cell: ({ cell: { value = '' } }) => String(value),
|
||||
show: true,
|
||||
}
|
||||
@@ -22,9 +21,15 @@ export function findMaxDepth(columns, depth = 0) {
|
||||
}, 0)
|
||||
}
|
||||
|
||||
export function decorateColumn(column, defaultColumn, parent, depth, index) {
|
||||
// Apply the defaultColumn
|
||||
column = { ...defaultColumn, ...column }
|
||||
export function decorateColumn(
|
||||
column,
|
||||
userDefaultColumn,
|
||||
parent,
|
||||
depth,
|
||||
index
|
||||
) {
|
||||
// Apply the userDefaultColumn
|
||||
column = { ...defaultColumn, ...userDefaultColumn, ...column }
|
||||
|
||||
// First check for string accessor
|
||||
let { id, accessor, Header } = column
|
||||
@@ -50,8 +55,10 @@ export function decorateColumn(column, defaultColumn, parent, depth, index) {
|
||||
}
|
||||
|
||||
column = {
|
||||
...columnFallbacks,
|
||||
// Make sure there is a fallback header, just in case
|
||||
Header: () => null,
|
||||
...column,
|
||||
// Materialize and override this stuff
|
||||
id,
|
||||
accessor,
|
||||
parent,
|
||||
@@ -232,22 +239,6 @@ export function defaultGroupByFn(rows, columnID) {
|
||||
}, {})
|
||||
}
|
||||
|
||||
export function setBy(obj = {}, path, value) {
|
||||
path = makePathArray(path)
|
||||
const recurse = (obj, depth = 0) => {
|
||||
const key = path[depth]
|
||||
const target = typeof obj[key] !== 'object' ? {} : obj[key]
|
||||
const subValue =
|
||||
depth === path.length - 1 ? value : recurse(target, depth + 1)
|
||||
return {
|
||||
...obj,
|
||||
[key]: subValue,
|
||||
}
|
||||
}
|
||||
|
||||
return recurse(obj)
|
||||
}
|
||||
|
||||
export function getElementDimensions(element) {
|
||||
const rect = element.getBoundingClientRect()
|
||||
const style = window.getComputedStyle(element)
|
||||
|
||||
Reference in New Issue
Block a user