Skip to content

Commit ed87fb6

Browse files
pythcoinerpythcoiner
pythcoiner
authored andcommitted
new_amount_btc
1 parent b22f22f commit ed87fb6

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

gui/src/app/view/spend/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ pub fn create_spend_tx<'a>(
319319

320320
pub fn recipient_view<'a>(
321321
index: usize,
322-
address: &form::Value<String>,
323-
amount: &form::Value<String>,
324-
label: &form::Value<String>,
322+
address: &'a form::Value<String>,
323+
amount: &'a form::Value<String>,
324+
label: &'a form::Value<String>,
325325
is_max_selected: bool,
326326
) -> Element<'a, CreateSpendMessage> {
327327
Container::new(
@@ -395,7 +395,7 @@ pub fn recipient_view<'a>(
395395
None
396396
})
397397
.push_maybe(if !is_max_selected {
398-
Some(form::Form::new_trimmed("0.001 (in BTC)", amount, move |msg| {
398+
Some(form::Form::new_amount_btc("0.001 (in BTC)", amount, move |msg| {
399399
CreateSpendMessage::RecipientEdited(index, "amount", msg)
400400
})
401401
.warning(

gui/ui/src/component/form.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bitcoin::Denomination;
12
use iced::{widget::text_input, Length};
23

34
use crate::{color, component::text, theme, util::Collection, widget::*};
@@ -62,6 +63,31 @@ where
6263
}
6364
}
6465

66+
/// Creates a new [`Form`] that restrict input values to valid btc amount before applying the
67+
/// `on_change` function.
68+
/// It expects:
69+
/// - a placeholder
70+
/// - the current value
71+
/// - a function that produces a message when the [`Form`] changes
72+
pub fn new_amount_btc<F>(placeholder: &str, value: &'a Value<String>, on_change: F) -> Self
73+
where
74+
F: 'static + Fn(String) -> Message,
75+
{
76+
Self {
77+
input: text_input::TextInput::new(placeholder, &value.value).on_input(move |s| {
78+
if bitcoin::Amount::from_str_in(&s, Denomination::Bitcoin).is_ok()
79+
|| s == *"".to_string()
80+
{
81+
on_change(s)
82+
} else {
83+
on_change(value.value.clone())
84+
}
85+
}),
86+
warning: None,
87+
valid: value.valid,
88+
}
89+
}
90+
6591
/// Sets the [`Form`] with a warning message
6692
pub fn warning(mut self, warning: &'a str) -> Self {
6793
self.warning = Some(warning);

0 commit comments

Comments
 (0)