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
[options text]
(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]
[text-input-class
(merge
{:underline-color-android :transparent
:max-font-size-multiplier max-font-size-multiplier
:placeholder-text-color colors/text-gray
:placeholder (i18n/label :t/type-a-message)
:ref (fn [r]
;; Store input and its defaultValue
;; one we receive a non-nil ref
(when (and r (nil? @input-ref))
(swap! text-input-refs assoc r (:default-value options)))
(reset! input-ref r)
(when (:ref options)
((:ref options) r)))
:value text}
(-> options
(dissoc :ref)
(update :style typography/get-style)
(update :style dissoc :line-height)))])})))
(let [render-fn (fn [options text]
[text-input-class
(merge
{:underline-color-android :transparent
:max-font-size-multiplier max-font-size-multiplier
:placeholder-text-color colors/text-gray
:placeholder (i18n/label :t/type-a-message)
: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
;; one we receive a non-nil ref
(when (and r (nil? @input-ref))
(swap! text-input-refs assoc r (:default-value options)))
(reset! input-ref r)
(when (:ref options)
((:ref options) r)))) text))})))))
(defn i18n-text
[{:keys [style key]}]

View File

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