import { useMemo, type ReactNode, type RefObject } from "react"; import { Box, Input, Text, useUiCapabilities, type InputRenderable } from "../../ui"; import { colors } from "../../theme/colors"; import { Icon } from "./icon"; export interface InlineQuickAddRowProps { value: string; active: boolean; paneFocused: boolean; width: number; rowWidth?: number | "100%"; placeholder: string; inputRef: RefObject; preview?: ReactNode; minInputWidth?: number; maxInputWidth?: number; onFocusRequest: () => void; onChange: (value: string) => void; onSubmit: (value: string) => void; onFocus?: () => void; onBlur: () => void; onCancel?: () => void; } /** Shared one-row composer used by pane-native inline add flows. */ export function InlineQuickAddRow({ value, active, paneFocused, width, rowWidth = "100%", placeholder, inputRef, preview, minInputWidth, maxInputWidth = 18, onFocusRequest, onChange, onSubmit, onFocus, onBlur, onCancel, }: InlineQuickAddRowProps) { const inputWidth = useMemo(() => { const queryWidth = [...value.trim()].length; if (queryWidth > 0) { // A typed query hugs its preview unless the caller asked for a wider field. return Math.max(minInputWidth ?? 4, Math.min(maxInputWidth, queryWidth + 1)); } return Math.max(minInputWidth ?? 6, Math.min(10, Math.floor(width * 0.18))); }, [maxInputWidth, minInputWidth, value, width]); const hasPreview = preview !== undefined && preview !== null; const previewWidth = Math.max(4, width - inputWidth - 5); const { nativePaneChrome } = useUiCapabilities(); const handleMouseDown = (event: { preventDefault?: () => void; target?: { tagName?: string }; }) => { if (event.target?.tagName?.toUpperCase() !== "INPUT") { event.preventDefault?.(); } onFocusRequest(); }; const input = ( ); if (nativePaneChrome) { // A row across the pane is a band like the query bar: chrome height, the // field as its leading segment. A fixed-width control (a chart's series // picker) keeps its one-cell row. const band = rowWidth === "100%"; return ( {input} {hasPreview ? ( {preview} ) : null} ); } return ( + {input} {hasPreview ? ( <> {preview} ) : null} ); }