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 searchable single selection selectables not correctly highlighting the checked option on initial render #6072

Merged
merged 4 commits into from
Jul 25, 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
14 changes: 14 additions & 0 deletions src/components/selectable/selectable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ describe('EuiSelectable', () => {
).toEqual('second value');
});

it('calls the searchProps.onChange callback on mount', () => {
const onChange = jest.fn();
mount(
<EuiSelectable
options={options}
searchable
searchProps={{ value: 'pandora', onChange }}
>
{(_, search) => <>{search}</>}
</EuiSelectable>
);
expect(onChange).toHaveBeenCalledWith('pandora', [options[2]]);
});

it('defaults to an empty string if no value or defaultValue is passed from searchProps', () => {
const component = render(
<EuiSelectable
Expand Down
14 changes: 6 additions & 8 deletions src/components/selectable/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export class EuiSelectable<T = {}> extends Component<
initialSearchValue,
isPreFiltered
);
searchProps?.onChange?.(initialSearchValue, visibleOptions);

// ensure that the currently selected single option is active if it is in the visibleOptions
const selectedOptions = options.filter((option) => option.checked);
Expand Down Expand Up @@ -277,10 +278,6 @@ export class EuiSelectable<T = {}> extends Component<
}
}

hasActiveOption = () => {
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.
Expand All @@ -294,7 +291,10 @@ export class EuiSelectable<T = {}> extends Component<
return;
}

if (!this.state.visibleOptions.length || this.state.activeOptionIndex) {
if (
!this.state.visibleOptions.length ||
this.state.activeOptionIndex != null
) {
return;
}

Expand Down Expand Up @@ -436,9 +436,7 @@ export class EuiSelectable<T = {}> extends Component<
}
}
);
if (this.props.searchProps && this.props.searchProps.onChange) {
this.props.searchProps.onChange(searchValue, visibleOptions);
}
this.props.searchProps?.onChange?.(searchValue, visibleOptions);
};

onContainerBlur = (e: React.FocusEvent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { render, mount, shallow } from 'enzyme';
import { render, shallow } from 'enzyme';
import { requiredProps } from '../../../test/required_props';

import { EuiSelectableSearch } from './selectable_search';
Expand Down Expand Up @@ -50,11 +50,6 @@ describe('EuiSelectableSearch', () => {
expect(component).toMatchSnapshot();
});

it('calls the onChange callback on mount', () => {
mount(<EuiSelectableSearch {...baseProps} value="w" />);
expect(onChange).toHaveBeenCalledWith('w', [{ label: 'world' }]);
});

it("calls the onChange callback when the EuiFieldSearch's change event fires", () => {
const component = shallow(<EuiSelectableSearch {...baseProps} />);
component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import React, { useEffect, useCallback, ChangeEvent } from 'react';
import React, { useCallback, ChangeEvent } from 'react';
import classNames from 'classnames';
import { CommonProps } from '../../common';
import { EuiFieldSearch, EuiFieldSearchProps } from '../../form';
Expand Down Expand Up @@ -52,16 +52,6 @@ export const EuiSelectableSearch = <T,>({
className,
...rest
}: _EuiSelectableSearchProps<T>) => {
useEffect(() => {
const matchingOptions = getMatchingOptions<T>(
options,
value,
isPreFiltered
);
onChangeCallback(value, matchingOptions);
// Call on mount only
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const onChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
const searchValue = e.target.value;
Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/6072.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed searchable single selection `EuiSelectable`s not correctly highlighting the checked option on initial render