Skip to content

Commit ba92d86

Browse files
fix: list sort preferences (#6731)
Fixes #6617 Sets preferences when list sort is set. Uses defaultSort when defined in config and preferences are not set.
1 parent 0fb14cf commit ba92d86

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

packages/next/src/views/List/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const ListView: React.FC<AdminViewProps> = async ({
9797
const sort =
9898
query?.sort && typeof query.sort === 'string'
9999
? query.sort
100-
: listPreferences?.sort || undefined
100+
: listPreferences?.sort || collectionConfig.defaultSort || undefined
101101

102102
const data = await payload.find({
103103
collection: collectionSlug,

packages/ui/src/elements/SortColumn/index.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use client'
22
// TODO: abstract the `next/navigation` dependency out from this component
3-
import { useRouter } from 'next/navigation.js'
43
import React, { useCallback } from 'react'
54

65
export type SortColumnProps = {
@@ -13,6 +12,7 @@ export type SortColumnProps = {
1312
import type { FieldBase } from 'payload/types'
1413

1514
import { Chevron } from '../../icons/Chevron/index.js'
15+
import { useListQuery } from '../../providers/ListQuery/index.js'
1616
import { useSearchParams } from '../../providers/SearchParams/index.js'
1717
import { useTranslation } from '../../providers/Translation/index.js'
1818
import './index.scss'
@@ -21,8 +21,8 @@ const baseClass = 'sort-column'
2121

2222
export const SortColumn: React.FC<SortColumnProps> = (props) => {
2323
const { name, Label, disable = false, label } = props
24-
const { searchParams, stringifyParams } = useSearchParams()
25-
const router = useRouter()
24+
const { searchParams } = useSearchParams()
25+
const { refineListData } = useListQuery()
2626
const { t } = useTranslation()
2727

2828
const { sort } = searchParams
@@ -38,16 +38,11 @@ export const SortColumn: React.FC<SortColumnProps> = (props) => {
3838

3939
const setSort = useCallback(
4040
(newSort) => {
41-
router.replace(
42-
stringifyParams({
43-
params: {
44-
sort: newSort,
45-
},
46-
replace: true,
47-
}),
48-
)
41+
refineListData({
42+
sort: newSort,
43+
})
4944
},
50-
[router, stringifyParams],
45+
[refineListData],
5146
)
5247

5348
return (

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type ListQueryContext = Handlers & {
3535
data: PaginatedDocs
3636
defaultLimit?: number
3737
defaultSort?: string
38+
refineListData: (args: RefineOverrides) => void
3839
}
3940

4041
const Context = createContext({} as ListQueryContext)
@@ -176,6 +177,7 @@ export const ListQueryProvider: React.FC<ListQueryProps> = ({
176177
handleSearchChange,
177178
handleSortChange,
178179
handleWhereChange,
180+
refineListData,
179181
}}
180182
>
181183
{children}

0 commit comments

Comments
 (0)