Skip to content

Commit

Permalink
Fix initialSort preventing sort by "No field" (#8909)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens authored Nov 14, 2023
1 parent 871bdcb commit 029b5cf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/no-sort-ok.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Fix ui.listView.initialSort preventing sort by "No field"
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,17 @@ function SortSelectionPopoverContent({
const fieldPath: string = (newVal as any).value;
if (fieldPath === noFieldOption.value) {
const { sortBy, ...restOfQuery } = router.query;
router.push({ query: restOfQuery });

if (list.initialSort) {
router.push({
query: {
...router.query,
sortBy: ''
}
});
} else {
router.push({ query: restOfQuery });
}
} else {
router.push({
query: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ const ListPage = ({ listKey }: ListPageProps) => {
<FilterAdd listKey={listKey} filterableFields={filterableFields} />
) : null}
{filters.filters.length ? <FilterList filters={filters.filters} list={list} /> : null}
{Boolean(filters.filters.length || query.sortBy || query.fields || query.search) && (
{Boolean(filters.filters.length || query.sortBy !== undefined || query.fields || query.search) && (
<Button size="small" onClick={resetToDefaults}>
Reset to defaults
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import { useRouter } from '../../../../admin-ui/router';

export function useSort(list: ListMeta, orderableFields: Set<string>) {
const { query } = useRouter();
let sortByFromUrl = typeof query.sortBy === 'string' ? query.sortBy : '';
let sortByFromUrl = typeof query.sortBy === 'string' ? query.sortBy : null;

return useMemo(() => {
if (sortByFromUrl === '') {
if (!list.initialSort || !orderableFields.has(list.initialSort.field)) {
return null;
}
return list.initialSort;
}
let direction: 'ASC' | 'DESC' = 'ASC';
let sortByField = sortByFromUrl;
if (sortByFromUrl.charAt(0) === '-') {
sortByField = sortByFromUrl.slice(1);
direction = 'DESC';
if (sortByFromUrl === '') return null;
if (sortByFromUrl === null) return list.initialSort;

if (sortByFromUrl.startsWith('-')) {
const field = sortByFromUrl.slice(1);
if (!orderableFields.has(field)) return null;

return {
field,
direction: 'DESC'
};
}
if (!orderableFields.has(sortByField)) return null;
return { field: sortByField, direction };

if (!orderableFields.has(sortByFromUrl)) return null;
return {
field: sortByFromUrl,
direction: 'ASC'
};
}, [sortByFromUrl, list, orderableFields]);
}

0 comments on commit 029b5cf

Please sign in to comment.