Fork me on GitHub
#re-frame
<
2020-03-28
>
Travis Smith21:03:24

I'm finding that implementing a drop down menu in re-frame is less intuitive than I'd hoped. There's a submit button almost by definition. How would I route a dropdown selection back to my app-db? Should I be using a framework for components like this? As you might imagine, I'm new to front end development, so sorry if this sounds strange.

p-himik21:03:11

First, think about this: how would you do it with any other framework, or library, or even vanilla JS?

Travis Smith22:03:59

Yeah, I'm super new at this. I've looked at plain HTML, and I've looked at a ReactJS component tutorial. The part that is tripping me up is that I understand how to implement options, but I don't see conceptually how choosing an option is captured into an action. With a button, you have the :on-click #(dispatch something) pattern, but I don't see any examples of this in a dropdown.

knubie22:03:27

Can you add :on-click handlers to your drop down items?

Travis Smith22:03:37

Here's where I'm at: (defn template-listing-dropdown [] [:div#template-listing-dropdown [:form [:fieldset [:legend "Templates"] [:p [:label "Select list"] [:select#myList {:on-change #(dispatch [:add-template-items test-director])} ;; need to find how to access the :option :value [:option {:value "nothing"} ""] [:option {:value test-director} "Test Director"] [:option {:value "nonced"} "Nonced!"] ]]] ] ])

Travis Smith22:03:24

my :on-change works to trigger a dispatch, but I don't know how to access the values in :option :value to go into that dispatch and customize the response.

Travis Smith22:03:07

I feel like there should be a reference somewhere that I'm missing...looking at React tutorials and it makes sense over there, but re-frame handles state differently.

knubie22:03:36

:on-change function probably accepts some argument (event most likely)

knubie22:03:13

:on-change (fn [event] (-> event .-target .-value))

knubie22:03:36

event.target.value should give you the value of the selected option

knubie22:03:43

from there you can pass that to your re-frame event

knubie22:03:06

#(dispatch […]) is syntactic sugar for (fn [& args] (dispatch […])) and you can access the first argument in #(dispatch …) with %. So % is the event object

Travis Smith23:03:11

hmm. Thank you. I'll work on this.

👍 4
mikethompson01:03:59

@UP5089ML1 You may be interested to look at re-com for ideas and further explanation

mikethompson02:03:35

Either that or the TodoMVC example in the re-frame repository under /examples

beders16:03:53

If you are dealing with a lot of forms, you might want to look into https://github.com/luciodale/fork

👍 4