All files / src/components/settings/common DebouncedTextField.tsx

82.35% Statements 14/17
100% Branches 1/1
50% Functions 1/2
82.35% Lines 14/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x 1x                                     1x     1x 1x 45x   45x 45x 45x 45x       45x 45x 45x     45x  
import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
import { useDebouncedInput } from "../utils/useDebouncedInput"
 
/**
 * Props for the DebouncedTextField component
 */
interface DebouncedTextFieldProps {
	// Custom props for debouncing functionality
	initialValue: string
	onChange: (value: string) => void
 
	// Common VSCodeTextField props
	style?: React.CSSProperties
	type?: "text" | "password"
	placeholder?: string
	id?: string
	children?: React.ReactNode
	disabled?: boolean
}
 
/**
 * A wrapper around VSCodeTextField that automatically handles debounced input
 * to prevent excessive API calls while typing
 */
export const DebouncedTextField = ({ initialValue, onChange, children, type, ...otherProps }: DebouncedTextFieldProps) => {
	const [localValue, setLocalValue] = useDebouncedInput(initialValue, onChange)
 
	return (
		<VSCodeTextField
			{...otherProps}
			onInput={(e: any) => {
				const value = e.target.value
				setLocalValue(value)
			}}
			type={type}
			value={localValue}>
			{children}
		</VSCodeTextField>
	)
}