fix: apply autofocus to workspace button search (#16905)

Fixes: https://github.com/coder/coder/issues/14816
This commit is contained in:
Marcin Tojek
2025-03-13 13:31:18 +00:00
committed by GitHub
parent 4994ba1e60
commit 30179aeaac
3 changed files with 18 additions and 1 deletions
@@ -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",
@@ -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
@@ -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">
@@ -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"