diff --git a/gui/src/app/view/spend/mod.rs b/gui/src/app/view/spend/mod.rs index 2bcabbb8..a5c9099f 100644 --- a/gui/src/app/view/spend/mod.rs +++ b/gui/src/app/view/spend/mod.rs @@ -319,9 +319,9 @@ pub fn create_spend_tx<'a>( pub fn recipient_view<'a>( index: usize, - address: &form::Value, - amount: &form::Value, - label: &form::Value, + address: &'a form::Value, + amount: &'a form::Value, + label: &'a form::Value, is_max_selected: bool, ) -> Element<'a, CreateSpendMessage> { Container::new( @@ -395,7 +395,7 @@ pub fn recipient_view<'a>( None }) .push_maybe(if !is_max_selected { - Some(form::Form::new_trimmed("0.001 (in BTC)", amount, move |msg| { + Some(form::Form::new_amount_btc("0.001 (in BTC)", amount, move |msg| { CreateSpendMessage::RecipientEdited(index, "amount", msg) }) .warning( diff --git a/gui/ui/src/component/form.rs b/gui/ui/src/component/form.rs index ea2fb214..1ff274c5 100644 --- a/gui/ui/src/component/form.rs +++ b/gui/ui/src/component/form.rs @@ -1,3 +1,4 @@ +use bitcoin::Denomination; use iced::{widget::text_input, Length}; use crate::{color, component::text, theme, util::Collection, widget::*}; @@ -62,6 +63,29 @@ where } } + /// Creates a new [`Form`] that restrict input values to valid btc amount before applying the + /// `on_change` function. + /// It expects: + /// - a placeholder + /// - the current value + /// - a function that produces a message when the [`Form`] changes + pub fn new_amount_btc(placeholder: &str, value: &'a Value, on_change: F) -> Self + where + F: 'static + Fn(String) -> Message, + { + Self { + input: text_input::TextInput::new(placeholder, &value.value).on_input(move |s| { + if bitcoin::Amount::from_str_in(&s, Denomination::Bitcoin).is_ok() || s.is_empty() { + on_change(s) + } else { + on_change(value.value.clone()) + } + }), + warning: None, + valid: value.valid, + } + } + /// Sets the [`Form`] with a warning message pub fn warning(mut self, warning: &'a str) -> Self { self.warning = Some(warning);