Clojurescript Semantic UI React Search自定义渲染器

时间:2018-03-16 18:11:46

标签: semantic-ui clojurescript reagent re-frame

我正在尝试使用试剂/重新框架和语义ui在Clojurescript中实现搜索。 Semantic-ui使用渲染器来提供建议。此渲染器默认为图像,价格,标题,说明。由于我想对地理编码提出建议,我想列出地址。这是我得到的返回数据。我基本上想在建议中显示姓名,城市和邮政编码。

{:hits
 [{:osm_type "W",
   :name "Am Pfuhl",
   :osm_value "residential",
   :city "Berlin",
   :postcode "12209",
   :state "Berlin",
   :osm_key "highway",
   :extent [13.322584 52.4205878 13.3258975 52.419743],
   :point {:lng 13.3241429, :lat 52.4201622},
   :osm_id 103012039,
   :country "Deutschland"}
 :took 7}

我写的代码没有告诉我任何结果。我尝试了很多,但我不知道如何查看组件以查看其状态是否发生变化以及是否存储结果。当我直接调用它时,订阅会给我回复结果。

(def search (helper/component "Search"))
(def grid (helper/component "Grid"))
(def grid-row (helper/component "Grid" "Row"))

(defn on-search-change [event props]
  (rf/dispatch [:get-geocode (:value (js->clj props :keywordize-keys true))]))

(defn on-result-select [event props]
  (rf/dispatch [:geocode-selected]))

(defn get-geocode-results []
  @(rf/subscribe [:geocode-results]))

(defn result-renderer [& meh]
  (fn [meh]
    [:div (str meh)]))

(defn geocode-component []
  [:> grid
   [:> grid-row
    [:> search {:minCharacters 3
                :loading (when (:geocode @(rf/subscribe [:loading])) )
                :defaultValue "Berlin"
                :selectFirstResult true
                :onSearchChange on-search-change
                :onResultSelect on-result-select
                :resultRenderer result-renderer
                :results get-geocode-results}]]])

我非常感谢一些帮助: 如何确定组件是否正确存储结果? 如何编写只渲染所有调试结果的渲染器?

谢谢和问候!蒂莫

编辑:https://gist.github.com/TimoKramer/7e93758afb81dcad985fafccc613153a上的解决方案

1 个答案:

答案 0 :(得分:1)

从文档看起来:resultRenderer需要一个React组件,并且你给它一个Clojure函数。您可以使用reagent.core/as-component将打嗝式组件转换为React组件。

我还没有对此进行过测试,但也许它可以像以下一样简单:

(defn result-renderer [& meh]
  (reagent.core/as-component [:div (str meh)]))

我在工具提示中使用了类似的策略:

(defn info-icon
  ([message]
   (info-icon {} message))

  ([options message]
   (let [popup (component "Popup")
         icon  (component "Icon")]
     [:> popup
      {:trigger (reagent/as-component [:> icon (merge {:name "info"} options)])}
      " "
      message])))

component可能与您的helper/component匹配,reagentreagent.core