Skip to content

Commit d6329b9

Browse files
efreetipaul-tavares
andcommitted
Added component to truncate text programmatically and add tooltips. (#80198)
* Added component to truncate text programmatically and add tooltips. Changed all field text values to use this component. * Fixed build failures. * Switched from FC component to one without children Co-authored-by: Paul Tavares <[email protected]> * Added some docs for the shared component. * Updated snapshots. Co-authored-by: Paul Tavares <[email protected]>
1 parent 71812f7 commit d6329b9

File tree

12 files changed

+2251
-1014
lines changed

12 files changed

+2251
-1014
lines changed

x-pack/plugins/security_solution/public/common/components/item_details_card/__snapshots__/index.test.tsx.snap

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/security_solution/public/common/components/item_details_card/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,13 @@ const DescriptionListDescription = styled(EuiDescriptionListDescription)`
6666
interface ItemDetailsPropertySummaryProps {
6767
name: ReactNode | ReactNode[];
6868
value: ReactNode | ReactNode[];
69-
title?: string;
7069
}
7170

72-
export const ItemDetailsPropertySummary: FC<ItemDetailsPropertySummaryProps> = memo(
73-
({ name, value, title = '' }) => (
71+
export const ItemDetailsPropertySummary = memo<ItemDetailsPropertySummaryProps>(
72+
({ name, value }) => (
7473
<>
7574
<DescriptionListTitle>{name}</DescriptionListTitle>
76-
<DescriptionListDescription>
77-
<span title={title}>{value}</span>
78-
</DescriptionListDescription>
75+
<DescriptionListDescription>{value}</DescriptionListDescription>
7976
</>
8077
)
8178
);

x-pack/plugins/security_solution/public/common/components/text_field_value/__snapshots__/index.test.tsx.snap

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import React from 'react';
7+
import { ThemeProvider } from 'styled-components';
8+
import { storiesOf, addDecorator } from '@storybook/react';
9+
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json';
10+
11+
import { TextFieldValue } from '.';
12+
13+
addDecorator((storyFn) => (
14+
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}>{storyFn()}</ThemeProvider>
15+
));
16+
17+
const longText = [...new Array(20).keys()].map((i) => ` super long text part ${i}`).join(' ');
18+
19+
storiesOf('Components/TextFieldValue', module)
20+
.add('short text, no limit', () => <TextFieldValue fieldName="Field name" value="Small value" />)
21+
.add('short text, with limit', () => (
22+
<TextFieldValue fieldName="Field name" value="Small value" maxLength={100} />
23+
))
24+
.add('long text, no limit', () => <TextFieldValue fieldName="Field name" value={longText} />)
25+
.add('long text, with limit', () => (
26+
<TextFieldValue fieldName="Field name" value={longText} maxLength={100} />
27+
));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { shallow } from 'enzyme';
7+
import React from 'react';
8+
9+
import { TextFieldValue } from '.';
10+
11+
describe('text_field_value', () => {
12+
describe('TextFieldValue', () => {
13+
const longText = [...new Array(20).keys()].map((i) => ` super long text part ${i}`).join(' ');
14+
15+
it('should render small text correctly, when there is no limit', () => {
16+
expect(shallow(<TextFieldValue fieldName="field 1" value="value 1" />)).toMatchSnapshot();
17+
});
18+
19+
it('should render small text correctly, when there is limit', () => {
20+
const element = shallow(
21+
<TextFieldValue fieldName="field 1" value="value 1" maxLength={100} />
22+
);
23+
24+
expect(element).toMatchSnapshot();
25+
});
26+
27+
it('should render long text correctly, when there is no limit', () => {
28+
expect(shallow(<TextFieldValue fieldName="field 1" value={longText} />)).toMatchSnapshot();
29+
});
30+
31+
it('should render long text correctly, when there is limit', () => {
32+
const element = shallow(
33+
<TextFieldValue fieldName="field 1" value={longText} maxLength={100} />
34+
);
35+
36+
expect(element).toMatchSnapshot();
37+
});
38+
});
39+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { EuiFlexGroup, EuiFlexItem, EuiToolTip } from '@elastic/eui';
8+
import React from 'react';
9+
10+
const trimTextOverflow = (text: string, maxLength?: number) => {
11+
if (maxLength !== undefined && text.length > maxLength) {
12+
return `${text.substr(0, maxLength)}...`;
13+
} else {
14+
return text;
15+
}
16+
};
17+
18+
interface Props {
19+
fieldName: string;
20+
value: string;
21+
maxLength?: number;
22+
className?: string;
23+
}
24+
25+
/*
26+
* Component to display text field value. Text field values can be large and need
27+
* programmatic truncation to a fixed text length. As text can be truncated the tooltip
28+
* is shown displaying the field name and full value. If the use case allows single
29+
* line truncation with CSS use eui-textTruncate class on this component instead of
30+
* maxLength property.
31+
*/
32+
export const TextFieldValue = ({ fieldName, value, maxLength, className }: Props) => {
33+
return (
34+
<EuiToolTip
35+
anchorClassName={className}
36+
content={
37+
<EuiFlexGroup data-test-subj="dates-container" direction="column" gutterSize="none">
38+
<EuiFlexItem grow={false}>{fieldName}</EuiFlexItem>
39+
<EuiFlexItem grow={false}>{value}</EuiFlexItem>
40+
</EuiFlexGroup>
41+
}
42+
>
43+
<span>{trimTextOverflow(value, maxLength)}</span>
44+
</EuiToolTip>
45+
);
46+
};

x-pack/plugins/security_solution/public/management/pages/trusted_apps/view/components/trusted_app_card/__snapshots__/index.test.tsx.snap

Lines changed: 54 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)