Skip to content

Commit

Permalink
Add reset handler to clean the form state
Browse files Browse the repository at this point in the history
  • Loading branch information
luciodale committed Feb 27, 2020
1 parent 917324f commit af253e5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -52,3 +52,7 @@ All notable changes to this project will be documented in this file.
## [1.2.4]
### Added
- `:throttle` option to `on-submit-response`

## [1.2.5]
### Added
- `reset` handler to props
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -31,7 +31,7 @@ As at this state you must be dying of curiosity, I will dive right into the code
#### In Deps

```clojure
fork {:mvn/version "1.2.4"}
fork {:mvn/version "1.2.5"}
```

or
Expand Down Expand Up @@ -423,6 +423,7 @@ You bet it does. The keys you can currently access from your form function are:
[db
props
state
reset
values
form-id
errors
Expand Down Expand Up @@ -450,6 +451,13 @@ Here is a demonstration on how to use the above handlers that have not been ment

(swap! state assoc :something :new)

;; to remove full state
(reset)

;; to reset state to given map
(reset {:values {"name" "John"}
:touched #{"name"}})

(set-touched "input" "another-input")

(set-untouched "input" "another-input")
Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject fork "1.2.4"
(defproject fork "1.2.5"
:description "Reagent & Re-Frame form library"
:url "https://github.com/luciodale/fork"
:license {:name "MIT"}
Expand Down
6 changes: 5 additions & 1 deletion src/fork/core.cljs
Expand Up @@ -25,7 +25,10 @@
(logic/send-server-request
e f (merge opt
{:state state
:path (:path props)})))}]
:path (:path props)})))
:reset (fn [& [m]] (reset! state (merge {:values {}
:touched #{}}
m)))}]
(r/create-class
{:component-did-mount
#(when-let [on-mount (:component-did-mount props)]
Expand Down Expand Up @@ -61,6 +64,7 @@
:handle-change (:handle-change handlers)
:handle-blur (:handle-blur handlers)
:send-server-request (:send-server-request handlers)
:reset (:reset handlers)
:handle-submit #(logic/handle-submit % (merge props
{:state state
:db db
Expand Down

0 comments on commit af253e5

Please sign in to comment.