Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { EuiFlexGroup, EuiFlexItem, EuiLink, EuiToolTip } from '@elastic/eui';

import { i18n } from '@kbn/i18n';
import React from 'react';
import React, { useEffect, useRef } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import useToggle from 'react-use/lib/useToggle';
import type { Field } from '../tabs/metadata/utils';
Expand All @@ -19,24 +19,39 @@ interface ExpandableContentProps {
export const ExpandableContent = (props: ExpandableContentProps) => {
const { values } = props;
const [isExpanded, toggle] = useToggle(false);
const showLessRef = useRef<HTMLAnchorElement | null>(null);
const showMoreRef = useRef<HTMLAnchorElement | null>(null);

const list = Array.isArray(values) ? values : [values];
const [first, ...others] = list;
const hasOthers = others.length > 0;
const shouldShowMore = hasOthers && !isExpanded;
const hasInteracted = useRef(false);

const handleToggle = () => {
hasInteracted.current = true;
toggle();
};

useEffect(() => {
if (!hasInteracted.current) return;

if (isExpanded) {
showLessRef.current?.focus();
} else {
showMoreRef.current?.focus();
}
}, [isExpanded]);

return (
<EuiFlexGroup gutterSize="xs" responsive={false} alignItems="baseline" wrap direction="column">
<EuiFlexItem className="eui-textTruncate">
<EuiToolTip delay="long" content={first}>
<p className="eui-textTruncate">{first}</p>
</EuiToolTip>
{shouldShowMore && (
<>
{' ... '}
<EuiLink
data-test-subj="infraAssetDetailsExpandableContentCountMoreLink"
onClick={toggle}
onClick={handleToggle}
ref={showMoreRef}
>
<FormattedMessage
id="xpack.infra.assetDetails.tabs.metadata.seeMore"
Expand All @@ -48,17 +63,24 @@ export const ExpandableContent = (props: ExpandableContentProps) => {
</EuiLink>
</>
)}
{hasOthers && isExpanded && (
<EuiFlexItem>
<EuiLink
data-test-subj="infraExpandableContentShowLessLink"
onClick={handleToggle}
ref={showLessRef}
>
{i18n.translate('xpack.infra.assetDetails.tabs.metadata.seeLess', {
defaultMessage: 'Show less',
})}
</EuiLink>
</EuiFlexItem>
)}
<EuiToolTip delay="long" content={first}>
<p className="eui-textTruncate">{first}</p>
</EuiToolTip>
</EuiFlexItem>
{isExpanded && others.map((item, index) => <EuiFlexItem key={index}>{item}</EuiFlexItem>)}
{hasOthers && isExpanded && (
<EuiFlexItem>
<EuiLink data-test-subj="infraExpandableContentShowLessLink" onClick={toggle}>
{i18n.translate('xpack.infra.assetDetails.tabs.metadata.seeLess', {
defaultMessage: 'Show less',
})}
</EuiLink>
</EuiFlexItem>
)}
</EuiFlexGroup>
);
};