 (defn- wrap [index count]
   (mod (+ count index) count))
 
-(defn- select-prev
-  [{:as state :keys [suggestions]}]
-  (cond-> state
-    (not-empty suggestions)
-    (update :selected-index #(-> % (or 0) dec (wrap (count suggestions))))))
-
-(defn- select-next
-  [{:as state :keys [suggestions]}]
-  (cond-> state
-    (not-empty suggestions)
-    (update :selected-index #(-> % (or -1) inc (wrap (count suggestions))))))
-
-(defn- select-set
-  [state i]
-  (assoc state :selected-index i))
-
 (defn- choice-made
-  [{:as state :keys [on-change]} choice]
+  [{:as state :keys [on-change change-on-blur?]} choice]
   (when on-change (on-change choice))
-  (-> state
-      (assoc :model choice :input-text "")
-      (dissoc :suggestions)))
+  (cond-> state
+    :always (assoc :model choice :input-text "")
+    ;; the trouble with this is that if change-on-blur? is false then clicking doesn't get rid of the suggestions either
+    (deref-or-value change-on-blur?) (dissoc :suggestions)))
 
 (defn- select-choose
   [{:as state :keys [suggestions selected-index on-change rigid? input-text]}]

           :width          width
           :height         height
           :placeholder    placeholder
           :on-change #(do (swap! state-atom assoc :input-text %)
                           (when-not (clojure.string/blank? %)
                             (put! c-input %)))
           :change-on-blur? false
           :attr {:on-key-down #(on-key-down state-atom %)}]
          (if (or (not-empty suggestions) waiting?)

  (swap! state-atom assoc :waiting? true))

(defn- handle-search
  [state-atom c-sugg c-search]
  (go-loop []
    (let [new-text (<! c-search)
          data-source (:data-source @state-atom)]
      (if (= "" new-text)
        (swap! state-atom reset-typeahead)
        (search-data-source data-source state-atom c-sugg new-text))
