Skip to content

fix: apply autofocus to workspace button search #16905

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

Merged
merged 2 commits into from
Mar 13, 2025
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
6 changes: 6 additions & 0 deletions site/src/components/SearchField/SearchField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type Story = StoryObj<typeof SearchField>;

export const Empty: Story = {};

export const Focused: Story = {
args: {
autoFocus: true,
},
};

export const DefaultValue: Story = {
args: {
value: "owner:me",
Expand Down
12 changes: 11 additions & 1 deletion site/src/components/SearchField/SearchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ import InputAdornment from "@mui/material/InputAdornment";
import TextField, { type TextFieldProps } from "@mui/material/TextField";
import Tooltip from "@mui/material/Tooltip";
import visuallyHidden from "@mui/utils/visuallyHidden";
import type { FC } from "react";
import { type FC, useEffect, useRef } from "react";

export type SearchFieldProps = Omit<TextFieldProps, "onChange"> & {
onChange: (query: string) => void;
autoFocus?: boolean;
};

export const SearchField: FC<SearchFieldProps> = ({
value = "",
onChange,
autoFocus = false,
InputProps,
...textFieldProps
}) => {
const theme = useTheme();
const inputRef = useRef<HTMLInputElement>(null);

if (autoFocus) {
useEffect(() => {
inputRef.current?.focus();
});
}
return (
<TextField
// Specifying `minWidth` so that the text box can't shrink so much
Expand All @@ -27,6 +36,7 @@ export const SearchField: FC<SearchFieldProps> = ({
size="small"
value={value}
onChange={(e) => onChange(e.target.value)}
inputRef={inputRef}
InputProps={{
startAdornment: (
<InputAdornment position="start">
Expand Down
1 change: 1 addition & 0 deletions site/src/pages/WorkspacesPage/WorkspacesButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const WorkspacesButton: FC<WorkspacesButtonProps> = ({
>
<MenuSearch
value={searchTerm}
autoFocus={true}
onChange={setSearchTerm}
placeholder="Type/select a workspace template"
aria-label="Template select for workspace"
Expand Down
Loading