Skip to content

Commit

Permalink
feat: add reset column resizing (#2461)
Browse files Browse the repository at this point in the history
Co-authored-by: Garg, Rohit <[email protected]>
  • Loading branch information
gargroh and Garg, Rohit authored Jul 17, 2020
1 parent 0981670 commit c4120e6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 32 deletions.
72 changes: 42 additions & 30 deletions examples/column-resizing/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ function Table({ columns, data }) {
headerGroups,
rows,
prepareRow,
state,
resetResizing,
} = useTable(
{
columns,
Expand All @@ -83,41 +85,51 @@ function Table({ columns, data }) {
)

return (
<div {...getTableProps()} className="table">
<>
<button onClick={resetResizing}>Reset Resizing</button>
<div>
{headerGroups.map(headerGroup => (
<div {...headerGroup.getHeaderGroupProps()} className="tr">
{headerGroup.headers.map(column => (
<div {...column.getHeaderProps()} className="th">
{column.render('Header')}
{/* Use column.getResizerProps to hook up the events correctly */}
<div
{...column.getResizerProps()}
className={`resizer ${column.isResizing ? 'isResizing' : ''}`}
/>
<div {...getTableProps()} className="table">
<div>
{headerGroups.map(headerGroup => (
<div {...headerGroup.getHeaderGroupProps()} className="tr">
{headerGroup.headers.map(column => (
<div {...column.getHeaderProps()} className="th">
{column.render('Header')}
{/* Use column.getResizerProps to hook up the events correctly */}
<div
{...column.getResizerProps()}
className={`resizer ${
column.isResizing ? 'isResizing' : ''
}`}
/>
</div>
))}
</div>
))}
</div>
))}
</div>

<div {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row)
return (
<div {...row.getRowProps()} className="tr">
{row.cells.map(cell => {
return (
<div {...cell.getCellProps()} className="td">
{cell.render('Cell')}
</div>
)
})}
</div>
)
})}
<div {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row)
return (
<div {...row.getRowProps()} className="tr">
{row.cells.map(cell => {
return (
<div {...cell.getCellProps()} className="td">
{cell.render('Cell')}
</div>
)
})}
</div>
)
})}
</div>
</div>
</div>
</div>
<pre>
<code>{JSON.stringify(state, null, 2)}</code>
</pre>
</>
)
}

Expand Down Expand Up @@ -164,7 +176,7 @@ function App() {
[]
)

const data = React.useMemo(() => makeData(20), [])
const data = React.useMemo(() => makeData(10), [])

return (
<Styles>
Expand Down
38 changes: 36 additions & 2 deletions src/plugin-hooks/useResizeColumns.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react'

import {
actions,
defaultColumn,
makePropGetter,
useGetLatest,
ensurePluginOrder,
useMountedLayoutEffect,
} from '../publicUtils'

import { getFirstDefined, passiveEventSupported } from '../utils'
Expand All @@ -15,6 +18,7 @@ defaultColumn.canResize = true
actions.columnStartResizing = 'columnStartResizing'
actions.columnResizing = 'columnResizing'
actions.columnDoneResizing = 'columnDoneResizing'
actions.resetResize = 'resetResize'

export const useResizeColumns = hooks => {
hooks.getResizerProps = [defaultGetResizerProps]
Expand Down Expand Up @@ -144,6 +148,15 @@ function reducer(state, action) {
}
}

if (action.type === actions.resetResize) {
return {
...state,
columnResizing: {
columnWidths: {},
},
}
}

if (action.type === actions.columnStartResizing) {
const { clientX, columnId, columnWidth, headerIdWidths } = action

Expand Down Expand Up @@ -217,7 +230,10 @@ const useInstanceBeforeDimensions = instance => {
)

header.canResize = canResize
header.width = columnResizing.columnWidths[header.id] || header.width
header.width =
columnResizing.columnWidths[header.id] ||
header.originalWidth ||
header.width
header.isResizing = columnResizing.isResizingColumn === header.id

if (canResize) {
Expand All @@ -229,8 +245,26 @@ const useInstanceBeforeDimensions = instance => {
})
}

function useInstance({ plugins }) {
function useInstance(instance) {
const { plugins, dispatch, autoResetResize = true, columns } = instance

ensurePluginOrder(plugins, ['useAbsoluteLayout'], 'useResizeColumns')

const getAutoResetResize = useGetLatest(autoResetResize)
useMountedLayoutEffect(() => {
if (getAutoResetResize()) {
dispatch({ type: actions.resetResize })
}
}, [columns])

const resetResizing = React.useCallback(
() => dispatch({ type: actions.resetResize }),
[dispatch]
)

Object.assign(instance, {
resetResizing,
})
}

function getLeafHeaders(header) {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export function decorateColumn(column, userDefaultColumn) {
...userDefaultColumn,
...column,
})

Object.assign(column, {
originalWidth: column.width,
})

return column
}

Expand Down

1 comment on commit c4120e6

@vercel
Copy link

@vercel vercel bot commented on c4120e6 Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.