Skip to content

Commit 08a6f88

Browse files
fix(ui): reset columns state throwing errors (#11903)
### What? Fixes `resetColumnsState` in `useTableColumns` react hook. ### Why? `resetColumnsState` threw errors when being executed, e.g. `Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'findIndex')` ### How? Removes unnecessary parsing of URL query parameters in `setActiveColumns` when resetting columns. --------- Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com>
1 parent ede5c67 commit 08a6f88

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

docs/admin/react-hooks.mdx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,25 +981,46 @@ const MyComponent: React.FC = () => {
981981

982982
## useTableColumns
983983

984-
Returns methods to manipulate table columns
984+
Returns properties and methods to manipulate table columns:
985+
| Property | Description |
986+
| ------------------------ | ------------------------------------------------------------------------------------------ |
987+
| **`columns`** | The current state of columns including their active status and configuration |
988+
| **`LinkedCellOverride`** | A component override for linked cells in the table |
989+
| **`moveColumn`** | A method to reorder columns. Accepts `{ fromIndex: number, toIndex: number }` as arguments |
990+
| **`resetColumnsState`** | A method to reset columns back to their default configuration as defined in the collection config |
991+
| **`setActiveColumns`** | A method to set specific columns to active state while preserving the existing column order. Accepts an array of column names to activate |
992+
| **`toggleColumn`** | A method to toggle a single column's visibility. Accepts a column name as string |
985993

986994
```tsx
987995
'use client'
988996
import { useTableColumns } from '@payloadcms/ui'
989997

990998
const MyComponent: React.FC = () => {
991999
// highlight-start
992-
const { setActiveColumns } = useTableColumns()
1000+
const { setActiveColumns, resetColumnsState } = useTableColumns()
9931001

994-
const resetColumns = () => {
995-
setActiveColumns(['id', 'createdAt', 'updatedAt'])
1002+
const activateSpecificColumns = () => {
1003+
// Only activates the id and createdAt columns
1004+
// Other columns retain their current active/inactive state
1005+
// The original column order is preserved
1006+
setActiveColumns(['id', 'createdAt'])
1007+
}
1008+
1009+
const resetToDefaults = () => {
1010+
// Resets to the default columns defined in the collection config
1011+
resetColumnsState()
9961012
}
9971013
// highlight-end
9981014

9991015
return (
1000-
<button type="button" onClick={resetColumns}>
1001-
Reset columns
1002-
</button>
1016+
<div>
1017+
<button type="button" onClick={activateSpecificColumns}>
1018+
Activate Specific Columns
1019+
</button>
1020+
<button type="button" onClick={resetToDefaults}>
1021+
Reset To Defaults
1022+
</button>
1023+
</div>
10031024
)
10041025
}
10051026
```

packages/ui/src/providers/TableColumns/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export const TableColumnsProvider: React.FC<TableColumnsProviderProps> = ({
8888
)
8989

9090
const resetColumnsState = React.useCallback(async () => {
91-
await setActiveColumns(defaultColumns)
92-
}, [defaultColumns, setActiveColumns])
91+
await refineListData({ columns: defaultColumns || [] })
92+
}, [defaultColumns, refineListData])
9393

9494
return (
9595
<TableColumnContext

test/admin/components/ResetColumns/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { Pill, useTableColumns } from '@payloadcms/ui'
55
function ResetDefaultColumnsButton() {
66
const { resetColumnsState } = useTableColumns()
77

8-
return <Pill onClick={resetColumnsState}>Reset to default columns</Pill>
8+
return (
9+
<Pill id="reset-columns-button" onClick={resetColumnsState}>
10+
Reset to default columns
11+
</Pill>
12+
)
913
}
1014

1115
export { ResetDefaultColumnsButton }

test/admin/e2e/list-view/e2e.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,18 @@ describe('List View', () => {
12451245
}),
12461246
).toBeVisible()
12471247
})
1248+
1249+
test('should reset default columns', async () => {
1250+
await page.goto(postsUrl.list)
1251+
await toggleColumn(page, { columnLabel: 'ID', targetState: 'off', columnName: 'id' })
1252+
1253+
// should not have the ID column #heading-id
1254+
await expect(page.locator('#heading-id')).toBeHidden()
1255+
1256+
await page.locator('#reset-columns-button').click()
1257+
1258+
await expect(page.locator('#heading-id')).toBeVisible()
1259+
})
12481260
})
12491261

12501262
describe('multi-select', () => {

0 commit comments

Comments
 (0)