Skip to content

Commit

Permalink
fix multi rawList bug (#861) (#867)
Browse files Browse the repository at this point in the history
* fix multi rawList bug (#861)

* feat: add  rawlist unit test
  • Loading branch information
xiaoxielnino authored and SBoudrias committed Dec 15, 2019
1 parent 45af563 commit 904e473
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/inquirer/lib/prompts/rawlist.js
Expand Up @@ -64,14 +64,13 @@ class RawListPrompt extends Base {
validation.success.forEach(this.onEnd.bind(this));
validation.error.forEach(this.onError.bind(this));

events.keypress
.pipe(takeUntil(validation.success))
.forEach(this.onKeypress.bind(this));
events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
events.normalizedDownKey
.pipe(takeUntil(events.line))
.forEach(this.onDownKey.bind(this));

events.keypress
.pipe(takeUntil(validation.success))
.forEach(this.onKeypress.bind(this));
// Init the prompt
this.render();

Expand All @@ -96,7 +95,6 @@ class RawListPrompt extends Base {
'\n' + this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
message += '\n Answer: ';
}

message += this.rl.line;

if (error) {
Expand All @@ -111,8 +109,10 @@ class RawListPrompt extends Base {
*/

getCurrentValue(index) {
if (index == null || index === '') {
if (index == null) {
index = this.rawDefault;
} else if (index === '') {
index = this.selected;
} else {
index -= 1;
}
Expand Down Expand Up @@ -148,7 +148,6 @@ class RawListPrompt extends Base {
} else {
this.selected = undefined;
}

this.render();
}

Expand All @@ -174,11 +173,12 @@ class RawListPrompt extends Base {
*/

onArrowKey(type) {
var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
if (type === 'up') index = index === 0 ? this.opt.choices.length - 1 : index - 1;
else index = index === this.opt.choices.length - 1 ? 0 : index + 1;
this.rl.line = String(index + 1);
this.onKeypress();
var len = this.opt.choices.realLength;

if (type === 'up') this.selected = this.selected > 0 ? this.selected - 1 : len - 1;
else this.selected = this.selected < len - 1 ? this.selected + 1 : 0;

this.rl.line = String(this.selected + 1);
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/inquirer/test/specs/prompts/rawlist.js
Expand Up @@ -97,4 +97,16 @@ describe('`rawlist` prompt', function() {

this.rl.emit('line');
});

it('should allow for arrow navigation', function(done) {
this.rawlist.run().then(answer => {
expect(answer).to.equal('bar');
done();
});

this.rl.input.emit('keypress', '', { name: 'down' });
this.rl.input.emit('keypress', '', { name: 'down' });
this.rl.input.emit('keypress', '', { name: 'up' });
this.rl.emit('line', this.rl.line);
});
});

0 comments on commit 904e473

Please sign in to comment.