Skip to content

Commit 67b7a73

Browse files
authored
docs: improve lexical code block documentation (#11416)
The existing code example had type errors when `strict: true` was enabled
1 parent 88a2841 commit 67b7a73

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

docs/rich-text/custom-features.mdx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Using the BlocksFeature, you can add both inline blocks (= can be inserted into
2929

3030
### Example: Code Field Block with language picker
3131

32-
This example demonstrates how to create a custom code field block with a language picker using the `BlocksFeature`. Make sure to manually install `@payloadcms/ui`first.
32+
This example demonstrates how to create a custom code field block with a language picker using the `BlocksFeature`. First, make sure to explicitly install `@payloadcms/ui` in your project.
3333

34-
Field config:
34+
Field Config:
3535

3636
```ts
3737
import {
@@ -91,7 +91,6 @@ CodeComponent.tsx:
9191

9292
```tsx
9393
'use client'
94-
9594
import type { CodeFieldClient, CodeFieldClientProps } from 'payload'
9695

9796
import { CodeField, useFormFields } from '@payloadcms/ui'
@@ -105,6 +104,8 @@ const languageKeyToMonacoLanguageMap = {
105104
tsx: 'typescript',
106105
}
107106

107+
type Language = keyof typeof languageKeyToMonacoLanguageMap
108+
108109
export const Code: React.FC<CodeFieldClientProps> = ({
109110
autoComplete,
110111
field,
@@ -118,20 +119,21 @@ export const Code: React.FC<CodeFieldClientProps> = ({
118119
}) => {
119120
const languageField = useFormFields(([fields]) => fields['language'])
120121

121-
const language: string =
122-
(languageField?.value as string) || (languageField.initialValue as string) || 'typescript'
122+
const language: Language =
123+
(languageField?.value as Language) || (languageField?.initialValue as Language) || 'ts'
123124

124-
const label = languages[language as keyof typeof languages]
125+
const label = languages[language]
125126

126127
const props: CodeFieldClient = useMemo<CodeFieldClient>(
127128
() => ({
128129
...field,
129130
type: 'code',
130131
admin: {
131132
...field.admin,
132-
label,
133+
editorOptions: undefined,
133134
language: languageKeyToMonacoLanguageMap[language] || language,
134135
},
136+
label,
135137
}),
136138
[field, language, label],
137139
)

0 commit comments

Comments
 (0)