Skip to content

Commit

Permalink
refactor(prompt): set list index to 0 instead of 1 when selecting a g…
Browse files Browse the repository at this point in the history
…roup option
  • Loading branch information
c4spar committed Jul 24, 2024
1 parent 3295c7a commit 57cafb6
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 3 deletions.
9 changes: 6 additions & 3 deletions prompt/_generic_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export abstract class GenericList<
protected submitGroupOption(selectedOption: TGroup) {
this.parentOptions.push(selectedOption);
this.match();
this.listIndex = 1;
this.listIndex = 0;
}

protected isBackButton(option: TOption | TGroup | undefined): boolean {
Expand Down Expand Up @@ -584,10 +584,13 @@ export abstract class GenericList<
if (
this.isKey(this.settings.keys, "open", event) &&
isOptionGroup(this.selectedOption) &&
!this.isBackButton(this.selectedOption) &&
!this.isSearchSelected()
) {
this.submitGroupOption(this.selectedOption);
if (this.isBackButton(this.selectedOption)) {
this.selectNext();
} else {
this.submitGroupOption(this.selectedOption);
}
} else if (
this.isKey(this.settings.keys, "back", event) &&
(this.isBackButton(this.selectedOption) || event.name === "escape") &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,42 @@ snapshot[`checkbox prompt with groups and breadcrumb > should render breadcrumb
stdout:
"? Select an option
❯ ✘ 📁 Foo\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo
✘ 📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo
❯ ✘ 📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo › Bar
✘ 📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo › Bar
❯ ✘ 📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo › Bar › Baz
✘ 📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo › Bar › Baz
❯ ✘ 📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo › Bar › Baz › Beep
✘ 📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo › Bar › Baz › Beep
❯ ✘ 📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo › .. › Baz › Beep › Boop
✘ 📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo › .. › Baz › Beep › Boop
❯ ✘ 📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo › .. › Beep › Boop › Fiz
✘ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo › .. › Beep › Boop › Fiz
❯ ✘ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo › .. › Beep › Boop › Fiz
❯ ✘ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
Expand All @@ -42,21 +63,42 @@ snapshot[`checkbox prompt with custom breadcrumb separator > should render bread
stdout:
"? Select an option
❯ ✘ 📁 Foo\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo
✘ 📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo
❯ ✘ 📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo # Bar
✘ 📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo # Bar
❯ ✘ 📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo # Bar # Baz
✘ 📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo # Bar # Baz
❯ ✘ 📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo # Bar # Baz # Beep
✘ 📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo # Bar # Baz # Beep
❯ ✘ 📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo # .. # Baz # Beep # Boop
✘ 📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo # .. # Baz # Beep # Boop
❯ ✘ 📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Foo # .. # Beep # Boop # Fiz
✘ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo # .. # Beep # Boop # Fiz
❯ ✘ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Foo # .. # Beep # Boop # Fiz
❯ ✘ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ stdout:
✘ Foo
✘ Bar
❯ ✘ 📁 Baz\\x1b[3A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ ✘ 📂 Baz
✘ Beep
✘ Boop\\x1b[3A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
✘ 📂 Baz
❯ ✘ Beep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,39 @@ snapshot[`select prompt with groups and breadcrumb > should render breadcrumb 1`
stdout:
"? Select an option
❯ 📁 Foo\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo
📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo
❯ 📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo › Bar
📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo › Bar
❯ 📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo › Bar › Baz
📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo › Bar › Baz
❯ 📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo › Bar › Baz › Beep
📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo › Bar › Baz › Beep
❯ 📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo › .. › Baz › Beep › Boop
📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo › .. › Baz › Beep › Boop
❯ 📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo › .. › Beep › Boop › Fiz
Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo › .. › Beep › Boop › Fiz
❯ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
Expand All @@ -32,21 +50,39 @@ snapshot[`select prompt with custom breadcrumb separator > should render breadcr
stdout:
"? Select an option
❯ 📁 Foo\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo
📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo
❯ 📁 Bar\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo # Bar
📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo # Bar
❯ 📁 Baz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo # Bar # Baz
📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo # Bar # Baz
❯ 📁 Beep\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo # Bar # Baz # Beep
📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo # Bar # Baz # Beep
❯ 📁 Boop\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo # .. # Baz # Beep # Boop
📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo # .. # Baz # Beep # Boop
❯ 📁 Fiz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Foo # .. # Beep # Boop # Fiz
Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Foo # .. # Beep # Boop # Fiz
❯ Faz\\x1b[2A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ stdout:
Foo
Bar
❯ 📁 Baz\\x1b[3A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
❮ 📂 Baz
Beep
Boop\\x1b[3A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J
? Select an option
📂 Baz
❯ Beep
Expand Down
14 changes: 14 additions & 0 deletions prompt/test/integration/checkbox_group_breadcrumb_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ await snapshotTest({
steps: {
"should render breadcrumb": {
stdin: ansi
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
Expand Down Expand Up @@ -59,6 +66,13 @@ await snapshotTest({
steps: {
"should render breadcrumb": {
stdin: ansi
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
.cursorForward
Expand Down
1 change: 1 addition & 0 deletions prompt/test/integration/checkbox_group_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ await snapshotTest({
.cursorDown
.cursorDown
.cursorForward
.cursorForward
.cursorDown
.text(" ")
.text("\n")
Expand Down
12 changes: 12 additions & 0 deletions prompt/test/integration/select_group_breadcrumb_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ await snapshotTest({
steps: {
"should render breadcrumb": {
stdin: ansi
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
Expand Down Expand Up @@ -56,6 +62,12 @@ await snapshotTest({
steps: {
"should render breadcrumb": {
stdin: ansi
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
.text("\n")
Expand Down
1 change: 1 addition & 0 deletions prompt/test/integration/select_group_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ await snapshotTest({
.cursorDown
.text("\n")
.text("\n")
.text("\n")
.toArray(),
},
},
Expand Down

0 comments on commit 57cafb6

Please sign in to comment.