Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snaps): Keep focus on input if interface re-renders #27429

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions ui/components/app/snaps/snap-ui-input/snap-ui-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {
ChangeEvent,
FunctionComponent,
useEffect,
useRef,
useState,
} from 'react';
import { useSnapInterfaceContext } from '../../../../contexts/snaps';
Expand All @@ -15,7 +16,10 @@ export type SnapUIInputProps = {
export const SnapUIInput: FunctionComponent<
SnapUIInputProps & FormTextFieldProps<'div'>
> = ({ name, form, ...props }) => {
const { handleInputChange, getValue } = useSnapInterfaceContext();
const { handleInputChange, getValue, focusedInput, setCurrentFocusedInput } =
useSnapInterfaceContext();

const inputRef = useRef<HTMLDivElement>(null);

const initialValue = getValue(name, form) as string;

Expand All @@ -27,14 +31,29 @@ export const SnapUIInput: FunctionComponent<
}
}, [initialValue]);

/*
* Focus input if the last focused input was this input
* This avoids loosing the focus when the UI is re-rendered
*/
useEffect(() => {
if (inputRef.current && name === focusedInput) {
(inputRef.current.children[0] as HTMLInputElement).focus();
}
}, [inputRef]);

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
setValue(event.target.value);
handleInputChange(name, event.target.value ?? null, form);
};

const handleFocus = () => setCurrentFocusedInput(name);
const handleBlur = () => setCurrentFocusedInput(null);

return (
<FormTextField
autoFocus
ref={inputRef}
onFocus={handleFocus}
onBlur={handleBlur}
className="snap-ui-renderer__input"
id={name}
value={value}
Expand Down
10 changes: 10 additions & 0 deletions ui/contexts/snaps/snap-interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export type HandleFileChange = (
form?: string,
) => void;

export type SetCurrentInputFocus = (name: string | null) => void;

export type SnapInterfaceContextType = {
handleEvent: HandleEvent;
getValue: GetValue;
handleInputChange: HandleInputChange;
handleFileChange: HandleFileChange;
setCurrentFocusedInput: SetCurrentInputFocus;
focusedInput: string | null;
snapId: string;
};

Expand Down Expand Up @@ -80,6 +84,7 @@ export const SnapInterfaceContextProvider: FunctionComponent<
// UI. It's kept in a ref to avoid useless re-rendering of the entire tree of
// components.
const internalState = useRef<InterfaceState>(initialState ?? {});
const focusedInput = useRef<string | null>(null);

// Since the internal state is kept in a reference, it won't update when the
// interface is updated. We have to manually update it.
Expand Down Expand Up @@ -237,13 +242,18 @@ export const SnapInterfaceContextProvider: FunctionComponent<
return undefined;
};

const setCurrentFocusedInput: SetCurrentInputFocus = (name) =>
(focusedInput.current = name);

return (
<SnapInterfaceContext.Provider
value={{
handleEvent,
getValue,
handleInputChange,
handleFileChange,
setCurrentFocusedInput,
focusedInput: focusedInput.current,
snapId,
}}
>
Expand Down