Skip to content

Commit

Permalink
Handle disabled option in new select prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Oct 3, 2018
1 parent 7d30fd4 commit e5fcd17
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/select/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { createPrompt } = require('@inquirer/core');
const { isUpKey, isDownKey } = require('@inquirer/core/lib/key');
const { isUpKey } = require('@inquirer/core/lib/key');
const Paginator = require('@inquirer/core/lib/Paginator');
const chalk = require('chalk');
const figures = require('figures');
Expand All @@ -9,18 +9,18 @@ module.exports = createPrompt(
readline => ({
onKeypress: (value, key, { cursorPosition = 0, choices }, setState) => {
let newCursorPosition = cursorPosition;
if (isUpKey(key)) {
newCursorPosition = (cursorPosition - 1 + choices.length) % choices.length;
} else if (isDownKey(key)) {
newCursorPosition = (cursorPosition + 1) % choices.length;
}
const offset = isUpKey(key) ? -1 : 1;
let selectedOption;

if (newCursorPosition !== cursorPosition) {
setState({
cursorPosition: newCursorPosition,
value: choices[newCursorPosition].value
});
while (!selectedOption || selectedOption.disabled) {
newCursorPosition = Math.abs(newCursorPosition + offset) % choices.length;
selectedOption = choices[newCursorPosition];
}

setState({
cursorPosition: newCursorPosition,
value: selectedOption.value
});
},
paginator: new Paginator(readline)
}),
Expand Down

0 comments on commit e5fcd17

Please sign in to comment.