Skip to content

Commit dc6d79e

Browse files
authored
fix(richtext-lexical): adds support for localized placeholder (#10523)
### What? This PR fixes a minor issue in `richtext-lexical` where editor placeholders were not localized. ### Why? To allow users to localize placeholders accordingly with their language preferences in config. ### How? By evaluating the placeholder in the editor RSC, if any is provided. The `ContentEditable` component falls back to a default in the event that no placeholder was provided as this was the existing behavior. Fixes #10518
1 parent c850bd4 commit dc6d79e

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

packages/richtext-lexical/src/field/rscEntry.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import type {
77
ServerComponentProps,
88
} from 'payload'
99

10+
import { getTranslation } from '@payloadcms/translations'
1011
import { renderField } from '@payloadcms/ui/forms/renderField'
1112
import React from 'react'
1213

1314
import type { SanitizedServerEditorConfig } from '../lexical/config/types.js'
14-
import type { LexicalFieldAdminProps, LexicalRichTextFieldProps } from '../types.js'
15+
import type {
16+
LexicalFieldAdminClientProps,
17+
LexicalFieldAdminProps,
18+
LexicalRichTextFieldProps,
19+
} from '../types.js'
1520

1621
// eslint-disable-next-line payload/no-imports-from-exports-dir
1722
import { RichTextField } from '../exports/client/index.js'
@@ -64,8 +69,20 @@ export const RscEntryLexicalField: React.FC<
6469
})
6570
}
6671

72+
const placeholderFromArgs = args.admin?.placeholder
73+
const placeholder = placeholderFromArgs
74+
? getTranslation(placeholderFromArgs, args.i18n)
75+
: undefined
76+
77+
const admin: LexicalFieldAdminClientProps = {}
78+
if (placeholder) {
79+
admin.placeholder = placeholder
80+
}
81+
if (args.admin?.hideGutter) {
82+
admin.hideGutter = true
83+
}
84+
6785
const props: LexicalRichTextFieldProps = {
68-
admin: args.admin,
6986
clientFeatures,
7087
featureClientSchemaMap, // TODO: Does client need this? Why cant this just live in the server
7188
field: args.clientField as RichTextFieldClient,
@@ -78,6 +95,9 @@ export const RscEntryLexicalField: React.FC<
7895
renderedBlocks: args.renderedBlocks,
7996
schemaPath,
8097
}
98+
if (Object.keys(admin).length) {
99+
props.admin = admin
100+
}
81101

82102
for (const key in props) {
83103
if (props[key as keyof LexicalRichTextFieldProps] === undefined) {

packages/richtext-lexical/src/lexical/config/client/sanitize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
ResolvedClientFeatureMap,
77
SanitizedClientFeatures,
88
} from '../../../features/typesClient.js'
9-
import type { LexicalFieldAdminProps } from '../../../types.js'
9+
import type { LexicalFieldAdminClientProps } from '../../../types.js'
1010
import type { SanitizedClientEditorConfig } from '../types.js'
1111

1212
export const sanitizeClientFeatures = (
@@ -219,7 +219,7 @@ export const sanitizeClientFeatures = (
219219
export function sanitizeClientEditorConfig(
220220
resolvedClientFeatureMap: ResolvedClientFeatureMap,
221221
lexical?: LexicalEditorConfig,
222-
admin?: LexicalFieldAdminProps,
222+
admin?: LexicalFieldAdminClientProps,
223223
): SanitizedClientEditorConfig {
224224
return {
225225
admin,

packages/richtext-lexical/src/lexical/config/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
ResolvedServerFeatureMap,
1111
SanitizedServerFeatures,
1212
} from '../../features/typesServer.js'
13-
import type { LexicalFieldAdminProps } from '../../types.js'
13+
import type { LexicalFieldAdminClientProps } from '../../types.js'
1414

1515
export type ServerEditorConfig = {
1616
features: FeatureProviderServer<any, any, any>[]
@@ -29,7 +29,7 @@ export type ClientEditorConfig = {
2929
}
3030

3131
export type SanitizedClientEditorConfig = {
32-
admin?: LexicalFieldAdminProps
32+
admin?: LexicalFieldAdminClientProps
3333
features: SanitizedClientFeatures
3434
lexical: LexicalEditorConfig
3535
resolvedFeatureMap: ResolvedClientFeatureMap

packages/richtext-lexical/src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import type { EditorConfig as LexicalEditorConfig, SerializedEditorState } from
22
import type {
33
ClientField,
44
DefaultCellComponentProps,
5+
LabelFunction,
56
RichTextAdapter,
67
RichTextFieldClient,
78
RichTextFieldClientProps,
89
SanitizedConfig,
910
ServerFieldBase,
11+
StaticLabel,
1012
} from 'payload'
1113

1214
import type {
@@ -25,9 +27,13 @@ export type LexicalFieldAdminProps = {
2527
/**
2628
* Changes the placeholder text in the editor if no content is present.
2729
*/
28-
placeholder?: string
30+
placeholder?: LabelFunction | StaticLabel
2931
}
3032

33+
export type LexicalFieldAdminClientProps = {
34+
placeholder?: string
35+
} & Omit<LexicalFieldAdminProps, 'placeholder'>
36+
3137
export type LexicalEditorProps = {
3238
admin?: LexicalFieldAdminProps
3339
features?:
@@ -92,7 +98,7 @@ export type FeatureClientSchemaMap = {
9298
}
9399

94100
export type LexicalRichTextFieldProps = {
95-
admin: LexicalFieldAdminProps
101+
admin?: LexicalFieldAdminClientProps
96102
// clientFeatures is added through the rsc field
97103
clientFeatures: {
98104
[featureKey: string]: {

0 commit comments

Comments
 (0)