Skip to content

Instantly share code, notes, and snippets.

@rwillig
Created April 25, 2016 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwillig/b9d8fceeef6442df2f43cd73aee3726a to your computer and use it in GitHub Desktop.
Save rwillig/b9d8fceeef6442df2f43cd73aee3726a to your computer and use it in GitHub Desktop.
(defelem typeahead [{:keys [options datasets id cell] :as attr :or {id (gensym)}} _]
(let [id (if (cell? id) @id id)
attrs (dissoc attr :options :datasets :cell :id)
field (input :id id :type "text" :change #(when (cell? cell) (reset! cell @%)) attrs)
select-fn (fn [cell ev s] (when (cell? cell) (reset! cell (aget s "value"))))]
(with-let [t (div (field))]
(when-dom t #(do
(.typeahead
(js/jQuery (str "#" id))
(clj->js options)
(apply clj->js datasets))
(.bind
(js/jQuery (str "#" id))
(clj->js :typeahead:select)
(partial select-fn cell)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment