Fork me on GitHub
#re-frame
<
2015-11-20
>
escherize00:11:12

I've got a re-com question. Let's say I've got an input-text and a button, how do I make hitting enter when the input-text is active trigger the button's on-click? Looking through the recom demo for modal popup has a similar example: https://github.com/Day8/re-com/blob/bf0107cedab51fa5a1789c4035aeebb9ba2edd8e/src/re_demo/modal_panel.cljs#L88, but it appears to just only trigger the first button child.

escherize00:11:02

In plain reagent I'd use:

[:form {:on-submit (fn [e] (.preventDefault e))}
 [input-box-thing]
 [:button.cancel ,,,] ;;<-- don't click this on enter from input-box-thing
 [:button.submitme {:type "submit",,,}]]

mikethompson00:11:18

Could you make the on-blur for the input to the same dispatch as the button's click handler?

gregg00:11:10

Code like this has worked for me:

[re-com/input-text
 :model some-text
 :on-change #(reset! some-text %)
 :attr {:auto-focus   true
        :on-focus     (handler-fn (-> event .-target .select))
        :on-key-press (handler-fn (case (.-which event)
                                    13 (submit-form)
                                    nil))}]
submit-form would be the same code/dispatch that is called when the button is clicked. I have also included the code to initially select the text (`:auto-focus`) and to then select the text when it receives the focus (`:on-focus`) in case you're interested.

gregg00:11:48

You could also add a 27 in the case function to handle the Esc key being pressed and run your cancel code/dispatch

richiardiandrea01:11:24

it is a pleasure to work with re-com guys, thanks, and the last figwheel does not even fully reload the page!

gregg04:11:16

Thanks @richiardiandrea. Do you want to expand on your figwheel comment?

escherize04:11:03

Thanks gregg and mikethompson , that works great

richiardiandrea04:11:41

Before it was reloading the whole page at every change

richiardiandrea04:11:34

version 0.4.1, now with version 0.5.0 it just reloads the changed components, or in any case, it is way ffaster, dunno what exactly Bruce has done 😄

richiardiandrea04:11:52

or you guys, I also switched to alpha2 my web app

gregg04:11:06

We're using figwheel "0.5.0-1" and it seems to be fine.

gregg04:11:58

Presumably if you take figwheel back to 0.4.1 it starts reloading as you expect again?

escherize04:11:38

actually gregg, what does event in your code snippet up there mean?

escherize04:11:07

as in: ,,,:on-focus (handler-fn (-> event .-target .select)),,,

gregg04:11:07

That is just like % in event handlers, except it comes from our handler-fn macro

escherize04:11:47

Ahh, never seen that before, thanks.

gregg04:11:39

There's a nice long explanation about why we created handler-fn in that file, but basically it guarantees that handler functions return nil as per React requirements