How To Detect “Enter” Keypress in Reagent?

前端 未结 2 1084
孤城傲影
孤城傲影 2021-02-19 07:22

Given the following code:

  [:input {:type \"text\"
           :value (:text @app-state)
           :on-change (fn [e]
                        (if (= 31 (.-keyCo         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 07:59

    that's how to fix it:

    1. you should listen to :on-key-press (rather than :on-change), because "enter" doesn't trigger :on-change event (it obviously doesn't change the text)
    2. key code for "enter" is 13, not 31
    3. use charCode instead of keyCode (not an expert in js, but keyCode doesn't work for me in firefox)

      [:input {:type "text"
               :value (:text @app-state)
               :on-key-press (fn [e]
                               (println "key press" (.-charCode e))
                               (if (= 13 (.-charCode e))
                                 (println "ENTER")
                                 (println "NOT ENTER")))}]
      

提交回复
热议问题