Comments (23)
onSubmit
is different from what @datitisev is asking for. I still want and need the filter myself. The feature is still being considered.
from prompts.
I'll take a look at this for a version v0.2. Feel free to add a PR
from prompts.
@yoavmmn That's essentially what my example is doing 😆 I agree, I think it's sufficient.
In order to display an error message, you could attach an onError
key to each prompt, then change mine to:
function onSubmit(obj, val) {
obj.isValid(val) ? defs.shift() : obj.onError();
}
Or handle all of that inside the isCustom
function:
function isCustom(val) {
if (val > 100) return true;
console.log('ERROR MESSAGE');
//=> returns undefined --> falsey
}
from prompts.
validate
option is added in v1.0.0 branch for Number
and Text
prompt.
const response = await prompts({
type: 'number',
name: 'value',
message: 'How old are you?',
validate: value => value < 18 ? `Nightclub is 18+ only` : true
});
Please give it a test run if you like @datitisev @lukeed @marcghorayeb
from prompts.
onCancel
is not preventing you from canceling a single prompt, it controls whether the prompt chain should cancel or continue. If you have an array of prompts you'll see it either continue or break the chain based on the onCancel
return value.
from prompts.
It's super important for me to keep the package really tight - without too many features or dependencies. What would the use case be for this?
I normally just do multiple prompts()
and do validation after each.
That being said I've thinking about doing a message
and split
prompt.
from prompts.
@lukeed I have no idea how I missed your comment. Maybe because it's midnight already over here 😅
from prompts.
Validate property would be nice yep 👍
from prompts.
@olstenlarck I haven't coded anything with prompts yet, and as this issue is still open, I thought maybe the end result was similar to validation (after reading all the comments), but not exactly.
Maybe I misread them, apologies if I did.
I'll see if I can implement it when I have the time.
from prompts.
The main difference of doing the validation after the prompt is that you can't show live errors to users.
Also it allows user to not have complicate logic to handle response rejection.
from prompts.
Personally, I'd call this a maybe. As you've both mentioned, this requires an entirely new error
-like module that might go beyond the scope of @terkelg's intentions.
A quick (and still manageable) way around this is to process prompts
one at a time & then "validate" them on the onSubmit
hook. You could choose to exit the prompt with an error (stdout.write('Reason'); return true
) or display the same prompts
definition, with an updated initial
or message
key.
function isCustom(val) {
// return true if I consider valid
}
let defs = [
{
type: 'text',
name: 'foobar',
isValid: isCustom
}, {
// ... etc
}
];
function onSubmit(obj, val) {
obj.isValid(val) && defs.shift(); // if valid, remove item
}
async function run() {
if (!defs.length) return 'done';
return await prompts(defs[0], { onSubmit }).then(run);
}
await run();
Not sure if the above will work 😆 Quick psuedo'ing
from prompts.
It just feel too complicated to handle this manually for each prompt.
Also because the error is supposed to disappear after a valid input is provided.
from prompts.
Wait, whats wrong with options.onSubmit
? It's built right into the API.
I think the only drawback is that you can't force the user to enter the expected input.
from prompts.
Here's a little demonstration of the capabilities of options.onSubmit
.
const prompts = require('prompts');
function isInputHiOrHey(greeting = '') {
return greeting === 'hi' || greeting === 'hey';
}
// a recursive function that checks user's input just for the prompt named greeting
function checkInput(prompt, answer) {
if (prompt.name === 'greeting') {
if (!isInputHiOrHey(answer)) {
console.log('Please enter a valid greeting.');
prompts(prompt, { onSubmit: checkInput })
}
}
}
prompts({
type: 'text',
name: 'greeting',
message: 'What\'s your greeting?'
}, {
onSubmit: checkInput
});
from prompts.
Just realized that the whole thing is that we need prop like onSubmit
option but per question object. Then we can do anything from validate
to required
and etc.
from prompts.
After #43 all prompts have a onSubmit
and onAbort
. It's in master but not yet released or documented. I'm still open for implementing some nice visuals for this later.
Focus right now is to get v0.1.5 out the door.
from prompts.
Sorry for the bump.
Is this feature still being considered?
Or at least, some method to make a custom implementation of it easier?
I would like to replace inquirer
with this great module 🙂
from prompts.
@datitisev it was while ago.. Can't you use onSubmit
to implement it?
from prompts.
I didn't tried onSubmit
still either, but.. In glance looks it seems it would work.
from prompts.
@terkelg Works great! 🙂 Thanks!
Edit: Not sure if it is a result from this change, but when the onCancel
function returns true, it still exists.
from prompts.
@datitisev is that a single prompt or a prompt chain?
from prompts.
@terkelg A single prompt.
let onCancel = prompt => {
console.log('Never stop prompting!');
return true;
}
const response = await prompts({
type: 'text',
name: 'email',
message: 'What\'s yar email?',
}, { onCancel });
console.log(response);
from prompts.
Coming in #80
from prompts.
Related Issues (20)
- How to test prompts in CircleCI?
- Select crashing in VsCode + Git Bash: Cannot read properties of undefined (reading 'split') HOT 2
- Multi-option toggle. Worth it?
- Ensure types are exported HOT 3
- How to cancel these unnecessary logs HOT 5
- logic diagram
- Ast prompts
- Roadmap for a 3.0 HOT 5
- prompts captures Ctrl-W "cut word" shortcut as control character
- How to make autocomplete required or select searchable?
- Add mask option to Text Type
- Somewhat different published module than the one in the repo HOT 2
- Keypress events may not be triggered on Windows if validate callback contains async actions HOT 2
- Bun support HOT 11
- confirm element: pressing a function key throws an error
- correctly type return/resolved values instead of `any`
- Initial value not passed to validate function for `"number"` types
- When the type is multiSelect, options cannot be switched through the arrows on the keyboard HOT 1
- Better index selection when providing initial for autocomplete
- Ability to prefill input on autocomplete HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prompts.