Skip to content

Commit

Permalink
Reset list of options when filter input is reset (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelmello committed Mar 25, 2024
1 parent 62ecc2e commit 2b1c882
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## [Unreleased] <!-- ReleaseDate -->

- Fix unexpected behaviour of `keep_filter` option in MultiSelect prompts, where the resulting behaviour was the opposite of what was expected.
- Fix unexpected behaviors of `keep_filter` option in MultiSelect prompts:
- Filter input is now correcly getting reset **only when** `keep_filter == false`.
- When the filter input is reset, the list of options is reset as well. Thanks @Swivelgames for reporting [#238](https://github.com/mikaelmello/inquire/issues/238).

## [0.7.3] - 2024-03-21

Expand Down
12 changes: 11 additions & 1 deletion inquire/src/prompts/multiselect/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,21 @@ where
return ActionResult::Clean;
}

let input_ref = match &mut self.input {
Some(input) => input,
None => return ActionResult::Clean,
};

if input_ref.is_empty() {
return ActionResult::Clean;
}

match action {
MultiSelectPromptAction::ToggleCurrentOption
| MultiSelectPromptAction::SelectAll
| MultiSelectPromptAction::ClearSelections => {
self.input.as_mut().map(Input::clear);
input_ref.clear();
self.run_scorer();
ActionResult::NeedsRedraw
}
_ => ActionResult::Clean,
Expand Down
21 changes: 21 additions & 0 deletions inquire/src/prompts/multiselect/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@ fn keep_filter_should_be_true_by_default() {
let expected_answer = vec![ListOption::new(0, 1)];
assert_eq!(expected_answer, ans);
}

#[test]
// Anti-regression test: https://github.com/mikaelmello/inquire/issues/238
fn keep_filter_false_should_reset_option_list() {
let mut backend = fake_backend(vec![
Key::Char('3', KeyModifiers::NONE), // filter to option 3
Key::Char(' ', KeyModifiers::NONE), // toggle option 3, filter input is reset
Key::Char(' ', KeyModifiers::NONE), // toggle option 1 after option list is reset
Key::Enter,
]);

let options = vec![1, 2, 3, 4, 5];

let ans = MultiSelect::new("Question", options)
.with_keep_filter(false)
.prompt_with_backend(&mut backend)
.unwrap();

let expected_answer = vec![ListOption::new(0, 1), ListOption::new(2, 3)];
assert_eq!(expected_answer, ans);
}

0 comments on commit 2b1c882

Please sign in to comment.