[Bug]: Combobox displays the value of an Option instead of the display text (child as string)
Library
React Components / v9 (@fluentui/react-components)
System Info
System:
OS: Windows 10 10.0.19042
CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
Memory: 7.05 GB / 15.67 GB
Browsers:
Chrome: 101.0.4951.64
Edge: Spartan (44.19041.1266.0), Chromium (100.0.1185.29)
Internet Explorer: 11.0.19041.1566
Are you reporting Accessibility issue?
No response
Reproduction
https://codesandbox.io/s/cocky-waterfall-goc5w8
Bug Description
Actual Behavior
1- When select an Option with a value, the value is displayed in the input. 2- The matching function searches in the value. 3- When freeform, hitting the space bar makes the list box close. You cannot type something with a space.
Expected Behavior
1- When select an Option with a value, the display text (child string value) is displayed in the input. 2- The matching function searches in the display text (child string value) 3- When freeform, you can type something that has a space in it.
<Combobox
freeform
>
<Option value="value1">This is the display text for 1</Option>
<Option value="value2">This is the display text for 2</Option>
<Option value="value3">This is the display text for 3</Option>
</Combobox>
Logs
No response
Requested priority
Normal
Products/sites affected
No response
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.
Not sure why this bug is not going to triage? Did I miss something when submitting? I'd like to use the dropdown in prod, but it is not ready yet.
Yeah, it's really frustrating that it does it that way
I was about to open a similar issue with the following minimum repro: https://codesandbox.io/s/combobox-and-dropdown-oml8x0?file=/example.tsx
Could you please look into this ASAP? It would be a blocker for us to upgrade the Dropdown components from v8.
JSX code
<div>
<div>Selected Combobox option: {selectedOption1}</div>
<Combobox
selectedOptions={[selectedOption1]}
onOptionSelect={(_, e) => setSelectedOption1(e.optionValue)}
>
<Option value="Workspace-GUID-1">Workspace 1</Option>
<Option value="Workspace-GUID-2">Workspace 2</Option>
<Option value="Workspace-GUID-3">Workspace 3</Option>
</Combobox>
<div>Selected Dropdown option: {selectedOption2}</div>
<Dropdown
selectedOptions={[selectedOption2]}
onOptionSelect={(_, e) => setSelectedOption2(e.optionValue)}
>
<Option value="Workspace-GUID-1">Workspace 1</Option>
<Option value="Workspace-GUID-2">Workspace 2</Option>
<Option value="Workspace-GUID-3">Workspace 3</Option>
</Dropdown>
</div>
Actual Behavior
The option values (Workspace-GUID-x) are being rendered in the dropdown field, while the option contents (Workspace 1) are rendered in the dropdown list.

Expected Behavior
The option contents (Workspace 1) are rendered both in the dropdown field and dropdown list.

