From 226a54b7ada1f4dfaf4f3b06a3d14339868a8851 Mon Sep 17 00:00:00 2001 From: BC Date: Fri, 25 Jan 2019 06:44:45 -0800 Subject: [PATCH] more typeahead improvements. --- .../auto_ap/views/components/typeahead.cljs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cljs/auto_ap/views/components/typeahead.cljs b/src/cljs/auto_ap/views/components/typeahead.cljs index 2f4d4b0e..4818902d 100644 --- a/src/cljs/auto_ap/views/components/typeahead.cljs +++ b/src/cljs/auto_ap/views/components/typeahead.cljs @@ -3,6 +3,15 @@ [reagent.ratom :as ra] [clojure.string :as str])) +(defn get-valid-matches [matches not-found-description not-found-value text] + (let [valid-matches (take 5 (for [[[id t :as match] i] (map vector matches (range)) + :when (str/includes? (or (some-> t .toLowerCase) "") (or (some-> text .toLowerCase) ""))] + match)) + valid-matches (if (and not-found-description text) + (concat valid-matches [[:not-found (not-found-description text) (not-found-value text)]]) + valid-matches)] + valid-matches)) + (defn typeahead [{:keys [matches on-change field text-field value class not-found-description not-found-value auto-focus]}] (let [text (r/atom (or (second (first (filter #(= (first %) value) matches))) "")) @@ -18,15 +27,8 @@ ] (r/create-class {:reagent-render (fn [{:keys [matches on-change field text-field value class not-found-description]}] - (let [text @text - valid-matches (take 5 (for [[[id t :as match] i] (map vector matches (range)) - :when (str/includes? (or (some-> t .toLowerCase) "") (or (some-> text .toLowerCase) ""))] - match)) - valid-matches (if (and not-found-description text) - (concat valid-matches [[:not-found (not-found-description text) (not-found-value text)]]) - - valid-matches)] + valid-matches (get-valid-matches matches not-found-description not-found-value text)] [:div.typeahead (if @selected [:div.input {:class class @@ -41,7 +43,6 @@ [:div.tags.has-addons [:span.tag text] [:a.tag.is-delete {:on-click (fn [] (select [nil "" nil]))}]]]] - [:input.input {:type "text" :class class :field field @@ -69,8 +70,9 @@ false) true)) :on-change (fn [e] - (println valid-matches) - (reset! highlighted (ffirst valid-matches)) + (let [new-matches (get-valid-matches matches not-found-description not-found-value (.. e -target -value))] + (reset! highlighted (ffirst new-matches))) + (select [nil (.. e -target -value)]))} ]) (cond @@ -78,6 +80,7 @@ (not @selected) (seq valid-matches)) (let [h @highlighted] + [:div.typeahead-menu [:ul (for [[id t :as match] valid-matches] @@ -85,7 +88,5 @@ "typeahead-highlighted") :on-mouse-down #(do (select match))} t])]]) - - :else nil)]))})))