/** * ScriptArguments Component * * Displays and manages script arguments as key-value pairs. * Each argument has a name (key) and value input field with delete button. * Includes an "Add Script Argument" button to add new entries. */ import { Trash2 } from 'lucide-react' import React from 'react' import { cn } from '../../utils/cn' import { Button } from '../ui/button' import { Input } from '../ui/input' import { Label } from '../ui/label' import { PlusCircleIcon } from '../icons-v2-generated' /** * Single script argument with key and value */ export interface ScriptArgument { /** Unique identifier for the argument */ id: string /** Argument name/key */ key: string /** Argument value (empty string for flag-type arguments) */ value: string } /** * Props for ScriptArguments component */ export interface ScriptArgumentsProps { /** Array of script arguments */ arguments: ScriptArgument[] /** Callback when arguments change */ onArgumentsChange?: (args: ScriptArgument[]) => void /** Placeholder for key input */ keyPlaceholder?: string /** Placeholder for value input */ valuePlaceholder?: string /** Label for the add button */ addButtonLabel?: string /** Whether the component is disabled */ disabled?: boolean /** Additional CSS classes */ className?: string /** Label for the title input */ titleLabel: string } /** * ScriptArguments - Displays and manages script arguments * * @example * ```tsx * setArguments(args)} * /> * ``` */ export const ScriptArguments: React.FC = ({ arguments: args, onArgumentsChange, keyPlaceholder = 'Enter Key', valuePlaceholder = 'Enter Value (empty=flag)', addButtonLabel = 'Add Script Argument', disabled = false, className, titleLabel }) => { const handleKeyChange = (id: string, newKey: string) => { if (!onArgumentsChange) return const updated = args.map((arg) => arg.id === id ? { ...arg, key: newKey } : arg ) onArgumentsChange(updated) } const handleValueChange = (id: string, newValue: string) => { if (!onArgumentsChange) return const updated = args.map((arg) => arg.id === id ? { ...arg, value: newValue } : arg ) onArgumentsChange(updated) } const handleDelete = (id: string) => { if (!onArgumentsChange) return const updated = args.filter((arg) => arg.id !== id) onArgumentsChange(updated) } const handleAdd = () => { if (!onArgumentsChange) return const newArg: ScriptArgument = { id: crypto.randomUUID(), key: '', value: '' } onArgumentsChange([...args, newArg]) } const isFirstRow = (index: number) => index === 0 return (
{args.map((arg, index) => (
{/* Key input - with label only on first row */}
{isFirstRow(index) && ( )} handleKeyChange(arg.id, e.target.value)} placeholder={keyPlaceholder} disabled={disabled} />
{/* Value input with delete button */} {/* Value input with delete button */}
handleValueChange(arg.id, e.target.value)} placeholder={valuePlaceholder} disabled={disabled} className={cn( !arg.value && 'placeholder:text-ods-text-muted' )} />
))} {/* Add button - aligned left */}
) } ScriptArguments.displayName = 'ScriptArguments'