You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Key Property in ComboBoxItem, MultiComboBoxItem and Option #493
I'm using the ComboBox control and quickly found a design issue. Usually, for controls that display options, such a Select, ComboBox or MultiComboBox, the options have a key and a name (or a description). For example, in the OpenUI5 documentation about the ComboBox control, the combo box items have a key and a text property.
However, the ComboBox, MultiComboBox and Select items of this library only have a text property that also plays the role of the item's key. This is a bit annoying, as it is not unusual that, for a particular item, the text displayed to the user is not that same as the key of the item.
For example, I need to display a ComboBox with multiple user names as items. However, when an item is selected, I don't really care about the user name but would like to know which user ID has been selected.
I considered parsing all users to find the ID of the user that has the selected name, but, frankly, not only is this a bit complicated, but it is also very bad performance-wise (if the control has 150 items, for example). Furthurmore, if two or more users have the same name, it's just not possible. And, of course, I can't use the user IDs as the item's text...
From what I can see, I really think that the ComboBoxItem, MultiComboBoxItem and Option should have a key property as well as a text property. This would also be more consistent with the current OpenUI5 controls. Here is a sample of what it could look like code-wise. Would it be possible to add such a feature in a next release?
Hi @mathlizee
thanks for that detailed description!
The thing is the key prop is a reserved property in React which is used for optimising updates the virtual DOM when JSX elements are created from an Array.map method like in your example. React even takes that prop out of the other props so it will never reach the component.
But you can use the HTML5 data- api to achieve the desired behaviour for e.g. the Select:
exportdefaultfunctionApp(){const_onValueChange=oEvent=>{// Would be either ID0, ID1, ID3 or ID4console.log(oEvent.detail.selectedOption.dataset.key);};return(<div><Selectid="select"onChange={_onValueChange}>{users.map(user=>{return<Optionkey={user.user_id}// still required for react to optimizedata-key={user.user_id}// this is your real key/>{user.user_name}</Option>})}</Select></div>);}
I think we need to add this to our documentation as well.
But for the ComboBox and MultiComboBox things are getting a bit more complex as there is no information about the selected items in the onChange event - I think this should be added by the UI5 Web Components - @MapTo0 what are your thoughts on that?
Hi @mathlizee,
I've looked at that issue again and using the data- api is also working fine for the MultiComboBox if you are using the onSelectionChange event.
Example:
exportdefaultfunctionApp(){const_onValueChange=oEvent=>{// Would be either ID0, ID1, ID3 or ID4console.log(oEvent.detail.items.map(item=>item.dataset.key));};return(<div><MultiComboBoxid="comboBox"onSelectionChange={_onValueChange}>{users.map(user=>{return(<MultiComboBoxItemkey={user.user_id}text={user.user_name}data-key={user.user_id}/>);})}</MultiComboBox></div>);}
Unfortunately there is so such API for the ComboBox itself, so I raised another issue at the UI5 Web Components repository: UI5/webcomponents#1651
Closing this issue as there is nothing more to do here in the React wrapper in my opinion.
Please feel free to reopen the issue in case there is still more work to do on our side.
Activity
MarcusNotheis commentedon May 8, 2020
Hi @mathlizee
thanks for that detailed description!
The thing is the
keyprop is a reserved property in React which is used for optimising updates the virtual DOM when JSX elements are created from anArray.mapmethod like in your example. React even takes that prop out of the other props so it will never reach the component.But you can use the HTML5
data-api to achieve the desired behaviour for e.g. theSelect:I think we need to add this to our documentation as well.
But for the
ComboBoxandMultiComboBoxthings are getting a bit more complex as there is no information about the selected items in theonChangeevent - I think this should be added by the UI5 Web Components - @MapTo0 what are your thoughts on that?Best regards,
Marcus
MarcusNotheis commentedon May 19, 2020
Hi @mathlizee,
I've looked at that issue again and using the data- api is also working fine for the MultiComboBox if you are using the
onSelectionChangeevent.Example:
Unfortunately there is so such API for the ComboBox itself, so I raised another issue at the UI5 Web Components repository: UI5/webcomponents#1651
MarcusNotheis commentedon Jul 16, 2020
Closing this issue as there is nothing more to do here in the React wrapper in my opinion.
Please feel free to reopen the issue in case there is still more work to do on our side.
Best regards,
Marcus