Skip to content

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.

feat: added disabled prop to switch (#4973)

Joaquin Coromina50c70775c629
diff --git a/frontend/src/plugins/impl/SwitchPlugin.tsx b/frontend/src/plugins/impl/SwitchPlugin.tsx
index 2554c83f79..018e713695 100644
--- a/frontend/src/plugins/impl/SwitchPlugin.tsx
+++ b/frontend/src/plugins/impl/SwitchPlugin.tsx
@@ -6,17 +6,22 @@ import { Switch } from "@/components/ui/switch";
import type { IPlugin, IPluginProps } from "@/plugins/types";
import { Labeled } from "./common/labeled";
-export class SwitchPlugin
- implements IPlugin<boolean, { label: string | null }>
-{
+type T = boolean;
+interface Data {
+ label: string | null;
+ disabled?: boolean;
+}
+
+export class SwitchPlugin implements IPlugin<T, Data> {
tagName = "marimo-switch";
validator = z.object({
initialValue: z.boolean(),
label: z.string().nullable(),
+ disabled: z.boolean().optional(),
});
- render(props: IPluginProps<boolean, { label: string | null }>): JSX.Element {
+ render(props: IPluginProps<T, Data>): JSX.Element {
return <SwitchComponent {...props} />;
}
}
@@ -25,7 +30,7 @@ const SwitchComponent = ({
value,
setValue,
data,
-}: IPluginProps<boolean, { label: string | null }>): JSX.Element => {
+}: IPluginProps<T, Data>): JSX.Element => {
const id = useId();
return (
@@ -36,6 +41,7 @@ const SwitchComponent = ({
onCheckedChange={setValue}
id={id}
className="data-[state=unchecked]:hover:bg-input/80 mb-0"
+ disabled={data.disabled}
/>
</Labeled>
);
diff --git a/marimo/_plugins/ui/_impl/switch.py b/marimo/_plugins/ui/_impl/switch.py
index c845988fdb..f223a30531 100644
--- a/marimo/_plugins/ui/_impl/switch.py
+++ b/marimo/_plugins/ui/_impl/switch.py
@@ -24,6 +24,7 @@ class switch(UIElement[bool, bool]):
label (str, optional): Markdown label for the element. Defaults to "".
on_change (Optional[Callable[[bool], None]], optional): Optional callback to run
when this element's value changes.
+ disabled (bool, optional): Whether the element is disabled. Defaults to False.
"""
_name: Final[str] = "marimo-switch"
@@ -33,6 +34,7 @@ class switch(UIElement[bool, bool]):
value: bool = False,
*,
label: str = "",
+ disabled: bool = False,
on_change: Optional[Callable[[bool], None]] = None,
) -> None:
if not isinstance(value, bool):
@@ -48,7 +50,7 @@ class switch(UIElement[bool, bool]):
component_name=switch._name,
initial_value=value,
label=label,
- args={},
+ args={"disabled": disabled},
on_change=on_change,
)