Last active
November 25, 2016 08:10
-
-
Save stefanbuck/3668ee97aba0cfa859203704be7041a6 to your computer and use it in GitHub Desktop.
See https://github.com/SBoudrias/Inquirer.js/issues/463 for details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inquirerList = require('inquirer/lib/prompts/list.js'); | |
const runAsync = require('run-async'); | |
const Rx = require('rx'); | |
function listItemRender(name, value, isSelected=false, isDisabled=false) { | |
const selected = isSelected ? 'checked ' : ''; | |
const disabled = isDisabled ? 'disabled ' : ''; | |
return `<br /><label> | |
<input ${disabled}${selected} value="${value}" name="aaa" type="radio"/> ${name} | |
</label>`; | |
} | |
function listRender(choices, pointer) { | |
let output = ''; | |
let separatorOffset = 0; | |
choices.forEach((choice, i) => { | |
if (choice.type === 'separator') { | |
separatorOffset++; | |
output += ' ' + choice + '<br/>'; | |
return; | |
} | |
if (choice.disabled) { | |
separatorOffset++; | |
output += listItemRender(choice.name, i, false, choice.disabled); | |
return; | |
} | |
const realIndex = i - separatorOffset; | |
const isSelected = (realIndex === pointer); | |
output += listItemRender(choice.name, realIndex, isSelected, choice.disabled); | |
}); | |
return output; | |
} | |
inquirerList.prototype.render = function() { | |
let message = this.getQuestion(); | |
if (this.status === 'answered') { | |
message += this.opt.choices.getChoice(this.selected).short; | |
return message; | |
} else { | |
message += '<br/>' + listRender(this.opt.choices, this.selected); | |
} | |
message += '<br /><button>Next</button>' | |
const el = document.createElement('div'); | |
el.innerHTML = message; | |
document.getElementById('app').appendChild(el); | |
const self = this; | |
const $button = document.getElementsByTagName('button')[0]; | |
Rx.Observable.fromEvent($button, 'click') | |
.map(this.getCurrentValue.bind(this)) | |
.flatMap(value => { | |
document.getElementById('app').innerHTML = ''; | |
return runAsync(self.opt.filter)(value).catch(err => { | |
return err; | |
}); | |
}) | |
.forEach(this.onSubmit.bind(this)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment