single event for handling changes.

This commit is contained in:
Bryce Covert
2019-04-26 14:32:37 -07:00
parent 5d5db63ed6
commit 48a8e0ce9f
3 changed files with 34 additions and 12 deletions

View File

@@ -107,11 +107,18 @@
]
children)))
:raw-field (fn [control]
(let [{:keys [data]} @(re-frame/subscribe [::form id])]
[bind-field (-> control
(assoc-in [1 :subscription] data)
(assoc-in [1 :event] change-event))]))
:field (fn [label control]
(let [{:keys [data]} @(re-frame/subscribe [::form id])]
[:div.field
(when label [:p.help label])
[:div.control [bind-field (assoc-in control [1 :subscription] data)]]]))
[:div.control [bind-field (-> control
(assoc-in [1 :subscription] data)
(assoc-in [1 :event] change-event))]]]))
:error-notification (fn []
(when-let [error (:error @(re-frame/subscribe [::form id]))]

View File

@@ -47,7 +47,8 @@
[:div.tags.has-addons
[:span.tag text]
[:a.tag.is-delete {:on-click (fn [] (select [nil "" nil]))}]]]]
^{:key "typeahead"} [:input.input {:type "text"
^{:key "typeahead"}
[:input.input {:type "text"
:class class
:value text
@@ -152,7 +153,10 @@
[:div.control
[:div.tags.has-addons
[:span.tag text]
[:a.tag.is-delete {:on-click (fn [] (select nil) (reset! text-atom nil))}]]]]
[:a.tag.is-delete {:on-click (fn []
(select nil)
(reset! text-atom nil)
(reset! highlighted nil))}]]]]
^{:key "typeahead"} [:input.input {:type "text"
:class class
@@ -172,6 +176,7 @@
:else
(do (select nil)
(reset! highlighted nil)
(reset! text-atom nil)
true)))
:on-key-down (fn [e]

View File

@@ -54,11 +54,13 @@
(update :accounts expense-accounts-field/from-graphql (:amount which) locations))))))
(re-frame/reg-event-fx
::change-vendor
::changed
[(forms/in-form ::form)]
(fn [{{:keys [data]} :db} [_ field value]]
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client data))])]
(if (and value (expense-accounts-field/can-replace-with-default? (:accounts data)))
(if (and (= [:vendor] field)
value
(expense-accounts-field/can-replace-with-default? (:accounts data)))
{:dispatch [::forms/change ::form
field value
[:accounts] (expense-accounts-field/default-account (:accounts data)
@@ -107,8 +109,8 @@
;; VIEWS
(def my-form (forms/vertical-form {:can-submit [::can-submit]
:change-event [::forms/change ::form]
(def transaction-form (forms/vertical-form {:can-submit [::can-submit]
:change-event [::changed]
:submit-event [::saving ]
:id ::form}))
@@ -137,7 +139,7 @@
(let [change-event [::forms/change ::form]
{:keys [data] } @(re-frame/subscribe [::forms/form ::form])
locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client data))])
{:keys [form field error-notification submit-button ]} my-form]
{:keys [form field raw-field error-notification submit-button ]} transaction-form]
[form {:title "Hello" :edit-completed edit-completed}
[field "Merchant"
[:input.input {:type "text"
@@ -164,8 +166,7 @@
:type "typeahead-entity"
:auto-focus true
:field [:vendor]
:disabled (boolean (:payment data))
:event [::change-vendor]}]]
:disabled (boolean (:payment data))}]]
[field nil
[expense-accounts-field
{:type "expense-accounts"
@@ -173,7 +174,16 @@
:max (Math/abs (js/parseFloat (:amount data)))
:descriptor "credit account"
:disabled (boolean (:payment data))
:locations locations
:event change-event}]]
:locations locations}]]
[:div.field
[:label.checkbox
[raw-field
[:input
{:type "checkbox"
:field [:exclude-from-ledger]}]]
" Exclude from ledger"]]
[error-notification]
[submit-button "Save"]])])])