Skip to content

Commit f664f5a

Browse files
committed
[Search][a11y] show tooltip show more or fewer fields (#237913)
## Summary Tool tip is not shown when navigated via keyboard. Adding ref helps with showing the tool tip when show more/hide button is on focus. ### Testing instructions: 1. In Stateful. Create an Index with documents more than 3 fields. Open documents page and has more than 3 documents 2. Navigate to the first (or any) table while using only keyboard by pressing Tab key. 3. Navigate to Show more fields button. 4. Press Esc. 5. Press Enter. 6. Observe tooltips for the button. Also test for: Same with the opposite: when user navigates to Show more button -> presses Enter -> presses Esc -> presses Enter. Result: no tooltip present on Show more button. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. (cherry picked from commit 37446f0) # Conflicts: # x-pack/platform/packages/shared/kbn-search-index-documents/components/result/result.tsx
1 parent e144237 commit f664f5a

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

  • x-pack/platform/packages/shared/kbn-search-index-documents/components/result
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import React, { useRef, useState } from 'react';
9+
10+
import {
11+
EuiButtonIcon,
12+
EuiFlexGroup,
13+
EuiFlexItem,
14+
EuiHorizontalRule,
15+
EuiSpacer,
16+
EuiSplitPanel,
17+
EuiToolTip,
18+
} from '@elastic/eui';
19+
20+
import { i18n } from '@kbn/i18n';
21+
22+
import { ResultFields } from './results_fields';
23+
24+
import type { MetaDataProps, ResultFieldProps } from './result_types';
25+
import { RichResultHeader } from './rich_result_header';
26+
import { ResultHeader } from './result_header';
27+
28+
export const DEFAULT_VISIBLE_FIELDS = 3;
29+
30+
export interface ResultProps {
31+
fields: ResultFieldProps[];
32+
metaData: MetaDataProps;
33+
defaultVisibleFields?: number;
34+
showScore?: boolean;
35+
compactCard?: boolean;
36+
onDocumentClick?: () => void;
37+
onDocumentDelete?: () => void;
38+
hasDeleteDocumentsPrivilege?: boolean;
39+
}
40+
41+
export const Result: React.FC<ResultProps> = ({
42+
metaData,
43+
fields,
44+
defaultVisibleFields = DEFAULT_VISIBLE_FIELDS,
45+
compactCard = true,
46+
showScore = false,
47+
onDocumentClick,
48+
onDocumentDelete,
49+
hasDeleteDocumentsPrivilege,
50+
}) => {
51+
const [isExpanded, setIsExpanded] = useState(false);
52+
const tooltipText =
53+
fields.length <= defaultVisibleFields
54+
? i18n.translate('xpack.searchIndexDocuments.result.expandTooltip.allVisible', {
55+
defaultMessage: 'All fields are visible',
56+
})
57+
: isExpanded
58+
? i18n.translate('xpack.searchIndexDocuments.result.expandTooltip.showFewer', {
59+
defaultMessage: 'Show {amount} fewer fields',
60+
values: { amount: fields.length - defaultVisibleFields },
61+
})
62+
: i18n.translate('xpack.searchIndexDocuments.result.expandTooltip.showMore', {
63+
defaultMessage: 'Show {amount} more fields',
64+
values: { amount: fields.length - defaultVisibleFields },
65+
});
66+
const toolTipContent = <>{tooltipText}</>;
67+
68+
const showResultsFields = isExpanded ? fields.length > 0 : defaultVisibleFields > 0;
69+
70+
const tooltipRef = useRef<EuiToolTip>(null);
71+
return (
72+
<>
73+
<EuiSplitPanel.Outer hasBorder={true} data-test-subj="search-index-documents-result">
74+
<EuiSplitPanel.Inner paddingSize="m" color="plain" className="resultHeaderContainer">
75+
<EuiFlexGroup gutterSize="none" alignItems="center">
76+
<EuiFlexItem>
77+
{compactCard && (
78+
<ResultHeader
79+
title={
80+
metaData.title ??
81+
i18n.translate('searchIndexDocuments.result.title.id', {
82+
defaultMessage: 'Document id: {id}',
83+
values: { id: metaData.id },
84+
})
85+
}
86+
metaData={metaData}
87+
/>
88+
)}
89+
90+
{!compactCard && (
91+
<RichResultHeader
92+
showScore={showScore}
93+
title={
94+
metaData.title ??
95+
i18n.translate('searchIndexDocuments.result.title.id', {
96+
defaultMessage: 'Document id: {id}',
97+
values: { id: metaData.id },
98+
})
99+
}
100+
onTitleClick={onDocumentClick}
101+
metaData={{
102+
...metaData,
103+
onDocumentDelete,
104+
hasDeleteDocumentsPrivilege,
105+
}}
106+
/>
107+
)}
108+
</EuiFlexItem>
109+
<EuiFlexItem grow={false}>
110+
<EuiToolTip position="left" content={toolTipContent} ref={tooltipRef}>
111+
<EuiButtonIcon
112+
size="xs"
113+
iconType={isExpanded ? 'fold' : 'unfold'}
114+
color={isExpanded ? 'danger' : 'primary'}
115+
data-test-subj={isExpanded ? 'documentShowLessFields' : 'documentShowMoreFields'}
116+
onClick={(e: React.MouseEvent<HTMLElement>) => {
117+
e.stopPropagation();
118+
setIsExpanded(!isExpanded);
119+
tooltipRef.current?.showToolTip();
120+
}}
121+
aria-label={tooltipText}
122+
/>
123+
</EuiToolTip>
124+
</EuiFlexItem>
125+
</EuiFlexGroup>
126+
</EuiSplitPanel.Inner>
127+
{showResultsFields && (
128+
<>
129+
<EuiHorizontalRule margin="none" />
130+
<EuiSplitPanel.Inner paddingSize="m">
131+
<ResultFields
132+
documentId={metaData.id}
133+
isExpanded={isExpanded}
134+
fields={isExpanded ? fields : fields.slice(0, defaultVisibleFields)}
135+
/>
136+
</EuiSplitPanel.Inner>
137+
</>
138+
)}
139+
</EuiSplitPanel.Outer>
140+
{showResultsFields && <EuiSpacer size="s" />}
141+
</>
142+
);
143+
};

0 commit comments

Comments
 (0)