{"action":"closed","issue":{"url":"https://api.github.com/repos/reagent-project/reagent-forms/issues/116","repository_url":"https://api.github.com/repos/reagent-project/reagent-forms","labels_url":"https://api.github.com/repos/reagent-project/reagent-forms/issues/116/labels{/name}","comments_url":"https://api.github.com/repos/reagent-project/reagent-forms/issues/116/comments","events_url":"https://api.github.com/repos/reagent-project/reagent-forms/issues/116/events","html_url":"https://github.com/reagent-project/reagent-forms/issues/116","id":186015095,"number":116,"title":"typeahead date-source returning a vector","user":{"login":"asok","id":120457,"avatar_url":"https://avatars.githubusercontent.com/u/120457?v=3","gravatar_id":"","url":"https://api.github.com/users/asok","html_url":"https://github.com/asok","followers_url":"https://api.github.com/users/asok/followers","following_url":"https://api.github.com/users/asok/following{/other_user}","gists_url":"https://api.github.com/users/asok/gists{/gist_id}","starred_url":"https://api.github.com/users/asok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asok/subscriptions","organizations_url":"https://api.github.com/users/asok/orgs","repos_url":"https://api.github.com/users/asok/repos","events_url":"https://api.github.com/users/asok/events{/privacy}","received_events_url":"https://api.github.com/users/asok/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-28T20:31:58Z","updated_at":"2016-10-30T11:37:27Z","closed_at":"2016-10-30T11:37:27Z","body":"Hi this https://github.com/reagent-project/reagent-forms/pull/63 has introduced possibility to specify a \"label\" for a value for the typeahead input. I had a hard time to crack it. Better docs on this could help :)\r\n\r\nThe field is an `id` of a person, but I want to display and search by `full-name` of the person. \r\nWhat I think has to be done:\r\n\r\n* the data-source needs to return a vector `[full-name id]`\r\n* in-fn takes `id` and finds the correct `full-name` to put into input's value\r\n* out-fn takes  the vector and returns `id` that has to be stored\r\n* result-fn returns what will be displayed as the candidates (`full-name` in my case) \r\n\r\nRoughly I do:\r\n```cljs\r\n(defn- full-name [person]\r\n  (if person\r\n    (str (:first_name person) \" \" (:last_name person))\r\n    \"\"))\r\n\r\n(defn people-source [people]\r\n  (fn [text]\r\n    (->> people\r\n         (filter #(-> (full-name %)\r\n                      (.toLowerCase)\r\n                      (.indexOf text)\r\n                      (> -1)))\r\n         (mapv #(vector (full-name %) (:id %))))))\r\n\r\n    [:div\r\n     [:label.label \"Lead author\"]\r\n     [:label.input\r\n      [:div {:field :typeahead,\r\n             :data-source (people-source people)\r\n             :input-class \"form-control\"\r\n             :list-class \"typeahead-list\"\r\n             :item-class \"typeahead-item\"\r\n             :clear-on-focus? false\r\n             :highlight-class \"highlighted\"\r\n             :in-fn (fn [id]\r\n                      [(full-name (first (filter #(= id (:id %)) people))) id])\r\n             :out-fn (fn [[name id]] id)\r\n             :result-fn (fn [[name id]] name)\r\n             :id :lead_author.documas_person_id}]]]\r\n```\r\n\r\nThis works until I remove option `clear-on-focus?`. I seems that [here](https://github.com/reagent-project/reagent-forms/blob/master/src/reagent_forms/core.cljs#L252) the on-focus event gets fired when a candidate is selected also. [UPDATE: this only happens when clicking on a candidate, using keyboard it works alright]\r\n\r\nSo 2 questions:\r\n* is that the correct way to use typeahead to be able to have value -> label functionality?\r\n* what to do to clear the input on focussing but not when a selection is made?\r\n\r\n"}}
{"actor":{"display_login":"asok"}}