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

FEAT: hotkey to active search #1287

Merged
merged 3 commits into from
Apr 19, 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
58 changes: 58 additions & 0 deletions xinference/web/ui/src/components/hotkeyFocusTextField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { TextField } from '@mui/material'
import InputAdornment from '@mui/material/InputAdornment'
import Typography from '@mui/material/Typography'
import React, { useEffect, useRef, useState } from 'react'

const HotkeyFocusTextField = ({
label,
value,
onChange,
hotkey = '/',
...props
}) => {
const [isFocused, setIsFocused] = useState(false)
const textFieldRef = useRef(null)
const handleKeyDown = (event) => {
if (
event.key === hotkey &&
document.activeElement !== textFieldRef.current
) {
event.preventDefault()
setIsFocused(true)
textFieldRef.current?.focus()
}
}

useEffect(() => {
document.addEventListener('keydown', handleKeyDown)

return () => {
document.removeEventListener('keydown', handleKeyDown)
}
}, [hotkey])

return (
<TextField
{...props}
label={label}
value={value}
onChange={onChange}
inputRef={textFieldRef}
autoFocus={isFocused}
onBlur={() => setIsFocused(false)}
onFocus={() => setIsFocused(true)}
InputProps={{
endAdornment:
!isFocused && !value ? (
<InputAdornment position="end">
<Typography color="textSecondary" style={{ fontSize: 'inherit' }}>
Type {hotkey} to search
</Typography>
</InputAdornment>
) : null,
}}
/>
)
}

export default HotkeyFocusTextField
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Box, FormControl, TextField } from '@mui/material'
import { Box, FormControl } from '@mui/material'
import React, { useContext, useEffect, useState } from 'react'

import { ApiContext } from '../../components/apiContext'
import fetcher from '../../components/fetcher'
import HotkeyFocusTextField from '../../components/hotkeyFocusTextField'
import ModelCard from './modelCard'
import style from './styles/launchModelStyle'

Expand Down Expand Up @@ -66,13 +67,14 @@ const LaunchModelComponent = ({ modelType }) => {
}}
>
<FormControl variant="outlined" margin="normal">
<TextField
<HotkeyFocusTextField
id="search"
type="search"
label={`Search for ${modelType} model name`}
value={searchTerm}
onChange={handleChange}
size="small"
hotkey="/"
/>
</FormControl>
</div>
Expand Down
12 changes: 8 additions & 4 deletions xinference/web/ui/src/scenes/launch_model/launchCustom.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { TabContext, TabList, TabPanel } from '@mui/lab'
import { Box, FormControl, Tab, TextField } from '@mui/material'
import { Box, FormControl, Tab } from '@mui/material'
import React, { useContext, useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'

import { ApiContext } from '../../components/apiContext'
import fetcher from '../../components/fetcher'
import HotkeyFocusTextField from '../../components/hotkeyFocusTextField'
import ModelCard from './modelCard'

const LaunchCustom = ({ gpuAvailable }) => {
Expand Down Expand Up @@ -169,13 +170,14 @@ const LaunchCustom = ({ gpuAvailable }) => {
}}
>
<FormControl variant="outlined" margin="normal">
<TextField
<HotkeyFocusTextField
id="search"
type="search"
label="Search for custom model name"
value={searchTerm}
onChange={handleChange}
size="small"
hotkey="/"
/>
</FormControl>
</div>
Expand Down Expand Up @@ -216,13 +218,14 @@ const LaunchCustom = ({ gpuAvailable }) => {
}}
>
<FormControl variant="outlined" margin="normal">
<TextField
<HotkeyFocusTextField
id="search"
type="search"
label="Search for custom model name"
value={searchTerm}
onChange={handleChange}
size="small"
hotkey="/"
/>
</FormControl>
</div>
Expand Down Expand Up @@ -256,13 +259,14 @@ const LaunchCustom = ({ gpuAvailable }) => {
}}
>
<FormControl variant="outlined" margin="normal">
<TextField
<HotkeyFocusTextField
id="search"
type="search"
label="Search for custom model name"
value={searchTerm}
onChange={handleChange}
size="small"
hotkey="/"
/>
</FormControl>
</div>
Expand Down
13 changes: 4 additions & 9 deletions xinference/web/ui/src/scenes/launch_model/launchLLM.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import {
Box,
FormControl,
InputLabel,
MenuItem,
Select,
TextField,
} from '@mui/material'
import { Box, FormControl, InputLabel, MenuItem, Select } from '@mui/material'
import React, { useContext, useEffect, useState } from 'react'
import { useCookies } from 'react-cookie'

import { ApiContext } from '../../components/apiContext'
import fetcher from '../../components/fetcher'
import HotkeyFocusTextField from '../../components/hotkeyFocusTextField'
import ModelCard from './modelCard'

const LaunchLLM = ({ gpuAvailable }) => {
Expand Down Expand Up @@ -133,13 +127,14 @@ const LaunchLLM = ({ gpuAvailable }) => {
</Select>
</FormControl>
<FormControl variant="outlined" margin="normal">
<TextField
<HotkeyFocusTextField
id="search"
type="search"
label="Search for model name and description"
value={searchTerm}
onChange={handleChange}
size="small"
hotkey="/"
/>
</FormControl>
</div>
Expand Down
Loading