feat: threads operation through field condition function (#12132)

This PR updates the field `condition` function property to include a new
`operation` argument.

The `operation` arg provides a string relating to which operation the
field type is currently executing within.

#### Changes:

- Added `operation: Operation` in the Condition type.
- Updated relevant condition checks to ensure correct parameter usage.
This commit is contained in:
Patrik
2025-04-16 15:38:53 -04:00
committed by GitHub
parent 4426625b83
commit c877b1ad43
8 changed files with 46 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ import type { Config } from '../../payload-types.js'
import {
ensureCompilationIsDone,
initPageConsoleErrorCatch,
saveDocAndAssert,
// throttleTest,
} from '../../../helpers.js'
import { AdminUrlUtil } from '../../../helpers/adminUrlUtil.js'
@@ -225,4 +226,19 @@ describe('Conditional Logic', () => {
await expect(numberField).toBeVisible()
})
test('should render field based on operation argument', async () => {
await page.goto(url.create)
const textField = page.locator('#field-text')
const fieldWithOperationCondition = page.locator('#field-fieldWithOperationCondition')
await textField.fill('some text')
await expect(fieldWithOperationCondition).toBeVisible()
await saveDocAndAssert(page)
await expect(fieldWithOperationCondition).toBeHidden()
})
})

View File

@@ -24,6 +24,19 @@ const ConditionalLogic: CollectionConfig = {
condition: ({ toggleField }) => Boolean(toggleField),
},
},
{
name: 'fieldWithOperationCondition',
type: 'text',
admin: {
condition: (data, siblingData, { operation }) => {
if (operation === 'create') {
return true
}
return false
},
},
},
{
name: 'customFieldWithField',
type: 'text',
@@ -217,7 +230,7 @@ const ConditionalLogic: CollectionConfig = {
name: 'numberField',
type: 'number',
admin: {
condition: (data, siblingData, { path, user }) => {
condition: (data, siblingData, { path }) => {
// Ensure path has enough depth
if (path.length < 5) {
return false

View File

@@ -790,6 +790,7 @@ export interface ConditionalLogic {
text: string;
toggleField?: boolean | null;
fieldWithCondition?: string | null;
fieldWithOperationCondition?: string | null;
customFieldWithField?: string | null;
customFieldWithHOC?: string | null;
customClientFieldWithCondition?: string | null;
@@ -2364,6 +2365,7 @@ export interface ConditionalLogicSelect<T extends boolean = true> {
text?: T;
toggleField?: T;
fieldWithCondition?: T;
fieldWithOperationCondition?: T;
customFieldWithField?: T;
customFieldWithHOC?: T;
customClientFieldWithCondition?: T;