Skip to content

Commit 30179ae

Browse files
authored
fix: apply autofocus to workspace button search (#16905)
Fixes: #14816
1 parent 4994ba1 commit 30179ae

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

site/src/components/SearchField/SearchField.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ type Story = StoryObj<typeof SearchField>;
2020

2121
export const Empty: Story = {};
2222

23+
export const Focused: Story = {
24+
args: {
25+
autoFocus: true,
26+
},
27+
};
28+
2329
export const DefaultValue: Story = {
2430
args: {
2531
value: "owner:me",

site/src/components/SearchField/SearchField.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ import InputAdornment from "@mui/material/InputAdornment";
66
import TextField, { type TextFieldProps } from "@mui/material/TextField";
77
import Tooltip from "@mui/material/Tooltip";
88
import visuallyHidden from "@mui/utils/visuallyHidden";
9-
import type { FC } from "react";
9+
import { type FC, useEffect, useRef } from "react";
1010

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

1516
export const SearchField: FC<SearchFieldProps> = ({
1617
value = "",
1718
onChange,
19+
autoFocus = false,
1820
InputProps,
1921
...textFieldProps
2022
}) => {
2123
const theme = useTheme();
24+
const inputRef = useRef<HTMLInputElement>(null);
25+
26+
if (autoFocus) {
27+
useEffect(() => {
28+
inputRef.current?.focus();
29+
});
30+
}
2231
return (
2332
<TextField
2433
// Specifying `minWidth` so that the text box can't shrink so much
@@ -27,6 +36,7 @@ export const SearchField: FC<SearchFieldProps> = ({
2736
size="small"
2837
value={value}
2938
onChange={(e) => onChange(e.target.value)}
39+
inputRef={inputRef}
3040
InputProps={{
3141
startAdornment: (
3242
<InputAdornment position="start">

site/src/pages/WorkspacesPage/WorkspacesButton.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const WorkspacesButton: FC<WorkspacesButtonProps> = ({
6969
>
7070
<MenuSearch
7171
value={searchTerm}
72+
autoFocus={true}
7273
onChange={setSearchTerm}
7374
placeholder="Type/select a workspace template"
7475
aria-label="Template select for workspace"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy