Skip to content

Commit

Permalink
Merge pull request #49 from swalkinshaw/fix-selected-attribute-on-opt…
Browse files Browse the repository at this point in the history
…ions

Options: only set selected attribute when its true
  • Loading branch information
iku000888 authored Dec 16, 2016
2 parents 531d780 + 204abcd commit b275478
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ You can also render individual fields using `formative.core/render-field`. Unlik
"Vanilla")
;; Returns:
[:select {:name "flavor"}
([:option {:value "Chocolate", :selected false} "Chocolate"]
([:option {:value "Chocolate"} "Chocolate"]
[:option {:value "Vanilla", :selected true} "Vanilla"]
[:option {:value "Strawberry", :selected false} "Strawberry"])]
[:option {:value "Strawberry"} "Strawberry"])]
```

Notice that the "Vanilla" option is selected in our generated element.
Expand Down
8 changes: 6 additions & 2 deletions src/formative/render.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@
[:textarea attrs (fu/escape-html (render-input-val field))]))

(defn ^:private build-opt-tag [v text val]
(let [v (str v)]
[:option {:value v :selected (= val v)} text]))
(let [v (str v)
attrs {:value v}
attrs (if (= val v)
(assoc attrs :selected "true")
attrs)]
[:option attrs text]))

(defmethod render-field :select [field]
(let [attrs (get-input-attrs field [:name :id :class :autofocus
Expand Down

0 comments on commit b275478

Please sign in to comment.