'use client' import type { TextFieldClientComponent } from 'payload' import { FieldDescription, FieldLabel, useField, useTranslation } from '@payloadcms/ui' import { useState } from 'react' import { ipalT } from '../../../modules/admin-i18n/t.js' import { SECRET_CLEAR, SECRET_MASK } from '../../../modules/secrets/constants.js' /** * Text input for a secret (SMTP password, Turnstile secret key, …). * * With a write-only field (modules/secrets/writeOnly.ts — the default for the plugin's own * secrets) the stored value never reaches the browser: the field arrives as `SECRET_MASK`. * The input then stays empty and says that a value is set; typing replaces it, leaving it * alone keeps it, "Remove the stored value" clears it on save. * * Without write-only (`secrets: { writeOnly: false }`, or a plain text field of a project) the * value itself arrives; it is drawn as dots with a Show / Hide toggle, as before. * * Uses a raw driven by useField rather than @payloadcms/ui's TextInput, because * TextInput doesn't expose inputProps, so its inner input cannot be type="password". * * Assigned via a field's admin.components.Field. Works on any `text` field. */ export const MaskedField: TextFieldClientComponent = ({ field, path }) => { const { t } = useTranslation() const { errorMessage, setValue, showError, value } = useField({ path }) const [revealed, setRevealed] = useState(false) // A string or one string per language — FieldLabel / FieldDescription translate it. const label = field?.label || field?.name || path const description = field?.admin?.description const stored = value === SECRET_MASK // a value is set on the server; this is not it const removing = value === SECRET_CLEAR const link = { background: 'none', border: 'none', cursor: 'pointer', font: 'inherit', opacity: 0.75, padding: '.25rem .5rem', textDecoration: 'underline', whiteSpace: 'nowrap' } as const return (
{/* Labelled by above — the rule can't see through the component. */} {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} setValue(e.target.value)} placeholder={stored ? '••••••••' : undefined} style={{ flex: 1 }} type={revealed ? 'text' : 'password'} value={stored || removing ? '' : (value ?? '')} /> {stored ? ( ) : removing ? ( ) : ( )}
{showError && errorMessage ? (

{errorMessage}

) : null} {stored || removing ? (

{ipalT(t as never, stored ? 'maskedSet' : 'maskedRemoved')}

) : null} {description && typeof description !== 'function' ? ( ) : null}
) } export default MaskedField