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] Fix logic to detect the blur event coming from child popover #5021

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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- Fixed content in `EuiFilterButton` when `numFilters` is not passed ([#5012](https://github.com/elastic/eui/pull/5012))
- Fixed default value of `outsideClickCloses` prop of `EuiFlyout` ([#5027](https://github.com/elastic/eui/pull/5027))
- Fixed `EuiSelectable`'s double click bug ([#5021](https://github.com/elastic/eui/pull/5021))

## [`37.2.0`](https://github.com/elastic/eui/tree/v37.2.0)

Expand Down
40 changes: 40 additions & 0 deletions src/components/selectable/__snapshots__/selectable.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,43 @@ exports[`EuiSelectable props singleSelection 1`] = `
class="euiSelectable"
/>
`;

exports[`EuiSelectable should not reset the activeOptionIndex nor isFocused when EuiSelectable is blurred in favour of its popover 1`] = `
Object {
"activeOptionIndex": 0,
"isFocused": true,
"searchValue": "",
"visibleOptions": Array [
Object {
"data-test-subj": "titanOption",
"label": "Titan",
},
Object {
"label": "Enceladus",
},
Object {
"label": "Pandora is one of Saturn's moons, named for a Titaness of Greek mythology",
},
],
}
`;

exports[`EuiSelectable should not reset the activeOptionIndex nor isFocused when EuiSelectable is blurred in favour of its popover 2`] = `
Object {
"activeOptionIndex": 0,
"isFocused": true,
"searchValue": "",
"visibleOptions": Array [
Object {
"data-test-subj": "titanOption",
"label": "Titan",
},
Object {
"label": "Enceladus",
},
Object {
"label": "Pandora is one of Saturn's moons, named for a Titaness of Greek mythology",
},
],
}
`;
25 changes: 25 additions & 0 deletions src/components/selectable/selectable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ describe('EuiSelectable', () => {
expect(component).toMatchSnapshot();
});

test('should not reset the activeOptionIndex nor isFocused when EuiSelectable is blurred in favour of its popover', () => {
const component = mount(
<EuiSelectable options={options} searchable>
{(list, search) => (
<>
{list}
{search}
</>
)}
</EuiSelectable>
);

component.setState({
activeOptionIndex: 0,
isFocused: true,
});
expect(component.state()).toMatchSnapshot();

component.find('.euiSelectable').simulate('blur', {
relatedTarget: { firstChild: { id: 'generated-id_listbox' } },
});
component.update();
expect(component.state()).toMatchSnapshot();
});

describe('props', () => {
test('searchable', () => {
const component = render(<EuiSelectable options={options} searchable />);
Expand Down
7 changes: 6 additions & 1 deletion src/components/selectable/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ export class EuiSelectable<T = {}> extends Component<

onContainerBlur = (e: React.FocusEvent) => {
// Ignore blur events when moving from search to option to avoid activeOptionIndex conflicts
if (this.containerRef.current!.contains(e.relatedTarget as Node)) return;
if (
((e.relatedTarget as Node)?.firstChild as HTMLElement)?.id ===
this.rootId('listbox')
) {
return;
}

this.setState({
activeOptionIndex: undefined,
Expand Down