Skip to content

Commit

Permalink
Fixed issue 6 <#6>
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jan 30, 2022
1 parent aee9615 commit 5ee8f88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions examples/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ impl Component<Msg, NoUserEvent> for SelectAlfa {
Event::Keyboard(KeyEvent {
code: Key::Enter, ..
}) => self.perform(Cmd::Submit),
Event::Keyboard(KeyEvent {
code: Key::Delete | Key::Backspace,
..
}) => self.perform(Cmd::Cancel),
Event::Keyboard(KeyEvent { code: Key::Tab, .. }) => return Some(Msg::SelectAlfaBlur),
Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => return Some(Msg::AppClose),
_ => CmdResult::None,
Expand Down Expand Up @@ -261,6 +265,10 @@ impl Component<Msg, NoUserEvent> for SelectBeta {
Event::Keyboard(KeyEvent {
code: Key::Enter, ..
}) => self.perform(Cmd::Submit),
Event::Keyboard(KeyEvent {
code: Key::Delete | Key::Backspace,
..
}) => self.perform(Cmd::Cancel),
Event::Keyboard(KeyEvent { code: Key::Tab, .. }) => return Some(Msg::SelectBetaBlur),
Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => return Some(Msg::AppClose),
_ => CmdResult::None,
Expand Down
33 changes: 32 additions & 1 deletion src/components/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ use tuirealm::{Frame, MockComponent, State, StateValue};
/// Component states
#[derive(Default)]
pub struct SelectStates {
pub choices: Vec<String>, // Available choices
/// Available choices
pub choices: Vec<String>,
/// Currently selected choice
pub selected: usize,
/// Choice selected before opening the tab
pub previously_selected: usize,
pub tab_open: bool,
}

Expand Down Expand Up @@ -110,9 +114,16 @@ impl SelectStates {
///
/// Open tab
pub fn open_tab(&mut self) {
self.previously_selected = self.selected;
self.tab_open = true;
}

/// Cancel tab open
pub fn cancel_tab(&mut self) {
self.close_tab();
self.selected = self.previously_selected;
}

/// ### is_tab_open
///
/// Returns whether the tab is open
Expand Down Expand Up @@ -379,6 +390,12 @@ impl MockComponent for Select {
self.states
.select(value.unwrap_payload().unwrap_one().unwrap_usize());
}
Attribute::Focus if self.states.is_tab_open() => {
if let AttrValue::Flag(false) = value {
self.states.cancel_tab();
}
self.props.set(attr, value);
}
attr => {
self.props.set(attr, value);
}
Expand Down Expand Up @@ -413,6 +430,10 @@ impl MockComponent for Select {
true => CmdResult::Changed(State::One(StateValue::Usize(self.states.selected))),
}
}
Cmd::Cancel => {
self.states.cancel_tab();
CmdResult::Submit(self.state())
}
Cmd::Submit => {
// Open or close tab
if self.states.is_tab_open() {
Expand Down Expand Up @@ -503,6 +524,16 @@ mod test {
assert_eq!(states.selected, 1);
states.prev_choice(true);
assert_eq!(states.selected, 0);
// Cancel tab
states.close_tab();
states.select(2);
states.open_tab();
states.prev_choice(true);
states.prev_choice(true);
assert_eq!(states.selected, 0);
states.cancel_tab();
assert_eq!(states.selected, 2);
assert_eq!(states.is_tab_open(), false);
}

#[test]
Expand Down

0 comments on commit 5ee8f88

Please sign in to comment.