Skip to content

Commit 889b048

Browse files
committed
token input refactoring
1 parent 2161c2e commit 889b048

File tree

12 files changed

+399
-466
lines changed

12 files changed

+399
-466
lines changed

ios/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ SPEC CHECKSUMS:
14851485
FBLazyVector: 56e0e498dbb513b96c40bac6284729ba4e62672d
14861486
FBReactNativeSpec: 146c741a3f40361f6bc13a4ba284678cbedb5881
14871487
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
1488-
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
1488+
glog: 36c15c01d70ed395c6e4f3619f98a23ee415b07a
14891489
hermes-engine: 1d1835b2cc54c381909d94d1b3c8e0a2f1a94a0e
14901490
HMSegmentedControl: 34c1f54d822d8308e7b24f5d901ec674dfa31352
14911491
Keycard: ac6df4d91525c3c82635ac24d4ddd9a80aca5fc8

src/quo/components/wallet/token_input/component_spec.cljs

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
(h/describe "Wallet: Token Input"
77
(h/test "Token label renders"
88
(h/render-with-theme-provider [token-input/view
9-
{:token :snt
10-
:currency :eur
11-
:crypto? true
12-
:conversion 1}])
9+
{:token-symbol :snt
10+
:currency-symbol :snt
11+
:on-swap nil
12+
:error? false
13+
:value "1"
14+
:converted-value "10"}])
1315
(h/is-truthy (h/get-by-text "SNT")))
1416

1517
(h/test "Amount renders"
1618
(h/render-with-theme-provider [token-input/view
17-
{:token :snt
18-
:currency :eur
19-
:converted-value "€0.00"
20-
:conversion 1}])
19+
{:token-symbol :snt
20+
:currency-symbol :snt
21+
:on-swap nil
22+
:error? false
23+
:value "1"
24+
:converted-value "€0.00"}])
2125
(h/is-truthy (h/get-by-text "€0.00"))))

src/quo/components/wallet/token_input/schema.cljs

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
[:=>
55
[:catn
66
[:props
7-
[:map
8-
[:token {:optional true} [:maybe [:or :string :keyword]]]
9-
[:currency {:optional true} [:maybe [:or :string :keyword]]]
10-
[:error? {:optional true} [:maybe :boolean]]
11-
[:title {:optional true} [:maybe :string]]
12-
[:conversion {:optional true} [:maybe :double]]
13-
[:show-keyboard? {:optional true} [:maybe :boolean]]
14-
[:networks {:optional true}
15-
[:maybe [:sequential [:map [:source [:maybe :schema.common/image-source]]]]]]
16-
[:customization-color {:optional true} [:maybe :schema.common/customization-color]]
17-
[:value {:optional true} [:maybe :string]]]]]
7+
[:map {:closed true}
8+
[:token-symbol [:maybe [:or :string :keyword]]]
9+
[:currency-symbol [:maybe [:or :string :keyword]]]
10+
[:hint-component {:optional true} [:maybe :schema.common/hiccup]]
11+
[:on-token-press {:optional true} [:maybe fn?]]
12+
[:on-swap [:maybe fn?]]
13+
[:container-style {:optional true} [:maybe :map]]
14+
[:error? [:maybe :boolean]]
15+
[:value [:maybe :string]]
16+
[:converted-value [:maybe :string]]]]]
1817
:any])

src/quo/components/wallet/token_input/style.cljs

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525

2626
(defn input-container
2727
[window-width]
28-
{:width (- window-width 120)
29-
:margin-left 8
30-
:margin-right 8})
28+
{:width (- window-width 120)
29+
:margin-left 8
30+
:margin-right 8
31+
:flex-direction :row
32+
:align-items :flex-end})
3133

3234
(def text-input-dimensions
3335
(-> typography/heading-1
@@ -65,6 +67,6 @@
6567
:justify-content :space-between
6668
:align-items :center})
6769

