Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Single-select drop down should not have pre-filled value (react-hook-form issue) #763

Open
coderbyheart opened this issue May 8, 2022 · 2 comments
Labels
front end good first issue Good for newcomers help wanted Extra attention is needed
Milestone

Comments

@coderbyheart
Copy link
Member

Right now required single-select fields have the first value in the list pre-selected visually, but this value is not actually selected (and should not, since the user should actively make a choice).

The first disabled value should be shown as selected.

image

This needs someone who can figure out how to convince react-hook-form to behave this way.

@coderbyheart coderbyheart added good first issue Good for newcomers help wanted Extra attention is needed front end labels May 8, 2022
@coderbyheart coderbyheart added this to the v1 milestone May 8, 2022
@Moshyfawn
Copy link

Moshyfawn commented May 8, 2022

Hey @coderbyheart! One of the RHF maintainers here. I just saw your issue on.. Twitter?.. for some reason; decided to pop in lol :P

If you're experiencing some unwanted UI behavior, it's most probably the web input's behavior, not RHF as it's only but a headless hook.

For select inputs, what I usually end up doing is

  • Have a default "Select a value" placeholder option (can be disabled for sure)
  • Set the placeholder option value to an empty string
  • Set the select field defaultValue to an empty string

This way you gonna achieve your desired behavior.

<select {...register("destination")} defaultValue="">
  <option value="" disabled>
    Pick a destination region
  </option>
  {options.map(option => (
    <option key={option.value} value={option.value}>{option.label}</option>
  ))}
</select>

Here's a working codesanbox

I'm willing to contribute some code, but I'll need some guidance 'cuz as I've mentioned above, I just stumbled upon your issue like 5 mins ago and I have no idea about your product xD

@coderbyheart
Copy link
Member Author

coderbyheart commented May 8, 2022

Hei, thanks for checking this out. I gues I was asking about another issue I have with react hook form.

However, in this case I am doing pretty much what you describe, but it does not have the effect:

<SelectField
options={[
{
label: 'Pick an origin region',
value: '',
disabled: true,
},
...regions.map((region) => ({
label: formatRegion(region),
value: region.id,
})),
]}
label="Origin region"
name="origin"
register={register}
required
errors={errors}
/>

Thanks for providing a working example, this should help us figure out what is different in our codebase, that makes this not work.

@coderbyheart coderbyheart modified the milestones: v1, v2 May 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
front end good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants