Skip to content

Commit

Permalink
[8.x] [Search][Connectors] Choose connector selectable custom icon (#…
Browse files Browse the repository at this point in the history
…197068) (#197440)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Search][Connectors] Choose connector selectable custom icon
(#197068)](#197068)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"José Luis
González","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-23T13:31:01Z","message":"[Search][Connectors]
Choose connector selectable custom icon (#197068)\n\n##
Summary\r\n\r\nThis PR shows the connectors selected icon when choosing
one from the\r\n`<ChooseConnectorSelectable/>` component.\r\nAt the same
time the selectedConnector was not updated when clearing
the\r\nselection. Now it updated this state and the related UI elements
like\r\nthe left doc links and the footer last block changed their state
based\r\non this.\r\n\r\n![CleanShot 2024-10-21 at 15
48\r\n58](https://github.com/user-attachments/assets/ac76c44a-7562-4f5a-adf0-4a41d70bad46)\r\n\r\n\r\n\r\n\r\n\r\n-
[x] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"d081884cb5eeba64a4d035c1bd01a5f809db0204","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","v9.0.0","Team:Search","v8.16.0","backport:version","v8.17.0"],"title":"[Search][Connectors]
Choose connector selectable custom
icon","number":197068,"url":"https://github.com/elastic/kibana/pull/197068","mergeCommit":{"message":"[Search][Connectors]
Choose connector selectable custom icon (#197068)\n\n##
Summary\r\n\r\nThis PR shows the connectors selected icon when choosing
one from the\r\n`<ChooseConnectorSelectable/>` component.\r\nAt the same
time the selectedConnector was not updated when clearing
the\r\nselection. Now it updated this state and the related UI elements
like\r\nthe left doc links and the footer last block changed their state
based\r\non this.\r\n\r\n![CleanShot 2024-10-21 at 15
48\r\n58](https://github.com/user-attachments/assets/ac76c44a-7562-4f5a-adf0-4a41d70bad46)\r\n\r\n\r\n\r\n\r\n\r\n-
[x] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"d081884cb5eeba64a4d035c1bd01a5f809db0204"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197068","number":197068,"mergeCommit":{"message":"[Search][Connectors]
Choose connector selectable custom icon (#197068)\n\n##
Summary\r\n\r\nThis PR shows the connectors selected icon when choosing
one from the\r\n`<ChooseConnectorSelectable/>` component.\r\nAt the same
time the selectedConnector was not updated when clearing
the\r\nselection. Now it updated this state and the related UI elements
like\r\nthe left doc links and the footer last block changed their state
based\r\non this.\r\n\r\n![CleanShot 2024-10-21 at 15
48\r\n58](https://github.com/user-attachments/assets/ac76c44a-7562-4f5a-adf0-4a41d70bad46)\r\n\r\n\r\n\r\n\r\n\r\n-
[x] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[x] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"d081884cb5eeba64a4d035c1bd01a5f809db0204"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: José Luis González <[email protected]>
  • Loading branch information
kibanamachine and JoseLuisGJ authored Oct 23, 2024
1 parent 4eb4ada commit 35686d8
Showing 1 changed file with 63 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
* 2.0.
*/

import React, { useCallback, useEffect, useMemo, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';

import { useActions, useValues } from 'kea';

import {
EuiBadge,
EuiFlexItem,
EuiIcon,
EuiInputPopover,
EuiSelectable,
EuiSelectableOption,
EuiComboBox,
EuiComboBoxOptionOption,
EuiFlexGroup,
EuiText,
useEuiTheme,
} from '@elastic/eui';

import { i18n } from '@kbn/i18n';

import connectorLogo from '../../../../../../assets/images/connector.svg';
import { KibanaLogic } from '../../../../../shared/kibana';
import { NewConnectorLogic } from '../../../new_index/method_connector/new_connector_logic';
import { SelfManagePreference } from '../create_connector';
Expand All @@ -36,9 +38,39 @@ export const ChooseConnectorSelectable: React.FC<ChooseConnectorSelectableProps>
selfManaged,
}) => {
const { euiTheme } = useEuiTheme();
const [isOpen, setIsOpen] = useState(false);
const [selectedOption, setSelectedOption] = useState<Array<EuiComboBoxOptionOption<OptionData>>>(
[]
);
const renderOption = (
option: EuiComboBoxOptionOption<OptionData>,
searchValue: string,
contentClassName: string
) => {
const { _append, key, label, _prepend } = option as EuiComboBoxOptionOption<OptionData> & {
_append: JSX.Element[];
_prepend: JSX.Element;
};
return (
<EuiFlexGroup
gutterSize="m"
key={key + '-span'}
justifyContent="spaceBetween"
className={contentClassName}
>
<EuiFlexGroup gutterSize="m">
<EuiFlexItem grow={false}>{_prepend}</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText size="s" textAlign="left">
{label}
</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
<EuiFlexItem grow={false}>{_append}</EuiFlexItem>
</EuiFlexGroup>
);
};
const [selectableOptions, selectableSetOptions] = useState<
Array<EuiSelectableOption<OptionData>>
Array<EuiComboBoxOptionOption<OptionData>>
>([]);
const { connectorTypes } = useValues(KibanaLogic);
const allConnectors = useMemo(
Expand All @@ -50,9 +82,9 @@ export const ChooseConnectorSelectable: React.FC<ChooseConnectorSelectableProps>

const getInitialOptions = () => {
return allConnectors.map((connector, key) => {
const append: JSX.Element[] = [];
const _append: JSX.Element[] = [];
if (connector.isTechPreview) {
append.push(
_append.push(
<EuiBadge key={key + '-preview'} iconType="beaker" color="hollow">
{i18n.translate(
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.thechPreviewBadgeLabel',
Expand All @@ -62,7 +94,7 @@ export const ChooseConnectorSelectable: React.FC<ChooseConnectorSelectableProps>
);
}
if (connector.isBeta) {
append.push(
_append.push(
<EuiBadge key={key + '-beta'} iconType={'beta'} color="hollow">
{i18n.translate(
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.BetaBadgeLabel',
Expand All @@ -74,7 +106,7 @@ export const ChooseConnectorSelectable: React.FC<ChooseConnectorSelectableProps>
);
}
if (selfManaged === 'native' && !connector.isNative) {
append.push(
_append.push(
<EuiBadge key={key + '-self'} iconType={'warning'} color="warning">
{i18n.translate(
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.OnlySelfManagedBadgeLabel',
Expand All @@ -85,12 +117,11 @@ export const ChooseConnectorSelectable: React.FC<ChooseConnectorSelectableProps>
</EuiBadge>
);
}

return {
append,
_append,
_prepend: <EuiIcon size="l" type={connector.iconPath} />,
key: key.toString(),
label: connector.name,
prepend: <EuiIcon size="l" type={connector.iconPath} />,
};
});
};
Expand All @@ -100,73 +131,35 @@ export const ChooseConnectorSelectable: React.FC<ChooseConnectorSelectableProps>
useEffect(() => {
selectableSetOptions(initialOptions);
}, [selfManaged]);
const [searchValue, setSearchValue] = useState('');

const openPopover = useCallback(() => {
setIsOpen(true);
}, []);
const closePopover = useCallback(() => {
setIsOpen(false);
}, []);

return (
<EuiFlexItem>
<EuiSelectable
<EuiComboBox
aria-label={i18n.translate(
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.euiSelectable.selectableInputPopoverLabel',
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.euiComboBox.accessibleScreenReaderLabelLabel',
{ defaultMessage: 'Select a data source for your connector to use.' }
)}
prepend={<EuiIcon type={selectedConnector?.iconPath ?? connectorLogo} size="l" />}
singleSelection
fullWidth
placeholder={i18n.translate(
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.placeholder.text',
{ defaultMessage: 'Choose a data source' }
)}
options={selectableOptions}
onChange={(newOptions, _, changedOption) => {
selectableSetOptions(newOptions);
closePopover();
if (changedOption.checked === 'on') {
const keySelected = Number(changedOption.key);
setSelectedConnector(allConnectors[keySelected]);
setSearchValue(allConnectors[keySelected].name);
} else {
selectedOptions={selectedOption}
onChange={(selectedItem) => {
setSelectedOption(selectedItem);
if (selectedItem.length === 0) {
setSelectedConnector(null);
setSearchValue('');
return;
}
const keySelected = Number(selectedItem[0].key);
setSelectedConnector(allConnectors[keySelected]);
}}
listProps={{
isVirtualized: true,
rowHeight: Number(euiTheme.base * 3),
showIcons: false,
}}
singleSelection
searchable
searchProps={{
fullWidth: true,
isClearable: true,
onChange: (value) => {
if (value !== selectedConnector?.name) {
setSearchValue(value);
}
},
onClick: openPopover,
onFocus: openPopover,
placeholder: i18n.translate(
'xpack.enterpriseSearch.createConnector.chooseConnectorSelectable.placeholder.text',
{ defaultMessage: 'Choose a data source' }
),
value: searchValue,
}}
>
{(list, search) => (
<EuiInputPopover
fullWidth
closePopover={closePopover}
disableFocusTrap
closeOnScroll
isOpen={isOpen}
input={search!}
panelPaddingSize="none"
>
{list}
</EuiInputPopover>
)}
</EuiSelectable>
renderOption={renderOption}
rowHeight={(euiTheme.base / 2) * 5}
/>
</EuiFlexItem>
);
};

0 comments on commit 35686d8

Please sign in to comment.