|
| 1 | +(ns quo2.components.inputs.recovery-phrase.view |
| 2 | + (:require [clojure.string :as string] |
| 3 | + [quo2.components.inputs.recovery-phrase.style :as style] |
| 4 | + [react-native.core :as rn] |
| 5 | + [reagent.core :as reagent])) |
| 6 | + |
| 7 | +(def ^:private custom-props |
| 8 | + [:customization-color :override-theme :blur? :cursor-color :multiline :on-focus :on-blur |
| 9 | + :placeholder-text-color :mark-errors? :error-pred :word-limit]) |
| 10 | + |
| 11 | +(defn- error-word |
| 12 | + [text] |
| 13 | + [rn/text {:style (style/error-word)} |
| 14 | + text]) |
| 15 | + |
| 16 | +(defn- mark-error-words |
| 17 | + [pred text word-limit] |
| 18 | + (let [word-limit (or word-limit ##Inf)] |
| 19 | + (into [:<>] |
| 20 | + (comp (map-indexed (fn [idx word] |
| 21 | + (if (or (pred word) (>= idx word-limit)) |
| 22 | + [error-word word] |
| 23 | + word))) |
| 24 | + (interpose " ")) |
| 25 | + (string/split text #" ")))) |
| 26 | + |
| 27 | +(defn recovery-phrase-input |
| 28 | + [_ _] |
| 29 | + (let [state (reagent/atom :default) |
| 30 | + set-focused #(reset! state :focused) |
| 31 | + set-default #(reset! state :default)] |
| 32 | + (fn [{:keys [customization-color override-theme blur? on-focus on-blur mark-errors? |
| 33 | + error-pred word-limit] |
| 34 | + :or {customization-color :blue} |
| 35 | + :as props} |
| 36 | + text] |
| 37 | + (let [extra-props (apply dissoc props custom-props)] |
| 38 | + [rn/view {:style style/container} |
| 39 | + [rn/text-input |
| 40 | + (merge {:accessibility-label :recovery-phrase-input |
| 41 | + :style (style/input) |
| 42 | + :placeholder-text-color (style/placeholder-color @state override-theme blur?) |
| 43 | + :cursor-color (style/cursor-color customization-color override-theme) |
| 44 | + :multiline true |
| 45 | + :on-focus (fn [] |
| 46 | + (set-focused) |
| 47 | + (when on-focus (on-focus))) |
| 48 | + :on-blur (fn [] |
| 49 | + (set-default) |
| 50 | + (when on-blur (on-blur)))} |
| 51 | + extra-props) |
| 52 | + (if mark-errors? |
| 53 | + (mark-error-words error-pred text word-limit) |
| 54 | + text)]])))) |
0 commit comments