Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Created September 20, 2019 07:32
Show Gist options
  • Save stathissideris/040f61f9b819a47cbab032fc995a9eb0 to your computer and use it in GitHub Desktop.
Save stathissideris/040f61f9b819a47cbab032fc995a9eb0 to your computer and use it in GitHub Desktop.
(defn dispatch-later! [event debounce-delay timeout-atom]
(when-let [t @timeout-atom] (js/clearTimeout t))
(reset! timeout-atom (js/setTimeout (fn [] (rf/dispatch event)) debounce-delay)))
(defn text-field [_]
(let [content-atom (r/atom nil)]
(fn [{:keys [id display-value required help label area error-msg enabled debounce]
:or {debounce 300}
:as f}]
(let [timeout (atom nil)
attr (merge
(when (= false enabled)
{:disabled "true"})
(when required
{:style {:background warning}})
{:id (util/qualified-string id)
:value (or @content-atom display-value)
:on-blur (fn [_] (reset! content-atom nil))
:on-change (fn [e]
(let [content (.-value (.-target e))]
(reset! content-atom content)
(dispatch-later! [::field-changed {:field f :value content}]
debounce timeout)))})]
[sui/form-field
(when label [:label label (when help (help-icon help))])
(if area
[:text-area (merge {:rows 5} attr)]
[:input attr])
(when error-msg
[:div.ui.pointing.red.basic.label error-msg])]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment