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

fix: cli cannot scroll down if multiple selections #11521

Merged
merged 4 commits into from
May 7, 2024
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
3 changes: 2 additions & 1 deletion packages/cli/src/prompts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export function computePrefixWidth(
const middle = Math.floor(pageSize / 2);
let pageStart;
if (current < middle) pageStart = 0;
else if (current > choices.length - middle) pageStart = choices.length - pageSize;
else if (current > choices.length - middle)
pageStart = choices.length < pageSize ? 0 : choices.length - pageSize;
else pageStart = current - middle;
let prefixWidth = 1;
choices.slice(pageStart, pageStart + pageSize).forEach((choice) => {
Expand Down
38 changes: 37 additions & 1 deletion packages/cli/tests/unit/prompts/customizedListPrompt.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,44 @@ describe("select prompt", () => {
(Use arrow keys to reveal more choices)`)
);

for (let i = 0; i < 8; i++) {
events.keypress("down");
}

events.keypress("enter");
expect(await answer).equal("id1");
expect(await answer).equal("id9");
});

it("allow setting the page size larger than choices length", async () => {
const { answer, events, getScreen } = await render(select, {
message: "Select a string",
choices,
pageSize: 14,
});

expect(getScreen()).equal(
trimOutput(`
? Select a string
(*) title 1 detail 1
( ) title 2 detail 2
( ) title 3 detail 3
( ) title 4 detail 4
( ) title 5 detail 5
( ) title 6 detail 6
( ) title 7 detail 7
( ) title 8 detail 8
( ) title 9 detail 9
( ) title 10 detail 10
( ) title 11 detail 11
( ) title 12 detail 12`)
);

for (let i = 0; i < 7; i++) {
events.keypress("down");
}

events.keypress("enter");
expect(await answer).equal("id8");
});

it("cycles through options", async () => {
Expand Down
Loading