Files
ipal-kit/dist/globals/SiteIntegrations/components/MaskedField.js
T
2026-10-15 15:53:32 +02:00

80 lines
3.3 KiB
JavaScript

'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useField, FieldLabel, FieldDescription } from '@payloadcms/ui';
import { useState } from 'react';
/**
* Masked text input for sensitive values (SMTP password, R2 secret key,
* Turnstile secret key, etc.).
*
* Renders the value as a password field (••••) so secrets aren't shown in
* plaintext on screen — protecting against shoulder-surfing and accidental
* exposure when the panel is open. The value is still stored and returned
* normally (UI masking, NOT hashing or encryption), because the plugin must
* read it back to authenticate with SMTP/R2/Turnstile. A toggle reveals the
* value when an authorised editor genuinely needs to check it.
*
* Uses a raw <input> driven by useField rather than @payloadcms/ui's TextInput,
* because TextInput doesn't expose inputProps, so there's no way to switch its
* inner input to type="password".
*
* Assigned via a field's admin.components.Field. Works on any `text` field.
*/ export const MaskedField = ({ field, path })=>{
const { value, setValue } = useField({
path
});
const [revealed, setRevealed] = useState(false);
const label = typeof field?.label === 'string' ? field.label : field?.name ?? path;
const description = field?.admin?.description;
return /*#__PURE__*/ _jsxs("div", {
className: "field-type text masked-field",
children: [
/*#__PURE__*/ _jsx(FieldLabel, {
label: label,
htmlFor: `field-${path}`
}),
/*#__PURE__*/ _jsxs("div", {
className: "field-type__wrap",
style: {
display: 'flex',
gap: '.5rem',
alignItems: 'center'
},
children: [
/*#__PURE__*/ _jsx("input", {
id: `field-${path}`,
type: revealed ? 'text' : 'password',
autoComplete: "off",
className: "field-type__input",
value: value ?? '',
onChange: (e)=>setValue(e.target.value),
style: {
flex: 1
}
}),
/*#__PURE__*/ _jsx("button", {
type: "button",
onClick: ()=>setRevealed((r)=>!r),
style: {
background: 'none',
border: 'none',
padding: '.25rem .5rem',
cursor: 'pointer',
font: 'inherit',
textDecoration: 'underline',
opacity: 0.75,
whiteSpace: 'nowrap'
},
children: revealed ? 'Hide' : 'Reveal'
})
]
}),
description && typeof description === 'string' ? /*#__PURE__*/ _jsx(FieldDescription, {
path: path,
description: description
}) : null
]
});
};
export default MaskedField;
//# sourceMappingURL=MaskedField.js.map