Skip to content

Commit 7344d64

Browse files
authored
fix(next): group by dates with null values (#13381)
When grouping by a date field and its value is null, the list view crashes. --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1210979856538208
1 parent 2211f3d commit 7344d64

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

packages/next/src/views/List/handleGroupBy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const handleGroupBy = async ({
143143
},
144144
})
145145

146-
let heading = valueOrRelationshipID || req.i18n.t('general:noValue')
146+
let heading = valueOrRelationshipID
147147

148148
if (
149149
groupByField?.type === 'relationship' &&
@@ -155,9 +155,9 @@ export const handleGroupBy = async ({
155155
valueOrRelationshipID
156156
}
157157

158-
if (groupByField.type === 'date') {
158+
if (groupByField.type === 'date' && valueOrRelationshipID) {
159159
heading = formatDate({
160-
date: String(heading),
160+
date: String(valueOrRelationshipID),
161161
i18n: req.i18n,
162162
pattern: clientConfig.admin.dateFormat,
163163
})
@@ -174,7 +174,7 @@ export const handleGroupBy = async ({
174174
enableRowSelections,
175175
groupByFieldPath,
176176
groupByValue: valueOrRelationshipID,
177-
heading,
177+
heading: heading || req.i18n.t('general:noValue'),
178178
i18n: req.i18n,
179179
key: `table-${valueOrRelationshipID}`,
180180
orderableFieldName: collectionConfig.orderable === true ? '_order' : undefined,

test/group-by/collections/Posts/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { CollectionConfig } from 'payload'
22

3-
import { lexicalEditor } from '@payloadcms/richtext-lexical'
4-
53
import { categoriesSlug } from '../Categories/index.js'
64

75
export const postsSlug = 'posts'
@@ -25,11 +23,8 @@ export const PostsCollection: CollectionConfig = {
2523
relationTo: categoriesSlug,
2624
},
2725
{
28-
name: 'content',
29-
type: 'richText',
30-
editor: lexicalEditor({
31-
features: ({ defaultFeatures }) => [...defaultFeatures],
32-
}),
26+
name: 'date',
27+
type: 'date',
3328
},
3429
{
3530
type: 'tabs',

test/group-by/e2e.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ test.describe('Group By', () => {
245245
).toBeVisible()
246246
})
247247

248+
test('should group by date fields even when their values are null', async () => {
249+
await payload.create({
250+
collection: postsSlug,
251+
data: {
252+
title: 'My Post',
253+
date: null,
254+
},
255+
})
256+
257+
await page.goto(url.list)
258+
259+
await addGroupBy(page, { fieldLabel: 'Date', fieldPath: 'date' })
260+
261+
await expect(page.locator('.table-wrap')).toHaveCount(1)
262+
263+
await expect(
264+
page.locator('.group-by-header__heading', { hasText: exactText('No value') }),
265+
).toBeVisible()
266+
})
267+
248268
test('should sort the group-by field globally', async () => {
249269
await page.goto(url.list)
250270

test/group-by/payload-types.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,7 @@ export interface Post {
125125
id: string;
126126
title?: string | null;
127127
category?: (string | null) | Category;
128-
content?: {
129-
root: {
130-
type: string;
131-
children: {
132-
type: string;
133-
version: number;
134-
[k: string]: unknown;
135-
}[];
136-
direction: ('ltr' | 'rtl') | null;
137-
format: 'left' | 'start' | 'center' | 'right' | 'end' | 'justify' | '';
138-
indent: number;
139-
version: number;
140-
};
141-
[k: string]: unknown;
142-
} | null;
128+
date?: string | null;
143129
tab1Field?: string | null;
144130
updatedAt: string;
145131
createdAt: string;
@@ -295,7 +281,7 @@ export interface PayloadMigration {
295281
export interface PostsSelect<T extends boolean = true> {
296282
title?: T;
297283
category?: T;
298-
content?: T;
284+
date?: T;
299285
tab1Field?: T;
300286
updatedAt?: T;
301287
createdAt?: T;

0 commit comments

Comments
 (0)