|
| 1 | +import * as React from 'react'; |
| 2 | +import { cn } from '@/utils/style'; |
| 3 | + |
| 4 | +export interface SecretInputProps |
| 5 | + extends Omit< |
| 6 | + React.InputHTMLAttributes<HTMLInputElement>, |
| 7 | + 'type' | 'value' | 'onChange' |
| 8 | + > { |
| 9 | + value?: string | null; |
| 10 | + onChange?: (value: string) => void; |
| 11 | + /** |
| 12 | + * The placeholder to show when the field has an existing value. |
| 13 | + * @default "******" |
| 14 | + */ |
| 15 | + maskPlaceholder?: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * SecretInput - A secure input component for sensitive data like API keys |
| 20 | + * |
| 21 | + * Features: |
| 22 | + * - Shows masked placeholder (******) when there's an existing value |
| 23 | + * - Actual field value is empty until user starts typing |
| 24 | + * - Clears the masked placeholder on focus |
| 25 | + * - Only submits new value if user has entered something |
| 26 | + * - Perfect for edit forms where you don't want to expose existing secrets |
| 27 | + * |
| 28 | + * @example |
| 29 | + * ```tsx |
| 30 | + * <SecretInput |
| 31 | + * value={existingApiKey} |
| 32 | + * onChange={(newValue) => setApiKey(newValue)} |
| 33 | + * placeholder="Enter your API key" |
| 34 | + * /> |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +const SecretInput = React.forwardRef<HTMLInputElement, SecretInputProps>( |
| 38 | + ( |
| 39 | + { |
| 40 | + className, |
| 41 | + value, |
| 42 | + onChange, |
| 43 | + maskPlaceholder = '******', |
| 44 | + placeholder, |
| 45 | + ...props |
| 46 | + }, |
| 47 | + ref |
| 48 | + ) => { |
| 49 | + const [internalValue, setInternalValue] = React.useState(''); |
| 50 | + const [isFocused, setIsFocused] = React.useState(false); |
| 51 | + const [hasBeenTouched, setHasBeenTouched] = React.useState(false); |
| 52 | + |
| 53 | + // Check if there's an existing value (from props) |
| 54 | + const hasExistingValue = Boolean(value); |
| 55 | + |
| 56 | + // Determine what placeholder to show |
| 57 | + const effectivePlaceholder = React.useMemo(() => { |
| 58 | + if (hasBeenTouched) { |
| 59 | + // After user has interacted, show normal placeholder |
| 60 | + return placeholder; |
| 61 | + } |
| 62 | + if (hasExistingValue && !isFocused) { |
| 63 | + // Show masked placeholder when there's existing value and not focused |
| 64 | + return maskPlaceholder; |
| 65 | + } |
| 66 | + // Show normal placeholder |
| 67 | + return placeholder; |
| 68 | + }, [ |
| 69 | + hasBeenTouched, |
| 70 | + hasExistingValue, |
| 71 | + isFocused, |
| 72 | + placeholder, |
| 73 | + maskPlaceholder, |
| 74 | + ]); |
| 75 | + |
| 76 | + const handleFocus = (e: React.FocusEvent<HTMLInputElement>) => { |
| 77 | + setIsFocused(true); |
| 78 | + setHasBeenTouched(true); |
| 79 | + props.onFocus?.(e); |
| 80 | + }; |
| 81 | + |
| 82 | + const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => { |
| 83 | + setIsFocused(false); |
| 84 | + props.onBlur?.(e); |
| 85 | + }; |
| 86 | + |
| 87 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 88 | + const newValue = e.target.value; |
| 89 | + setInternalValue(newValue); |
| 90 | + setHasBeenTouched(true); |
| 91 | + onChange?.(newValue); |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <input |
| 96 | + type="password" |
| 97 | + className={cn( |
| 98 | + 'flex h-9 w-full rounded-md border border-zinc-200 bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-zinc-500 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-zinc-950 disabled:cursor-not-allowed disabled:opacity-50 dark:border-zinc-800 dark:placeholder:text-zinc-400 dark:focus-visible:ring-zinc-300', |
| 99 | + className |
| 100 | + )} |
| 101 | + ref={ref} |
| 102 | + value={internalValue} |
| 103 | + onChange={handleChange} |
| 104 | + onFocus={handleFocus} |
| 105 | + onBlur={handleBlur} |
| 106 | + placeholder={effectivePlaceholder} |
| 107 | + {...props} |
| 108 | + /> |
| 109 | + ); |
| 110 | + } |
| 111 | +); |
| 112 | +SecretInput.displayName = 'SecretInput'; |
| 113 | + |
| 114 | +export { SecretInput }; |
0 commit comments