tries to allow percentages.

This commit is contained in:
Bryce Covert
2022-12-21 11:16:42 -08:00
parent 7d0f723ef6
commit 54dbfcfbe0

View File

@@ -2,7 +2,7 @@
(:require [reagent.core :as r] (:require [reagent.core :as r]
[clojure.string :as str] [clojure.string :as str]
[react :as react])) [react :as react]))
(def good-% #"^\d{1,3}$") (def good-% #"^\d{1,3}(?:\.\d{1,3})?$")
(defn -percentage-field [{:keys [min max disabled on-blur on-change value class style placeholder]}] (defn -percentage-field [{:keys [min max disabled on-blur on-change value class style placeholder]}]
(let [[ parsed-amount set-parsed-amount] (react/useState {:parsed value (let [[ parsed-amount set-parsed-amount] (react/useState {:parsed value
@@ -10,11 +10,11 @@
(str/blank? value) (str/blank? value)
"" ""
(js/Number.isNaN (js/parseInt value)) (js/Number.isNaN (js/parseFloat value))
"" ""
:else :else
(str (js/parseInt value)))})] (str (js/parseFloat value)))})]
(react/useEffect (fn [] (react/useEffect (fn []
;; allow the controlling field to change the raw representation ;; allow the controlling field to change the raw representation
;; when the raw amount is a valid representation, so that 33. ;; when the raw amount is a valid representation, so that 33.
@@ -30,11 +30,11 @@
(str/blank? value) (str/blank? value)
"" ""
(js/Number.isNaN (js/parseInt value)) (js/Number.isNaN (js/parseFloat value))
"" ""
:else :else
(str (js/parseInt value)))))) (str (js/parseFloat value))))))
nil)) nil))
[:div.control.has-icons-left [:div.control.has-icons-left
[:input.input {:type "text" [:input.input {:type "text"