Fix turnstile
This commit is contained in:
+51
-25
@@ -1,51 +1,77 @@
|
||||
'use client';
|
||||
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
||||
import { useField, FieldLabel, FieldDescription } from '@payloadcms/ui';
|
||||
import { useState } from 'react';
|
||||
export const MaskedField = (props)=>{
|
||||
const { field, path, value: propValue, setValue: propSetValue, onChange: propOnChange } = props || {};
|
||||
const [internalValue, setInternalValue] = useState(propValue ?? '');
|
||||
/**
|
||||
* 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 handleChange = (e)=>{
|
||||
const newVal = e.target.value;
|
||||
setInternalValue(newVal);
|
||||
if (typeof propSetValue === 'function') {
|
||||
propSetValue(newVal);
|
||||
}
|
||||
if (typeof propOnChange === 'function') {
|
||||
propOnChange(e);
|
||||
}
|
||||
};
|
||||
const currentValue = propValue !== undefined ? propValue : internalValue;
|
||||
const description = field?.admin?.description;
|
||||
return /*#__PURE__*/ _jsxs("div", {
|
||||
className: "field-type text",
|
||||
className: "field-type text masked-field",
|
||||
children: [
|
||||
label && /*#__PURE__*/ _jsx("label", {
|
||||
className: "field-label",
|
||||
children: label
|
||||
/*#__PURE__*/ _jsx(FieldLabel, {
|
||||
label: label,
|
||||
htmlFor: `field-${path}`
|
||||
}),
|
||||
/*#__PURE__*/ _jsxs("div", {
|
||||
className: "field-type__wrap",
|
||||
style: {
|
||||
display: 'flex',
|
||||
gap: '.5rem'
|
||||
gap: '.5rem',
|
||||
alignItems: 'center'
|
||||
},
|
||||
children: [
|
||||
/*#__PURE__*/ _jsx("input", {
|
||||
id: `field-${path}`,
|
||||
type: revealed ? 'text' : 'password',
|
||||
autoComplete: "off",
|
||||
onChange: handleChange,
|
||||
className: "field-type__input",
|
||||
value: value ?? '',
|
||||
onChange: (e)=>setValue(e.target.value),
|
||||
style: {
|
||||
flex: 1
|
||||
},
|
||||
type: revealed ? 'text' : 'password',
|
||||
value: currentValue ?? ''
|
||||
}
|
||||
}),
|
||||
/*#__PURE__*/ _jsx("button", {
|
||||
onClick: ()=>setRevealed((r)=>!r),
|
||||
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
|
||||
]
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user