Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiSelectable] Clear Searchbar Bug Fix #6317

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/selectable/selectable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

/// <reference types="cypress" />
/// <reference types="../../../cypress/support"/>

import React, { useState } from 'react';
Expand Down
35 changes: 35 additions & 0 deletions src/components/selectable/selectable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,41 @@ describe('EuiSelectable', () => {
).toEqual('second value');
});

it('updates options list when searchValue state is controlled by searchProps.value', () => {
const searchProps = {
value: 'Enceladus',
'data-test-subj': 'searchInput',
};
const component = mount(
<EuiSelectable options={options} searchable searchProps={searchProps}>
{(list, search) => (
<>
{list}
{search}
</>
)}
</EuiSelectable>
);

expect(
(component.find('EuiSelectableList').props() as any).visibleOptions
).toHaveLength(1);

component.setProps({
searchProps: { ...searchProps, value: 'value not in list' },
});

expect(component.find('EuiSelectableList').exists()).toBeFalsy();

component.setProps({
searchProps: { ...searchProps, value: '' },
});

expect(
(component.find('EuiSelectableList').props() as any).visibleOptions
).toEqual(options);
});

it('calls the searchProps.onChange callback on mount', () => {
const onChange = jest.fn();
mount(
Expand Down
24 changes: 12 additions & 12 deletions src/components/selectable/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,28 @@ export class EuiSelectable<T = {}> extends Component<
const { options, isPreFiltered, searchProps } = nextProps;
const { activeOptionIndex, searchValue } = prevState;

const matchingOptions = getMatchingOptions<T>(
options,
searchValue,
isPreFiltered
);

const stateUpdate: Partial<EuiSelectableState<T>> = {
visibleOptions: matchingOptions,
searchValue,
activeOptionIndex,
};

if (searchProps?.value != null && searchProps.value !== searchValue) {
stateUpdate.searchValue = searchProps.value;
}

stateUpdate.visibleOptions = getMatchingOptions<T>(
options,
stateUpdate.searchValue ?? '',
isPreFiltered
);

if (
activeOptionIndex != null &&
activeOptionIndex >= matchingOptions.length
activeOptionIndex >= stateUpdate.visibleOptions.length
Comment on lines 243 to +260
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM / no issues with the code order of operations change/moves - my main blocker with this PR is that I don't see a valid reproduction of the bug that this is fixing to QA

) {
stateUpdate.activeOptionIndex = -1;
}

if (searchProps?.value != null && searchProps.value !== searchValue) {
stateUpdate.searchValue = searchProps.value;
}

return stateUpdate;
}

Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/6317.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiSelectable` to ensure the full options list is re-displayed when the search bar is controlled and cleared using `searchProps.value`