Skip to content

Commit

Permalink
Store initial-values and initial-touched in state
Browse files Browse the repository at this point in the history
  • Loading branch information
luciodale committed Oct 6, 2021
1 parent 3e1cf63 commit 7890e34
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [2.4.2]
### Fix
- store initial-values and initial-touched in state so they can be modified after first mount

## [2.4.1]
### Added
- set-handle-change takes a vector of maps vs one map
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ As at this state you must be dying of curiosity, I will dive right into the impl
#### In Deps

```clojure
fork {:mvn/version "2.4.1"}
fork {:mvn/version "2.4.2"}
```

or
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject fork "2.4.1"
(defproject fork "2.4.2"
:description "Reagent & Re-Frame form library"
:url "https://github.com/luciodale/fork"
:license {:name "MIT"}
Expand Down
28 changes: 14 additions & 14 deletions src/fork/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
(get (:touched @state) k)))

(defn initialize-state
[props]
(let [values (or (merge (:initial-values props)
(:initial-touched props))
[{:keys [state keywordize-keys initial-values initial-touched]}]
(let [values (or (merge initial-values initial-touched)
{})
initialized-state {:keywordize-keys (:keywordize-keys props)
initialized-state {:keywordize-keys keywordize-keys
:initial-values initial-values
:initial-touched initial-touched
:values values
:touched (into #{} (keys (:initial-touched props)))}]
(if-let [user-provided-state (:state props)]
:touched (into #{} (keys initial-touched))}]
(if-let [user-provided-state state]
(do (swap! user-provided-state merge initialized-state)
user-provided-state)
(r/atom initialized-state))))
Expand Down Expand Up @@ -223,8 +224,7 @@

(defn handle-submit
[evt {:keys [state server on-submit prevent-default?
initial-values touched-values path
validation already-submitting? reset]}]
path validation already-submitting? reset]}]
(when prevent-default? (.preventDefault evt))
(swap! state update :attempted-submissions inc)
(when (and (not already-submitting?)
Expand All @@ -236,14 +236,14 @@
{:state state
:path path
:values (:values @state)
:dirty (dirty (:values @state) (merge initial-values
touched-values))
:dirty (dirty (:values @state) (merge (:initial-values @state)
(:touched-values @state)))
:reset reset})))

(defn send-server-request
[http-fn
{:keys [state validation evt name value path server-dispatch-logic
debounce throttle initial-values touched-values]}]
{:keys [state validation evt name value path
server-dispatch-logic debounce throttle]}]
(let [input-key name
input-value value
values (merge
Expand All @@ -254,8 +254,8 @@
(conj (:touched @state) input-key)
(:touched @state))
props {:path path
:dirty (dirty values (merge initial-values
touched-values))
:dirty (dirty values (merge (:initial-values @state)
(:touched-values @state)))
:errors (when validation (handle-validation {:values values}
validation))
:values values
Expand Down
4 changes: 2 additions & 2 deletions src/fork/re_frame.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
:path path
:form-id form-id
:values (:values @state)
:dirty (core/dirty (:values @state) (merge (:initial-values props)
(:touched-values props)))
:dirty (core/dirty (:values @state) (merge (:initial-values @state)
(:touched-values @state)))
:errors validation
:server-errors server-validation
:on-submit-server-message on-submit-server-message
Expand Down
4 changes: 2 additions & 2 deletions src/fork/reagent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
:path path
:form-id form-id
:values (:values @state)
:dirty (core/dirty (:values @state) (merge (:initial-values props)
(:touched-values props)))
:dirty (core/dirty (:values @state) (merge (:initial-values @state)
(:touched-values @state)))
:errors validation
:server-errors server-validation
:on-submit-server-message on-submit-server-message
Expand Down

0 comments on commit 7890e34

Please sign in to comment.