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
fix: keep exact mo.ui.number values instead of snapping to step (#11287)
namupta-max41c31850694b
diff --git a/frontend/src/plugins/impl/NumberPlugin.tsx b/frontend/src/plugins/impl/NumberPlugin.tsxindex 3f8006f53c..4ce41629a9 100644--- a/frontend/src/plugins/impl/NumberPlugin.tsx+++ b/frontend/src/plugins/impl/NumberPlugin.tsx@@ -4,6 +4,7 @@ import { z } from "zod";import { NumberField } from "@/components/ui/number-field";import { useDebounceControlledState } from "@/hooks/useDebounce";import { cn } from "@/utils/cn";+import { clamp } from "@/utils/math";import type { IPlugin, IPluginProps, Setter } from "../types";import { Labeled } from "./common/labeled";@@ -65,8 +66,16 @@ const NumberComponent = (props: NumberComponentProps): JSX.Element => {onChange: props.setValue,});+ const clampToRange = (n: number) =>+ clamp(n, props.start ?? -Infinity, props.stop ?? Infinity);+const handleChange = (newValue: number) => {- onChange(withoutNaN(newValue));+ const n = withoutNaN(newValue);+ const next = n == null ? null : clampToRange(n);+ // e.g. typing past `stop` while already at `stop`+ if (next !== value) {+ onChange(next);+ }};return (@@ -76,11 +85,19 @@ const NumberComponent = (props: NumberComponentProps): JSX.Element => {className={cn("min-w-[3em]", props.fullWidth && "w-full")}minValue={props.start ?? undefined}maxValue={props.stop ?? undefined}- // This needs to be `?? NaN` since `?? undefined` makes uncontrolled component+ // This needs to be `NaN` since `undefined` makes uncontrolled component// and can lead to leaving the old value in forms (https://github.com/marimo-team/marimo/issues/7352)// We out NaNs later- value={value ?? Number.NaN}+ value={value == null ? Number.NaN : clampToRange(value)}step={props.step}+ // By default React Aria rounds every value to `start + n * step`, but+ // `step` is only the stepper increment: users can enter any value in+ // range (#9106). "validate" turns off that rounding (and the clamping,+ // done above instead); its step validation is turned off so off-step+ // values aren't flagged invalid or block `mo.ui.form` submission.+ commitBehavior="validate"+ validationBehavior="aria"+ isInvalid={false}onChange={handleChange}id={id}aria-label={props.label || "Number input"}diff --git a/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx b/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsxindex aa643e5399..725bb94d7f 100644--- a/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx+++ b/frontend/src/plugins/impl/__tests__/NumberPlugin.test.tsx@@ -1,6 +1,7 @@/* Copyright 2026 Marimo. All rights reserved. */-import { act, render } from "@testing-library/react";+import { act, fireEvent, render } from "@testing-library/react";+import { useState } from "react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { z } from "zod";import { initialModeAtom } from "@/core/mode";@@ -474,4 +475,161 @@ describe("NumberPlugin", () => {);expect(numberField?.classList.contains("w-full")).toBe(true);});++ describe("does not snap values to step (#9106)", () => {+ // Feeds `setValue` back into `value`, as the kernel round-trip does.+ const renderNumber = (+ initialValue: number,+ data: Pick<z.infer<NumberPlugin["validator"]>, "start" | "stop" | "step">,+ { debounce = false } = {},+ ) => {+ const plugin = new NumberPlugin();+ const setValue = vi.fn();+ const Harness = () => {+ const [value, setState] = useState<number | null>(initialValue);+ return plugin.render({+ host: document.createElement("div"),+ value,+ setValue: (next) => {+ setValue(next);+ setState(next);+ },+ data: {+ ...data,+ label: null,+ debounce,+ fullWidth: false,+ },+ functions: {},+ });+ };+ const { getByRole } = render(<Harness />);+ act(() => {+ vi.advanceTimersByTime(0);+ });+ const input = getByRole("textbox", {+ name: "Number input",+ }) as HTMLInputElement;+ return { input, setValue };+ };++ const typeAndCommit = (input: HTMLInputElement, text: string) => {+ act(() => {+ input.focus();+ });+ fireEvent.change(input, { target: { value: text } });+ act(() => {+ input.blur();+ });+ };++ it.each([+ { value: 2.23, start: 1.255_57, step: 0.001 },+ { value: 2.233_45, start: 1, step: 0.001 },+ ])(+ "displays $value as-is with start=$start and step=$step",+ ({ value, start, step }) => {+ const { input } = renderNumber(value, { start, stop: 20, step });+ expect(input.value).toBe(String(value));+ },+ );++ it.each([+ { typed: "2.3377", start: 1, step: 0.01 },+ { typed: "2.23111", start: 1.222, step: 0.01 },+ ])(+ "keeps typed value $typed with start=$start and step=$step",+ ({ typed, start, step }) => {+ const { input, setValue } = renderNumber(2.233_45, {+ start,+ stop: 20,+ step,+ });+ typeAndCommit(input, typed);+ expect(setValue).toHaveBeenLastCalledWith(Number(typed));+ expect(input.value).toBe(typed);+ expect(input.getAttribute("aria-invalid")).not.toBe("true");+ // React Aria sets a custom validity on this text input in native mode,+ // which would block `mo.ui.form` submission.+ expect(input.validity.valid).toBe(true);+ },+ );++ it("clamps typed values to start and stop", () => {+ const { input, setValue } = renderNumber(5, {+ start: 2,+ stop: 10,+ step: 1,+ });+ typeAndCommit(input, "25");+ expect(setValue).toHaveBeenLastCalledWith(10);+ expect(input.value).toBe("10");+ typeAndCommit(input, "1");+ expect(setValue).toHaveBeenLastCalledWith(2);+ expect(input.value).toBe("2");+ });++ it("shows the bound when typing past a bound it is already at", () => {+ const { input, setValue } = renderNumber(10, {+ start: 2,+ stop: 10,+ step: 1,+ });+ typeAndCommit(input, "50");+ expect(setValue).not.toHaveBeenCalled();+ expect(input.value).toBe("10");+ });++ it("increments and decrements by step", () => {+ const { input, setValue } = renderNumber(5, {+ start: 0,+ stop: 10,+ step: 0.5,+ });+ fireEvent.keyDown(input, { key: "ArrowUp" });+ expect(setValue).toHaveBeenLastCalledWith(5.5);+ expect(input.value).toBe("5.5");+ fireEvent.keyDown(input, { key: "ArrowDown" });+ expect(setValue).toHaveBeenLastCalledWith(5);+ expect(input.value).toBe("5");+ });++ it.each([+ { key: "ArrowUp", expected: 3 },+ { key: "ArrowDown", expected: 2 },+ ])(+ "$key from an off-step value moves to the nearest step ($expected)",+ ({ key, expected }) => {+ const { input, setValue } = renderNumber(2.23, {+ start: 0,+ stop: 10,+ step: 1,+ });+ fireEvent.keyDown(input, { key });+ expect(setValue).toHaveBeenLastCalledWith(expected);+ expect(input.value).toBe(String(expected));+ },+ );++ it.each([+ { typed: "2.3377", expected: 2.3377 },+ { typed: "50", expected: 20 },+ ])(+ "commits typed $typed as $expected after the debounce delay",+ ({ typed, expected }) => {+ const { input, setValue } = renderNumber(+ 2.233_45,+ { start: 1, stop: 20, step: 0.01 },+ { debounce: true },+ );+ typeAndCommit(input, typed);+ expect(setValue).not.toHaveBeenCalled();+ act(() => {+ vi.advanceTimersByTime(200);+ });+ expect(setValue).toHaveBeenLastCalledWith(expected);+ expect(input.value).toBe(String(expected));+ },+ );+ });});