Remove broken key/value enter/backspace logic

This commit is contained in:
Gregory Schier
2023-10-29 10:45:05 -07:00
parent 705e30b6e0
commit 9e2803fcfb
3 changed files with 29 additions and 69 deletions

View File

@@ -36,7 +36,6 @@ export interface EditorProps {
onChange?: (value: string) => void; onChange?: (value: string) => void;
onFocus?: () => void; onFocus?: () => void;
onBlur?: () => void; onBlur?: () => void;
onEnter?: () => void;
onKeyDown?: (e: KeyboardEvent) => void; onKeyDown?: (e: KeyboardEvent) => void;
singleLine?: boolean; singleLine?: boolean;
wrapLines?: boolean; wrapLines?: boolean;
@@ -62,7 +61,6 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
onFocus, onFocus,
onBlur, onBlur,
onKeyDown, onKeyDown,
onEnter,
className, className,
singleLine, singleLine,
format, format,
@@ -85,12 +83,6 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
handleChange.current = onChange; handleChange.current = onChange;
}, [onChange]); }, [onChange]);
// Use ref so we can update the onChange handler without re-initializing the editor
const handleEnter = useRef<EditorProps['onEnter']>(onEnter);
useEffect(() => {
handleEnter.current = onEnter;
}, [onEnter]);
// Use ref so we can update the onChange handler without re-initializing the editor // Use ref so we can update the onChange handler without re-initializing the editor
const handleFocus = useRef<EditorProps['onFocus']>(onFocus); const handleFocus = useRef<EditorProps['onFocus']>(onFocus);
useEffect(() => { useEffect(() => {
@@ -172,7 +164,6 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
container, container,
readOnly, readOnly,
singleLine, singleLine,
onEnter: handleEnter,
onChange: handleChange, onChange: handleChange,
onFocus: handleFocus, onFocus: handleFocus,
onBlur: handleBlur, onBlur: handleBlur,
@@ -252,13 +243,11 @@ function getExtensions({
onFocus, onFocus,
onBlur, onBlur,
onKeyDown, onKeyDown,
onEnter,
}: Pick<EditorProps, 'singleLine' | 'readOnly'> & { }: Pick<EditorProps, 'singleLine' | 'readOnly'> & {
container: HTMLDivElement | null; container: HTMLDivElement | null;
onChange: MutableRefObject<EditorProps['onChange']>; onChange: MutableRefObject<EditorProps['onChange']>;
onFocus: MutableRefObject<EditorProps['onFocus']>; onFocus: MutableRefObject<EditorProps['onFocus']>;
onBlur: MutableRefObject<EditorProps['onBlur']>; onBlur: MutableRefObject<EditorProps['onBlur']>;
onEnter: MutableRefObject<EditorProps['onEnter']>;
onKeyDown: MutableRefObject<EditorProps['onKeyDown']>; onKeyDown: MutableRefObject<EditorProps['onKeyDown']>;
}) { }) {
// TODO: Ensure tooltips render inside the dialog if we are in one. // TODO: Ensure tooltips render inside the dialog if we are in one.
@@ -276,18 +265,6 @@ function getExtensions({
...(readOnly ...(readOnly
? [EditorState.readOnly.of(true), EditorView.contentAttributes.of({ tabindex: '-1' })] ? [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 // Handle onFocus
EditorView.domEventHandlers({ EditorView.domEventHandlers({

View File

@@ -101,7 +101,10 @@ export const Input = forwardRef<EditorView | undefined, InputProps>(function Inp
const wrapperRef = useRef<HTMLDivElement>(null); const wrapperRef = useRef<HTMLDivElement>(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'); const form = wrapperRef.current?.closest('form');
if (!isValid || form == null) return; if (!isValid || form == null) return;
@@ -147,7 +150,7 @@ export const Input = forwardRef<EditorView | undefined, InputProps>(function Inp
id={id} id={id}
singleLine singleLine
wrapLines={size === 'auto'} wrapLines={size === 'auto'}
onEnter={handleEnter} onKeyDown={handleKeyDown}
type={type === 'password' && !obscured ? 'text' : type} type={type === 'password' && !obscured ? 'text' : type}
defaultValue={defaultValue} defaultValue={defaultValue}
forceUpdateKey={forceUpdateKey} forceUpdateKey={forceUpdateKey}

View File

@@ -111,15 +111,6 @@ export const PairEditor = memo(function PairEditor({
[hoveredIndex, setPairsAndSave], [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( const handleChange = useCallback(
(pair: PairContainer) => (pair: PairContainer) =>
setPairsAndSave((pairs) => pairs.map((p) => (pair.id !== p.id ? p : pair))), setPairsAndSave((pairs) => pairs.map((p) => (pair.id !== p.id ? p : pair))),
@@ -127,18 +118,26 @@ export const PairEditor = memo(function PairEditor({
); );
const handleDelete = useCallback( const handleDelete = useCallback(
(pair: PairContainer) => (pair: PairContainer, focusPrevious: boolean) => {
setPairsAndSave((oldPairs) => oldPairs.filter((p) => p.id !== pair.id)), if (focusPrevious) {
[setPairsAndSave], 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) => { const handleFocus = useCallback(
return setPairs((pairs) => { (pair: PairContainer) =>
setForceFocusPairId(null); setPairs((pairs) => {
const isLast = pair.id === pairs[pairs.length - 1]?.id; setForceFocusPairId(null); // Remove focus override when something focused
return isLast ? [...pairs, newPairContainer()] : pairs; const isLast = pair.id === pairs[pairs.length - 1]?.id;
}); return isLast ? [...pairs, newPairContainer()] : pairs;
}, []); }),
[],
);
// Ensure there's always at least one pair // Ensure there's always at least one pair
useEffect(() => { useEffect(() => {
@@ -177,7 +176,6 @@ export const PairEditor = memo(function PairEditor({
nameValidate={nameValidate} nameValidate={nameValidate}
valueValidate={valueValidate} valueValidate={valueValidate}
showDelete={!isLast} showDelete={!isLast}
onSubmit={handleSubmitRow}
onChange={handleChange} onChange={handleChange}
onFocus={handleFocus} onFocus={handleFocus}
onDelete={handleDelete} onDelete={handleDelete}
@@ -203,7 +201,7 @@ type FormRowProps = {
onMove: (id: string, side: 'above' | 'below') => void; onMove: (id: string, side: 'above' | 'below') => void;
onEnd: (id: string) => void; onEnd: (id: string) => void;
onChange: (pair: PairContainer) => void; onChange: (pair: PairContainer) => void;
onDelete?: (pair: PairContainer) => void; onDelete?: (pair: PairContainer, focusPrevious: boolean) => void;
onFocus?: (pair: PairContainer) => void; onFocus?: (pair: PairContainer) => void;
onSubmit?: (pair: PairContainer) => void; onSubmit?: (pair: PairContainer) => void;
isLast?: boolean; isLast?: boolean;
@@ -235,7 +233,6 @@ const FormRow = memo(function FormRow({
onEnd, onEnd,
onFocus, onFocus,
onMove, onMove,
onSubmit,
pairContainer, pairContainer,
showDelete, showDelete,
valueAutocomplete, valueAutocomplete,
@@ -268,20 +265,9 @@ const FormRow = memo(function FormRow({
); );
const handleFocus = useCallback(() => onFocus?.(pairContainer), [onFocus, pairContainer]); const handleFocus = useCallback(() => onFocus?.(pairContainer), [onFocus, pairContainer]);
const handleDelete = useCallback(() => onDelete?.(pairContainer), [onDelete, pairContainer]); const handleDelete = useCallback(
() => onDelete?.(pairContainer, false),
const handleKeyDownName = useMemo( [onDelete, pairContainer],
() => (e: KeyboardEvent) => {
if (
e.key === 'Backspace' &&
pairContainer.pair.name === '' &&
pairContainer.pair.value === ''
) {
e.preventDefault();
onDelete?.(pairContainer);
}
},
[pairContainer, onDelete],
); );
const [, connectDrop] = useDrop<PairContainer>( const [, connectDrop] = useDrop<PairContainer>(
@@ -341,12 +327,7 @@ const FormRow = memo(function FormRow({
className={classNames('mr-2', isLast && '!opacity-disabled')} className={classNames('mr-2', isLast && '!opacity-disabled')}
onChange={handleChangeEnabled} onChange={handleChangeEnabled}
/> />
<form <div
onSubmit={(e) => {
e.preventDefault();
e.stopPropagation();
onSubmit?.(pairContainer);
}}
className={classNames( className={classNames(
'grid items-center', 'grid items-center',
'@xs:gap-2 @xs:!grid-rows-1 @xs:!grid-cols-[minmax(0,1fr)_minmax(0,1fr)]', '@xs:gap-2 @xs:!grid-rows-1 @xs:!grid-cols-[minmax(0,1fr)_minmax(0,1fr)]',
@@ -365,7 +346,6 @@ const FormRow = memo(function FormRow({
defaultValue={pairContainer.pair.name} defaultValue={pairContainer.pair.name}
label="Name" label="Name"
name="name" name="name"
onKeyDown={handleKeyDownName}
onChange={handleChangeName} onChange={handleChangeName}
onFocus={handleFocus} onFocus={handleFocus}
placeholder={namePlaceholder ?? 'name'} placeholder={namePlaceholder ?? 'name'}
@@ -388,7 +368,7 @@ const FormRow = memo(function FormRow({
autocomplete={valueAutocomplete?.(pairContainer.pair.name)} autocomplete={valueAutocomplete?.(pairContainer.pair.name)}
autocompleteVariables={valueAutocompleteVariables} autocompleteVariables={valueAutocompleteVariables}
/> />
</form> </div>
<IconButton <IconButton
aria-hidden={!showDelete} aria-hidden={!showDelete}
disabled={!showDelete} disabled={!showDelete}