add money field.

This commit is contained in:
Bryce Covert
2020-05-04 18:20:01 -07:00
parent 8b994de231
commit 515d15797b
4 changed files with 49 additions and 9 deletions

View File

@@ -2,6 +2,7 @@
(:require [auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.views.components.typeahead :refer [typeahead typeahead-entity]]
[auto-ap.views.components.money-field :refer [money-field]]
[auto-ap.views.utils :refer [bind-field dispatch-event ->$]]
[goog.string :as gstring]
[re-frame.core :as re-frame]
@@ -37,7 +38,7 @@
(update :amount js/parseFloat)
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
(Math/abs (js/parseFloat total)))))
(Math/abs total))))
(assoc :amount-mode "$")))
accounts))
[{:id (str "new-" (random-uuid))
@@ -78,7 +79,7 @@
(assoc ea :amount
(js/parseFloat
(goog.string/format "%.2f"
(* (/ (js/parseFloat (:amount-percentage ea)) 100.0) (js/parseFloat total))))))
(* (/ (js/parseFloat (:amount-percentage ea)) 100.0) total)))))
expense-accounts))
(re-frame/reg-event-fx

View File

@@ -0,0 +1,23 @@
(ns auto-ap.views.components.money-field
(:require [reagent.core :as r]
[clojure.string :as str]))
(defn money-field [{:keys [min max disabled on-change value]}]
(let [parsed-amount (r/atom {:parsed value
:raw (str value)})]
(fn [{:keys [min max disabled on-change value]}]
[:input.input {:type "number"
:disabled disabled
:on-change (fn [e]
(let [raw (.. e -target -value)
new-value (when (and raw (not (str/blank? raw)))
(js/parseFloat raw))]
(swap! parsed-amount assoc
:raw raw
:parsed new-value)
(when (not= value new-value)
(on-change new-value))))
:value (:raw @parsed-amount)
:min min
:max max
:step "0.01"}])))