Skip to content

Commit

Permalink
[EuiSelectable] Prevent onFocus from negating selection (#5117)
Browse files Browse the repository at this point in the history
* pass initial click event through to option

* revert me

* CL
  • Loading branch information
thompsongl authored Sep 1, 2021
1 parent c5bf1c8 commit 005f9e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Bug fixes**

- Fixed `EuiSuperSelect`'s focus keyboard behavior when no initial value is passed, and focus label behavior ([#5097](https://github.com/elastic/eui/pull/5097))
- Fixed `EuiSelectable` sometimes requiring two clicks to change selection ([#5117](https://github.com/elastic/eui/pull/5117))

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

Expand Down
2 changes: 1 addition & 1 deletion src-docs/src/views/selectable/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const Options: EuiSelectableOption[] = [
},
{
label: 'Mimas',
checked: 'on',
// checked: 'on',
},
{
label: 'Dione',
Expand Down
15 changes: 8 additions & 7 deletions src-docs/src/views/selectable/selectable_popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
EuiSpacer,
EuiTitle,
} from '../../../../src/components';
import { Comparators } from '../../../../src/services/sort';
// import { Comparators } from '../../../../src/services/sort';

import { Options } from './data';
import { createDataStore } from '../tables/data_store';
Expand All @@ -32,7 +32,7 @@ export default () => {
const [isFlyoutVisible, setIsFlyoutVisible] = useState(false);

const onButtonClick = () => {
setOptions(options.slice().sort(Comparators.property('checked')));
// setOptions(options.slice().sort(Comparators.property('checked')));
setIsPopoverOpen(!isPopoverOpen);
};

Expand Down Expand Up @@ -72,11 +72,12 @@ export default () => {
closePopover={closePopover}
>
<EuiSelectable
searchable
searchProps={{
placeholder: 'Filter list',
compressed: true,
}}
// searchable
singleSelection={true}
// searchProps={{
// placeholder: 'Filter list',
// compressed: true,
// }}
options={options}
onChange={onChange}
>
Expand Down
14 changes: 14 additions & 0 deletions src/components/selectable/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class EuiSelectable<T = {}> extends Component<
};
private containerRef = createRef<HTMLDivElement>();
private optionsListRef = createRef<EuiSelectableList<T>>();
private preventOnFocus = false;
rootId = htmlIdGenerator();
constructor(props: EuiSelectableProps<T>) {
super(props);
Expand Down Expand Up @@ -212,7 +213,19 @@ export class EuiSelectable<T = {}> extends Component<
return this.state.activeOptionIndex != null;
};

onMouseDown = () => {
// Bypass onFocus when a click event originates from this.containerRef.
// Prevents onFocus from scrolling away from a clicked option and negating the selection event.
// https://github.com/elastic/eui/issues/4147
this.preventOnFocus = true;
};

onFocus = () => {
if (this.preventOnFocus) {
this.preventOnFocus = false;
return;
}

if (!this.state.visibleOptions.length || this.state.activeOptionIndex) {
return;
}
Expand Down Expand Up @@ -618,6 +631,7 @@ export class EuiSelectable<T = {}> extends Component<
onKeyDown={this.onKeyDown}
onBlur={this.onContainerBlur}
onFocus={this.onFocus}
onMouseDown={this.onMouseDown}
{...rest}
>
{children && children(list, search)}
Expand Down

0 comments on commit 005f9e3

Please sign in to comment.