68-
(defn fiat-amount
70+
(defn converted-amount
6971
[theme]
7072
{:color (colors/theme-colors colors/neutral-50 colors/neutral-40 theme)})
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
(ns quo.components.wallet.token-input.view
22
(:require
33
[clojure.string :as string]
4-
[oops.core :as oops]
54
[quo.components.buttons.button.view :as button]
65
[quo.components.dividers.divider-line.view :as divider-line]
76
[quo.components.markdown.text :as text]
8-
[quo.components.tags.network-tags.view :as network-tag]
97
[quo.components.utilities.token.view :as token]
108
[quo.components.wallet.token-input.schema :as component-schema]
119
[quo.components.wallet.token-input.style :as style]
1210
[quo.theme :as quo.theme]
1311
[react-native.core :as rn]
1412
[schema.core :as schema]))
1513

16-
(defn- data-info
17-
[{:keys [theme networks title converted-value error?]}]
18-
[rn/view {:style style/data-container}
19-
[network-tag/view
20-
{:networks networks
21-
:title title
22-
:status (when error? :error)}]
23-
[text/text
24-
{:size :paragraph-2
25-
:weight :medium
26-
:style (style/fiat-amount theme)}
27-
converted-value]])
28-
2914
(defn- token-name-text
3015
[theme text]
3116
[text/text
@@ -35,85 +20,63 @@
3520
(string/upper-case (or (clj->js text) ""))])
3621

3722
(defn input-section
38-
[{:keys [on-change-text value value-internal set-value-internal on-selection-change
39-
on-token-press]}]
40-
(let [input-ref (atom nil)
41-
set-ref #(reset! input-ref %)
42-
focus-input #(when-let [ref ^js @input-ref]
43-
(.focus ref))
44-
controlled-input? (some? value)
45-
handle-on-change-text (fn [v]
46-
(when-not controlled-input?
47-
(set-value-internal v))
48-
(when on-change-text
49-
(on-change-text v)))
50-
handle-selection-change (fn [^js e]
51-
(when on-selection-change
52-
(-> e
53-
(oops/oget "nativeEvent.selection")
54-
(js->clj :keywordize-keys true)
55-
(on-selection-change))))]
56-
(fn [{:keys [token customization-color show-keyboard? crypto? currency value error? selection
57-
handle-on-swap allow-selection?]
58-
:or {show-keyboard? true
59-
allow-selection? true}}]
60-
(let [theme (quo.theme/use-theme)
61-
window-width (:width (rn/get-window))]
62-
[rn/pressable
63-
{:style {:width "100%"
64-
:flex-direction :row}
65-
:on-press on-token-press}
66-
[token/view
67-
{:token token
68-
:size :size-32}]
69-
[rn/view {:style (style/input-container window-width)}
70-
[rn/pressable
71-
{:style style/text-input-container
72-
:on-press focus-input}
73-
[rn/text-input
74-
(cond-> {:style (style/text-input theme error?)
75-
:placeholder-text-color (style/placeholder-text theme)
76-
:auto-focus true
77-
:ref set-ref
78-
:placeholder "0"
79-
:keyboard-type :numeric
80-
:on-change-text handle-on-change-text
81-
:selection-color customization-color
82-
:show-soft-input-on-focus show-keyboard?
83-
:on-selection-change handle-selection-change
84-
:selection (clj->js selection)
85-
:editable allow-selection?}
86-
controlled-input? (assoc :value value)
87-
(not controlled-input?) (assoc :default-value value-internal))]
88-
[token-name-text theme (if crypto? token currency)]]]
89-
[button/button
90-
{:icon true
91-
:icon-only? true
92-
:size 32
93-
:on-press handle-on-swap
94-
:type :outline
95-
:accessibility-label :reorder}
96-
:i/reorder]]))))
23+
[{:keys [token-symbol on-token-press value error? on-swap currency-symbol]}]
24+
(let [theme (quo.theme/use-theme)
25+
window-width (:width (rn/get-window))]
26+
[rn/pressable
27+
{:style {:width "100%"
28+
:flex-direction :row}
29+
:on-press on-token-press}
30+
[token/view
31+
{:token token-symbol
32+
:size :size-32}]
33+
[rn/view {:style (style/input-container window-width)}
34+
[rn/text-input
35+
{:style (style/text-input theme error?)
36+
:placeholder-text-color (style/placeholder-text theme)
37+
:placeholder "0"
38+
:keyboard-type :numeric
39+
:editable false
40+
:value value}]
41+
[token-name-text theme currency-symbol]]
42+
[button/button
43+
{:icon true
44+
:icon-only? true
45+
:size 32
46+
:on-press #(when on-swap (on-swap))
47+
:type :outline
48+
:accessibility-label :reorder}
49+
:i/reorder]]))
9750

