more typeahead improvements.

This commit is contained in:
BC
2019-01-25 06:44:45 -08:00
parent c677d0191f
commit 226a54b7ad

View File

@@ -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)]))})))