Skip to content

Commit 2b1c882

Browse files
authored
Reset list of options when filter input is reset (#240)
1 parent 62ecc2e commit 2b1c882

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

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

7-
- Fix unexpected behaviour of `keep_filter` option in MultiSelect prompts, where the resulting behaviour was the opposite of what was expected.
7+
- Fix unexpected behaviors of `keep_filter` option in MultiSelect prompts:
8+
- Filter input is now correcly getting reset **only when** `keep_filter == false`.
9+
- 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).
810

911
## [0.7.3] - 2024-03-21
1012

inquire/src/prompts/multiselect/prompt.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,21 @@ where
148148
return ActionResult::Clean;
149149
}
150150

151+
let input_ref = match &mut self.input {
152+
Some(input) => input,
153+
None => return ActionResult::Clean,
154+
};
155+
156+
if input_ref.is_empty() {
157+
return ActionResult::Clean;
158+
}
159+
151160
match action {
152161
MultiSelectPromptAction::ToggleCurrentOption
153162
| MultiSelectPromptAction::SelectAll
154163
| MultiSelectPromptAction::ClearSelections => {
155-
self.input.as_mut().map(Input::clear);
164+
input_ref.clear();
165+
self.run_scorer();
156166
ActionResult::NeedsRedraw
157167
}
158168
_ => ActionResult::Clean,

inquire/src/prompts/multiselect/test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,24 @@ fn keep_filter_should_be_true_by_default() {
234234
let expected_answer = vec![ListOption::new(0, 1)];
235235
assert_eq!(expected_answer, ans);
236236
}
237+
238+
#[test]
239+
// Anti-regression test: https://github.com/mikaelmello/inquire/issues/238
240+
fn keep_filter_false_should_reset_option_list() {
241+
let mut backend = fake_backend(vec![
242+
Key::Char('3', KeyModifiers::NONE), // filter to option 3
243+
Key::Char(' ', KeyModifiers::NONE), // toggle option 3, filter input is reset
244+
Key::Char(' ', KeyModifiers::NONE), // toggle option 1 after option list is reset
245+
Key::Enter,
246+
]);
247+
248+
let options = vec![1, 2, 3, 4, 5];
249+
250+
let ans = MultiSelect::new("Question", options)
251+
.with_keep_filter(false)
252+
.prompt_with_backend(&mut backend)
253+
.unwrap();
254+
255+
let expected_answer = vec![ListOption::new(0, 1), ListOption::new(2, 3)];
256+
assert_eq!(expected_answer, ans);
257+
}

0 commit comments

Comments
 (0)