9851
(defn- view-internal
99-
[{:keys [container-style on-swap crypto?] :as props}]
100-
(let [theme (quo.theme/use-theme)
101-
width (:width (rn/get-window))
102-
[value-internal set-value-internal] (rn/use-state nil)
103-
handle-on-swap (rn/use-callback
104-
(fn []
105-
(when on-swap (on-swap (not crypto?))))
106-
[on-swap])]
52+
[{:keys [token-symbol
53+
value
54+
on-token-press
55+
error?
56+
container-style
57+
on-swap
58+
converted-value
59+
hint-component
60+
currency-symbol]}]
61+
(let [theme (quo.theme/use-theme)
62+
width (:width (rn/get-window))]
10763
[rn/view {:style (merge (style/main-container width) container-style)}
10864
[rn/view {:style style/amount-container}
10965
[input-section
110-
(assoc props
111-
:value-internal value-internal
112-
:theme theme
113-
:set-value-internal set-value-internal
114-
:handle-on-swap handle-on-swap
115-
:crypto? crypto?)]]
66+
{:theme theme
67+
:token-symbol token-symbol
68+
:on-token-press on-token-press
69+
:value value
70+
:error? error?
71+
:on-swap on-swap
72+
:currency-symbol currency-symbol}]]
11673
[divider-line/view {:container-style (style/divider theme)}]
117-
[data-info (assoc props :theme theme)]]))
74+
[rn/view {:style style/data-container}
75+
hint-component
76+
[text/text
77+
{:size :paragraph-2
78+
:weight :medium
79+
:style (style/converted-amount theme)}
80+
converted-value]]]))
11881

11982
(def view (schema/instrument #'view-internal component-schema/?schema))

src/status_im/common/controlled_input/utils.cljs

+45-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(ns status-im.common.controlled-input.utils
22
(:require
3-
[clojure.string :as string]))
3+
[clojure.string :as string]
4+
[status-im.contexts.wallet.common.utils :as wallet-utils]
5+
[utils.money :as money]))
46

57
(def init-state
68
{:value ""
@@ -14,7 +16,7 @@
1416

1517
(defn numeric-value
1618
[state]
17-
(or (parse-double (input-value state)) 0))
19+
(money/bignumber (input-value state)))
1820

1921
(defn input-error
2022
[state]
@@ -26,23 +28,27 @@
2628

2729
(defn upper-limit
2830
[state]
29-
(:upper-limit state))
31+
(money/bignumber (:upper-limit state)))
3032

3133
(defn lower-limit
3234
[state]
33-
(:lower-limit state))
35+
(money/bignumber (:lower-limit state)))
3436

3537
(defn upper-limit-exceeded?
3638
[state]
37-
(and
38-
(upper-limit state)
39-
(> (numeric-value state) (upper-limit state))))
39+
(let [num-value (numeric-value state)]
40+
(and
41+
(upper-limit state)
42+
(when (money/bignumber? num-value)
43+
(money/greater-than (numeric-value state) (upper-limit state))))))
4044

41-
(defn lower-limit-exceeded?
45+
(defn- lower-limit-exceeded?
4246
[state]
43-
(and
44-
(lower-limit state)
45-
(< (numeric-value state) (lower-limit state))))
47+
(let [num-value (numeric-value state)]
48+
(and
49+
(lower-limit state)
50+
(when (money/bignumber? num-value)
51+
(money/less-than (numeric-value state) (lower-limit state))))))
4652

4753
(defn- recheck-errorness
4854
[state]
@@ -75,13 +81,11 @@
7581

7682
(defn increase
7783
[state]
78-
(let [new-val (inc (numeric-value state))]
79-
(set-input-value state (str new-val))))
84+
(set-input-value state (str (money/add (numeric-value state) 1))))
8085

8186
(defn decrease
8287
[state]
83-
(let [new-val (dec (numeric-value state))]
84-
(set-input-value state (str new-val))))
88+
(set-input-value state (str (money/add (numeric-value state) -1))))
8589

8690
(def ^:private not-digits-or-dot-pattern
8791
#"[^0-9+\.]")
@@ -130,3 +134,29 @@
130134
(defn empty-value?
131135
[state]
132136
(string/blank? (:value state)))
137+
138+
(defn- fiat->crypto
139+
[value conversion-rate]
140+
(-> value
141+
(money/fiat->crypto conversion-rate)
142+
(wallet-utils/cut-crypto-decimals-to-fit-usd-cents conversion-rate)))
143+
144+
(defn- crypto->fiat
145+
[value conversion-rate]
146+
(-> value
147+
(money/crypto->fiat conversion-rate)
148+
(wallet-utils/cut-fiat-balance-to-two-decimals)))
149+
150+
(defn ->crypto
151+
[state conversion-rate]
152+
(-> state
153+
(set-input-value (fiat->crypto (input-value state) conversion-rate))
154+
(set-upper-limit (fiat->crypto (upper-limit state) conversion-rate))
155+
(set-lower-limit (fiat->crypto (lower-limit state) conversion-rate))))
156+
157+
(defn ->fiat
158+
[state conversion-rate]
159+
(-> state
160+
(set-input-value (crypto->fiat (input-value state) conversion-rate))
161+
(set-upper-limit (crypto->fiat (upper-limit state) conversion-rate))
162+
(set-lower-limit (crypto->fiat (lower-limit state) conversion-rate))))

0 commit comments

Comments
 (0)