From 9e2803fcfb52cdaf1036deafa643a9b3ca9e8c76 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sun, 29 Oct 2023 10:45:05 -0700 Subject: [PATCH] Remove broken key/value enter/backspace logic --- src-web/components/core/Editor/Editor.tsx | 23 -------- src-web/components/core/Input.tsx | 7 ++- src-web/components/core/PairEditor.tsx | 68 ++++++++--------------- 3 files changed, 29 insertions(+), 69 deletions(-) diff --git a/src-web/components/core/Editor/Editor.tsx b/src-web/components/core/Editor/Editor.tsx index c3f18486..cffc5c74 100644 --- a/src-web/components/core/Editor/Editor.tsx +++ b/src-web/components/core/Editor/Editor.tsx @@ -36,7 +36,6 @@ export interface EditorProps { onChange?: (value: string) => void; onFocus?: () => void; onBlur?: () => void; - onEnter?: () => void; onKeyDown?: (e: KeyboardEvent) => void; singleLine?: boolean; wrapLines?: boolean; @@ -62,7 +61,6 @@ const _Editor = forwardRef(function Editor( onFocus, onBlur, onKeyDown, - onEnter, className, singleLine, format, @@ -85,12 +83,6 @@ const _Editor = forwardRef(function Editor( handleChange.current = onChange; }, [onChange]); - // Use ref so we can update the onChange handler without re-initializing the editor - const handleEnter = useRef(onEnter); - useEffect(() => { - handleEnter.current = onEnter; - }, [onEnter]); - // Use ref so we can update the onChange handler without re-initializing the editor const handleFocus = useRef(onFocus); useEffect(() => { @@ -172,7 +164,6 @@ const _Editor = forwardRef(function Editor( container, readOnly, singleLine, - onEnter: handleEnter, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur, @@ -252,13 +243,11 @@ function getExtensions({ onFocus, onBlur, onKeyDown, - onEnter, }: Pick & { container: HTMLDivElement | null; onChange: MutableRefObject; onFocus: MutableRefObject; onBlur: MutableRefObject; - onEnter: MutableRefObject; onKeyDown: MutableRefObject; }) { // TODO: Ensure tooltips render inside the dialog if we are in one. @@ -276,18 +265,6 @@ function getExtensions({ ...(readOnly ? [EditorState.readOnly.of(true), EditorView.contentAttributes.of({ tabindex: '-1' })] : []), - ...(singleLine - ? [ - EditorView.domEventHandlers({ - keydown: (e) => { - // Submit nearest form on enter if there is one - if (onEnter != null && e.key === 'Enter') { - onEnter.current?.(); - } - }, - }), - ] - : []), // Handle onFocus EditorView.domEventHandlers({ diff --git a/src-web/components/core/Input.tsx b/src-web/components/core/Input.tsx index dc726cb0..bcbed993 100644 --- a/src-web/components/core/Input.tsx +++ b/src-web/components/core/Input.tsx @@ -101,7 +101,10 @@ export const Input = forwardRef(function Inp const wrapperRef = useRef(null); - const handleEnter = useCallback(() => { + // Submit nearest form on Enter key press + const handleKeyDown = useCallback((e: KeyboardEvent) => { + if (e.key !== 'Enter') return; + const form = wrapperRef.current?.closest('form'); if (!isValid || form == null) return; @@ -147,7 +150,7 @@ export const Input = forwardRef(function Inp id={id} singleLine wrapLines={size === 'auto'} - onEnter={handleEnter} + onKeyDown={handleKeyDown} type={type === 'password' && !obscured ? 'text' : type} defaultValue={defaultValue} forceUpdateKey={forceUpdateKey} diff --git a/src-web/components/core/PairEditor.tsx b/src-web/components/core/PairEditor.tsx index c91dd9b1..01e1e547 100644 --- a/src-web/components/core/PairEditor.tsx +++ b/src-web/components/core/PairEditor.tsx @@ -111,15 +111,6 @@ export const PairEditor = memo(function PairEditor({ [hoveredIndex, setPairsAndSave], ); - const handleSubmitRow = useCallback( - (pair: PairContainer) => { - const index = pairs.findIndex((p) => p.id === pair.id); - const id = pairs[index + 1]?.id ?? null; - setForceFocusPairId(id); - }, - [pairs], - ); - const handleChange = useCallback( (pair: PairContainer) => setPairsAndSave((pairs) => pairs.map((p) => (pair.id !== p.id ? p : pair))), @@ -127,18 +118,26 @@ export const PairEditor = memo(function PairEditor({ ); const handleDelete = useCallback( - (pair: PairContainer) => - setPairsAndSave((oldPairs) => oldPairs.filter((p) => p.id !== pair.id)), - [setPairsAndSave], + (pair: PairContainer, focusPrevious: boolean) => { + if (focusPrevious) { + const index = pairs.findIndex((p) => p.id === pair.id); + const id = pairs[index - 1]?.id ?? null; + setForceFocusPairId(id); + } + return setPairsAndSave((oldPairs) => oldPairs.filter((p) => p.id !== pair.id)); + }, + [setPairsAndSave, setForceFocusPairId, pairs], ); - const handleFocus = useCallback((pair: PairContainer) => { - return setPairs((pairs) => { - setForceFocusPairId(null); - const isLast = pair.id === pairs[pairs.length - 1]?.id; - return isLast ? [...pairs, newPairContainer()] : pairs; - }); - }, []); + const handleFocus = useCallback( + (pair: PairContainer) => + setPairs((pairs) => { + setForceFocusPairId(null); // Remove focus override when something focused + const isLast = pair.id === pairs[pairs.length - 1]?.id; + return isLast ? [...pairs, newPairContainer()] : pairs; + }), + [], + ); // Ensure there's always at least one pair useEffect(() => { @@ -177,7 +176,6 @@ export const PairEditor = memo(function PairEditor({ nameValidate={nameValidate} valueValidate={valueValidate} showDelete={!isLast} - onSubmit={handleSubmitRow} onChange={handleChange} onFocus={handleFocus} onDelete={handleDelete} @@ -203,7 +201,7 @@ type FormRowProps = { onMove: (id: string, side: 'above' | 'below') => void; onEnd: (id: string) => void; onChange: (pair: PairContainer) => void; - onDelete?: (pair: PairContainer) => void; + onDelete?: (pair: PairContainer, focusPrevious: boolean) => void; onFocus?: (pair: PairContainer) => void; onSubmit?: (pair: PairContainer) => void; isLast?: boolean; @@ -235,7 +233,6 @@ const FormRow = memo(function FormRow({ onEnd, onFocus, onMove, - onSubmit, pairContainer, showDelete, valueAutocomplete, @@ -268,20 +265,9 @@ const FormRow = memo(function FormRow({ ); const handleFocus = useCallback(() => onFocus?.(pairContainer), [onFocus, pairContainer]); - const handleDelete = useCallback(() => onDelete?.(pairContainer), [onDelete, pairContainer]); - - const handleKeyDownName = useMemo( - () => (e: KeyboardEvent) => { - if ( - e.key === 'Backspace' && - pairContainer.pair.name === '' && - pairContainer.pair.value === '' - ) { - e.preventDefault(); - onDelete?.(pairContainer); - } - }, - [pairContainer, onDelete], + const handleDelete = useCallback( + () => onDelete?.(pairContainer, false), + [onDelete, pairContainer], ); const [, connectDrop] = useDrop( @@ -341,12 +327,7 @@ const FormRow = memo(function FormRow({ className={classNames('mr-2', isLast && '!opacity-disabled')} onChange={handleChangeEnabled} /> -
{ - e.preventDefault(); - e.stopPropagation(); - onSubmit?.(pairContainer); - }} +
- +