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: autocomplete #1160

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
22 changes: 17 additions & 5 deletions src/renderer/components/ui/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CommandList,
} from "./command";
import { Check } from "lucide-react";
import { forwardRef, useState } from "react";
import { forwardRef, useRef, useState } from "react";

type Props = {
options: string[];
Expand All @@ -19,26 +19,38 @@ type Props = {
export const Autocomplete = forwardRef<HTMLInputElement, Props>(
({ options, value, onChange, placeholder }, ref) => {
const [selected, setSelected] = useState(false);
const cmdRef = useRef<HTMLDivElement | null>(null);

return (
<div className="relative">
<Command>
<Command ref={cmdRef}>
<CommandInput
ref={ref}
placeholder={placeholder}
value={value}
onValueChange={onChange}
onSelect={() => setSelected(true)}
className="h-10"
// FIXME: Select hack
onBlur={() => setTimeout(() => setSelected(false), 100)}
onBlur={(e) => {
if (e.relatedTarget === cmdRef.current) {
return;
}
setSelected(false);
}}
autoFocus={true}
/>
{selected && options.length > 0 && (
<CommandList className="absolute top-[45px] z-10 max-h-[160px] w-full rounded-lg border bg-background">
<CommandGroup>
{options.map((opt) => (
<CommandItem key={opt} value={opt} onSelect={onChange}>
<CommandItem
key={opt}
value={opt}
onSelect={(val) => {
onChange(val);
setSelected(false);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
Expand Down
Loading