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
components: make select dropdown clearable and add switchable multi-select (#262)
Myles Scolnick219bf09c216e
diff --git a/frontend/.storybook/preview.tsx b/frontend/.storybook/preview.tsxindex 2c7d7991a5..e8af99616b 100644--- a/frontend/.storybook/preview.tsx+++ b/frontend/.storybook/preview.tsx@@ -5,6 +5,7 @@ import "./sb.css";import "tailwindcss/tailwind.css";import { useEffect } from "react";import { cn } from "../src/lib/utils";+import { TooltipProvider } from "../src/components/ui/tooltip";const withTheme: Decorator = (Story, context) => {const theme = context.globals.theme || "light";@@ -15,7 +16,9 @@ const withTheme: Decorator = (Story, context) => {return (<div className={cn(theme, "p-5")}>- <Story />+ <TooltipProvider>+ <Story />+ </TooltipProvider></div>);};diff --git a/frontend/package.json b/frontend/package.jsonindex a8707ed5bd..5657a73315 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -43,7 +43,7 @@"@radix-ui/react-popover": "^1.0.7","@radix-ui/react-progress": "^1.0.3","@radix-ui/react-radio-group": "^1.1.3",- "@radix-ui/react-select": "^1.2.2",+ "@radix-ui/react-select": "^2.0.0","@radix-ui/react-slider": "^1.1.2","@radix-ui/react-slot": "^1.0.2","@radix-ui/react-switch": "^1.0.3",diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yamlindex 9151581478..dc13ea9047 100644--- a/frontend/pnpm-lock.yaml+++ b/frontend/pnpm-lock.yaml@@ -102,8 +102,8 @@ dependencies:specifier: ^1.1.3version: 1.1.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-select':- specifier: ^1.2.2- version: 1.2.2(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ specifier: ^2.0.0+ version: 2.0.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])'@radix-ui/react-slider':specifier: ^1.1.2version: 1.1.2(@types/[email protected])(@types/[email protected])([email protected])([email protected])@@ -3872,6 +3872,47 @@ packages:react-dom: 18.2.0([email protected])react-remove-scroll: 2.5.5(@types/[email protected])([email protected])+ /@radix-ui/[email protected](@types/[email protected])(@types/[email protected])([email protected])([email protected]):+ resolution: {integrity: sha512-RH5b7af4oHtkcHS7pG6Sgv5rk5Wxa7XI8W5gvB1N/yiuDGZxko1ynvOiVhFM7Cis2A8zxF9bTOUVbRDzPepe6w==}+ peerDependencies:+ '@types/react': '*'+ '@types/react-dom': '*'+ react: ^16.8 || ^17.0 || ^18.0+ react-dom: ^16.8 || ^17.0 || ^18.0+ peerDependenciesMeta:+ '@types/react':+ optional: true+ '@types/react-dom':+ optional: true+ dependencies:+ '@babel/runtime': 7.22.5+ '@radix-ui/number': 1.0.1+ '@radix-ui/primitive': 1.0.1+ '@radix-ui/react-collection': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-direction': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-focus-guards': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-focus-scope': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-id': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-popper': 1.1.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-portal': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])+ '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-use-controllable-state': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-use-layout-effect': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-use-previous': 1.0.1(@types/[email protected])([email protected])+ '@radix-ui/react-visually-hidden': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])+ '@types/react': 18.2.31+ '@types/react-dom': 18.2.14+ aria-hidden: 1.1.3+ react: 18.2.0+ react-dom: 18.2.0([email protected])+ react-remove-scroll: 2.5.5(@types/[email protected])([email protected])+ dev: false+/@radix-ui/[email protected](@types/[email protected])(@types/[email protected])([email protected])([email protected]):resolution: {integrity: sha512-itYmTy/kokS21aiV5+Z56MZB54KrhPgn6eHDKkFeOLR34HMN2s8PaN47qZZAGnvupcjxHaFZnW4pQEh0BvvVuw==}peerDependencies:@@ -9143,7 +9184,7 @@ packages:resolution: {integrity: sha512-QlZ5yJC0VxHxQQsQhXvBaC7VRJ2uaxTf+Tfpu4Z/OcVQJVpZO+DGU0rkoVW5ce2SccxugvpBJoMvUs59iILYdw==}engines: {node: '>= 12'}dependencies:- tslib: 2.5.0+ tslib: 2.6.2dev: falsediff --git a/frontend/src/components/forms/switchable-multi-select.tsx b/frontend/src/components/forms/switchable-multi-select.tsxnew file mode 100644index 0000000000..9731a00818--- /dev/null+++ b/frontend/src/components/forms/switchable-multi-select.tsx@@ -0,0 +1,94 @@+/* Copyright 2023 Marimo. All rights reserved. */+import React, { useState } from "react";+import { Textarea } from "../ui/textarea";+import { Combobox, ComboboxItem } from "../ui/combobox";+import { Tooltip } from "../ui/tooltip";+import { EditIcon } from "lucide-react";+import { Toggle } from "../ui/toggle";+import { cn } from "@/lib/utils";++interface Props {+ options: string[];+ value: string[];+ className?: string;+ placeholder?: string;+ comboBoxClassName?: string;+ textAreaClassName?: string;+ onChange: (value: string[]) => void;+}++// new-line separated+const DELIMINATOR = "\n";++/**+ * Switch between multi-select options or using a textarea.+ */+export const SwitchableMultiSelect: React.FC<Props> = ({+ value,+ options,+ onChange,+ className,+ placeholder,+ textAreaClassName,+ comboBoxClassName,+}) => {+ const [showTextArea, setShowTextArea] = useState(false);+ const valueAsArray = Array.isArray(value) ? value : [value];++ const renderInput = () => {+ if (showTextArea) {+ return (+ <Textarea+ value={valueAsArray.join(DELIMINATOR)}+ className={textAreaClassName}+ onChange={(e) => {+ if (e.target.value === "") {+ onChange([]);+ return;+ }+ onChange(e.target.value.split(DELIMINATOR));+ }}+ placeholder={+ placeholder ? `${placeholder}: one per line` : "One value per line"+ }+ />+ );+ }++ return (+ <Combobox+ placeholder={placeholder}+ displayValue={(option: string) => option}+ className={comboBoxClassName}+ multiple={true}+ value={valueAsArray}+ onValueChange={onChange}+ keepPopoverOpenOnSelect={true}+ chips={true}+ >+ {options.map((option) => (+ <ComboboxItem key={option} value={option}>+ {option}+ </ComboboxItem>+ ))}+ </Combobox>+ );+ };++ return (+ <div className={cn("flex gap-1", className)}>+ {renderInput()}+ <Tooltip+ content={showTextArea ? "Switch to multi-select" : "Switch to textarea"}+ >+ <Toggle+ size="xs"+ onPressedChange={setShowTextArea}+ pressed={showTextArea}+ >+ <EditIcon className="w-3 h-3" />+ </Toggle>+ </Tooltip>+ </div>+ );+};diff --git a/frontend/src/components/ui/combobox.tsx b/frontend/src/components/ui/combobox.tsxindex 4d7dfaa10b..1fe0eca4ce 100644--- a/frontend/src/components/ui/combobox.tsx+++ b/frontend/src/components/ui/combobox.tsx@@ -1,5 +1,5 @@/* Copyright 2023 Marimo. All rights reserved. */-import { ChevronDownIcon, Check } from "lucide-react";+import { ChevronDownIcon, Check, XCircle } from "lucide-react";import { createContext, useContext } from "react";import { cn } from "../../lib/utils";import { useControllableState } from "@radix-ui/react-use-controllable-state";@@ -12,6 +12,7 @@ import {} from "./command";import { Popover, PopoverContent, PopoverTrigger } from "./popover";import { Functions } from "../../utils/functions";+import { Badge } from "./badge";interface ComboboxContextValue {isSelected: (value: unknown) => boolean;@@ -35,6 +36,7 @@ interface ComboboxCommonProps<TValue> {onSearchChange?: (search: string) => void;emptyState?: React.ReactNode;className?: string;+ keepPopoverOpenOnSelect?: boolean;}type ComboboxFilterProps =@@ -50,12 +52,14 @@ type ComboboxFilterProps =type ComboboxValueProps<TValue> =| {multiple?: false;+ chips?: false;value?: TValue | null;defaultValue?: TValue | null;onValueChange?(value: TValue | null): void;}| {multiple: true;+ chips?: boolean;value?: TValue[] | null;defaultValue?: TValue[] | null;onValueChange?(value: TValue[] | null): void;@@ -83,6 +87,8 @@ export const Combobox = <TValue,>({search,onSearchChange,emptyState = "Nothing found.",+ chips = false,+ keepPopoverOpenOnSelect = false,}: ComboboxProps<TValue>) => {const [open = false, setOpen] = useControllableState({prop: openProp,@@ -123,10 +129,17 @@ export const Combobox = <TValue,>({}setValue(newValue);- setOpen(false);+ if (!keepPopoverOpenOnSelect) {+ setOpen(false);+ }};const renderValue = (): string => {+ // If we show chips, we don't want to change the placeholder+ if (multiple && chips) {+ return placeholder;+ }+if (value) {if (Array.isArray(value)) {if (value.length === 0) {@@ -170,7 +183,7 @@ export const Combobox = <TValue,>({value={search}onValueChange={onSearchChange}/>- <CommandList className="max-h-60">+ <CommandList className="max-h-60 py-.5"><CommandEmpty>{emptyState}</CommandEmpty><ComboboxContext.Providervalue={{ isSelected, onSelect: handleSelect }}@@ -180,6 +193,22 @@ export const Combobox = <TValue,>({</CommandList></Command></PopoverContent>+ {multiple && chips && (+ <div className="flex flex-col gap-1 items-start">+ {Array.isArray(value) &&+ value.map((val) => (+ <Badge key={String(val)} variant="secondary">+ {displayValue?.(val) ?? String(val)}+ <XCircle+ onClick={() => {+ handleSelect(val);+ }}+ className="w-3 h-3 opacity-50 hover:opacity-100 ml-1 cursor-pointer"+ />+ </Badge>+ ))}+ </div>+ )}</Popover>);};diff --git a/frontend/src/components/ui/select.tsx b/frontend/src/components/ui/select.tsxindex 1ede499523..88c846f565 100644--- a/frontend/src/components/ui/select.tsx+++ b/frontend/src/components/ui/select.tsx@@ -1,7 +1,13 @@/* Copyright 2023 Marimo. All rights reserved. */import * as React from "react";import * as SelectPrimitive from "@radix-ui/react-select";-import { CheckIcon, ChevronDown } from "lucide-react";+import {+ CheckIcon,+ ChevronDown,+ ChevronDownIcon,+ ChevronUpIcon,+ XIcon,+} from "lucide-react";import { cn } from "@/lib/utils";@@ -13,8 +19,10 @@ const SelectValue = SelectPrimitive.Value;const SelectTrigger = React.forwardRef<React.ElementRef<typeof SelectPrimitive.Trigger>,- React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>->(({ className, children, ...props }, ref) => (+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> & {+ onClear?: () => void;+ }+>(({ className, children, onClear, ...props }, ref) => (<SelectPrimitive.Triggerref={ref}className={cn(@@ -25,7 +33,19 @@ const SelectTrigger = React.forwardRef<>{children}<SelectPrimitive.Icon asChild={true}>- <ChevronDown className="h-4 w-4 opacity-50" />+ {onClear ? (+ <span+ onPointerDown={(e) => {+ e.preventDefault();+ e.stopPropagation();+ onClear();+ }}+ >+ <XIcon className="h-4 w-4 opacity-50 hover:opacity-90" />+ </span>+ ) : (+ <ChevronDown className="h-4 w-4 opacity-50" />+ )}</SelectPrimitive.Icon></SelectPrimitive.Trigger>));@@ -47,6 +67,10 @@ const SelectContent = React.forwardRef<position={position}{...props}>+ <SelectPrimitive.ScrollUpButton className="flex items-center justify-center h-[20px] bg-white text-muted-foreground cursor-default">+ <ChevronUpIcon className="h-4 w-4" />+ </SelectPrimitive.ScrollUpButton>+<SelectPrimitive.ViewportclassName={cn("p-1",@@ -56,6 +80,10 @@ const SelectContent = React.forwardRef<>{children}</SelectPrimitive.Viewport>++ <SelectPrimitive.ScrollDownButton className="flex items-center justify-center h-[20px] bg-white text-muted-foreground cursor-default">+ <ChevronDownIcon className="h-4 w-4" />+ </SelectPrimitive.ScrollDownButton></SelectPrimitive.Content></SelectPrimitive.Portal>));diff --git a/frontend/src/components/ui/toggle.tsx b/frontend/src/components/ui/toggle.tsxindex af911f6afc..9454ec8f36 100644--- a/frontend/src/components/ui/toggle.tsx+++ b/frontend/src/components/ui/toggle.tsx@@ -19,6 +19,7 @@ const toggleVariants = cva(},size: {default: "h-9 px-3",+ xs: "h-4 w-4",sm: "h-6 px-2",lg: "h-10 px-5",},diff --git a/frontend/src/stories/combobox.stories.tsx b/frontend/src/stories/combobox.stories.tsxindex f96f45649e..d7cd9a06fd 100644--- a/frontend/src/stories/combobox.stories.tsx+++ b/frontend/src/stories/combobox.stories.tsx@@ -65,6 +65,57 @@ export const Multiple = () => (</div>);+export const MultipleWithChips = () => (+ <div className="w-64 m-10">+ <Combobox+ placeholder="Select favorite frameworks"+ displayValue={(framework: Framework) => framework.label}+ multiple={true}+ chips={true}+ keepPopoverOpenOnSelect={true}+ >+ {frameworks.map((framework) => (+ <ComboboxItem key={framework.value} value={framework}>+ {framework.label}+ </ComboboxItem>+ ))}+ </Combobox>+ </div>+);++const OPTIONS = [+ "Apple",+ "Banana",+ "Blueberry",+ "Grapes",+ "Pineapple",+ "Aubergine",+ "Broccoli",+ "Carrot",+ "Courgette",+ "Leek",+ "Beef",+ "Chicken",+ "Lamb",+ "Pork",+] as const;++export const Large = () => (+ <div className="w-64 m-10">+ <Combobox+ placeholder="Select favorite frameworks"+ displayValue={(option: string) => option}+ multiple={true}+ >+ {OPTIONS.map((option) => (+ <ComboboxItem key={option} value={option}>+ {option}+ </ComboboxItem>+ ))}+ </Combobox>+ </div>+);+export const WithCustomFilterFn = () => (<div className="w-64 m-10"><Comboboxdiff --git a/frontend/src/stories/select.stories.mdx b/frontend/src/stories/select.stories.mdxdeleted file mode 100644index 946af84d48..0000000000--- a/frontend/src/stories/select.stories.mdx+++ /dev/null@@ -1,53 +0,0 @@-import { Meta, Story } from "@storybook/addon-docs";--import {- Select,- SelectContent,- SelectGroup,- SelectItem,- SelectLabel,- SelectSeparator,- SelectTrigger,- SelectValue,-} from "@/components/ui/select";--<Meta title="Select" component={Select} />--# Select--{" "}--<Select>- <SelectTrigger>- <SelectValue placeholder="Select a fruit" />- </SelectTrigger>- <SelectContent>- <SelectGroup>- <SelectLabel>Fruits</SelectLabel>- <SelectItem value="apple">Apple</SelectItem>- <SelectItem value="banana">Banana</SelectItem>- <SelectItem value="blueberry">Blueberry</SelectItem>- <SelectItem value="grapes">Grapes</SelectItem>- <SelectItem value="pineapple">Pineapple</SelectItem>- </SelectGroup>- <SelectSeparator />- <SelectGroup>- <SelectLabel>Vegetables</SelectLabel>- <SelectItem value="aubergine">Aubergine</SelectItem>- <SelectItem value="broccoli">Broccoli</SelectItem>- <SelectItem value="carrot" disabled>- Carrot- </SelectItem>- <SelectItem value="courgette">Courgette</SelectItem>- <SelectItem value="leek">Leek</SelectItem>- </SelectGroup>- <SelectSeparator />- <SelectGroup>- <SelectLabel>Meat</SelectLabel>- <SelectItem value="beef">Beef</SelectItem>- <SelectItem value="chicken">Chicken</SelectItem>- <SelectItem value="lamb">Lamb</SelectItem>- <SelectItem value="pork">Pork</SelectItem>- </SelectGroup>- </SelectContent>-</Select>diff --git a/frontend/src/stories/select.stories.tsx b/frontend/src/stories/select.stories.tsxnew file mode 100644index 0000000000..9b8cbdce50--- /dev/null+++ b/frontend/src/stories/select.stories.tsx@@ -0,0 +1,91 @@+/* Copyright 2023 Marimo. All rights reserved. */+/* eslint-disable react-hooks/rules-of-hooks */+import {+ Select,+ SelectContent,+ SelectGroup,+ SelectItem,+ SelectLabel,+ SelectSeparator,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";+import { Meta, StoryObj } from "@storybook/react";+import { useState } from "react";++const meta: Meta = {+ title: "Select",+ component: Select,+ args: {},+};++export default meta;+type Story = StoryObj;++export const Primary: Story = {+ render: (args, ctx) => (+ <Select>+ <SelectTrigger className="w-64">+ <SelectValue placeholder="Select a fruit" />+ </SelectTrigger>+ <SelectContent>+ <SelectGroup>+ <SelectLabel>Fruits</SelectLabel>+ <SelectItem value="apple">Apple</SelectItem>+ <SelectItem value="banana">Banana</SelectItem>+ <SelectItem value="blueberry">Blueberry</SelectItem>+ <SelectItem value="grapes">Grapes</SelectItem>+ <SelectItem value="pineapple">Pineapple</SelectItem>+ </SelectGroup>+ <SelectSeparator />+ <SelectGroup>+ <SelectLabel>Vegetables</SelectLabel>+ <SelectItem value="aubergine">Aubergine</SelectItem>+ <SelectItem value="broccoli">Broccoli</SelectItem>+ <SelectItem value="carrot" disabled={true}>+ Carrot+ </SelectItem>+ <SelectItem value="courgette">Courgette</SelectItem>+ <SelectItem value="leek">Leek</SelectItem>+ </SelectGroup>+ <SelectSeparator />+ <SelectGroup>+ <SelectLabel>Meat</SelectLabel>+ <SelectItem value="beef">Beef</SelectItem>+ <SelectItem value="chicken">Chicken</SelectItem>+ <SelectItem value="lamb">Lamb</SelectItem>+ <SelectItem value="pork">Pork</SelectItem>+ </SelectGroup>+ </SelectContent>+ </Select>+ ),+};++export const Clearable: Story = {+ render: (args, ctx) => {+ const [value, setValue] = useState<string>("");+ return (+ <>+ <Select value={value} onValueChange={setValue}>+ <SelectTrigger+ onClear={value ? () => setValue("") : undefined}+ className="w-64"+ >+ <SelectValue placeholder="Select a fruit" />+ </SelectTrigger>+ <SelectContent>+ <SelectGroup>+ <SelectLabel>Fruits</SelectLabel>+ <SelectItem value="apple">Apple</SelectItem>+ <SelectItem value="banana">Banana</SelectItem>+ <SelectItem value="blueberry">Blueberry</SelectItem>+ <SelectItem value="grapes">Grapes</SelectItem>+ <SelectItem value="pineapple">Pineapple</SelectItem>+ </SelectGroup>+ </SelectContent>+ </Select>+ Value: {value}+ </>+ );+ },+};diff --git a/frontend/src/stories/switchable-multi-select.stories.tsx b/frontend/src/stories/switchable-multi-select.stories.tsxnew file mode 100644index 0000000000..f87c424a62--- /dev/null+++ b/frontend/src/stories/switchable-multi-select.stories.tsx@@ -0,0 +1,31 @@+/* Copyright 2023 Marimo. All rights reserved. */+/* eslint-disable react-hooks/rules-of-hooks */+import { Meta, StoryObj } from "@storybook/react";+import { useState } from "react";+import { SwitchableMultiSelect } from "@/components/forms/switchable-multi-select";+import { TooltipProvider } from "@/components/ui/tooltip";++const meta: Meta = {+ title: "SwitchableMultiSelect",+ component: SwitchableMultiSelect,+ args: {},+};++export default meta;+type Story = StoryObj;++export const Primary: Story = {+ render: (args, ctx) => {+ const [value, setValue] = useState<string[]>([]);+ return (+ <TooltipProvider>+ <SwitchableMultiSelect+ options={["apple", "banana", "blueberry", "grapes", "pineapple"]}+ value={value}+ placeholder="Select a fruit"+ onChange={(value) => setValue(value)}+ />+ </TooltipProvider>+ );+ },+};