Skip to content

Commit 79b4e4c

Browse files
authored
fix(ui): hide entire tabs field when admin.condition is false (#16809)
Identical to #16799, back-ported for 3.x.
1 parent 6c81355 commit 79b4e4c

5 files changed

Lines changed: 73 additions & 0 deletions

File tree

packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,16 @@ export const addFieldStatePromise = async (args: AddFieldStatePromiseArgs): Prom
947947
state,
948948
})
949949
} else if (field.type === 'tabs') {
950+
if (!filter || filter(args)) {
951+
state[path] = {
952+
disableFormData: true,
953+
}
954+
955+
if (passesCondition === false) {
956+
state[path].passesCondition = false
957+
}
958+
}
959+
950960
return iterateFields({
951961
id,
952962
addErrorPathToParent: addErrorPathToParentArg,

test/fields/collections/ConditionalLogic/e2e.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,25 @@ describe('Conditional Logic', () => {
289289
await expect(fieldWithOperationCondition).toBeHidden()
290290
})
291291

292+
test('should hide entire tabs field UI when admin.condition is false', async () => {
293+
await page.goto(url.create)
294+
295+
const tabsField = page.locator('.tabs-field').filter({
296+
has: page.locator('button:has-text("Tab With Condition 1")'),
297+
})
298+
299+
await expect(tabsField).toBeHidden()
300+
301+
const enableTabsToggle = page.locator('label[for=field-enableTabs]')
302+
await enableTabsToggle.click()
303+
304+
await expect(tabsField).toBeVisible()
305+
await expect(tabsField.locator('button:has-text("Tab With Condition 1")')).toBeVisible()
306+
307+
await enableTabsToggle.click()
308+
await expect(tabsField).toBeHidden()
309+
})
310+
292311
test('should toggle conditional field when radio changes inside a block', async () => {
293312
await page.goto(url.create)
294313

test/fields/collections/ConditionalLogic/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,38 @@ const ConditionalLogic: CollectionConfig = {
231231
},
232232
],
233233
},
234+
{
235+
name: 'enableTabs',
236+
type: 'checkbox',
237+
},
238+
{
239+
type: 'tabs',
240+
admin: {
241+
condition: ({ enableTabs }) => Boolean(enableTabs),
242+
},
243+
tabs: [
244+
{
245+
label: 'Tab With Condition 1',
246+
description: 'Description for conditional tab 1',
247+
fields: [
248+
{
249+
name: 'conditionalTabsField1',
250+
type: 'text',
251+
},
252+
],
253+
},
254+
{
255+
label: 'Tab With Condition 2',
256+
description: 'Description for conditional tab 2',
257+
fields: [
258+
{
259+
name: 'conditionalTabsField2',
260+
type: 'text',
261+
},
262+
],
263+
},
264+
],
265+
},
234266
{
235267
name: 'arrayOne',
236268
type: 'array',

test/fields/payload-types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ export interface ConditionalLogic {
984984
blockType: 'blockWithRadioCondition';
985985
}[]
986986
| null;
987+
enableTabs?: boolean | null;
988+
conditionalTabsField1?: string | null;
989+
conditionalTabsField2?: string | null;
987990
arrayOne?:
988991
| {
989992
title?: string | null;
@@ -2845,6 +2848,9 @@ export interface ConditionalLogicSelect<T extends boolean = true> {
28452848
blockName?: T;
28462849
};
28472850
};
2851+
enableTabs?: T;
2852+
conditionalTabsField1?: T;
2853+
conditionalTabsField2?: T;
28482854
arrayOne?:
28492855
| T
28502856
| {

test/form-state/int.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ describe('Form State', () => {
138138
})
139139

140140
expect(state).toStrictEqual({
141+
'_index-7': {
142+
disableFormData: true,
143+
},
144+
'_index-7-0-0': {
145+
disableFormData: true,
146+
},
141147
title: {
142148
value: postData.title,
143149
initialValue: postData.title,

0 commit comments

Comments
 (0)