-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add searchable dropdown functionality (#3723)
Fixes #3719 Add searchable functionality to mo.ui.dropdown by implementing a new SearchableSelect component, similar to the existing Multiselect component. Changes: - Add `searchable` boolean parameter to mo.ui.dropdown that defaults to False - Create SearchableSelect component for searchable dropdown functionality - Update DropdownPlugin to use SearchableSelect when searchable=True - Add comprehensive tests for searchable dropdown functionality - Update SearchableSelect to handle single string values instead of arrays - Add type declarations for test assertions Link to Devin run: https://app.devin.ai/sessions/dd320d3ed4e44aaca33a156e885da89f Requested by: Myles --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Myles Scolnick <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
45c34e1
commit ce90ae8
Showing
11 changed files
with
515 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* Copyright 2024 Marimo. All rights reserved. */ | ||
import { useId, useMemo, useState } from "react"; | ||
import { Combobox, ComboboxItem } from "../../components/ui/combobox"; | ||
import { Labeled } from "./common/labeled"; | ||
import { cn } from "../../utils/cn"; | ||
import { multiselectFilterFn } from "./multiselectFilterFn"; | ||
import { Virtuoso } from "react-virtuoso"; | ||
|
||
interface SearchableSelectProps { | ||
options: string[]; | ||
value: string | null; | ||
setValue: (value: string | null) => void; | ||
label: string | null; | ||
allowSelectNone: boolean; | ||
fullWidth: boolean; | ||
} | ||
|
||
const NONE_KEY = "__none__"; | ||
|
||
export const SearchableSelect = (props: SearchableSelectProps): JSX.Element => { | ||
const { options, value, setValue, label, allowSelectNone, fullWidth } = props; | ||
const id = useId(); | ||
const [searchQuery, setSearchQuery] = useState<string>(""); | ||
|
||
const filteredOptions = useMemo(() => { | ||
if (!searchQuery) { | ||
return options; | ||
} | ||
return options.filter( | ||
(option) => multiselectFilterFn(option, searchQuery) === 1, | ||
); | ||
}, [options, searchQuery]); | ||
|
||
const handleValueChange = (newValue: string | null) => { | ||
if (newValue == null) { | ||
return; | ||
} | ||
|
||
if (newValue === NONE_KEY) { | ||
setValue(null); | ||
} else { | ||
setValue(newValue); | ||
} | ||
}; | ||
|
||
const renderList = () => { | ||
const extraOptions = allowSelectNone ? ( | ||
<ComboboxItem key={NONE_KEY} value={NONE_KEY}> | ||
-- | ||
</ComboboxItem> | ||
) : null; | ||
|
||
if (filteredOptions.length > 200) { | ||
return ( | ||
<Virtuoso | ||
style={{ height: "200px" }} | ||
totalCount={filteredOptions.length} | ||
overscan={50} | ||
itemContent={(i: number) => { | ||
const option = filteredOptions[i]; | ||
|
||
const comboboxItem = ( | ||
<ComboboxItem key={option} value={option}> | ||
{option} | ||
</ComboboxItem> | ||
); | ||
|
||
if (i === 0) { | ||
return ( | ||
<> | ||
{extraOptions} | ||
{comboboxItem} | ||
</> | ||
); | ||
} | ||
|
||
return comboboxItem; | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
const list = filteredOptions.map((option) => ( | ||
<ComboboxItem key={option} value={option}> | ||
{option} | ||
</ComboboxItem> | ||
)); | ||
|
||
return ( | ||
<> | ||
{extraOptions} | ||
{list} | ||
</> | ||
); | ||
}; | ||
|
||
return ( | ||
<Labeled label={label} id={id} fullWidth={fullWidth}> | ||
<Combobox<string> | ||
displayValue={(option) => { | ||
if (option === NONE_KEY) { | ||
return "--"; | ||
} | ||
return option; | ||
}} | ||
placeholder="Select..." | ||
multiple={false} | ||
className={cn("w-full", { "w-full": fullWidth })} | ||
value={value ?? NONE_KEY} | ||
onValueChange={handleValueChange} | ||
shouldFilter={false} | ||
search={searchQuery} | ||
onSearchChange={setSearchQuery} | ||
data-testid="marimo-plugin-searchable-dropdown" | ||
> | ||
{renderList()} | ||
</Combobox> | ||
</Labeled> | ||
); | ||
}; |
Oops, something went wrong.