Skip to content

Commit

Permalink
fix: fix header regeneration and colspan, better sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Aug 22, 2019
1 parent 7ae4c4f commit 4537f28
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 136 deletions.
22 changes: 13 additions & 9 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ The following options are supported on any column object you can pass to `column

The following properties are available on the table instance returned from `useTable`

- `headerGroups: Array<HeaderGroup>`
- An array of normalized header groups, each containing a flattened array of final column objects for that row.
- See [Header Group Properties](#headergroup-properties) for more information
- `headers[] Array<Column>`
- A **nested** array of final column objects, similar in structure to the original columns configuration option.
- See [Column Properties](#column-properties) for more information
- `columns: Array<Column>`
- A **flat** array of all final column objects computed from the original columns configuration option.
- See [Column Properties](#column-properties) for more information
- `headers[] Array<Column>`
- A **nested** array of final column objects, similar in structure to the original columns configuration option.
- `headerGroups: Array<HeaderGroup>`
- An array of normalized header groups, each containing a flattened array of final column objects for that row.
- See [Header Group Properties](#headergroup-properties) for more information
- `flatHeaders[] Array<Column>`
- A **flat** array of final header objects found in each header group. Columns may be duplicated (if columns are not adjacent)
- See [Column Properties](#column-properties) for more information
- `rows: Array<Row>`
- An array of **materialized row objects** from the original `data` array and `columns` passed into the table options
Expand Down Expand Up @@ -402,12 +405,13 @@ The following options are supported on any `Column` object passed to the `column
- If set to `true`, the underlying sorting direction will be inverted, but the UI will not.
- This may be useful in situations where positive and negative connotation is inverted, eg. a Golfing score where a lower score is considered more positive than a higher one.
- `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)
- 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 `function` is passed, it will be used.
- For mor information on sort types, see [Sorting](TODO)
- For more information on sort types, see [Sorting](TODO)

### Instance Properties

Expand Down Expand Up @@ -520,7 +524,7 @@ The following options are supported via the main options object passed to `useTa
- The function (or resolved function from the string) will be used as the default/fallback filter method for every column that has filtering enabled.
- If a `string` is passed, the function with that name located on the `filterTypes` option object will be used.
- If a `function` is passed, it will be used.
- For mor information on filter types, see [Filtering](TODO)
- For more information on filter types, see [Filtering](TODO)
- `manualFilters: Bool`
- Enables filter detection functionality, but does not automatically perform row filtering.
- Turn this on if you wish to implement your own row filter outside of the table (eg. server-side or manual row grouping/nesting)
Expand All @@ -529,7 +533,7 @@ The following options are supported via the main options object passed to `useTa
- `filterTypes: Object<filterKey: filterType>`
- Must be **memoized**
- Allows overriding or adding additional filter types for columns to use. If a column's filter type isn't found on this object, it will default to using the [built-in filter types](TODO).
- For mor information on filter types, see [Filtering](TODO)
- For more information on filter types, see [Filtering](TODO)

### Column Options

Expand All @@ -549,7 +553,7 @@ The following options are supported on any `Column` object passed to the `column
- The resolved function from the this string/function will be used to filter the this column's data.
- If a `string` is passed, the function with that name located on either the custom `filterTypes` option or the built-in filtering types object will be used. If
- If a `function` is passed, it will be used directly.
- For mor information on filter types, see [Filtering](TODO)
- For more information on filter types, see [Filtering](TODO)
- If a **function** is passed, it must be **memoized**

### Instance Properties
Expand Down
6 changes: 0 additions & 6 deletions examples/sorting/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ const Styles = styled.div`
}
`

const defaultColumn = {
sort: 'numeric',
}