@smhigley feedback/issue with ComboBox, let's take a look quickly
Hi! This is actually intentional -- the value prop is intended to hold the string-only value that is displayed in the <input>'s value when the option is selected. The reason we don't use the children of the Option is because <input> can only take a string value, while <Option>s can have JSX children. We also use it for type-to-move-active-option functionality for keyboard interaction.
I'm looking at including other props (probably the id) in the onOptionSelect callback's data. If you were able to get the id string when an option is selected, would that resolve what you're currently using value for now?
Another option might be to rename <Option value="string"> to something like <Option text="string"> to avoid the confusion, though I'm hesitant to change the API unless it's a big issue.
It should either have "key" and "text" properties like the Dropdown and ComboBox components from version 8 or "label" and "value" properties like the Radio component in version 9.
Without those properties, the component is useless.
the
valueprop is intended to hold the string-only value that is displayed in the<input>'s value when the option is selected.
I haven't seen any design similar to this, especially the "...displayed in the <input>'s value when the option is selected" part.
- The HTML
<option>element renders its inner text both as dropdown item candidate and as selected item. - React MUI
<MenuItem>component used in<Select>component accepts any react node (e.g.,<span>) as direct child.- The child is rendered both as dropdown item candidate and as selected item.
- Carbon React
<Dropdown>component accepts anArrayofstringorobjectas data source (itemsprop), and also exposesitemToStringandrenderSelectedItemprops. The first one is used to convert objects into string for rendering the dropdown list; the second one is used to render selected object as React DOM inside the dropdown field.- The child is rendered both as dropdown item candidate and as selected item, unless
renderSelectedItemis specified to render otherwise.
- The child is rendered both as dropdown item candidate and as selected item, unless
- Lastly, Fluent UI v8
<Dropdown>component accepts anArrayofobject(IDropdownOption[]) as data source. It also exposesonRenderTitleandonRenderOptionto customize how to change the look of the options.IDropdownOption.textis rendered both as dropdown item candidate and as selected item, unlessonRenderTitle/onRenderOptionis specified to render otherwise.
What I'm try to say is
valueof an option is different fromtext(or innerText/innerHtml) of an option. Thevalueof an option is what we, downstream users matter, which does not have to bestring-only. We use it to distinguish between different items.- It's unusual for users to see a dropdown option that has different appearances when in dropdown list and in dropdown field (as selected item)
The reason we [...] is because
<input>can only take a string value, while<Option>s can have JSX children.
These are facts, but I didn't see why <Dropdown> should align with <input> in a way that even the type restrictions on value property should be the same. Again, the text property can be designed to contain value of type any.
You have already rendered the whole dropdown component by yourself, why not just render the selected item inside the dropdown field? I failed to see any blocker stopping us to render rich content inside a React element.
Any updates on this? Not having a key/text option makes this component pretty useless.
In the meantime, I am using the version 8 dropdown because it has a key/text option. It would be nice to start using the version 9.
Any updates on this? Not having a key/text option makes this component pretty useless.
In the meantime, I am using the version 8 dropdown because it has a key/text option. It would be nice to start using the version 9.
At this time, as a workaround, I've created a dropdown wrapper component, that fixes the problem. Though I hope Microsoft will push out an update to this issue
Wrapper component example
import React from "react";
import { Dropdown, DropdownProps, Option } from "@fluentui/react-components/unstable";
interface IProps
{
options?: { [key: string]: string };
onSelect?: (key: string) => void;
selectedOption?: string;
}
export default class DropdownWrapper extends React.Component<DropdownProps & IProps>
{
public render(): JSX.Element
{
let selectedOption = this.props.selectedOption ? this.props.options[this.props.selectedOption] : null;
return (
<Dropdown
{ ...this.props }
selectedOptions={ [ selectedOption ] }
onOptionSelect={ (_, e) => this.props.onSelect(this.GetSelectedOption(e.optionValue)) }>
{ Object.entries(this.props.options).map(([key, name]) =>
<Option key={key}>{name}</Option>
) }
</Dropdown>
);
}
private GetSelectedOption(key : string): string
{
let index = Object.values(this.props.options).indexOf(key);
return Object.keys(this.props.options)[index];
}
}
Component usage example
<DropdownWrapper
aria-labelledby="label-GroupSavePolicy"
selectedOption={ opt?.GroupSavePolicy }
onSelect={ (e) => SettingsService.Update({ GroupSavePolicy: e as GroupSavePolicy }) }
options={ {
"separate": "Save each group as new collection",
"subgroups": "Preserve grouping inside one collection",
"omit": "Omit grouping"
} }
/>
Good idea. I wrote one that works a little differently. It transforms the data.optionValue and data.optionText and data.selectedOptions in the onOptionSelect callback.
type DropdownWrapperProps = DropdownProps & {
options?: [string, string][];
};
const DropdownWrapper = (props: DropdownWrapperProps) => {
const { options, ...dropdownProps } = props;
const optionsMap = new Map(options);
return <FluentDropdown
{...dropdownProps}
onOptionSelect={(event, data) => {
if (props.onOptionSelect) {
props.onOptionSelect(event, {
optionText: data.optionValue,
optionValue: optionsMap.get(data.optionValue),
selectedOptions: data.selectedOptions.map((value) => optionsMap.get(value))
});
}
}}
>
{options.map(value => <Option key={`Option_${value[1]}`}>{value[0]}</Option>)}
</FluentDropdown>;
};
You can use it like this:
<DropdownWrapper
onOptionSelect={(_, data) => {
console.log(data);
}}
options={[
["Text 1", "value1"],
["Text 2", "value2"],
["Text 3", "value3"]
]}
/>
Forgot to update this issue, but this should be fixed in #25379
The value prop is now used as a key for selectedOptions, and you can put any misc string in there since it won't be displayed to the user. Now the text prop is used to provide a user-facing string that will be used for type-to-highlight behavior and the display text when an option is selected.
So now you should be able to do:
import React from "react";
import { Dropdown, DropdownProps, Option } from "@fluentui/react-components/unstable";
const optionData = [
['key1', 'Display Text 1'],
['key2', 'Display Text 2'],
['key3', 'Display Text 3'],
...etc.
];
const [selectedOptions, setSelectedOptions] = React.useState([]);
return <Dropdown
{...dropdownProps}
selectedOptions={selectedOptions}
onOptionSelect={(event, data) => {
// data.optionValue will be e.g. 'key2'
// data.optionText will be e.g. 'Display Text 2'
// data.selectedOptions will be e.g. ['key1', 'key2']
setSelectedOptions(data.selectedOptions);
}}
>
{options.map((value) => <Option key={`Option_${value[1]}`} value={value[0]}>{value[1]}</Option>)}
</Dropdown>;
The button's inner text if you select the option with value 'key 2' will be "Display Text 2", customizable with the text prop
@smhigley are you saying that this is implemented? Because as of version 9.12.0 your example does not work.
It clearly does not work like you mention.

