Skip to content

Commit

Permalink
Improve item search open state
Browse files Browse the repository at this point in the history
  • Loading branch information
sandervspl committed Jul 27, 2024
1 parent 19aa210 commit de41b51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
18 changes: 15 additions & 3 deletions apps/website/src/components/common/item-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type Props = {
className?: string;
onBlur?: () => void;
searchItem?: (props: { item: SearchItem }) => React.ReactNode;
closeOnSelect?: boolean;
};

export type SearchItem = {
Expand All @@ -32,6 +31,7 @@ export type SearchItem = {
};

export const ItemSearch = React.forwardRef((props: Props, ref) => {
const [open, setOpen] = React.useState(false);
const [selectedItem, setSelectedItem] = React.useState<number>();
const [inputValue, setValue] = React.useState('');
const [pending, startTransition] = React.useTransition();
Expand All @@ -53,13 +53,20 @@ export const ItemSearch = React.forwardRef((props: Props, ref) => {
[],
);

if (!pending && open && !inputValue) {
setOpen(false);
}

return (
<Combobox.Root
items={searchQuery.data ?? []}
selectionBehavior="clear"
className={props.className}
closeOnSelect={props.closeOnSelect ?? false}
open={open}
closeOnSelect={false}
onValueChange={(details) => {
setValue('');

// If we have a custom item component we don't want to use the default action
if (!props.searchItem) {
const slug = details.value[0];
Expand Down Expand Up @@ -87,7 +94,12 @@ export const ItemSearch = React.forwardRef((props: Props, ref) => {
ref={inputRef}
autoFocus={props.autoFocus}
onBlur={props.onBlur}
onChange={(e) => setValue(e.currentTarget.value)}
onChange={(e) => {
if (e.currentTarget.value.length > 0 && !open) {
setOpen(true);
}
setValue(e.currentTarget.value);
}}
/>
</Combobox.Input>
</Combobox.Control>
Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/components/modules/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const Navbar = () => {
<Image src={IconSvg} alt="logo" className="size-6" />
<div className="hidden sm:block text-lg font-bold ">auctionoton</div>
</Link>
<ItemSearch closeOnSelect />
<ItemSearch />
</div>

<div className="ml-auto self-center gap-2 hidden md:flex">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const DashboardSection = ({ section }: Props) => {
// Component needs to always be mounted or else the results don't render
className={cn({ 'sr-only': !isSearching, 'w-full': isSearching })}
searchItem={(props) => <SearchItemComponent {...props} section={section} />}
closeOnSelect
/>
{!isSearching && (
<Button
Expand Down

0 comments on commit de41b51

Please sign in to comment.