Skip to content

[Bug] Adding the placeholder attribute to the text method will affect the result of the validate method #282

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

Open
xuoutput opened this issue Apr 10, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@xuoutput
Copy link

Environment

  • OS: [macOS]
  • Node Version: [v20.11.0]
  • Package: [@clack/prompts]
  • Package Version: [v0.10.1]

Describe the bug

[Bug] validate method of text take the placeholder #29

@xuoutput xuoutput added the bug Something isn't working label Apr 10, 2025
@github-project-automation github-project-automation bot moved this to Needs triage in Triage Board Apr 10, 2025
@43081j
Copy link
Collaborator

43081j commented Apr 10, 2025

what do you expect the behaviour to be?

if you <tab> or <return> on the placeholder, it will set the value to the placeholder.

we could alternatively set the value to the placeholder after running validate, but that feels odd too. since it means value will be undefined even though we're about to set it to the placeholder.

@xuoutput
Copy link
Author

// index.js
async function main() {
  const name = await text({
    message: "What is your name?",
    // placeholder: "any", 
    initialValue: "42",
    validate(value) {
      if (!value) return `Value is required!`;
    },
  });
}
main()

start node index.js

Image

When I deleted '42', The validate method is working properly, and preventing me from using enter to proceed to the next step

Image

But if I add a placeholder, even if it's an empty string, The validate method is not working properly

// index.js
async function main() {
  const name = await text({
    message: "What is your name?",
    placeholder: "", 
    initialValue: "42",
    validate(value) {
      if (!value) return `Value is required!`;
    },
  });
}
main()
Image

The expected behavior of the placeholder will not affect the validate method

@43081j
Copy link
Collaborator

43081j commented Apr 11, 2025

that is because submitting when there is a placeholder will set the value to the placeholder

if you press <tab> or <return>, you're saying "use the placeholder as my value"

so naturally the value isn't empty, because you chose the placeholder as your value.

what did you expect the placeholder to do? behave more like an <input> placeholder? (i.e. not affect the value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Needs triage
Development

No branches or pull requests

2 participants