Skip to content

Commit fe539aa

Browse files
fix: don't count hidden fields in the version view
1 parent 48f493b commit fe539aa

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

Diff for: packages/next/src/views/Version/RenderFieldsToDiff/index.tsx

+2-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { diffComponents as _diffComponents } from './fields/index.js'
88
import type { FieldDiffProps, Props } from './types.js'
99

1010
import { diffMethods } from './fields/diffMethods.js'
11+
import { isFieldHiddenInVersionView } from './utilities/isFieldHiddenInVersionView.js'
1112
import './index.scss'
1213

1314
const baseClass = 'render-field-diffs'
@@ -28,22 +29,7 @@ export const RenderFieldsToDiff: React.FC<Props> = ({
2829
return (
2930
<div className={baseClass}>
3031
{fields?.map((field, i) => {
31-
if (fieldIsID(field)) {
32-
return null
33-
}
34-
35-
// Don't render hidden fields
36-
if (field.hidden) {
37-
return null
38-
}
39-
40-
// Don't render fields with admin.disabled
41-
if (field.admin.disabled) {
42-
return null
43-
}
44-
45-
// Don't render fields with admin.hiddenInVersionView
46-
if (field.admin.hiddenInVersionView) {
32+
if (isFieldHiddenInVersionView(field)) {
4733
return null
4834
}
4935

Diff for: packages/next/src/views/Version/RenderFieldsToDiff/utilities/countChangedFields.spec.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { countChangedFields, countChangedFieldsInRows } from './countChangedFields.js'
2+
import { isFieldHiddenInVersionView } from './isFieldHiddenInVersionView.js'
3+
24
import type { ClientField } from 'payload'
35

6+
// Mock isFieldHiddenInVersionView with it's original implementation so we can
7+
// replace it later in the hidden fields tests.
8+
// (Couldn't use jest.spyOn directly due to "TypeError: Cannot redefine property")
9+
jest.mock('./isFieldHiddenInVersionView', () => {
10+
const actual = jest.requireActual('./isFieldHiddenInVersionView')
11+
return {
12+
...actual,
13+
isFieldHiddenInVersionView: jest.fn(actual.isFieldHiddenInVersionView),
14+
}
15+
})
16+
417
describe('countChangedFields', () => {
518
// locales can be undefined when not configured in payload.config.js
619
const locales = undefined
@@ -40,16 +53,20 @@ describe('countChangedFields', () => {
4053
expect(result).toBe(2)
4154
})
4255

43-
it('should not count the id field because it is not displayed in the version view', () => {
56+
it('should not count changed fields when isFieldHiddenInVersionView() returns true', async () => {
57+
// Make isFieldHiddenInVersionView return true for one of the fields
58+
// jest.mocked() is used to correct the type of isFieldHiddenInVersionView
59+
jest.mocked(isFieldHiddenInVersionView).mockImplementationOnce(() => true)
60+
4461
const fields: ClientField[] = [
45-
{ name: 'id', type: 'text' },
4662
{ name: 'a', type: 'text' },
63+
{ name: 'b', type: 'text' },
4764
]
48-
const comparison = { id: 'original', a: 'original' }
49-
const version = { id: 'changed', a: 'original' }
65+
const comparison = { a: 'original', b: 'original' }
66+
const version = { a: 'changed', b: 'changed' }
5067

5168
const result = countChangedFields({ comparison, fields, version, locales })
52-
expect(result).toBe(0)
69+
expect(result).toBe(1)
5370
})
5471

5572
it('should count changed fields inside collapsible fields', () => {

Diff for: packages/next/src/views/Version/RenderFieldsToDiff/utilities/countChangedFields.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ArrayFieldClient, BlocksFieldClient, ClientField } from 'payload'
22

33
import { fieldHasChanges } from './fieldHasChanges.js'
44
import { getFieldsForRowComparison } from './getFieldsForRowComparison.js'
5+
import { isFieldHiddenInVersionView } from './isFieldHiddenInVersionView.js'
56

67
type Args = {
78
comparison: unknown
@@ -18,10 +19,11 @@ export function countChangedFields({ comparison, fields, locales, version }: Arg
1819
let count = 0
1920

2021
fields.forEach((field) => {
21-
// Don't count the id field since it is not displayed in the UI
22-
if ('name' in field && field.name === 'id') {
22+
// Don't count fields that are hidden in the version view
23+
if (isFieldHiddenInVersionView(field)) {
2324
return
2425
}
26+
2527
const fieldType = field.type
2628
switch (fieldType) {
2729
// Iterable fields are arrays and blocks fields. We iterate over each row and
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { ClientField } from 'payload'
2+
3+
import { fieldIsID } from 'payload/shared'
4+
5+
export function isFieldHiddenInVersionView(field: ClientField) {
6+
// Don't render id fields
7+
if (fieldIsID(field)) {
8+
return true
9+
}
10+
11+
// Don't render hidden fields, because they are supposed to be hidden everywhere
12+
if (field.hidden) {
13+
return true
14+
}
15+
16+
// Don't render fields with admin.disabled, because they are supposed to be
17+
// hidden everywhere in the admin UI.
18+
if (field.admin?.disabled) {
19+
return true
20+
}
21+
22+
// Don't render fields with admin.hiddenInVersionView, because they are
23+
// supposed to be hidden specifically in the version view.
24+
if (field.admin?.hiddenInVersionView) {
25+
return true
26+
}
27+
28+
return false
29+
}

0 commit comments

Comments
 (0)