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