mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 21:57:52 +00:00
🐛 Keep typed values within the limits of editable selects (#13251)
* 🐛 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 * 🐛 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 * 🐛 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
This commit is contained in:
parent
a474de4abf
commit
bb79d2f207
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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}]])]
|
||||
|
||||
@ -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
|
||||
|
||||
152
frontend/test/frontend_tests/ui/editable_select_test.cljs
Normal file
152
frontend/test/frontend_tests/ui/editable_select_test.cljs
Normal file
@ -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. "<!doctype html><div id=\"root\"></div>"))
|
||||
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")))))
|
||||
Loading…
x
Reference in New Issue
Block a user