-
-
Save jeaye/291fc4f7f9cd4df7e3c27cccb8ab37e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn instant-text-input | |
[{:keys [value format-value on-change-text]}] | |
(let [value' (r/atom value) | |
this' (atom nil) | |
on-change-text' (fn [new-value] | |
(reset! value' (format-value new-value)) ; Do formatting | |
(some-> @this' r/force-update) | |
(on-change-text new-value))] | |
(r/create-class | |
{:display-name "instant-text-input" | |
:component-did-mount | |
(fn [this] (reset! this' this)) | |
:component-will-unmount | |
(fn [] (reset! this' nil)) | |
:reagent-render | |
(fn [props] | |
[text-input (assoc props | |
:on-change-text on-change-text' | |
:value @value')])}))) | |
;; Usage | |
[instant-text-input {:style {:flex 0.45 | |
:text-align :right} | |
:keyboard-type :numeric | |
:value (fmt/money amount) | |
:format-value (comp fmt/money fmt/parse-money) | |
:on-change-text #(dispatch-sync [::events/set-amount | |
(fmt/parse-money %)])}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment