🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor InputWithSuggestions component to simplify dropdown rendering
Bill Yangfd072be13ee4
diff --git a/client/src/components/ui/input-with-suggestions.tsx b/client/src/components/ui/input-with-suggestions.tsxindex 6a6a75187..f6b52f445 100644--- a/client/src/components/ui/input-with-suggestions.tsx+++ b/client/src/components/ui/input-with-suggestions.tsx@@ -1,9 +1,7 @@"use client";-import * as React from "react";-import * as Portal from "@radix-ui/react-portal";-import { cn } from "@/lib/utils";import { Input } from "@/components/ui/input";+import * as React from "react";export interface SuggestionOption {value: string;@@ -19,9 +17,7 @@ export const InputWithSuggestions = React.forwardRef<HTMLInputElement, InputWith({ suggestions, onValueChange, className, value: controlledValue, ...props }, ref) => {const [open, setOpen] = React.useState(false);const [internalValue, setInternalValue] = React.useState("");- const [dropdownPosition, setDropdownPosition] = React.useState({ top: 0, left: 0, width: 0 });const wrapperRef = React.useRef<HTMLDivElement>(null);- const dropdownRef = React.useRef<HTMLDivElement>(null);// Use controlled value if provided, otherwise use internal stateconst value = controlledValue !== undefined ? controlledValue : internalValue;@@ -30,39 +26,9 @@ export const InputWithSuggestions = React.forwardRef<HTMLInputElement, InputWithsuggestion.value.toLowerCase().includes(String(value).toLowerCase()));- // Update dropdown position when open changes or on scroll/resize- React.useEffect(() => {- const updatePosition = () => {- if (wrapperRef.current && open) {- const rect = wrapperRef.current.getBoundingClientRect();- setDropdownPosition({- top: rect.bottom + window.scrollY,- left: rect.left + window.scrollX,- width: rect.width,- });- }- };-- updatePosition();-- if (open) {- window.addEventListener("scroll", updatePosition, true);- window.addEventListener("resize", updatePosition);- return () => {- window.removeEventListener("scroll", updatePosition, true);- window.removeEventListener("resize", updatePosition);- };- }- }, [open]);-React.useEffect(() => {const handleClickOutside = (event: MouseEvent) => {- if (- wrapperRef.current &&- !wrapperRef.current.contains(event.target as Node) &&- dropdownRef.current &&- !dropdownRef.current.contains(event.target as Node)- ) {+ if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) {setOpen(false);}};@@ -107,28 +73,17 @@ export const InputWithSuggestions = React.forwardRef<HTMLInputElement, InputWith{...props}/>{open && filteredSuggestions.length > 0 && (- <Portal.Root>- <div- ref={dropdownRef}- style={{- position: "absolute",- top: dropdownPosition.top,- left: dropdownPosition.left,- width: dropdownPosition.width,- }}- className="z-50 max-h-60 overflow-auto rounded-md border border-neutral-150 bg-white shadow-md dark:border-neutral-800 dark:bg-neutral-900"- >- {filteredSuggestions.map(suggestion => (- <div- key={suggestion.value}- className="relative cursor-pointer px-3 py-2 text-sm hover:bg-neutral-100 dark:hover:bg-neutral-800"- onClick={() => handleSuggestionClick(suggestion.value)}- >- {suggestion.label || suggestion.value}- </div>- ))}- </div>- </Portal.Root>+ <div className="absolute z-50 mt-1 w-full max-h-60 overflow-auto rounded-md border border-neutral-150 bg-white shadow-md dark:border-neutral-800 dark:bg-neutral-900">+ {filteredSuggestions.map(suggestion => (+ <div+ key={suggestion.value}+ className="relative cursor-pointer px-3 py-2 text-sm hover:bg-neutral-100 dark:hover:bg-neutral-800"+ onClick={() => handleSuggestionClick(suggestion.value)}+ >+ {suggestion.label || suggestion.value}+ </div>+ ))}+ </div>)}</div>);