you can add vendors while creating.

This commit is contained in:
BC
2018-06-14 21:17:07 -07:00
parent d110755939
commit a17b6b9688
11 changed files with 179 additions and 151 deletions

View File

@@ -2,31 +2,38 @@
(:require [reagent.core :as r]
[clojure.string :as str]))
(defn typeahead [{:keys [matches on-change field value class]}]
(defn typeahead [{:keys [matches on-change field text-field value class not-found-description
not-found-value]}]
(let [text (r/atom (or (second (first (filter #(= (first %) value) matches))) ""))
highlighted (r/atom 0)
selected (r/atom (first (first (filter #(= (first %) value) matches))))
select (fn [[id t]]
(reset! selected id)
(reset! text t)
(println [id t])
select (fn [[id text-description text-value]]
(reset! selected id)
(reset! text text-description)
(println (= :not-found id))
(when on-change
(on-change id)))]
(fn [{:keys [matches on-change field value class]}]
(if (= :not-found id)
(on-change nil text-description text-value)
(on-change id text-description (or text-value text-description)))))]
(fn [{:keys [matches on-change field text-field value class not-found-description]}]
(let [valid-matches (take 5 (for [[[id t :as match] i] (map vector matches (range))
:when (str/includes? (.toLowerCase t) (.toLowerCase @text))]
match))]
match))
valid-matches (if (and not-found-description @text)
(concat valid-matches [[:not-found (not-found-description @text) (not-found-value @text)]])
valid-matches)]
[:div.typeahead
(if @selected
[:div.input {:class class}
[:div.control
[:div.tags.has-addons
[:span.tag @text]
[:a.tag.is-delete {:on-click (fn [] (select [nil ""]))}]]]]
[:a.tag.is-delete {:on-click (fn [] (select [nil "" nil]))}]]]]
[:input.input {:type "text"
:class class
:field [:vendor]
:field field
:value @text
:on-blur (fn [e]
(cond @selected
@@ -52,13 +59,19 @@
(reset! highlighted (ffirst valid-matches))
(select [nil (.. e -target -value)]))}
])
(when (and (seq @text)
(not @selected)
(seq valid-matches))
(cond
(and (seq @text)
(not @selected)
(seq valid-matches))
[:div.typeahead-menu
[:ul
(for [[id t :as match] valid-matches]
[:li.typeahead-suggestion {:class (if (= id @highlighted)
"typeahead-highlighted")
:on-mouse-down #(do (println "MATCH" match) (select match))} t])]])]))))
:on-mouse-down #(do (select match))} t])]]
:else
nil)]))))