letsubs macro (#1392)

* letsubs macro
This commit is contained in:
Roman Volosovskyi 2017-06-30 16:49:28 +03:00 committed by GitHub
parent 9665ea466d
commit 173d4afbf3
2 changed files with 37 additions and 30 deletions

View File

@ -1,5 +1,5 @@
(ns status-im.chat.views.input.input
(:require-macros [status-im.utils.views :refer [defview]])
(:require-macros [status-im.utils.views :refer [defview letsubs]])
(:require [clojure.string :as str]
[reagent.core :as r]
[re-frame.core :refer [subscribe dispatch]]
@ -213,7 +213,7 @@
[icon :close_gray style/input-clear-icon]]]))]))})))
(defview input-container [{:keys [anim-margin]}]
[command-completion [:command-completion]
(letsubs [command-completion [:command-completion]
selected-command [:selected-chat-command]
input-text [:chat :input-text]
seq-arg-input-text [:chat :seq-argument-input-text]
@ -236,7 +236,7 @@
100))
(dispatch [:send-current-message]))}
[view style/send-message-container
[icon :arrow_top style/send-message-icon]]]))]))
[icon :arrow_top style/send-message-icon]]]))])))
(defn container []
(let [margin (subscribe [:chat-input-margin])

View File

@ -34,12 +34,19 @@
[form `(deref ~sym)]))
pairs))]))
(defmacro letsubs [args body])
(defmacro defview
[n params & rest]
(let [[subs component-map body] (case (count rest)
1 [nil {} (first rest)]
2 [(first rest) {} (second rest)]
3 rest)
[n params & rest-body]
(let [first-symbol (ffirst rest-body)
rest-body' (if (and (symbol? first-symbol)
(= (name first-symbol) "letsubs"))
(rest (first rest-body))
rest-body)
[subs component-map body] (case (count rest-body')
1 [nil {} (first rest-body')]
2 [(first rest-body') {} (second rest-body')]
3 rest-body')
[subs-bindings vars-bindings] (prepare-subs subs)]
`(defn ~n ~params
(let [~@subs-bindings]