Here is my Clojurescript code:
(def focus-wrapper
(with-meta identity
{:component-did-update #(.focus (dom-node %))}))
(defn solution-input []
(let [current-char (subscribe [:current-char])
input (subscribe [:input])]
[focus-wrapper
(fn []
[:input {:type "text"
:auto-focus true
:value (:value @input)
:disabled (:disabled @input)
:on-change #(dispatch [:input-value-updated (.-target.value %)])
:on-key-press #(when (= 13 (.-which %))
(if (= (.-target.value %) (:solution @current-char))
(dispatch [:right-option-picked])
(dispatch [:wrong-option-picked])))}])]))
(defn quiz [props childrn this]
(let [current-char (subscribe [:current-char])
counter (subscribe [:counter])
feedback (subscribe [:feedback])]
(fn []
[:div.panel-container
[:h2 "Quiz"]
[:div.counter (str (:correct-guesses @counter) "/" (:total-guesses @counter))]
[:div.char (:hint @current-char)]
[solution-input]
[:div.feedback (when (not= @feedback "off") @feedback)]
[:button {:type "button"
:on-click #(dispatch [:next-char])}
"Skip"]
[:button {:type "button"
:on-click #(dispatch [:panel-changed "result"])}
"Finish"]])))
Notice I am also using re-frame. I am keeping the input state (its value and disabled state) in my app-db and subscribing to them in my component. Therefore, it is easy to force the focus on the component once any of these props change (that is what my focus-wrapper is for).
Nevertheless, I am struggling to find a way to force focus on the input once the skip button is clicked. As far as I am aware, this is a typical justified use case for ref in React, which seem to work differently in Reagent.
Can someone recommend a way to solve this specific focus management problem in Reagent/re-frame?