Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selecting all options in a multiselect #147

Merged
merged 14 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/MultiSearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ public function __construct(
public mixed $validate = null,
public string $hint = '',
) {
$this->trackTypedValue(submit: false, ignore: fn ($key) => Key::oneOf([Key::SPACE, Key::HOME, Key::END, Key::CTRL_A, Key::CTRL_E], $key) && $this->highlighted !== null);
$this->trackTypedValue(submit: false, ignore: fn ($key) => Key::oneOf([Key::SPACE, Key::HOME, Key::END, Key::CTRL_A], $key) && $this->highlighted !== null);

$this->initializeScrolling(null);

$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::UP_ARROW, Key::SHIFT_TAB => $this->highlightPrevious(count($this->matches), true),
Key::DOWN, Key::DOWN_ARROW, Key::TAB => $this->highlightNext(count($this->matches), true),
Key::oneOf([Key::HOME, Key::CTRL_A], $key) => $this->highlighted !== null ? $this->highlight(0) : null,
Key::oneOf([Key::END, Key::CTRL_E], $key) => $this->highlighted !== null ? $this->highlight(count($this->matches()) - 1) : null,
Key::CTRL_A => $this->highlighted !== null ? $this->toggleAll() : null,
Key::CTRL_E => null,
jessarcher marked this conversation as resolved.
Show resolved Hide resolved
Key::oneOf([Key::HOME], $key) => $this->highlighted !== null ? $this->highlight(0) : null,
Key::oneOf([Key::END], $key) => $this->highlighted !== null ? $this->highlight(count($this->matches()) - 1) : null,
jessarcher marked this conversation as resolved.
Show resolved Hide resolved
Key::SPACE => $this->highlighted !== null ? $this->toggleHighlighted() : null,
Key::ENTER => $this->submit(),
Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null,
Expand Down Expand Up @@ -132,6 +134,20 @@ public function visible(): array
return array_slice($this->matches(), $this->firstVisible, $this->scroll, preserve_keys: true);
}

/**
* Toggle all options.
*/
protected function toggleAll(): void
{
if (count($this->values) === count($this->matches)) {
$this->values = [];
} else {
$this->values = $this->isList()
? array_combine(array_values($this->matches), array_values($this->matches))
: array_combine(array_keys($this->matches), array_values($this->matches));
}
}
jessarcher marked this conversation as resolved.
Show resolved Hide resolved

/**
* Toggle the highlighted entry.
*/
Expand Down
17 changes: 16 additions & 1 deletion src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function __construct(
$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::UP_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::SHIFT_TAB, Key::CTRL_P, Key::CTRL_B, 'k', 'h' => $this->highlightPrevious(count($this->options)),
Key::DOWN, Key::DOWN_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::TAB, Key::CTRL_N, Key::CTRL_F, 'j', 'l' => $this->highlightNext(count($this->options)),
Key::oneOf([Key::HOME, Key::CTRL_A], $key) => $this->highlight(0),
Key::CTRL_A => $this->toggleAll(),
Key::oneOf([Key::HOME], $key) => $this->highlight(0),
Key::oneOf([Key::END, Key::CTRL_E], $key) => $this->highlight(count($this->options) - 1),
Key::SPACE => $this->toggleHighlighted(),
Key::ENTER => $this->submit(),
Expand Down Expand Up @@ -115,6 +116,20 @@ public function isSelected(string $value): bool
return in_array($value, $this->values);
}

/**
* Toggle all options.
*/
protected function toggleAll(): void
{
if (count($this->values) === count($this->options)) {
$this->values = [];
} else {
$this->values = array_is_list($this->options)
? array_values($this->options)
: array_keys($this->options);
}
}

/**
* Toggle the highlighted entry.
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/MultiSearchPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,31 @@

Prompt::validateUsing(fn () => null);
});

it('supports selecting all options', function () {
Prompt::fake([Key::DOWN, Key::CTRL_A, Key::ENTER]);

$result = multisearch(
label: 'What are your favorite colors?',
options: fn () => [
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue',
],
);

expect($result)->toBe(['red', 'green', 'blue']);

Prompt::fake([Key::DOWN, Key::CTRL_A, Key::CTRL_A, Key::ENTER]);

$result = multisearch(
label: 'What are your favorite colors?',
options: fn () => [
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue',
],
);

expect($result)->toBe([]);
});
28 changes: 28 additions & 0 deletions tests/Feature/MultiSelectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@
expect($result)->toBe(['blue', 'red']);
});

it('supports selecting all options', function () {
Prompt::fake([Key::CTRL_A, Key::ENTER]);

$result = multiselect(
label: 'What are your favorite colors?',
options: [
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue',
]
);

expect($result)->toBe(['red', 'green', 'blue']);

Prompt::fake([Key::CTRL_A, Key::CTRL_A, Key::ENTER]);

$result = multiselect(
label: 'What are your favorite colors?',
options: [
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue',
]
);

expect($result)->toBe([]);
});

it('returns an empty array when non-interactive', function () {
Prompt::interactive(false);

Expand Down
Loading