fix(richtext-lexical): toggling between internal and custom links does not update fields (#11074)

Fixes https://github.com/payloadcms/payload/issues/11062

In https://github.com/payloadcms/payload/pull/9869 we fixed a bug where `data` passed to lexical fields did not reflect the document data. Our LinkFeature, however, was depending on this incorrect behavior. This PR updates the LinkFeature field conditions to depend on the `siblingData` instead of `data`
This commit is contained in:
Alessio Gravili
2025-02-10 00:05:23 -07:00
committed by GitHub
parent c6c65ac842
commit 9fb7160c2c
3 changed files with 23 additions and 17 deletions

View File

@@ -4,9 +4,12 @@ import type {
RadioField, RadioField,
SanitizedConfig, SanitizedConfig,
TextField, TextField,
TextFieldSingleValidation,
User, User,
} from 'payload' } from 'payload'
import type { LinkFields } from '../nodes/types.js'
import { validateUrl, validateUrlMinimal } from '../../../lexical/utils/url.js' import { validateUrl, validateUrlMinimal } from '../../../lexical/utils/url.js'
export const getBaseFields = ( export const getBaseFields = (
@@ -80,15 +83,14 @@ export const getBaseFields = (
}, },
label: ({ t }) => t('fields:enterURL'), label: ({ t }) => t('fields:enterURL'),
required: true, required: true,
// @ts-expect-error - TODO: fix this validate: ((value: string, options) => {
validate: (value: string, options) => { if ((options?.siblingData as LinkFields)?.linkType === 'internal') {
if (options?.siblingData?.linkType === 'internal') {
return // no validation needed, as no url should exist for internal links return // no validation needed, as no url should exist for internal links
} }
if (!validateUrlMinimal(value)) { if (!validateUrlMinimal(value)) {
return 'Invalid URL' return 'Invalid URL'
} }
}, }) as TextFieldSingleValidation,
}, },
] ]
@@ -99,14 +101,16 @@ export const getBaseFields = (
value: 'internal', value: 'internal',
}) })
;(baseFields[2] as TextField).admin = { ;(baseFields[2] as TextField).admin = {
condition: ({ linkType }) => linkType !== 'internal', condition: (_data, _siblingData) => {
return _siblingData.linkType !== 'internal'
},
} }
baseFields.push({ baseFields.push({
name: 'doc', name: 'doc',
admin: { admin: {
condition: ({ linkType }) => { condition: (_data, _siblingData) => {
return linkType === 'internal' return _siblingData.linkType === 'internal'
}, },
}, },
// when admin.hidden is a function we need to dynamically call hidden with the user to know if the collection should be shown // when admin.hidden is a function we need to dynamically call hidden with the user to know if the collection should be shown

View File

@@ -1,4 +1,4 @@
import { Config } from 'payload' import { Config, type TextFieldSingleValidation } from 'payload'
import { import {
BoldFeature, BoldFeature,
ItalicFeature, ItalicFeature,
@@ -6,6 +6,7 @@ import {
ParagraphFeature, ParagraphFeature,
lexicalEditor, lexicalEditor,
UnderlineFeature, UnderlineFeature,
type LinkFields,
} from '@payloadcms/richtext-lexical' } from '@payloadcms/richtext-lexical'
export const defaultLexical: Config['editor'] = lexicalEditor({ export const defaultLexical: Config['editor'] = lexicalEditor({
@@ -29,16 +30,16 @@ export const defaultLexical: Config['editor'] = lexicalEditor({
name: 'url', name: 'url',
type: 'text', type: 'text',
admin: { admin: {
condition: ({ linkType }) => linkType !== 'internal', condition: (_data, siblingData) => siblingData?.linkType !== 'internal',
}, },
label: ({ t }) => t('fields:enterURL'), label: ({ t }) => t('fields:enterURL'),
required: true, required: true,
validate: (value: any, options: any) => { validate: ((value, options) => {
if (options?.siblingData?.linkType === 'internal') { if ((options?.siblingData as LinkFields)?.linkType === 'internal') {
return true // no validation needed, as no url should exist for internal links return true // no validation needed, as no url should exist for internal links
} }
return value ? true : 'URL is required' return value ? true : 'URL is required'
}, }) as TextFieldSingleValidation,
}, },
] ]
}, },

View File

@@ -1,4 +1,4 @@
import { Config } from 'payload' import { Config, type TextFieldSingleValidation } from 'payload'
import { import {
BoldFeature, BoldFeature,
ItalicFeature, ItalicFeature,
@@ -6,6 +6,7 @@ import {
ParagraphFeature, ParagraphFeature,
lexicalEditor, lexicalEditor,
UnderlineFeature, UnderlineFeature,
type LinkFields,
} from '@payloadcms/richtext-lexical' } from '@payloadcms/richtext-lexical'
export const defaultLexical: Config['editor'] = lexicalEditor({ export const defaultLexical: Config['editor'] = lexicalEditor({
@@ -29,16 +30,16 @@ export const defaultLexical: Config['editor'] = lexicalEditor({
name: 'url', name: 'url',
type: 'text', type: 'text',
admin: { admin: {
condition: ({ linkType }) => linkType !== 'internal', condition: (_data, siblingData) => siblingData?.linkType !== 'internal',
}, },
label: ({ t }) => t('fields:enterURL'), label: ({ t }) => t('fields:enterURL'),
required: true, required: true,
validate: (value: any, options: any) => { validate: ((value, options) => {
if (options?.siblingData?.linkType === 'internal') { if ((options?.siblingData as LinkFields)?.linkType === 'internal') {
return true // no validation needed, as no url should exist for internal links return true // no validation needed, as no url should exist for internal links
} }
return value ? true : 'URL is required' return value ? true : 'URL is required'
}, }) as TextFieldSingleValidation,
}, },
] ]
}, },