首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作用于试剂中不同成分后的聚焦和模糊成分

作用于试剂中不同成分后的聚焦和模糊成分
EN

Stack Overflow用户
提问于 2017-06-22 22:37:57
回答 1查看 573关注 0票数 2

下面是我的Clojurescript代码:

代码语言:javascript
复制
(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中refthis is a typical justified用例在Reagent中似乎有不同的工作方式。

有人能在Reagent/re-frame中推荐一种方法来解决这个特定的焦点管理问题吗?

EN

回答 1

Stack Overflow用户

发布于 2017-06-23 17:33:57

我认为效果的使用是合理的。

代码语言:javascript
复制
(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"]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44702533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档