Skip to content

Commit

Permalink
Don't perform any keywordization when initiating the values.
Browse files Browse the repository at this point in the history
* This is an issue if fork is fed with state that involves other components as
well, as it will end up wrongly keywordizing parts of the map
  • Loading branch information
luciodale committed Mar 1, 2021
1 parent a9c37bc commit aa5f4e2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [2.2.6]
### Added
- `:keywordize-top-level-keys` to main props to only keywordize top-level-key values
## [2.2.7]
### Changed
- Remove keywordization logic on init values

## [2.2.5]
### Added
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
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.6"}
fork {:mvn/version "2.2.7"}
```

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

`:keywordize-keys` allows you to work with keywords instead of strings. - Boolean

`:keywordize-top-level-keys` allows you to work with top level keywords instead of strings. - Boolean

`:prevent-default?` does not automatically send your form to the server on submit. - Boolean

`:clean-on-unmount?` resets the state when your component is unmounted. (Useful when used with re-frame). - Boolean
Expand Down Expand Up @@ -371,7 +369,7 @@ and pass the password value when giving the function to *Fork* i.e.

### Keywords as input names

By passing the option `{:keywordize-keys true}` to *Fork*, the values and all other state keys will be keywords instead of strings. To make sure the input names are properly registered, use the function `normalize-name` available in the props, when giving the name to the input components i.e. `{:name (normalize-name :bar/foo)}`. Namespaced keys are also supported. If `normalize-name` is used when `keywordize-keys` is not set, the function will simply return the same value.
By passing the option `{:keywordize-keys true}` to *Fork*, the values and all other state keys will be treated as keywords instead of strings. To make sure the input names are properly registered, use the function `normalize-name` available in the props, when giving the name to the input components i.e. `{:name (normalize-name :bar/foo)}`. Namespaced keys are also supported. If `normalize-name` is used when `keywordize-keys` is not set, the function will simply return the same value.

### Dealing with server requests

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.2.6"
(defproject fork "2.2.7"
:description "Reagent & Re-Frame form library"
:url "https://github.com/luciodale/fork"
:license {:name "MIT"}
Expand Down
12 changes: 2 additions & 10 deletions src/fork/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@

(defn initialize-state
[props]
(let [kw? (:keywordize-keys props)
only-top-level-kw? (:keywordize-top-level-keys props)
values (or (merge (:initial-values props)
(let [values (or (merge (:initial-values props)
(:initial-touched props))
{})
initialized-state {:keywordize-keys (:keywordize-keys props)
:values (cond
kw? (walk/keywordize-keys values)
only-top-level-kw? (into {} (mapv (fn [k v] [(keyword k) v]) values))
:else values)
;; TODO - support nested initial-touched keys
:touched (into #{} (map (if kw? keyword identity)
(keys (:initial-touched props))))}]
:values values}]
(if-let [user-provided-state (:state props)]
(do (swap! user-provided-state merge initialized-state)
user-provided-state)
Expand Down

0 comments on commit aa5f4e2

Please sign in to comment.