Skip to content

Key Property in ComboBoxItem, MultiComboBoxItem and Option #493

Closed

Description

@mathlizee

Hi,

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?

Thanks!
Math

Activity

  1. MarcusNotheis commented on May 8, 2020

    @MarcusNotheis
    Member

    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:

    export default function App() {
      const _onValueChange = oEvent => {
        // Would be either ID0, ID1, ID3 or ID4
        console.log(oEvent.detail.selectedOption.dataset.key);
      };
    
      return (
        <div>
          <Select id="select" onChange={_onValueChange}>
            {users.map(user => {
              return <Option
                key={user.user_id}  // still required for react to optimize
                data-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?

    Best regards,
    Marcus

  2. MarcusNotheis commented on May 19, 2020

    @MarcusNotheis
    Member

    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:

    export default function App() {
      const _onValueChange = oEvent => {
        // Would be either ID0, ID1, ID3 or ID4
        console.log(oEvent.detail.items.map(item => item.dataset.key));
      };
    
      return (
        <div>
          <MultiComboBox id="comboBox" onSelectionChange={_onValueChange}>
            {users.map(user => {
              return (
                <MultiComboBoxItem
                  key={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

  3. MarcusNotheis commented on Jul 16, 2020

    @MarcusNotheis
    Member

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions