'use client' import type { CodeFieldClientProps } from 'payload' import { getTranslation } from '@payloadcms/translations' import React, { useCallback, useEffect, useMemo, useState } from 'react' import { CodeEditor } from '../../elements/CodeEditor/index.js' import { RenderCustomComponent } from '../../elements/RenderCustomComponent/index.js' import { FieldDescription } from '../../fields/FieldDescription/index.js' import { FieldError } from '../../fields/FieldError/index.js' import { FieldLabel } from '../../fields/FieldLabel/index.js' import { useField } from '../../forms/useField/index.js' import { withCondition } from '../../forms/withCondition/index.js' import { useTranslation } from '../../providers/Translation/index.js' import { mergeFieldStyles } from '../mergeFieldStyles.js' import { fieldBaseClass } from '../shared/index.js' import './index.css' const prismToMonacoLanguageMap = { js: 'javascript', ts: 'typescript', tsx: 'typescript', } const baseClass = 'code-field' const CodeFieldComponent: React.FC = (props) => { const { field, field: { admin: { className, description, editorOptions, editorProps, language = 'javascript' } = {}, label, localized, required, }, onMount, path: pathFromProps, readOnly, validate, } = props const { i18n } = useTranslation() const codeEditorProps = editorProps as | Partial> | undefined const editorRef = React.useRef['onMount']>>[0]>( null, ) const inputChangeFromRef = React.useRef<'formState' | 'internalEditor'>('formState') // A counter that tells CodeEditor to recalculate its height const [recalculatedHeightAt, setRecalculatedHeightAt] = useState(0) const memoizedValidate = useCallback( (value, options) => { if (typeof validate === 'function') { return validate(value, { ...options, required }) } }, [validate, required], ) const { customComponents: { AfterInput, BeforeInput, Description, Error, Label } = {}, disabled, initialValue, path, setValue, showError, value, } = useField({ potentiallyStalePath: pathFromProps, validate: memoizedValidate, }) const stringValueRef = React.useRef( (value || initialValue) !== undefined ? (value ?? initialValue) : undefined, ) const handleChange = useCallback( (val: string) => { if (readOnly || disabled) { return } inputChangeFromRef.current = 'internalEditor' try { setValue(val ? val : null) stringValueRef.current = val } catch (e) { setValue(val ? val : null) stringValueRef.current = val } }, [readOnly, disabled, setValue], ) useEffect(() => { if (inputChangeFromRef.current === 'formState') { stringValueRef.current = (value || initialValue) !== undefined ? (value ?? initialValue) : undefined setRecalculatedHeightAt((count) => count + 1) } inputChangeFromRef.current = 'formState' }, [initialValue, path, value]) const styles = useMemo(() => mergeFieldStyles(field), [field]) return (
editorRef.current?.focus()} path={path} required={required} /> } />
} /> {BeforeInput} { editorRef.current = editor ;(codeEditorProps?.onMount ?? onMount)?.(editor, monaco) }} options={{ ariaLabel: getTranslation(label || '', i18n) || undefined, ariaRequired: required, ...((codeEditorProps?.options ?? editorOptions) || {}), }} /> {AfterInput}
} />
) } export const CodeField = withCondition(CodeFieldComponent)