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.

1.4 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import type { Checkbox as CheckboxPrimitive } from "radix-ui";

type CheckedState = CheckboxPrimitive.CheckedState;

import { type JSX, useId } from "react";
import { z } from "zod";
import { Checkbox } from "../../components/ui/checkbox";
import type { IPlugin, IPluginProps } from "../types";
import { Labeled } from "./common/labeled";

type T = boolean;

interface Data {
  label: string | null;
  disabled?: boolean;
}

export class CheckboxPlugin implements IPlugin<T, Data> {
  public tagName = "marimo-checkbox";

  public validator = z.object({
    initialValue: z.boolean(),
    label: z.string().nullable(),
    disabled: z.boolean().optional(),
  });

  public render(props: IPluginProps<T, Data>): JSX.Element {
    return <CheckboxComponent {...props} />;
  }
}

const CheckboxComponent = ({
  value,
  setValue,
  data,
}: IPluginProps<T, Data>): JSX.Element => {
  const onClick = (newValue: CheckedState) => {
    // unsupported state
    if (newValue === "indeterminate") {
      return;
    }
    setValue(newValue);
  };
  const id = useId();

  return (
    <Labeled label={data.label} align="right" controlId={id}>
      <Checkbox
        data-testid="marimo-plugin-checkbox"
        checked={value}
        onCheckedChange={onClick}
        id={id}
        disabled={data.disabled}
      />
    </Labeled>
  );
};