fix(ui): select all should reset when params change, page, filter, etc (#12612)

Fixes #11938
Fixes https://github.com/payloadcms/payload/issues/13154

When select-all is checked and you filter or change the page, the
selected documents should reset.
This commit is contained in:
Jarrod Flesch
2025-07-22 15:23:02 -04:00
committed by GitHub
parent 246a42b727
commit 412bf4ff73
2 changed files with 34 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import * as qs from 'qs-esm'
import React, { createContext, use, useCallback, useEffect, useRef, useState } from 'react' import React, { createContext, use, useCallback, useEffect, useRef, useState } from 'react'
import { parseSearchParams } from '../../utilities/parseSearchParams.js' import { parseSearchParams } from '../../utilities/parseSearchParams.js'
import { useListQuery } from '../ListQuery/index.js'
import { useLocale } from '../Locale/index.js' import { useLocale } from '../Locale/index.js'
export enum SelectAllStatus { export enum SelectAllStatus {
@@ -54,6 +55,7 @@ export const SelectionProvider: React.FC<Props> = ({ children, docs = [], totalD
const [selectAll, setSelectAll] = useState<SelectAllStatus>(SelectAllStatus.None) const [selectAll, setSelectAll] = useState<SelectAllStatus>(SelectAllStatus.None)
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
const searchParams = useSearchParams() const searchParams = useSearchParams()
const { query } = useListQuery()
const toggleAll = useCallback( const toggleAll = useCallback(
(allAvailable = false) => { (allAvailable = false) => {
@@ -201,7 +203,11 @@ export const SelectionProvider: React.FC<Props> = ({ children, docs = [], totalD
setCount(newCount) setCount(newCount)
}, [selectAll, selected, totalDocs]) }, [selectAll, selected, totalDocs])
// eslint-disable-next-line react-compiler/react-compiler -- TODO: fix useEffect(() => {
setSelectAll(SelectAllStatus.None)
setSelected(new Map())
}, [query])
contextRef.current = { contextRef.current = {
count, count,
getQueryParams, getQueryParams,
@@ -213,7 +219,6 @@ export const SelectionProvider: React.FC<Props> = ({ children, docs = [], totalD
totalDocs, totalDocs,
} }
// eslint-disable-next-line react-compiler/react-compiler -- TODO: fix
return <Context value={contextRef.current}>{children}</Context> return <Context value={contextRef.current}>{children}</Context>
} }

View File

@@ -1649,6 +1649,33 @@ describe('List View', () => {
'Custom placeholder', 'Custom placeholder',
) )
}) })
test('should reset list selection when query params change', async () => {
await deleteAllPosts()
await Promise.all(Array.from({ length: 12 }, (_, i) => createPost({ title: `post${i + 1}` })))
await page.goto(postsUrl.list)
const pageOneButton = page.locator('.paginator__page', { hasText: '1' })
await expect(pageOneButton).toBeVisible()
await pageOneButton.click()
await page.locator('.checkbox-input:has(#select-all)').locator('input').click()
await expect(page.locator('.checkbox-input:has(#select-all)').locator('input')).toBeChecked()
await expect(page.locator('.list-selection')).toContainText('5 selected')
const pageTwoButton = page.locator('.paginator__page', { hasText: '2' })
await expect(pageTwoButton).toBeVisible()
await pageTwoButton.click()
await expect(
page.locator('.checkbox-input:has(#select-all) input:not([checked])'),
).toBeVisible()
await page.locator('.row-1 .cell-_select input').check()
await page.locator('.row-2 .cell-_select input').check()
await expect(page.locator('.list-selection')).toContainText('2 selected')
})
}) })
async function createPost(overrides?: Partial<Post>): Promise<Post> { async function createPost(overrides?: Partial<Post>): Promise<Post> {