Skip to content

fix(select): default value not being centered when not visible #59

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

Merged
merged 7 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
/vendor
composer.lock
40 changes: 30 additions & 10 deletions playground/select.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,41 @@
require __DIR__.'/../vendor/autoload.php';

$role = select(
label: 'What role should the user have?',
label: 'Where are you from?',
options: [
'read-only' => 'Read only',
'member' => 'Member',
'contributor' => 'Contributor',
'supervisor' => 'Supervisor',
'manager' => 'Manager',
'admin' => 'Administrator',
'owner' => 'Owner',
'argentina' => 'Argentina',
'australia' => 'Australia',
'belgium' => 'Belgium',
'brazil' => 'Brazil',
'canada' => 'Canada',
'chile' => 'Chile',
'china' => 'China',
'colombia' => 'Colombia',
'egypt' => 'Egypt',
'france' => 'France',
'germany' => 'Germany',
'india' => 'India',
'italy' => 'Italy',
'japan' => 'Japan',
'kenya' => 'Kenya',
'mexico' => 'Mexico',
'morocco' => 'Morocco',
'nigeria' => 'Nigeria',
'new-zealand' => 'New Zealand',
'portugal' => 'Portugal',
'south-africa' => 'South Africa',
'south-korea' => 'South Korea',
'spain' => 'Spain',
'switzerland' => 'Switzerland',
'united-kingdom' => 'United Kingdom',
'united-states' => 'United States',
],
default: 'france',
validate: fn ($value) => match ($value) {
'owner' => 'The owner role is already assigned.',
'spain' => 'Spain is not available yet.',
default => null
},
hint: 'The role will determine what the user can do.',
hint: 'The country will determine the currency and the timezone of the user.',
);

var_dump($role);
Expand Down
18 changes: 18 additions & 0 deletions src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public function __construct(
} else {
$this->highlighted = array_search($this->default, array_keys($this->options)) ?: 0;
}

// If the default is not visible, scroll and center it.
// If it's near the end of the list, we just scroll to the end.
if($this->highlighted >= $this->scroll){
$optionsLeft = count($this->options) - $this->highlighted - 1;
$halfScroll = (int) floor($this->scroll / 2);
$endOffset = max(0, $halfScroll - $optionsLeft);

// If the scroll is even, we need to subtract one more
// in order to take the highlighted option into account.
// Since when the scroll is odd the halfScroll is floored,
// we don't need to do anything.
if($this->scroll % 2 === 0){
$endOffset--;
}

$this->firstVisible = $this->highlighted - $halfScroll - $endOffset;
}
}

$this->on('key', fn ($key) => match ($key) {
Expand Down
76 changes: 76 additions & 0 deletions tests/Feature/SelectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,79 @@

expect($result)->toBe('Blue');
});

it('centers the default value when it\'s not visible', function () {
Prompt::fake([Key::ENTER]);

$result = select(
label: 'What is your favorite color?',
options: [
'Red',
'Green',
'Blue',
'Yellow',
'Orange',
'Purple',
'Pink',
'Brown',
'Black',
],
default: 'Purple',
scroll: 3
);

expect($result)->toBe('Purple');

Prompt::assertOutputContains('Orange');
Prompt::assertOutputContains('Purple');
Prompt::assertOutputContains('Pink');
});

it('scrolls to the bottom when the default value is near the end', function (int $scroll, array $outputContains) {
Prompt::fake([Key::ENTER]);

$result = select(
label: 'What is your favorite color?',
options: [
'Red',
'Green',
'Blue',
'Yellow',
'Orange',
'Purple',
'Pink',
'Brown',
'Black',
],
default: 'Brown',
scroll: $scroll
);

expect($result)->toBe('Brown');

foreach ($outputContains as $output) {
Prompt::assertOutputContains($output);
}
})->with([
'odd' => [
'scroll' => 5,
'outputContains' => [
'Orange',
'Purple',
'Pink',
'Brown',
'Black',
]
],
'even' => [
'scroll' => 6,
'outputContains' => [
'Yellow',
'Orange',
'Purple',
'Pink',
'Brown',
'Black',
]
],
]);