prompts icon indicating copy to clipboard operation
prompts copied to clipboard

unable to clear initial value

Open juliangruber opened this issue 4 years ago • 4 comments

I would expect a text prompt with an initial value to also let the user submit an empty string for example by pressing the backspace key. With the current implementation a text prompt with an invalid value can't submit an empty string and will always submit the initial value instead.

juliangruber avatar Oct 24 '19 10:10 juliangruber

The downside of this would be of course that the user can't get back to the initial value any more. But less convenient is better than not functional (in this respect).

juliangruber avatar Oct 24 '19 15:10 juliangruber

IMO, this is important to have. There's many times I want to provide a default answer for the user, but I also want them to be able to just skip past, resulting in an empty value. What I wind up having to do is put a confirm before the question:

Do you want to enter some text? (Y/n):

Kind of lame, to make them answer MORE questions, just to see if they want to actually answer a question.

However, I think it should be something a little more explicit than backspace for them to skip the question. You still want them to be able to erase what they've typed, and get their default value back. Maybe something like ESC, DELETE, or TAB.

Better yet, give the developer the ability to hook into various keyboard inputs, so they can determine their own methods. End result could be something like this:

type PromptsAction = "abort" | "submit" | undefined;

await prompts({
  type: 'text',
  name: 'input',
  message: 'Please enter some text',
  initial: 'foo bar',
  hint: 'Press <ESC> to skip',
  onKeyPress: (value, keyEvent): { value: string; action: PromptsAction } => {
    let action: PromptsAction = undefined;
    switch (keyEvent.keyCode) {
      case 27: {
        value = '';
        action = 'submit';
        break;
      }
    }
    return [value, action];
  }
})

With a hook defined like this, if the user hits escape, it will change the currently entered value to an empty string, and then define a chained action to move to the next prompt.

The keypress listen event could hit the hook first, and if the action comes back undefined, then continue as normal. But if it comes back with a valid action, then execute that one. Valid actions should of course only be one of the major actions, like moving to the next question, or aborting the process altogether, so abort or submit, none of the finer internal controls.

I think if I have time later, I'll work on putting together a PR 🤔.

Js-Brecht avatar Nov 05 '19 20:11 Js-Brecht

Valid points! And I agree we should find a solution to this. I'm currently starting to plan out the next major release of prompts and discussions like these are very helpful. I'm curious what the ideal API would look like for the next major release – which is going to have breaking changes anyway

terkelg avatar Nov 06 '19 01:11 terkelg

FWIW Since tab already converts from default to editable I would think that if you delete the value and want to revert to initial again then I'd use a tab again. It's certainly odd (unexpected) that the value gets replaced when you delete its last character.

One solution which would also make validation simpler, is to have a 'required' option.

nottledim avatar Nov 13 '19 10:11 nottledim