View: return String instead of id

This commit is contained in:
ChristianVisintin
2021-03-10 11:26:40 +01:00
parent 5980bc1fcb
commit 9dbfbd0dc3
2 changed files with 8 additions and 8 deletions

View File

@@ -41,7 +41,7 @@ use tui::widgets::Widget;
/// ///
/// Msg is an enum returned after an event is raised for a certain component /// Msg is an enum returned after an event is raised for a certain component
/// Yep, I took inspiration from Elm. /// Yep, I took inspiration from Elm.
#[derive(std::fmt::Debug, PartialEq)] #[derive(std::fmt::Debug, PartialEq, Eq)]
pub enum Msg { pub enum Msg {
OnSubmit(Payload), OnSubmit(Payload),
OnChange(Payload), OnChange(Payload),
@@ -52,7 +52,7 @@ pub enum Msg {
/// ## Payload /// ## Payload
/// ///
/// Payload describes a component value /// Payload describes a component value
#[derive(std::fmt::Debug, PartialEq)] #[derive(std::fmt::Debug, PartialEq, Eq)]
pub enum Payload { pub enum Payload {
Text(String), Text(String),
Signed(isize), Signed(isize),

View File

@@ -85,10 +85,10 @@ impl View {
/// ///
/// Update component properties /// Update component properties
/// Returns `None` if component doesn't exist /// Returns `None` if component doesn't exist
pub fn update<'a>(&mut self, id: &'a str, props: Props) -> Option<(&'a str, Msg)> { pub fn update(&mut self, id: &str, props: Props) -> Option<(String, Msg)> {
match self.components.get_mut(id) { match self.components.get_mut(id) {
None => None, None => None,
Some(cmp) => Some((id, cmp.update(props))), Some(cmp) => Some((id.to_string(), cmp.update(props))),
} }
} }
@@ -110,12 +110,12 @@ impl View {
/// ///
/// Handle event for the focused component (if any) /// Handle event for the focused component (if any)
/// Returns `None` if no component is focused /// Returns `None` if no component is focused
pub fn on(&mut self, ev: InputEvent) -> Option<(&str, Msg)> { pub fn on(&mut self, ev: InputEvent) -> Option<(String, Msg)> {
match self.focus.as_ref() { match self.focus.as_ref() {
None => None, None => None,
Some(id) => match self.components.get_mut(id) { Some(id) => match self.components.get_mut(id) {
None => None, None => None,
Some(cmp) => Some((id, cmp.on(ev))), Some(cmp) => Some((id.to_string(), cmp.on(ev))),
}, },
} }
} }
@@ -341,7 +341,7 @@ mod tests {
assert_eq!( assert_eq!(
view.on(InputEvent::Key(KeyEvent::from(KeyCode::Char('1')))) view.on(InputEvent::Key(KeyEvent::from(KeyCode::Char('1'))))
.unwrap(), .unwrap(),
(input, Msg::None) (input.to_string(), Msg::None)
); );
// Verify new value // Verify new value
assert_eq!( assert_eq!(
@@ -352,7 +352,7 @@ mod tests {
assert_eq!( assert_eq!(
view.on(InputEvent::Key(KeyEvent::from(KeyCode::Enter))) view.on(InputEvent::Key(KeyEvent::from(KeyCode::Enter)))
.unwrap(), .unwrap(),
(input, Msg::OnSubmit(Payload::Text(String::from("text1")))) (input.to_string(), Msg::OnSubmit(Payload::Text(String::from("text1"))))
); );
} }