Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty input by removing default if user enters backspace on empty input #700

Merged
merged 1 commit into from
Aug 7, 2018

Conversation

danmactough
Copy link
Contributor

@danmactough danmactough commented Jul 11, 2018

Strip surrounding quotes from input value; enable empty values

As mentioned a loooong time ago in #108, there's currently no way to input an empty value for an input with a default value.

But what if we allowed users to put quotes around their input values the way you would when setting a bash variable? And then the value would be set to the unquoted value.

In other words, when a user enters "foo" the value would be set to foo (without quotes).

This feels like expected behavior to me, and it would enable an empty value for an input with a default value, because "" would become an empty string.

Is this too magical or unexpected?

Would it be better to simply treat a value of "" or '' as a special case that should be transformed to an empty string?

I think some solution would be preferable to the current recommendation (adding an extra Y/N question).

UPDATED in line with comment to use backspace instead of stripping quotes.

@codecov
Copy link

codecov bot commented Jul 11, 2018

Codecov Report

Merging #700 into master will decrease coverage by 0.04%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #700      +/-   ##
==========================================
- Coverage   92.73%   92.69%   -0.05%     
==========================================
  Files          26       26              
  Lines        1033     1040       +7     
  Branches       19       19              
==========================================
+ Hits          958      964       +6     
- Misses         75       76       +1
Impacted Files Coverage Δ
packages/inquirer/lib/prompts/input.js 97.77% <85.71%> (-2.23%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08432db...ef148f5. Read the comment docs.

@SBoudrias
Copy link
Owner

I think stripping the quotes might have side effet for other users who might need them. I guess you could allow spaces and strip them in the filter function.

Or maybe use backspace on empty answer to get rid of the default?

@danmactough
Copy link
Contributor Author

Are you suggesting backspace would clear the default and rerender the prompt? That sounds great!

@SBoudrias
Copy link
Owner

Yeah, I think this would be more intuitive and more clearly communicate it with the user.

Want to send a PR?

@danmactough
Copy link
Contributor Author

Yeah, I think this would be more intuitive and more clearly communicate it with the user.

Want to send a PR?

I've looked into using backspace, and I don't actually see how we could do it correctly.

It's possible to intercept the backspace here and remove the default value, but it's not possible to distinguish whether the user has hit backspace on an already empty line (for which we want to clear the default) or instead has hit backspace to remove the last previously-typed character to leave the line blank (which I don't think should trigger clearing the default.

Maybe an example will clarify.

If we change that onKeypress method to:

  onKeypress(event) {
    const { key } = event;
    // Empty the default when a user clears the input
    if (key.name === 'backspace' && !this.rl.line) {
      console.log('');
      console.log('key', key);
      console.log('this.rl.line', JSON.stringify(this.rl.line));
      this.opt.default = undefined;
    }

    this.render();
  }
$ node packages/inquirer/examples/input.js
? What's your first name # hit enter
? What's your last name (Doe) # hit backspace
key { sequence: '',
  name: 'backspace',
  ctrl: false,
  meta: false,
  shift: false }
this.rl.line ""
? What's your last name # ^D

$ node packages/inquirer/examples/input.js
? What's your first name # hit enter
? What's your last name (Doe) a # hit a followed by backspace
key { sequence: '',
  name: 'backspace',
  ctrl: false,
  meta: false,
  shift: false }
this.rl.line ""
? What's your last name # ^D

@SBoudrias
Copy link
Owner

I see the problem. I think on keypress, we can check the value of this.line. And if the line is empty then we set a flag like this.isEmpty or a relevant name. Then if this.isEmpty === true && key.name === 'backspace' we clear the default.

When the input is empty and backspace key is entered, the default is cleared. This allows the user to enter an empty value.
@danmactough
Copy link
Contributor Author

@SBoudrias Ok, I updated this to use the backspace key instead of stripping surrounding quotes. I realized that we want to get this behavior even if the user has made no keypress before the backspace, so I implemented that little state tracking as hasInput instead of isEmpty.

@danmactough danmactough changed the title Allow empty string input by stripping surrounding quotes from input value Allow empty input by removing default if user enters backspace on empty input Aug 6, 2018
@SBoudrias
Copy link
Owner

Thanks! The latest version looks great :)

@SBoudrias SBoudrias merged commit 52de730 into SBoudrias:master Aug 7, 2018
@danmactough
Copy link
Contributor Author

Thanks for your thoughtful feedback, Simon.

@danmactough danmactough deleted the allow-empty-string-input branch August 7, 2018 13:43
danmactough added a commit to mapbox/cfn-config that referenced this pull request Aug 7, 2018
This version includes SBoudrias/Inquirer.js#700, which enables a user to enter an empty input value for an input with a default value.

To enter an empty value, a user hits the backspace key when the input is empty. This clears the default value.
@orgads
Copy link
Contributor

orgads commented Jul 7, 2019

This works for a text field, but doesn't work for password.

jdoyle65 pushed a commit to jdoyle65/Inquirer.js that referenced this pull request Jan 19, 2021
When the input is empty and backspace key is entered, the default is cleared. This allows the user to enter an empty value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants