-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select input doesn't repaint selected value when state is changed externally #19
Comments
Ah looks like the bug was caused by trying to set the default selection to the value instead of the label in the control. I just pushed out |
I just gave |
I'm not able to reproduce the problem with |
My mistake. I had 0.2.4 installed. |
Looks like I found another corner case with this issue. If you update the atom after the form has been bound with Interestingly enough, nothing happens after the first button click -- you have to click it a second time for the state to change. Perhaps it is "fighting" with the bound select field? |
The atom is meant to be managed by the |
Ok, so if I instead I use an event attached to |
Looks like you have to explicitly set selected on the option when the document value is changed, and the library currently doesn't support this behavior. I'll take a look to see about adding that. |
Just wanted to note that wrapping It looks like it just re-binds and then re-renders the whole form every time the atom changes. Nice that it works for external changes -- but it breaks everything else! ;; Causes form to re-render for both external (good) and internal (bad) changes
[(fn []
[bind-fields my-form doc])]
|
yeah there are definitely a few quirks with this, not sure what the right way to fix it would be |
Hello, is there any progress on that or someone could provide some initial help so I could work on a PR to fix this? I've following form: (ns survey.components.surveys.edit
(:require [re-frame.core :refer [subscribe dispatch]]
[taoensso.timbre :refer-macros (log spy)]
[reagent-forms.core :refer [bind-fields]]))
(defn- row [label input]
[:div.row
[:div.col-md-2 [:label label]]
[:div.col-md-5 input]])
(defn- add-question [s]
(reset! s (update-in @s [:questions] assoc (survey.utils/unique-id) {:value ""})))
(defn- to-path [& keys]
(spy :debug
(str
(first keys) "."
(clojure.string/join "."
(rest (map #(if (keyword? %) (name %) %) keys))))))
(defn- form [id]
(let [s (subscribe [:find-survey id])
survey (reagent.core/atom @s)]
(fn []
[:div ^{:key id} (str "Display: " id " with: " (-> @survey :questions keys count) " questions")
[:form
[bind-fields (row "name" [:input {:field :text :id :name}]) survey]
[:div (for [k (keys (:questions @survey))]
^{:key k}
[bind-fields (row (str "question " k) [:input {:fields :text :id (to-path :questions k :value)}]) survey])]
[:button.btn.btn-primary {:on-click #(dispatch [:save-survey @survey])} "Save"]
[:button.btn.btn-primary {:on-click #(add-question survey)} "Add question (local component state)"]]])))
(defn render [id]
[(with-meta
(partial form id)
{:component-will-unmount #(dispatch [:destroy-survey id])})]) and when I call One more question, is there any particular reason why the |
The |
I am facing the same issue. I was looking at the code and it seems that |
I can fix need for :value to be text (although replacing it with need for :value to be same as :key) and fix external updates with the code at the following: https://gist.github.com/khdegraaf/55b8464b9b007500cf5dcd50de40d21f |
This seems to be related to the problem I'm seeing with a "dynamic" form-template argument to |
Yeah, the current approach is to use events to modify the atom as seen here. |
I've been toying with the events, thinking they might help, but I don't think I've grokked how they would work. If I add an event handler to |
The forms aren't really meant to be used with dynamic content. The way you'd implement your example would be as follows: (defn row [label input]
[:div.row
[:div.col-md-2 [:label label]]
[:div.col-md-5 input]])
(defn test-form [doc]
[:div
[bind-fields
[:button {:on-click #(swap! doc update :users conj {:username ""})} "Add User"]
doc]
[:div
(for [idx (range (count (:users @doc)))]
^{:key idx}
[bind-fields
[:div [:span (str "User #" (inc idx) ":")]
(row "user name" [:input {:field :text :id :username}])]
(reagent/cursor doc [:users idx])])]
[:label (str @doc)]])
(defn home-page []
(let [doc (reagent/atom {:users []})]
[:div [:h2 "Welcome to Reagent"]
[test-form doc]])) The layout can be managed outside |
Ah, I see! I'd forgotten about cursors (spent some time in Om-land). I'll give that a shot. Thanks! |
Shouldn't using it with re-frame solve the problem? "(...) reagent-forms will not hold any internal state and functions provided by you will be used to get, save, and update the field's value". |
@efraimmgon Not sure about that, I had to do something like this to get dynamic fields to work like I wanted. Which is, I think, basically the same as yogthos' approach above. |
The
:select
list control doesn't display the correct value when initiated with state.For example, if I pass `(atom {:status "P"}) into this form:
NOTE: I've tried both strings and keywords for the value of the
:key
attribute.The form will render with the first option displayed ("Active"). The atom will continue to hold the value of "P" until the user selects a new value from the select.
The issue is also illustrated on the examples page where the select should display
bar
but instead displaysfoo
.I tested this in both the latest releases of Chrome and Firefox on OSX.
The text was updated successfully, but these errors were encountered: