Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
example showing intent of bones-editable library
(defn detect-controls [{:keys [enter escape]}]
(fn [keypress]
(case (.-which keypress)
13 (enter)
;; chrome won't fire 27, so use on-blur instead
27 (escape)
(defn field [form-type identifier attr html-attrs]
(let [path [:editable form-type identifier :inputs attr]
value (subscribe path)
input-type (or (:input-type html-attrs) :input)
value-attr (or (:value-attr html-attrs) :value)
opts (dissoc html-attrs :value-attr :input-type)]
(fn []
[input-type (merge {:on-change #(dispatch-sync (conj path (-> % .-target .-value)))
value-attr @value}
(defn checkbox [form-type identifier attr & {:as html-attrs}]
(field form-type identifier attr (merge {:type "checkbox"
:value-attr :checked}
(defn input [form-type identifier attr & {:as html-attrs}]
(field form-type identifier attr html-attrs))
(defn todo-item
(let [id (get-in todo [:inputs :id])
inputs (subscribe [:editable :todos id :inputs])
;; defaults (subscribe [:editable :todos id :defaults]) ;; like new
done (subscribe [:editable :todos id :inputs :done])
label (subscribe [:editable :todos id :inputs :todo])
errors (subscribe [:editable :todos id :errors])
state (subscribe [:editable :todos id :state])
reset #(dispatch (e/editable-reset :todos id (:reset @state)))
incomplete #(dispatch [:editable :todos id :inputs :done false])
toggle #(dispatch [:editable :todos id :inputs :done (not %)])
save #(dispatch [:request/command :todos id {:command :todos/update}])
delete #(dispatch [:request/command :todos id {:command :todos/delete}])
edit #(dispatch [:editable
[:editable :todos id :state :reset @inputs]
[:editable :todos id :state :editing true]])]
(fn []
[:li {:class (str (when @done "completed ")
(when (:editing @state) "editing"))}
[checkbox :todos id :done
:class "toggle"
:on-change #(do (toggle @done) (save))]
{:on-double-click edit}
{:on-click delete}]]
(when (:editing @state)
[input :todos id :todo
:id (str id "-id")
:class "edit"
:on-blur reset
:on-key-down (detect-controls {:enter #(do (incomplete) (save))
:escape reset})])])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.