fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

[Bug]: Select component does not accept empty value

Open tahoon-ms opened this issue 3 years ago • 2 comments

Library

React Components / v9 (@fluentui/react-components)

System Info

Same environment as sandboxes on https://react.fluentui.dev/?path=/docs/preview-components-select--default, Edge browser

Are you reporting Accessibility issue?

no

Reproduction

https://codesandbox.io/s/compassionate-northcutt-nu8nsd?file=/example.tsx

Bug Description

Actual Behavior

For controlled Select, setting value={''} does not clear its value. It defaults to the first option. For uncontrolled Select, setting selectHtmlElement.value = '' does clear its value. However, empty defaultValue is not respected -- it defaults to the first option.

Expected Behavior

For controlled Select, setting value={''} should clear its value, like with a native HTML Select. For uncontrolled Select, setting defaultValue={''} should make the component start off with an empty value.

Also, the documentation needs to be updated for https://react.fluentui.dev/?path=/docs/preview-components-select--default#initial-value.

A Select can have its initial value defined by using selected on a child option should be changed to A Select can have its initial value defined by using defaultValue prop

Logs

No response

Requested priority

High

Products/sites affected

Microsoft Power Apps canvas apps

Are you willing to submit a PR to fix?

yes

Validations

  • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • [X] The provided reproduction is a minimal reproducible example of the bug.

tahoon-ms avatar Jan 09 '23 21:01 tahoon-ms

@tahoon-ms

For controlled Select, setting value={''} does not clear its value. It defaults to the first option.

Select is just a wrapper around native select element, I put them side by side in CodeSandbox and they behave exactly same: https://codesandbox.io/s/zen-pine-8ejiy6?file=/example.tsx

If you want to have an empty option, you need to have it explicitly, https://codesandbox.io/s/strange-mountain-sed0f8?file=/example.tsx

For uncontrolled Select, setting defaultValue={''} should make the component start off with an empty value.

It's the same as for controlled Select/select, you need to have an empty option for this case.


As native select behaves in the same way, I don't think that something should be changed.

Also, the documentation needs to be updated for https://react.fluentui.dev/?path=/docs/preview-components-select--default#initial-value.

A Select can have its initial value defined by using selected on a child option should be changed to A Select can have its initial value defined by using defaultValue prop

I will update this (#26493), thanks 👍

layershifter avatar Jan 25 '23 09:01 layershifter

@layershifter Your reasoning makes sense. I didn't realize that Select was intended to behave like a HTML <select> as closely as possible.

In the context of a form, it's a rather common design request for dropdowns to start off with empty value. Once users select a value, they can't revert to the empty state. I was hoping that Select would offer some convenient way to do this with a controlled value. I guess I can implement it the same way as with a HTML <select>: obtain a ref on mount and set its value to empty string.

One documentation suggestion: consider mentioning that defaultValue={''} does not have the same effect as selectHtmlElement.value = '' It will not clear the value.

tahoon-ms avatar Jan 26 '23 17:01 tahoon-ms