Skip to content

Commit

Permalink
Add :state config key to allow the usage of user custom state
Browse files Browse the repository at this point in the history
  • Loading branch information
luciodale committed Dec 2, 2020
1 parent d4e4911 commit bed9ebd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [2.2.5]
### Added
- `state` to main props to allow users to pass their own ratoms

## [2.2.3]
### Added
- `dirty` to main props
Expand Down
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -33,7 +33,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.2.4"}
fork {:mvn/version "2.2.5"}
```

or
Expand Down Expand Up @@ -178,6 +178,8 @@ If some parts look a bit obscure, the will be explained thoroughly in the follow

`:form-id` makes fork aware of your form elements. If it is not specified, a random id will be generated and will be provided through the same `:form-id` key. - Key

`:state` lets you pass a ratom that will be used as a db. Useful if you need to access it outside the fork component. - Ratom

`:path` lets you choose where to store your form global events i.e. server related stuff. - Keyword/String OR Vector of keys

`:keywordize-keys` allows you to work with keywords instead of strings. - Boolean
Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject fork "2.2.4"
(defproject fork "2.2.5"
:description "Reagent & Re-Frame form library"
:url "https://github.com/luciodale/fork"
:license {:name "MIT"}
Expand Down
19 changes: 12 additions & 7 deletions src/fork/core.cljs
@@ -1,7 +1,8 @@
(ns fork.core
(:require
[clojure.data :as data]
[clojure.walk :as walk]))
[clojure.walk :as walk]
[reagent.core :as r]))

(defn- vec-remove
[coll pos]
Expand All @@ -17,12 +18,16 @@
(let [kw? (:keywordize-keys props)
values (or (merge (:initial-values props)
(:initial-touched props))
{})]
{:keywordize-keys (:keywordize-keys props)
:values (if kw? (walk/keywordize-keys values) values)
;; TODO - support nested initial-touched keys
:touched (into #{} (map (if kw? keyword identity)
(keys (:initial-touched props))))}))
{})
initialized-state {:keywordize-keys (:keywordize-keys props)
:values (if kw? (walk/keywordize-keys values) values)
;; TODO - support nested initial-touched keys
:touched (into #{} (map (if kw? keyword identity)
(keys (:initial-touched props))))}]
(if-let [user-provided-state (:state props)]
(do (swap! user-provided-state merge initialized-state)
user-provided-state)
(r/atom initialized-state))))

(defn element-value
[evt]
Expand Down
2 changes: 1 addition & 1 deletion src/fork/re_frame.cljs
Expand Up @@ -56,7 +56,7 @@

(defn form
[props _]
(let [state (r/atom (core/initialize-state props))
(let [state (core/initialize-state props)
p (:path props)
path (cond
(and p (vector? p)) p
Expand Down
2 changes: 1 addition & 1 deletion src/fork/reagent.cljs
Expand Up @@ -41,7 +41,7 @@

(defn form
[props _]
(let [state (r/atom (core/initialize-state props))
(let [state (core/initialize-state props)
p (:path props)
path (cond
(and p (vector? p)) p
Expand Down

0 comments on commit bed9ebd

Please sign in to comment.