Add preserve-input flag to text-input

Signed-off-by: Vitaliy Vlasov <siphiuel@gmail.com>
This commit is contained in:
Vitaliy Vlasov 2019-09-30 13:51:05 +03:00
parent d2ff665d6e
commit 227127f153
No known key found for this signature in database
GPG Key ID: A7D57C347F2B2964
2 changed files with 33 additions and 28 deletions

View File

@ -137,31 +137,35 @@
(defn text-input (defn text-input
[options text] [options text]
(let [input-ref (atom nil)] (let [render-fn (fn [options text]
(reagent/create-class
{:component-will-unmount #(when @input-ref
(swap! text-input-refs dissoc @input-ref))
:reagent-render
(fn [options text]
[text-input-class [text-input-class
(merge (merge
{:underline-color-android :transparent {:underline-color-android :transparent
:max-font-size-multiplier max-font-size-multiplier :max-font-size-multiplier max-font-size-multiplier
:placeholder-text-color colors/text-gray :placeholder-text-color colors/text-gray
:placeholder (i18n/label :t/type-a-message) :placeholder (i18n/label :t/type-a-message)
:ref (fn [r] :value text}
(-> options
(dissoc :preserve-input?)
(update :style typography/get-style)
(update :style dissoc :line-height)))])]
(if (:preserve-input? options)
render-fn
(let [input-ref (atom nil)]
(reagent/create-class
{:component-will-unmount #(when @input-ref
(swap! text-input-refs dissoc @input-ref))
:reagent-render
(fn [options text]
(render-fn (assoc options :ref
(fn [r]
;; Store input and its defaultValue ;; Store input and its defaultValue
;; one we receive a non-nil ref ;; one we receive a non-nil ref
(when (and r (nil? @input-ref)) (when (and r (nil? @input-ref))
(swap! text-input-refs assoc r (:default-value options))) (swap! text-input-refs assoc r (:default-value options)))
(reset! input-ref r) (reset! input-ref r)
(when (:ref options) (when (:ref options)
((:ref options) r))) ((:ref options) r)))) text))})))))
:value text}
(-> options
(dissoc :ref)
(update :style typography/get-style)
(update :style dissoc :line-height)))])})))
(defn i18n-text (defn i18n-text
[{:keys [style key]}] [{:keys [style key]}]

View File

@ -37,9 +37,10 @@
(re-frame/dispatch [:contact.ui/contact-code-submitted])) (re-frame/dispatch [:contact.ui/contact-code-submitted]))
:placeholder (i18n/label :t/enter-contact-code) :placeholder (i18n/label :t/enter-contact-code)
:style (add-new.styles/input @tw) :style (add-new.styles/input @tw)
;; Set default-value as otherwise it will ;; This input is fine to preserve inputs
;; be erased in global `onWillBlur` handler ;; so its contents will not be erased
:default-value new-identity ;; in onWillBlur navigation event handler
:preserve-input? true
:accessibility-label :enter-contact-code-input :accessibility-label :enter-contact-code-input
:return-key-type :go}]] :return-key-type :go}]]
(when-not platform/desktop? (when-not platform/desktop?