Skip to content
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

Component State #137

Open
njj opened this issue Mar 12, 2018 · 8 comments
Open

Component State #137

njj opened this issue Mar 12, 2018 · 8 comments

Comments

@njj
Copy link

njj commented Mar 12, 2018

Hey there! I'm trying to track the state change of an input to apply various CSS classes to it based on valid/invalid state.

For some reason it doesn't seem like the input is being re-rendered after I update component state on blur when I'm using () to implement the inputs. Strangely enough, I can see the event firing via console log when I render the input as [] but the actual state updating still does not happen.

Here's a snippet, ignore some of the random stuff in there. The key piece here is the swap! on-blur:

(defn input [label type id error-id]
  (let [key (keyword (name id))
        component-state (r/atom {:count 0})]
    (js/console.log "hi" label (get @component-state :count))
   [:div.col-sm-6
    [:label.input-label label]
    [:div.input-container
     [:input.input-field.form-control
      {:field type
       :id id
       :on-blur #(swap! component-state update-in [:count] inc)}]
     [:div.error-container {:field :alert :id error-id}]]]))
@yogthos
Copy link
Member

yogthos commented Mar 12, 2018

The form definition has to be a data structure, and it's not possible to put in functions because bind-fields users postwalk to walk the structure and create UI components from it. The fields inside your input will never be processed by the library. There's a bit more on this in the binding the form to a document section.

@njj
Copy link
Author

njj commented Mar 12, 2018

@yogthos Thanks for the reply. How might you recommend we approach something like this? Basically the scenario is we want to conditionally apply a CSS class to inputs based on if they are errored out or not.

@yogthos
Copy link
Member

yogthos commented Mar 12, 2018

Unfortunately, that's a bit of an omission at the moment. The only field that allows setting class on validation is :container, so you can do something like:

[:span {:field :container :valid? #(when (empty? (:first-name %)) "error")}
 [:input {:field :text :id :first-name}]]

I'll take a look at extending this for all field types.

@njj
Copy link
Author

njj commented Mar 12, 2018

@yogthos Thanks! I think this will work as a stop gap.

@njj njj closed this as completed Mar 12, 2018
@yogthos
Copy link
Member

yogthos commented Mar 12, 2018

I'll keep this open actually, since it does look like something that would be good to add.

@yogthos yogthos reopened this Mar 12, 2018
@njj
Copy link
Author

njj commented Mar 12, 2018

@yogthos Maybe if I find the time, I can try and make a PR soon.

@nenadalm
Copy link

Hi. I wanted to try this library with material-ui.

Material-ui has component TextField, which can be used instead of :input and data flows flawlessly. The problem is with validation. TextField has prop error which should be set to true if error occurs (so the input is red) and prop helperText to which error message can be put.

Based on the docs, it doesn't seems to be currently possible. Maybe additional prop could be added into reagent-forms (:on-document-change?) Which could accept callback that can alter props of the :input and read the document?

@yogthos
Copy link
Member

yogthos commented Mar 12, 2018

Yeah, I think having a general function that can modify the attributes would be very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants