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.

fix: connect text widget labels to their inputs (#11149)

Shahmir Varqha82eb5884462c
diff --git a/frontend/src/plugins/impl/TextAreaPlugin.tsx b/frontend/src/plugins/impl/TextAreaPlugin.tsx
index ea30759749..e7620c0fe5 100644
--- a/frontend/src/plugins/impl/TextAreaPlugin.tsx
+++ b/frontend/src/plugins/impl/TextAreaPlugin.tsx
@@ -1,6 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import type { JSX } from "react";
+import { type JSX, useId } from "react";
import { z } from "zod";
import { cn } from "@/utils/cn";
import {
@@ -56,6 +56,7 @@ interface TextAreaComponentProps extends Data {
}
const TextAreaComponent = (props: TextAreaComponentProps) => {
+ const textareaId = useId();
const bottomAdornment = props.maxLength ? (
<span className="text-muted-foreground text-xs font-medium">
{props.value.length}/{props.maxLength}
@@ -64,8 +65,14 @@ const TextAreaComponent = (props: TextAreaComponentProps) => {
if (props.debounce === true) {
return (
- <Labeled label={props.label} align="top" fullWidth={props.fullWidth}>
+ <Labeled
+ controlId={textareaId}
+ label={props.label}
+ align="top"
+ fullWidth={props.fullWidth}
+ >
<OnBlurredTextarea
+ id={textareaId}
className={cn("font-code", {
"w-full": props.fullWidth,
})}
@@ -86,8 +93,14 @@ const TextAreaComponent = (props: TextAreaComponentProps) => {
if (typeof props.debounce === "number") {
return (
- <Labeled label={props.label} align="top" fullWidth={props.fullWidth}>
+ <Labeled
+ controlId={textareaId}
+ label={props.label}
+ align="top"
+ fullWidth={props.fullWidth}
+ >
<DebouncedTextarea
+ id={textareaId}
className={cn("font-code", {
"w-full": props.fullWidth,
})}
@@ -108,8 +121,14 @@ const TextAreaComponent = (props: TextAreaComponentProps) => {
}
return (
- <Labeled label={props.label} align="top" fullWidth={props.fullWidth}>
+ <Labeled
+ controlId={textareaId}
+ label={props.label}
+ align="top"
+ fullWidth={props.fullWidth}
+ >
<Textarea
+ id={textareaId}
className={cn("font-code", {
"w-full": props.fullWidth,
})}
diff --git a/frontend/src/plugins/impl/TextInputPlugin.tsx b/frontend/src/plugins/impl/TextInputPlugin.tsx
index c011a8d20a..820be520a3 100644
--- a/frontend/src/plugins/impl/TextInputPlugin.tsx
+++ b/frontend/src/plugins/impl/TextInputPlugin.tsx
@@ -1,7 +1,7 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { AtSignIcon, GlobeIcon, LockIcon } from "lucide-react";
-import { type JSX, useRef, useState } from "react";
+import { type JSX, useId, useRef, useState } from "react";
import { z } from "zod";
import {
DebouncedInput,
@@ -73,6 +73,7 @@ interface TextComponentProps extends Data {
}
const TextComponent = (props: TextComponentProps) => {
+ const inputId = useId();
// Before first real keystroke: show masked placeholder, suppress setValue.
// After first keystroke: normal password field.
const initiallyMasked =
@@ -125,6 +126,7 @@ const TextComponent = (props: TextComponentProps) => {
if (props.debounce === true) {
input = (
<OnBlurredInput
+ id={inputId}
data-testid="marimo-plugin-text-input"
type={props.kind}
icon={icon[props.kind]}
@@ -143,6 +145,7 @@ const TextComponent = (props: TextComponentProps) => {
} else if (typeof props.debounce === "number") {
input = (
<DebouncedInput
+ id={inputId}
data-testid="marimo-plugin-text-input"
type={props.kind}
icon={icon[props.kind]}
@@ -163,6 +166,7 @@ const TextComponent = (props: TextComponentProps) => {
} else {
input = (
<Input
+ id={inputId}
data-testid="marimo-plugin-text-input"
type={props.kind}
icon={icon[props.kind]}
@@ -182,7 +186,11 @@ const TextComponent = (props: TextComponentProps) => {
}
return (
- <Labeled label={props.label} fullWidth={props.fullWidth}>
+ <Labeled
+ controlId={inputId}
+ label={props.label}
+ fullWidth={props.fullWidth}
+ >
{input}
</Labeled>
);
diff --git a/frontend/src/plugins/impl/__test__/text-labels.test.tsx b/frontend/src/plugins/impl/__test__/text-labels.test.tsx
new file mode 100644
index 0000000000..e452d7bb56
--- /dev/null
+++ b/frontend/src/plugins/impl/__test__/text-labels.test.tsx
@@ -0,0 +1,141 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import { act, fireEvent, render, screen } from "@testing-library/react";
+import { expect, test, vi } from "vitest";
+import type { IPlugin } from "../../types";
+import { TextInputPlugin } from "../TextInputPlugin";
+import { TextAreaPlugin } from "../TextAreaPlugin";
+
+function testLabelBehavior<D extends { label: string | null }>(
+ plugin: IPlugin<string, D>,
+) {
+ for (const debounce of [false, true, 200]) {
+ test(`${plugin.tagName} associates formatted label with debounce=${debounce}`, () => {
+ const setValue = vi.fn();
+ const props = {
+ host: document.createElement("div"),
+ value: "",
+ setValue,
+ functions: {},
+ data: plugin.validator.parse({
+ initialValue: "",
+ placeholder: "",
+ label: "<strong>Notes &amp; details</strong>",
+ debounce,
+ }),
+ };
+ const { rerender } = render(plugin.render(props));
+ const control = screen.getByLabelText("Notes & details");
+ expect(control).toHaveAccessibleName("Notes & details");
+ const id = control.id;
+ rerender(
+ plugin.render({
+ ...props,
+ data: { ...props.data, label: "Updated notes" },
+ }),
+ );
+ expect(screen.getByLabelText("Updated notes").id).toBe(id);
+ rerender(
+ plugin.render({ ...props, data: { ...props.data, label: null } }),
+ );
+ expect(control).toHaveAccessibleName("");
+ });
+ test(`${plugin.tagName} preserves commit timing with debounce=${debounce}`, () => {
+ vi.useFakeTimers();
+ try {
+ const setValue = vi.fn();
+ const props = {
+ host: document.createElement("div"),
+ value: "",
+ setValue,
+ functions: {},
+ data: plugin.validator.parse({
+ initialValue: "",
+ placeholder: "",
+ label: "Notes",
+ debounce,
+ }),
+ };
+ render(plugin.render(props));
+ const control = screen.getByLabelText("Notes");
+ if (debounce === false) {
+ fireEvent.input(control, { target: { value: "hello" } });
+ } else {
+ fireEvent.change(control, { target: { value: "hello" } });
+ }
+ const immediateCalls = debounce === false ? 1 : 0;
+ expect(setValue).toHaveBeenCalledTimes(immediateCalls);
+ act(() => vi.advanceTimersByTime(199));
+ expect(setValue).toHaveBeenCalledTimes(immediateCalls);
+ if (debounce === true) {
+ fireEvent.blur(control);
+ } else if (typeof debounce === "number") {
+ act(() => vi.advanceTimersByTime(1));
+ }
+ expect(setValue).toHaveBeenCalledWith("hello");
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+ }
+}
+
+for (const kind of ["text", "password", "email", "url"] as const) {
+ test(`native association preserves ${kind} input attributes`, () => {
+ const plugin = new TextInputPlugin();
+ render(
+ plugin.render({
+ host: document.createElement("div"),
+ value: "",
+ setValue: vi.fn(),
+ functions: {},
+ data: plugin.validator.parse({
+ initialValue: "",
+ placeholder: "",
+ label: "Account",
+ kind,
+ disabled: true,
+ minLength: 2,
+ maxLength: 30,
+ }),
+ }),
+ );
+ const control = screen.getByLabelText("Account");
+ expect(control).toHaveAttribute("type", kind);
+ expect(control).toBeDisabled();
+ expect(control).toBeRequired();
+ expect(control).toHaveAttribute("minlength", "2");
+ expect(control).toHaveAttribute("maxlength", "30");
+ });
+}
+
+function testMultipleInstances<D>(plugin: IPlugin<string, D>) {
+ test(`${plugin.tagName} keeps repeated labels independently associated`, () => {
+ const props = {
+ host: document.createElement("div"),
+ value: "",
+ setValue: vi.fn(),
+ functions: {},
+ data: plugin.validator.parse({
+ initialValue: "",
+ placeholder: "",
+ label: "Notes",
+ }),
+ };
+ render(
+ <>
+ {plugin.render(props)}
+ {plugin.render(props)}
+ </>,
+ );
+ const controls = screen.getAllByLabelText("Notes");
+ expect(controls).toHaveLength(2);
+ expect(controls[0].id).not.toBe(controls[1].id);
+ expect(controls[0]).toHaveAccessibleName("Notes");
+ expect(controls[1]).toHaveAccessibleName("Notes");
+ });
+}
+
+testLabelBehavior(new TextInputPlugin());
+testLabelBehavior(new TextAreaPlugin());
+testMultipleInstances(new TextInputPlugin());
+testMultipleInstances(new TextAreaPlugin());