Minor tweaks for percentages

This commit is contained in:
Bryce Covert
2023-01-01 15:24:05 -08:00
parent 0f062e0236
commit 7223ef8c0d
2 changed files with 29 additions and 39 deletions

View File

@@ -15,50 +15,32 @@
:else :else
(str (js/parseFloat value)))})] (str (js/parseFloat value)))})]
(react/useEffect (fn []
;; allow the controlling field to change the raw representation
;; when the raw amount is a valid representation, so that 33.
;; doesn't get unset
(when (or
(and (:raw parsed-amount)
(re-find good-% (:raw parsed-amount)))
(str/blank? (:raw parsed-amount)))
(set-parsed-amount
(assoc parsed-amount
:parsed value
:raw (cond
(str/blank? value)
""
(js/Number.isNaN (js/parseFloat value))
""
:else
(str (js/parseFloat value))))))
nil))
[:div.control.has-icons-left [:div.control.has-icons-left
[:input.input {:type "text" [:input.input {:type "text"
:disabled disabled :disabled disabled
:placeholder placeholder :placeholder placeholder
:class class :class class
:on-change (fn [e] :on-change (fn [e]
(let [raw (.. e -target -value) (set-parsed-amount
new-value (when (and raw (assoc parsed-amount :raw (.. e -target -value))))
(not (str/blank? raw))
(re-find good-% raw))
(js/parseFloat raw))]
(set-parsed-amount {:raw raw
:parsed new-value})
(when (not= value new-value)
(on-change new-value))))
:value (or (:raw parsed-amount) :value (or (:raw parsed-amount)
"") "")
:on-blur (fn [] :on-blur (fn []
(when-not (re-find good-% (:raw parsed-amount)) (let [raw (:raw parsed-amount)]
(if (re-find good-% raw)
(set-parsed-amount {:raw "" (let [new-value (when (and raw
:parsed nil}) (not (str/blank? raw))
(on-change nil)) (re-find good-% raw))
(js/parseFloat raw))]
(set-parsed-amount {:raw raw
:parsed new-value})
(when (not= value new-value)
(on-change new-value)))
(do
(set-parsed-amount {:raw ""
:parsed nil})
(on-change nil))))
(when on-blur (when on-blur
(on-blur))) (on-blur)))
:min min :min min

View File

@@ -104,15 +104,22 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::updated ::updated
[(re-frame/inject-cofx ::inject/sub [::subs/client])] [
(fn [{:keys [db] ::subs/keys [client]} [_ _ command]] (re-frame/inject-cofx ::inject/sub (fn [[_ _ _ client]]
[::subs/locations-for-client (:id client)]))]
(fn [{:keys [db] ::subs/keys [locations-for-client]} [_ _ command client]]
(println locations-for-client)
(when (= :create command) (when (= :create command)
{:db {:db
(-> db (-> db
(forms/stop-form ::form ) (forms/stop-form ::form )
(forms/start-form ::form {:client client (forms/start-form ::form {:client client
:status :unpaid :status :unpaid
:date (c/now)}))}))) :date (c/now)
:expense-accounts
(eaf/from-graphql []
0.0
locations-for-client)}))})))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::adding ::adding
@@ -253,7 +260,8 @@
:class "live-added") :class "live-added")
(if (:id data) (if (:id data)
:edit :edit
:create)]) :create)
(:client data)])
:on-error [::forms/save-error ::form]}}))) :on-error [::forms/save-error ::form]}})))
(re-frame/reg-event-fx (re-frame/reg-event-fx