clack icon indicating copy to clipboard operation
clack copied to clipboard

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

Open xuoutput opened this issue 7 months ago • 3 comments

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 avatar Apr 10 '25 09:04 xuoutput

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.

43081j avatar Apr 10 '25 14:04 43081j

// 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

xuoutput avatar Apr 11 '25 05:04 xuoutput

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)

43081j avatar Apr 11 '25 08:04 43081j