A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: add select all and deselect all to multi-select (#2433)
Myles Scolnick405c84f1061f
diff --git a/frontend/src/components/ui/combobox.tsx b/frontend/src/components/ui/combobox.tsxindex 72ab07c3ed..871193baa1 100644--- a/frontend/src/components/ui/combobox.tsx+++ b/frontend/src/components/ui/combobox.tsx@@ -240,7 +240,13 @@ export interface ComboboxItemProps<TValue>export const ComboboxItem = React.forwardRef(<TValue extends string | number | { value: string }>(- { children, className, value, onSelect }: ComboboxItemProps<TValue>,+ {+ children,+ className,+ value,+ onSelect,+ disabled,+ }: ComboboxItemProps<TValue>,ref: React.Ref<HTMLDivElement>,) => {const valueAsString =@@ -255,6 +261,7 @@ export const ComboboxItem = React.forwardRef(className={cn("pl-6 m-1 py-1", className)}role="option"value={valueAsString}+ disabled={disabled}onSelect={() => {context.onSelect(value);onSelect?.(value);diff --git a/frontend/src/plugins/impl/MultiselectPlugin.tsx b/frontend/src/plugins/impl/MultiselectPlugin.tsxindex 52f81a59b3..1feaf3af3e 100644--- a/frontend/src/plugins/impl/MultiselectPlugin.tsx+++ b/frontend/src/plugins/impl/MultiselectPlugin.tsx@@ -7,6 +7,7 @@ import { Combobox, ComboboxItem } from "../../components/ui/combobox";import { Labeled } from "./common/labeled";import { cn } from "@/utils/cn";import { Virtuoso } from "react-virtuoso";+import { CommandSeparator } from "../../components/ui/command";interface Data {label: string | null;@@ -52,60 +53,139 @@ interface MultiselectProps extends Data {setValue: Setter<T>;}-const Multiselect = (props: MultiselectProps): JSX.Element => {+const SELECT_ALL_KEY = "__select_all__";+const DESELECT_ALL_KEY = "__deselect_all__";++const Multiselect = ({+ options,+ label,+ value,+ setValue,+ fullWidth,+ maxSelections,+}: MultiselectProps): JSX.Element => {const id = useId();const [searchQuery, setSearchQuery] = useState<string>("");const filteredOptions = useMemo(() => {if (!searchQuery) {- return props.options;+ return options;}- return props.options.filter(+ return options.filter((option) => multiselectFilterFn(option, searchQuery) === 1,);- }, [props.options, searchQuery]);+ }, [options, searchQuery]);- function setValue(newValues: T) {- if (props.maxSelections != null && newValues.length > props.maxSelections) {+ const handleValueChange = (newValues: string[] | null) => {+ if (!newValues) {+ setValue([]);return;}- props.setValue(newValues);- }+ if (maxSelections != null && newValues.length > maxSelections) {+ return;+ }+ // Remove select all and deselect all from the new values+ newValues = newValues.filter(+ (value) => value !== SELECT_ALL_KEY && value !== DESELECT_ALL_KEY,+ );+ setValue(newValues);+ };++ const handleSelectAll = () => {+ setValue(options);+ };++ const handleDeselectAll = () => {+ setValue([]);+ };++ const shouldShowSelectAll =+ options.length > 0 && value.length < options.length;+ const shouldShowDeselectAll = options.length > 0 && value.length > 0;++ // Only show when more than 2 options+ const extraOptions = options.length > 2 && (+ <>+ <ComboboxItem+ key={SELECT_ALL_KEY}+ value={SELECT_ALL_KEY}+ onSelect={handleSelectAll}+ disabled={!shouldShowSelectAll}+ >+ Select all+ </ComboboxItem>+ <ComboboxItem+ key={DESELECT_ALL_KEY}+ value={DESELECT_ALL_KEY}+ onSelect={handleDeselectAll}+ disabled={!shouldShowDeselectAll}+ >+ Deselect all+ </ComboboxItem>+ <CommandSeparator />+ </>+ );++ const renderList = () => {+ // List virtualization+ if (filteredOptions.length > 200) {+ return (+ <Virtuoso+ style={{ height: "200px" }}+ totalCount={filteredOptions.length}+ overscan={50}+ itemContent={(i: number) => {+ const comboboxItem = (+ <ComboboxItem key={filteredOptions[i]} value={filteredOptions[i]}>+ {filteredOptions[i]}+ </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={props.label} id={id} fullWidth={props.fullWidth}>+ <Labeled label={label} id={id} fullWidth={fullWidth}><Combobox<string>displayValue={(option) => option}placeholder="Select..."multiple={true}className={cn({- "w-full": props.fullWidth,+ "w-full": fullWidth,})}- value={props.value}- onValueChange={(newValues) => setValue(newValues || [])}+ value={value}+ onValueChange={handleValueChange}shouldFilter={false}search={searchQuery}onSearchChange={setSearchQuery}>- {filteredOptions.length > 200 ? (- // List virtualization- <Virtuoso- style={{ height: "200px" }}- totalCount={filteredOptions.length}- overscan={50}- itemContent={(i: number) => (- <ComboboxItem key={filteredOptions[i]} value={filteredOptions[i]}>- {filteredOptions[i]}- </ComboboxItem>- )}- />- ) : (- filteredOptions.map((option) => (- <ComboboxItem key={option} value={option}>- {option}- </ComboboxItem>- ))- )}+ {renderList()}</Combobox></Labeled>);diff --git a/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py b/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.pyindex f71b54c391..d8d1d6fce2 100644--- a/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py+++ b/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py@@ -2,26 +2,42 @@import marimo-__generated_with = "0.8.0"+__generated_with = "0.8.20"app = marimo.App(app_title="1654 - Virtualize Multiselect")@app.celldef __():import marimo as mo- return mo,+ return (mo,)@app.celldef __(mo):fuzzy_match_test = ["foo bar", "bar foo", "foob", "foobar", "barfoo"]mo.ui.multiselect(fuzzy_match_test, label="Fuzzy match test")- return fuzzy_match_test,+ return (fuzzy_match_test,)+++def __(mo):+ (+ mo.ui.multiselect([], label="Empty"),+ mo.ui.multiselect(["1", "2"], label="2 items"),+ )+ return@app.celldef __(mo, xs_list):- mo.ui.multiselect(xs_list, label="Extra small list with 10 items")+ v = mo.ui.multiselect(xs_list, label="Extra small list with 10 items")+ v+ return (v,)+++def __(v):+ print(v.value)return