   (componentWillMount [this]
     (.update-text this "")
     (.update-selected this [])
-    (.update-caret this 0)
+    (.update-caret this (constantly 0))
     (.show-matches this false))
 
   (componentWillReceiveProps [this props]


  (update-selected [this items]
    (om/update-state! this assoc :selected items)
    (.update-caret this (:caret (om/get-state this)))
    (some-> this om/props :change-fn (apply [items])))

  (select-match [this match]

        (->> (concat before [match] after)
             (into [])
             (.update-selected this)))
      (.update-caret this inc)
      (.update-text this "")))

  (show-matches [this show?]
    (om/update-state! this assoc :show-matches show?))

  (focus [this e]
    (-> this
        (om/react-ref :input/box)
        (om/react-ref :input/input)
        dom/node
        .focus)
    (.show-matches this true))

  (set-active-match [this child]
    (om/update-state! this assoc :active-match (-> child om/props :item)))

  (update-caret [this f]
    (let [selected (:selected (om/get-state this))]
      (om/update-state! this update :caret
                        (fn [caret]
                          (-> (f caret)
                              (max 0)
                              (min (count selected)))))))

  (key-backspace [this e]
    (when (empty? (:text (om/get-state this)))
      (let [selected (:selected (om/get-state this))
            caret (:caret (om/get-state this))]
        (let [[before after] (split-at caret selected)]
          (->> (concat (butlast before) after)
               (into [])
               (.update-selected this)))
        (.update-caret this dec)
        (.update-text this ""))))

  (key-left [this e]
    (when (empty? (:text (om/get-state this)))
      (.update-caret this dec)))

  (key-right [this e]
    (when (empty? (:text (om/get-state this)))
      (.update-caret this inc)))

  (key-escape [this e]
    (.show-matches this false))