function Table({ columns, data }) {
const { getTableProps, headerGroups, rows, prepareRow } = useTable(
{
columns,
data,
defaultColumn,
debug: true,
},
useSortBy
)
Expand Down
Binary file modified media/src/logo.sketch
Binary file not shown.
115 changes: 54 additions & 61 deletions src/hooks/useTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
makeHeaderGroups,
findMaxDepth,
flattenBy,
determineColumnVisibility,
} from '../utils'

import { useTableState } from './useTableState'
Expand Down Expand Up @@ -49,27 +50,24 @@ export const useTable = (props, ...plugins) => {

debug = process.env.NODE_ENV === 'production' ? false : debug

// Always provide a default state
// Always provide a default table state
const defaultState = useTableState()

// But use the users state if provided
// But use the users table state if provided
const state = userState || defaultState

// The initial api
// The table instance ref
let instanceRef = React.useRef({})

Object.assign(instanceRef.current, {
...props,
data,
state,
plugins,
data, // The raw data
state, // The resolved table state
plugins, // All resolved plugins
hooks: {
columnsBeforeHeaderGroups: [],
columnsBeforeHeaderGroupsDeps: [],
useMain: [],
useColumns: [],
useHeaders: [],
useHeaderGroups: [],
useRows: [],
prepareRow: [],
getTableProps: [],
Expand All @@ -82,34 +80,34 @@ export const useTable = (props, ...plugins) => {

// Allow plugins to register hooks
if (process.env.NODE_ENV === 'development' && debug) console.time('plugins')

plugins.filter(Boolean).forEach(plugin => {
plugin(instanceRef.current.hooks)
})

if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('plugins')

if (process.env.NODE_ENV === 'development' && debug)
console.info('buildColumns/headerGroup/headers')
// Decorate All the columns
let columnTree = React.useMemo(
let headers = React.useMemo(
() => decorateColumnTree(userColumns, defaultColumn),
[defaultColumn, userColumns]
)

// Get the flat list of all columns
let columns = React.useMemo(() => flattenBy(columnTree, 'columns'), [
columnTree,
])
let columns = React.useMemo(() => flattenBy(headers, 'columns'), [headers])

// Allow hooks to decorate columns (and trigger this memoization via deps)
columns = React.useMemo(() => {
if (process.env.NODE_ENV === 'development' && debug)
console.time('hooks.columnsBeforeHeaderGroups')
const newColumns = applyHooks(

let newColumns = applyHooks(
instanceRef.current.hooks.columnsBeforeHeaderGroups,
columns,
instanceRef.current
)

if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('hooks.columnsBeforeHeaderGroups')
return newColumns
Expand All @@ -126,14 +124,10 @@ export const useTable = (props, ...plugins) => {

// Make the headerGroups
const headerGroups = React.useMemo(
() => makeHeaderGroups(columns, findMaxDepth(columnTree), defaultColumn),
[columnTree, columns, defaultColumn]
() => makeHeaderGroups(columns, findMaxDepth(headers), defaultColumn),
[columns, defaultColumn, headers]
)

const headers = React.useMemo(() => flattenBy(headerGroups, 'headers'), [
headerGroups,
])

Object.assign(instanceRef.current, {
columns,
headerGroups,
Expand Down Expand Up @@ -210,12 +204,13 @@ export const useTable = (props, ...plugins) => {
instanceRef.current.flatRows = flatRows

// Determine column visibility
instanceRef.current.columns.forEach(column => {
column.isVisible =
typeof column.show === 'function'
? column.show(instanceRef.current)
: !!column.show
})
determineColumnVisibility(instanceRef.current)

// Provide a flat header list for utilities
instanceRef.current.flatHeaders = headerGroups.reduce(
(all, headerGroup) => [...all, ...headerGroup.headers],
[]
)

if (process.env.NODE_ENV === 'development' && debug)
console.time('hooks.useMain')
Expand All @@ -225,43 +220,42 @@ export const useTable = (props, ...plugins) => {
)
if (process.env.NODE_ENV === 'development' && debug)
console.timeEnd('hooks.useMain')
;[...instanceRef.current.columns, ...instanceRef.current.headers].forEach(
column => {
// Give columns/headers rendering power
column.render = (type, userProps = {}) => {
const Comp = typeof type === 'string' ? column[type] : type

if (typeof Comp === 'undefined') {
throw new Error(renderErr)
}
// Each materialized header needs to be assigned a render function and other
// prop getter properties here.
instanceRef.current.flatHeaders.forEach(column => {
// Give columns/headers rendering power
column.render = (type, userProps = {}) => {
const Comp = typeof type === 'string' ? column[type] : type

return flexRender(Comp, {
...instanceRef.current,
column,
...userProps,
})
if (typeof Comp === 'undefined') {
throw new Error(renderErr)
}

// Give columns/headers a default getHeaderProps
column.getHeaderProps = props =>
mergeProps(
{
key: ['header', column.id].join('_'),
colSpan: column.columns
? column.columns.filter(column => column.isVisible).length
: 1,
},
applyPropHooks(
instanceRef.current.hooks.getHeaderProps,
column,
instanceRef.current
),
props
)
return flexRender(Comp, {
...instanceRef.current,
column,
...userProps,
})
}
)

instanceRef.current.headerGroups.filter((headerGroup, i) => {
// Give columns/headers a default getHeaderProps
column.getHeaderProps = props =>
mergeProps(
{
key: ['header', column.id].join('_'),
colSpan: column.totalHeaderCount,
},
applyPropHooks(
instanceRef.current.hooks.getHeaderProps,
column,
instanceRef.current
),
props
)
})

instanceRef.current.headerGroups.forEach((headerGroup, i) => {
// Filter out any headers and headerGroups that don't have visible columns
headerGroup.headers = headerGroup.headers.filter(header => {
const recurse = columns =>
Expand Down Expand Up @@ -291,10 +285,9 @@ export const useTable = (props, ...plugins) => {
),
props
)

return true
}

return false
})

// Run the rows (this could be a dangerous hook with a ton of data)
Expand Down
16 changes: 8 additions & 8 deletions src/plugin-hooks/tests/__snapshots__/useExpanded.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Snapshot Diff:
</pre>
<table
class=""
@@ -82,10 +84,53 @@
@@ -78,10 +80,53 @@
<td
class=""
>
Expand Down Expand Up @@ -73,7 +73,7 @@ Snapshot Diff:
</span>
</td>
<td
@@ -115,10 +160,139 @@
@@ -111,10 +156,139 @@
</td>
<td
class=""
Expand Down Expand Up @@ -235,7 +235,7 @@ Snapshot Diff:
</code>
</pre>
<table
@@ -127,10 +129,53 @@
@@ -123,10 +125,53 @@
<td
class=""
>
Expand Down Expand Up @@ -289,7 +289,7 @@ Snapshot Diff:
</span>
</td>
<td
@@ -160,10 +205,139 @@
@@ -156,10 +201,139 @@
</td>
<td
class=""
Expand Down Expand Up @@ -451,7 +451,7 @@ Snapshot Diff:
}
</code>
</pre>
@@ -172,10 +174,53 @@
@@ -168,10 +170,53 @@
<td
class=""
>
Expand Down Expand Up @@ -505,7 +505,7 @@ Snapshot Diff:
</span>
</td>
<td
@@ -205,10 +250,139 @@
@@ -201,10 +246,139 @@
</td>
<td
class=""
Expand Down Expand Up @@ -667,7 +667,7 @@ Snapshot Diff:
}
}
</code>
@@ -218,11 +220,11 @@
@@ -214,11 +216,11 @@
class=""
>
<span
Expand All @@ -680,7 +680,7 @@ Snapshot Diff:
<td
class=""
>
@@ -250,10 +252,30 @@
@@ -246,10 +248,30 @@
</td>
<td
class=""
Expand Down
Loading

0 comments on commit 4537f28

Please sign in to comment.