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

[Backport 2.x] For selectable component, remove group label and close popover after selection #6536

Merged
merged 1 commit into from
Apr 18, 2024
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -398,89 +398,4 @@ describe('DataSourceSelectable', () => {
expect(onSelectedDataSource).toBeCalledWith([{ id: 'test2', label: 'test2' }]);
expect(onSelectedDataSource).toHaveBeenCalled();
});

it('should render opensearch cluster group label at the top of options, when there are options availiable', async () => {
const onSelectedDataSource = jest.fn();
component = shallow(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
/>
);

component.instance().componentDidMount!();
await nextTick();
const optionsProp = component.find(EuiSelectable).prop('options');
expect(optionsProp[0]).toEqual(dataSourceOptionGroupLabel.opensearchCluster);
});

it('should not render opensearch cluster group label, when there is no option availiable', async () => {
const onSelectedDataSource = jest.fn();
spyOn(utils, 'getDefaultDataSource').and.returnValue([]);
component = shallow(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={false}
fullWidth={false}
/>
);

component.instance().componentDidMount!();
await nextTick();
const optionsProp = component.find(EuiSelectable).prop('options');
expect(optionsProp).toEqual([]);
});

it('should render group lablel normally after onChange', async () => {
const onSelectedDataSource = jest.fn();
component = shallow(
<DataSourceSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={onSelectedDataSource}
disabled={false}
hideLocalCluster={true}
fullWidth={false}
selectedOption={[{ id: 'test1', label: 'test1' }]}
/>
);
const componentInstance = component.instance();

componentInstance.componentDidMount!();
await nextTick();
const optionsPropBefore = component.find(EuiSelectable).prop('options');
expect(optionsPropBefore).toEqual([
dataSourceOptionGroupLabel.opensearchCluster,
{
id: 'test1',
label: 'test1',
checked: 'on',
},
{
id: 'test2',
label: 'test2',
},
{
id: 'test3',
label: 'test3',
},
]);
componentInstance.onChange([
dataSourceOptionGroupLabel.opensearchCluster,
{ id: 'test2', label: 'test2', checked: 'on' },
]);
await nextTick();
const optionsPropAfter = component.find(EuiSelectable).prop('options');
expect(optionsPropAfter).toEqual([
dataSourceOptionGroupLabel.opensearchCluster,
{ id: 'test2', label: 'test2', checked: 'on' },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
ToastsStart,
} from 'opensearch-dashboards/public';
import {
dataSourceOptionGroupLabel,
getDataSourcesWithFields,
getDefaultDataSource,
getFilteredDataSources,
Expand All @@ -30,11 +29,7 @@ import {
import { LocalCluster } from '../data_source_selector/data_source_selector';
import { SavedObject } from '../../../../../core/public';
import { DataSourceAttributes } from '../../types';
import {
DataSourceBaseState,
DataSourceGroupLabelOption,
DataSourceOption,
} from '../data_source_menu/types';
import { DataSourceBaseState, DataSourceOption } from '../data_source_menu/types';
import { DataSourceErrorMenu } from '../data_source_error_menu';
import { DataSourceItem } from '../data_source_item';
import { DataSourceDropDownHeader } from '../drop_down_header';
Expand All @@ -61,12 +56,6 @@ interface DataSourceSelectableState extends DataSourceBaseState {
defaultDataSource: string | null;
}

export const opensearchClusterGroupLabel: DataSourceGroupLabelOption = {
id: 'opensearchClusterGroupLabel',
label: 'OpenSearch cluster',
isGroupLabel: true,
};

export class DataSourceSelectable extends React.Component<
DataSourceSelectableProps,
DataSourceSelectableState
Expand Down Expand Up @@ -214,16 +203,14 @@ export class DataSourceSelectable extends React.Component<

onChange(options: DataSourceOption[]) {
if (!this._isMounted) return;
const optionsWithoutGroupLabel = options.filter(
(option) => !option.hasOwnProperty('isGroupLabel')
);
const selectedDataSource = options.find(({ checked }) => checked);

this.setState({ dataSourceOptions: optionsWithoutGroupLabel });
this.setState({ dataSourceOptions: options });

if (selectedDataSource) {
this.setState({
selectedOption: [selectedDataSource],
isPopoverOpen: false,
});

this.props.onSelectedDataSources([
Expand All @@ -232,16 +219,6 @@ export class DataSourceSelectable extends React.Component<
}
}

getOptionsWithGroupLabel = (dataSourceOptions: DataSourceOption[]): DataSourceOption[] => {
let optionsWithGroupLabel: DataSourceOption[] = [];
if (dataSourceOptions.length === 0) {
optionsWithGroupLabel = [];
} else {
optionsWithGroupLabel = [dataSourceOptionGroupLabel.opensearchCluster, ...dataSourceOptions];
}
return optionsWithGroupLabel;
};

render() {
if (this.state.showError) {
return <DataSourceErrorMenu />;
Expand Down Expand Up @@ -293,7 +270,7 @@ export class DataSourceSelectable extends React.Component<
placeholder: 'Search',
compressed: true,
}}
options={this.getOptionsWithGroupLabel(this.state.dataSourceOptions)}
options={this.state.dataSourceOptions}
onChange={(newOptions) => this.onChange(newOptions)}
singleSelection={true}
data-test-subj={'dataSourceSelectable'}
Expand Down
Loading