下面是我的Clojurescript代码:
(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"]])))请注意,我还使用了re-frame。我将输入状态(其值和禁用状态)保留在我的app-db中,并在我的组件中订阅它们。因此,一旦这些道具中的任何一个发生变化,就很容易将焦点强制放在组件上(这就是我的焦点包装器的用途)。
尽管如此,我正在努力寻找一种方法,一旦点击跳过按钮,就强制将焦点放在输入上。据我所知,React中ref的this is a typical justified用例在Reagent中似乎有不同的工作方式。
有人能在Reagent/re-frame中推荐一种方法来解决这个特定的焦点管理问题吗?
发布于 2017-06-23 17:33:57
我认为效果的使用是合理的。
(reframe/reg-event-fx
:next-char
(fn [cofx [_ a]]
(.focus (.getElementById js/document "text-field-id"))
{}))
...
[:button {:type "button"
:on-click #(dispatch [:next-char])}
"Skip"]https://stackoverflow.com/questions/44702533
复制相似问题