From bb79d2f207963421a0652a30edcf4c417f9cd532 Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Thu, 26 Nov 2026 09:40:30 +0100 Subject: [PATCH] :bug: Keep typed values within the limits of editable selects (#13251) * :bug: Keep typed values within the limits of editable selects A number editable select passed no :min or :max to its input, so only the arrow keys kept the value in range. A typed guide size of 0 or less, or a typography font size out of range, reached the backend, which rejected the shape and the save. Pass the limits to the input, which already clamps the typed value. Closes #13250 AI-assisted-by: claude-opus-5-5 * :bug: Keep values unchanged when leaving a number input untouched The number input clamped its value on focus, so with the new limits leaving an Auto grid size without typing saved 1, and leaving a field with an old value out of range rewrote it. Focus now keeps the current value; only typed and stepped values are clamped. The grid size field is also nillable now: Auto shows its placeholder instead of 0, and clearing the field goes back to Auto. AI-assisted-by: claude-opus-5-5 * :bug: Keep untouched and mixed number inputs unchanged on leave The keydown handler stored the clamped value for every key, so Tab or Escape on a font size of 2 saved 3 with no typing. Store it only for arrows and Enter; typed text is stored by the change handler. A mixed value has no number, so leaving the field fell back to the default 0 and saved a font size the backend rejects. Leaving a field that is not nillable without a value now saves nothing. AI-assisted-by: claude-opus-5-5 --- .../main/ui/components/editable_select.cljs | 5 +- .../app/main/ui/components/numeric_input.cljs | 24 ++- .../sidebar/options/menus/frame_grid.cljs | 1 + frontend/test/frontend_tests/runner.cljs | 2 + .../ui/editable_select_test.cljs | 152 ++++++++++++++++++ 5 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 frontend/test/frontend_tests/ui/editable_select_test.cljs diff --git a/frontend/src/app/main/ui/components/editable_select.cljs b/frontend/src/app/main/ui/components/editable_select.cljs index 3c9474a98f..361232e275 100644 --- a/frontend/src/app/main/ui/components/editable_select.cljs +++ b/frontend/src/app/main/ui/components/editable_select.cljs @@ -20,7 +20,7 @@ [rumext.v2 :as mf])) (mf/defc editable-select - [{:keys [value type options class on-change placeholder on-blur input-class aria-label] :as params}] + [{:keys [value type options class on-change placeholder on-blur input-class aria-label is-nillable] :as params}] (let [state* (mf/use-state {:id (uuid/next) :is-open? false :current-value value @@ -164,6 +164,9 @@ :ref on-node-load} (if (= type "number") [:> deprecated-input/numeric-input* {:value (or (some-> current-value value->label) "") + :min min-val + :max max-val + :is-nillable is-nillable :class input-class :on-change set-value :on-focus handle-focus diff --git a/frontend/src/app/main/ui/components/numeric_input.cljs b/frontend/src/app/main/ui/components/numeric_input.cljs index b933c8f1ae..8e33e58c79 100644 --- a/frontend/src/app/main/ui/components/numeric_input.cljs +++ b/frontend/src/app/main/ui/components/numeric_input.cljs @@ -161,7 +161,12 @@ node (mf/ref-val ref)] (when (or up? down?) (set-delta event up? down?)) - (reset! last-value* (parse-value)) + ;; Only a step or a confirmed value is stored: parsing clamps + ;; it to the limits, so Tab or Escape on an untouched input + ;; would save a value the user did not enter. Typed text is + ;; stored by handle-change. + (when (or up? down? enter?) + (reset! last-value* (parse-value))) (when enter? (dom/blur! node)) (when esc? @@ -188,13 +193,13 @@ handle-blur (mf/use-fn - (mf/deps parse-value apply-value update-input on-blur) + (mf/deps apply-value on-blur is-nillable default) (fn [event] (when (mf/ref-val dirty-ref) - (let [new-value (or @last-value* default)] - (if (or is-nillable new-value) - (apply-value event new-value) - (update-input new-value))) + ;; A mixed value has no number. Unless the input is nillable, + ;; leaving it untouched saves nothing instead of the default. + (when (or is-nillable (some? @last-value*)) + (apply-value event (or @last-value* default))) (when (fn? on-blur) (on-blur event))))) @@ -210,10 +215,13 @@ handle-focus (mf/use-callback - (mf/deps on-focus select-on-focus?) + (mf/deps on-focus select-on-focus? value) (fn [event] (when-not (= :dragging (mf/ref-val drag-state*)) - (reset! last-value* (parse-value)) + ;; Keep the current value, not the parsed one: parsing clamps + ;; it to the limits, so leaving the input without typing would + ;; save a value the user did not enter. + (reset! last-value* value) (let [target (dom/get-target event)] (when on-focus (mf/set-ref-val! dirty-ref true) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/frame_grid.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/frame_grid.cljs index 1304fd3c1d..4edfb0f922 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/frame_grid.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/frame_grid.cljs @@ -221,6 +221,7 @@ :class (stl/css :column-select) :input-class (stl/css :numeric-input) :min 1 + :is-nillable true :options size-options :placeholder "Auto" :on-change handle-change-size}]])] diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index cddca81fc2..c8d2240821 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -161,6 +161,7 @@ [frontend-tests.ui.dashboard-import-linking-test] [frontend-tests.ui.drawarea-test] [frontend-tests.ui.ds-controls-numeric-input-test] + [frontend-tests.ui.editable-select-test] [frontend-tests.ui.error-boundary-test] [frontend-tests.ui.file-restored-modal-test] [frontend-tests.ui.gradient-handlers-test] @@ -377,6 +378,7 @@ 'frontend-tests.ui.dashboard-import-linking-test 'frontend-tests.ui.drawarea-test 'frontend-tests.ui.ds-controls-numeric-input-test + 'frontend-tests.ui.editable-select-test 'frontend-tests.ui.error-boundary-test 'frontend-tests.ui.file-restored-modal-test 'frontend-tests.ui.gradient-handlers-test diff --git a/frontend/test/frontend_tests/ui/editable_select_test.cljs b/frontend/test/frontend_tests/ui/editable_select_test.cljs new file mode 100644 index 0000000000..96294a4a17 --- /dev/null +++ b/frontend/test/frontend_tests/ui/editable_select_test.cljs @@ -0,0 +1,152 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.ui.editable-select-test + "A typed value in a number `editable-select` is kept within `:min` and + `:max`, as the arrow keys already do. The frame grid size and the + typography font size depend on it: a value out of range fails the + backend schema and the save is rejected. The documents come from jsdom + since the runner has no DOM." + (:require + ["jsdom" :refer [JSDOM]] + ["react-dom" :as rd] + ["react-dom/client" :as rdc] + [app.main.ui.components.editable-select :refer [editable-select]] + [cljs.test :as t :include-macros true] + [rumext.v2 :as mf])) + +(defn- next-immediate + "Resolves after the tasks React's scheduler queued with `setImmediate` + so far have run." + [] + (js/Promise. (fn [resolve] (js/setImmediate resolve)))) + +(defn- ^:async with-editable-select + "Mounts a number `editable-select` with `props`, calls `f` with its + input and the jsdom window, then unmounts it. Resolves to the result of + `f` and the values passed to `:on-change`, as `[result changes]`. + + react-dom reads the global `window` and `document` while it renders and + handles events, so the jsdom ones stay installed until the root is + unmounted and the scheduler has run." + [props f] + (let [changes (volatile! []) + window (.-window (JSDOM. "
")) + document (.-document window) + container (.getElementById document "root") + root (rdc/createRoot container) + prev-win (.-window js/globalThis) + prev-doc (.-document js/globalThis)] + (set! (.-window js/globalThis) window) + (set! (.-document js/globalThis) document) + (try + (rd/flushSync + (fn [] + (let [props (merge {:type "number" + :value 12 + :options [8 12 16] + :on-change #(vswap! changes conj %)} + props) + js-props (clj->js props)] + ;; clj->js turns keywords into strings: keep the :multiple + ;; value of a mixed selection as the menus pass it. + (set! (.-value js-props) (:value props)) + (.render root (mf/element editable-select js-props))))) + (let [input (.querySelector container "input") + _ (set! (.-attachEvent input) (fn [_ _])) + _ (set! (.-detachEvent input) (fn [_ _])) + ;; react-dom loaded without a document, so it uses its old IE + ;; fallback for change events, which calls the two above on + ;; focus. + result (f input window)] + (rd/flushSync #(.unmount root)) + (await (next-immediate)) + [result @changes]) + (finally + (set! (.-window js/globalThis) prev-win) + (set! (.-document js/globalThis) prev-doc))))) + +(defn- ^:async type-value + "Focuses the input of a number `editable-select` with `props`, types + `text` and presses Enter. With a nil `text` it only focuses the input + and leaves it. Resolves to the values passed to `:on-change`." + [props text] + (let [[_ changes] + (await (with-editable-select + props + (fn [input window] + (rd/flushSync #(.focus input)) + (if (some? text) + (do + (set! (.-value input) text) + (rd/flushSync + #(.dispatchEvent input (new (.-KeyboardEvent window) "keydown" + #js {:key "Enter" :bubbles true})))) + (rd/flushSync #(.blur input))))))] + changes)) + +(defn- ^:async press-key + "Focuses the input of a number `editable-select` with `props`, presses + `key` without typing and leaves the input. Resolves to the values passed + to `:on-change`." + [props key] + (let [[_ changes] + (await (with-editable-select + props + (fn [input window] + (rd/flushSync #(.focus input)) + (rd/flushSync + #(.dispatchEvent input (new (.-KeyboardEvent window) "keydown" + #js {:key key :bubbles true}))) + (rd/flushSync #(.blur input)))))] + changes)) + +(defn- ^:async rendered-text + "Resolves to the text shown by the input of a number `editable-select` + with `props`." + [props] + (let [[text _] (await (with-editable-select props (fn [input _] (.-value input))))] + text)) + +(t/deftest ^:async typed-value-below-min-is-raised-to-min + (t/is (= [1] (await (type-value {:min 1} "0")))) + (t/is (= [1] (await (type-value {:min 1} "-5"))))) + +(t/deftest ^:async typed-value-above-max-is-lowered-to-max + (t/is (= [1000] (await (type-value {:min 3 :max 1000} "5000"))))) + +(t/deftest ^:async typed-value-in-range-is-kept + (t/is (= [7] (await (type-value {:min 1} "7"))))) + +(t/deftest ^:async leaving-an-auto-field-keeps-it-auto + ;; An empty grid size means Auto. Focusing the field and leaving it + ;; without typing must not save the minimum. + (t/is (= [] (await (type-value {:value nil :min 1 :is-nillable true} nil))))) + +(t/deftest ^:async leaving-a-field-keeps-a-stored-value-out-of-range + (t/is (= [] (await (type-value {:value 2 :min 3 :max 1000} nil))))) + +(t/deftest ^:async clearing-a-nillable-field-saves-nil + ;; The grid size is nillable: an empty field means Auto. + (t/is (= [nil] (await (type-value {:value 4 :min 1 :is-nillable true} ""))))) + +(t/deftest ^:async an-auto-nillable-field-shows-its-placeholder + ;; Auto is a nil size: the field stays empty so the placeholder shows, + ;; instead of the input default 0. + (t/is (= "" (await (rendered-text {:value nil :is-nillable true :placeholder "Auto"})))) + (t/is (= "0" (await (rendered-text {:value nil}))))) + +(t/deftest ^:async tab-or-escape-keeps-a-stored-value-out-of-range + ;; Only typed and stepped values are clamped: leaving a font size of 2 + ;; with Tab or Escape must not save the minimum. + (t/is (= [] (await (press-key {:value 2 :min 3 :max 1000} "Tab")))) + (t/is (= [] (await (press-key {:value 2 :min 3 :max 1000} "Escape"))))) + +(t/deftest ^:async leaving-a-mixed-field-saves-nothing + ;; A mixed font size has no value. Leaving it without typing must not + ;; save the input default 0, which the backend rejects. + (t/is (= [] (await (type-value {:value :multiple :min 3 :max 1000} nil)))) + (t/is (= [] (await (press-key {:value :multiple :min 3 :max 1000} "Tab")))))