more progress for client fdorm.

This commit is contained in:
2022-07-20 08:10:30 -07:00
parent 86aaa4a933
commit 899efe0efa
5 changed files with 130 additions and 100 deletions

View File

@@ -44,19 +44,21 @@
(defn builder [{:keys [value on-change can-submit data-sub error-messages change-event submit-event id fullwidth? schema validation-error-string]}]
(when (and change-event on-change)
(throw "Either the form is to be managed by ::forms, or it should have value and on-change passed in"))
(let [data-sub (or data-sub [::forms/form id])
change-event (when-not on-change
(or change-event [::forms/change id]))
(let [data-sub (or data-sub [::forms/form id])
change-event (when-not on-change
(or change-event [::forms/change id]))
{:keys [data visited attempted-submit?] form-key :id} @(re-frame/subscribe data-sub)
data (or value data)
status @(re-frame/subscribe [::status/single id])
can-submit (if can-submit @(re-frame/subscribe can-submit)
true)
problems (when schema
(m/explain schema data))]
data (or value data)
status @(re-frame/subscribe [::status/single id])
can-submit (if can-submit @(re-frame/subscribe can-submit)
true)
;; TODO ONLY validate on blur
problems (when schema
(m/explain schema data))]
(r/create-element Provider #js {:value #js {:can-submit can-submit
:error-messages (or error-messages
nil)
nil)
:on-change on-change
:change-event change-event
:blur-event [::forms/visited id]
@@ -155,14 +157,14 @@
["visited" "attempted-submit?" "problems" "error-messages"]
(fn [visited attempted-submit? problems error-messages]
(consume FormScopeConsumer
["form-scope"]
(fn [form-scope]
["scope"]
(fn [scope]
(let [full-field-path (cond
(sequential? field)
(into form-scope field)
(into scope field)
field
(conj form-scope field)
(conj scope field)
:else
nil)
@@ -181,43 +183,43 @@
["visited" "attempted-submit?" "data" "on-change" "change-event" "blur-event" "problems"]
(fn [visited attempted-submit? data on-change change-event blur-event problems]
(consume FormScopeConsumer
["form-scope"]
(fn [form-scope]
["scope"]
(fn [scope]
(update child 1 (fn [child-props]
(let [
full-field-path (cond
(sequential? field)
(into form-scope field)
(let [
full-field-path (cond
(sequential? field)
(into scope field)
field
(conj form-scope field)
:else
nil)
visited? (get visited full-field-path)
value (get-in data full-field-path)]
(-> child-props
(assoc :on-change
(if on-change
(partial form-change-handler data full-field-path on-change)
(partial change-handler full-field-path change-event))
:on-blur (partial blur-handler full-field-path blur-event)
:value value)
(update :class (fn [class]
(str class
(cond
(and (not visited?) (not attempted-submit?))
""
(not (valid-field? problems full-field-path))
" is-danger"
value
"is-success"
field
(conj scope field)
:else
""))))))))))))))
nil)
visited? (get visited full-field-path)
value (get-in data full-field-path)]
(-> child-props
(assoc :on-change
(if on-change
(partial form-change-handler data full-field-path on-change)
(partial change-handler full-field-path change-event))
:on-blur (partial blur-handler full-field-path blur-event)
:value value)
(update :class (fn [class]
(str class
(cond
(and (not visited?) (not attempted-submit?))
""
(not (valid-field? problems full-field-path))
" is-danger"
value
"is-success"
:else
""))))))))))))))
(defn with-scope [{:keys [scope]}]
(r/create-element FormScopeProvider #js {:value scope}
(r/create-element FormScopeProvider #js {:value #js {:scope scope}}
(r/as-element (into [:<>]
(r/children (r/current-component))))))