From ed368cd251b237313b4c2063aa530aca79be93e9 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 19 Sep 2024 08:55:35 -0700 Subject: [PATCH] Fix pair editor adding new entry when clicking below --- src-web/components/core/PairEditor.tsx | 81 +++++++++++++++++++------- src-web/components/core/PlainInput.tsx | 2 +- 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src-web/components/core/PairEditor.tsx b/src-web/components/core/PairEditor.tsx index 8ff6a275..0328bfee 100644 --- a/src-web/components/core/PairEditor.tsx +++ b/src-web/components/core/PairEditor.tsx @@ -23,6 +23,7 @@ import { Icon } from './Icon'; import { IconButton } from './IconButton'; import type { InputProps } from './Input'; import { Input } from './Input'; +import { PlainInput } from './PlainInput'; import { RadioDropdown } from './RadioDropdown'; export interface PairEditorRef { @@ -172,7 +173,14 @@ export const PairEditor = forwardRef(function Pa setForceFocusNamePairId(null); // Remove focus override when something focused setForceFocusValuePairId(null); // Remove focus override when something focused const isLast = pair.id === pairs[pairs.length - 1]?.id; - return isLast ? [...pairs, newPairContainer()] : pairs; + if (isLast) { + const newPair = newPairContainer(); + const prevPair = pairs[pairs.length - 1]; + setForceFocusNamePairId(prevPair?.id ?? null); + return [...pairs, newPair]; + } else { + return pairs; + } }), [], ); @@ -192,7 +200,7 @@ export const PairEditor = forwardRef(function Pa 'pb-2 mb-auto h-full', !noScroll && 'overflow-y-auto max-h-full', // Move over the width of the drag handle - '-ml-3', + '-ml-3 -mr-2 pr-2', // Pad to make room for the drag divider 'pt-0.5', )} @@ -224,6 +232,7 @@ export const PairEditor = forwardRef(function Pa onDelete={handleDelete} onEnd={handleEnd} onMove={handleMove} + index={i} /> ); @@ -248,6 +257,7 @@ type PairEditorRowProps = { onFocus?: (pair: PairContainer) => void; onSubmit?: (pair: PairContainer) => void; isLast?: boolean; + index: number; } & Pick< PairEditorProps, | 'nameAutocomplete' @@ -270,6 +280,7 @@ function PairEditorRow({ forceFocusValuePairId, forceUpdateKey, isLast, + index, nameAutocomplete, nameAutocompleteVariables, namePlaceholder, @@ -406,25 +417,39 @@ function PairEditorRow({ 'gap-0.5 grid-cols-1 grid-rows-2', )} > - + {isLast ? ( + // Use PlainInput for last ones because there's a unique bug where clicking below + // the Codemirror input focuses it. + + ) : ( + + )}
{pairContainer.pair.isFile ? ( + ) : isLast ? ( + // Use PlainInput for last ones because there's a unique bug where clicking below + // the Codemirror input focuses it. + ) : ( (function const inputClassName = classNames( className, '!bg-transparent min-w-0 h-auto w-full focus:outline-none placeholder:text-placeholder', - 'px-1.5 text-xs font-mono cursor-text', + 'px-2 text-xs font-mono cursor-text', ); const isValid = useMemo(() => {