The optionValue is displayed instead of the children.
I also noticed that the component no longer works with react 18.2.0

I stopped testing this component a while ago. To help, I figured out that the component stopped working with react 18.2 starting at @fluentui/[email protected]. Prior versions worked with react 8.2.0.
The component is now stable in 9.13.0!!! Really?
The value is still being shown when selected instead of the text and the component does not work at all with react 8.

@ParadimeWeb you're right, I'd forgotten the specifics of the earlier change. The central issue is around when the parent Combobox is aware of its Options. Since they're not rendered until they're needed, setting defaultSelectedOptions or selectedOptions to the value string means that the Combobox will not be able to display anything as its value, since it can't know what the child Option's human-readable text is until it's rendered. This is why I initially kept the value as the display text, since it allows us to handle the display value off only the defaultSelectedOptions or selectedOptions props.
However, I think you make a good point that the base use case (no controlled or default selected options, only child <Option value="x">) is probably the one that should work, so I've added a PR that requires authors to set defaultValue or value if using defaultSelectedOptions or selectedOptions. This means that an uncontrolled Combobox or Dropdown can use <Option value="a">Apple</Option> and display "Apple" when the option is selected: #26617
I'll try to make sure that's updated today, to minimize the amount of churn.
Re: the React 18 issue. This is a breaking change in useId, not specific to combobox. As of React 18, it is returning :r#: instead of a usable id string. Since colons are not a valid character for id strings, it's throwing an error in combobox, and silently failing elsewhere. Thanks for finding it, and I'll investigate it as soon as the combobox change is in.
Oh, I forgot to mention -- one workaround right now for the React 18 issue is to specify ids in props, so our internal useId hook isn't called. In the case of Combobox, that would be on the <Option> components, so something like:
{options.map(option => (
<Option id={option.key} key={option.key}>{option.name}</Option>
))}