Eui: [EuiSelectable] list not filtered when `onChange` or `onSearch` included in searchProps

Created on 13 Oct 2020  路  2Comments  路  Source: elastic/eui

When searchable and one or more of searchProps.onChange and searchProps.onSearch is passed to EuiSelectable, the options are not correctly filtered in the UI. I created a sandbox showing both issues.

Code

I believe in both cases the searchProps are being spread over more important props, which seems unintended.

onChange

https://github.com/elastic/eui/blob/01c6986afdd7321f8e86b433be4892345aa7c0fe/src/components/selectable/selectable.tsx#L534-L551

  • onChange={this.onSearchChange} appears to be key to updating the selectable internal state
  • {...cleanedSearchProps} overrides it

onSearch

I'm less sure about the cause of this issue, but have a feeling it could be:

https://github.com/elastic/eui/blob/01c6986afdd7321f8e86b433be4892345aa7c0fe/src/components/selectable/selectable_search/selectable_search.tsx#L113-L125

  • Same problem as above, where onSearch can exist in ...rest (which comes from cleanedSearchProps above), overriding the important call to this.onSearchChange

Context

The feature I'm working on extends the EuiSelectable component, by adding "Select all" and "Select none" buttons above the searchable EuiSelectable. Here's an example of the UI.

This works without any hassle. I update the local options state when a user clicks on either button, and pass this state down to EuiSelectable. After building this, I realized that it would be a better UX to combine this behaviour with the client-side search offered by adding searchable. In my current implementation the user could filter (let's say to view 5 of 100 items), then click "Select all". This would select not only the visible/filtered options, but all of them.

To fix this UX issue, I tried to access the currently filtered items. I found I could do this as shown:

searchProps={{
  onChange: (filteredOptions) => {
     // here I set some local state. Implementation details not important.
  },
}}

Elsewhere, I could use this data to compare the filteredOptions and options, and work out which options should be updated when a user clicks on one of the Select all/none buttons. Again, no problems here.

I believe the data-flow is working fine using the existing props, and the only issue is that my combination of props prevents the EuiSelectable from accurately reflecting the search filters in the UI.

anyone bug

All 2 comments

Thanks for the investigation, @smhutch

It seems like your assessment is correct. We should be calling searchProps.onChange and searchProps.onSearch in addition to the "standard" onChange and onSearch callbacks.

Fixed in #4153

Was this page helpful?
0 / 5 - 0 ratings