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

[EuiSuggest] Fix labelDisplay and add labelWidth and descriptionDisplay props #4180

Merged
merged 10 commits into from
Oct 29, 2020
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added more exports for `EuiInMemoryTable` types ([#4179](https://github.com/elastic/eui/pull/4179))
- Added `labelWidth` and `descriptionDisplay` props to `EuiSuggestItem` ([#4180](https://github.com/elastic/eui/pull/4180))

**Bug fixes**

- Removed unnecessary shadow on hover of `EuiButtonGroup` ([#4186](https://github.com/elastic/eui/pull/4186))
- Fixed position of `EuiScreenReaderOnly` elements within `EuiButtonGroup` ([#4189](https://github.com/elastic/eui/pull/4189))
- Fixed issue with `labelDisplay` not being passed to `EuiSuggestItem` ([#4180](https://github.com/elastic/eui/pull/4180))

## [`30.1.0`](https://github.com/elastic/eui/tree/v30.1.0)

Expand Down
18 changes: 17 additions & 1 deletion src-docs/src/views/suggest/suggest_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const suggestItemSnippet = [
label={sampleItem.label}
description={sampleItem.description}
labelDisplay="expand"
/>`,
`<EuiSuggestItem
type={sampleItem.type}
label={sampleItem.label}
description={sampleItem.description}
labelWidth="30"
/>`,
`<EuiSuggestItem
type={sampleItem.type}
label={sampleItem.label}
description={sampleItem.description}
descriptionDisplay="wrap"
/>`,
];

Expand Down Expand Up @@ -106,7 +118,11 @@ export const SuggestExample = {
<strong>EuiSuggestItem</strong> is a list item component to display
suggestions when typing queries in <strong>EuiSuggest</strong>. Use{' '}
<EuiCode>labelDisplay</EuiCode> to set whether the{' '}
<EuiCode>label</EuiCode> has a fixed width or not.
<EuiCode>label</EuiCode> has a fixed width or not. By default, fixed
labels will have a width of 50%, you can adjust this by setting{' '}
<EuiCode>labelWidth</EuiCode>. Use{' '}
<EuiCode>descriptionDisplay</EuiCode> to set whether the{' '}
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
<EuiCode>description</EuiCode> truncates or wraps.
</p>
</div>
),
Expand Down
50 changes: 48 additions & 2 deletions src-docs/src/views/suggest/suggest_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { EuiSuggestItem, EuiSpacer } from '../../../../src/components';

const shortDescription = 'This is the description';

const longDescription =
'This is a long description. Fusce euismod dui eu metus sagittis molestie.';

const sampleItems = [
{
type: { iconType: 'kqlField', color: 'tint5' },
Expand Down Expand Up @@ -35,13 +38,34 @@ const sampleItems = [
},
];

const sampleItems2 = [
{
type: { iconType: 'kqlField', color: 'tint5' },
label: 'Field sample with label at 30%',
labelWidth: 30,
description: shortDescription,
},
{
type: { iconType: 'kqlField', color: 'tint5' },
label: 'Field sample with label at 50%',
labelWidth: 50,
description: shortDescription,
},
{
type: { iconType: 'kqlField', color: 'tint5' },
label: 'Field sample with label at 80%',
labelWidth: 80,
description: shortDescription,
},
];

const typeObj = { iconType: 'kqlValue', color: 'tint0' };

const longLabel =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ut quam eget augue pulvinar.';
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ut quam.';

export default () => (
<div>
<div style={{ maxWidth: '800px' }}>
{sampleItems.map((item, index) => (
<EuiSuggestItem
type={item.type}
Expand All @@ -67,5 +91,27 @@ export default () => (
type={{ iconType: 'search', color: 'tint10' }}
label="Items with no description will expand their label"
/>
<EuiSpacer size="m" />
{sampleItems2.map((item, index) => (
<EuiSuggestItem
type={item.type}
key={index}
labelWidth={item.labelWidth}
label={item.label}
description={item.description}
/>
))}
<EuiSpacer size="m" />
<EuiSuggestItem
type={typeObj}
label="Item with a description that truncates"
description={longDescription}
/>
<EuiSuggestItem
type={typeObj}
label="Item with a description that wraps"
description={longDescription}
descriptionDisplay="wrap"
/>
</div>
);
58 changes: 53 additions & 5 deletions src/components/suggest/__snapshots__/suggest_item.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,40 @@ exports[`EuiSuggestItem is rendered 1`] = `
/>
</span>
<span
class="euiSuggestItem__label euiSuggestItem__labelDisplay--fixed euiSuggestItem__labelDisplay--expand"
class="euiSuggestItem__label euiSuggestItem__labelDisplay--expand euiSuggestItem__label--width50"
>
Test label
</span>
<span
class="euiSuggestItem__description"
class="euiSuggestItem__description euiSuggestItem__description--truncate"
/>
</div>
`;

exports[`props descriptionDisplay as wrap is rendered 1`] = `
<div
class="euiSuggestItem"
>
<span
class="euiSuggestItem__type euiSuggestItem__type--tint2"
>
<span
data-euiicon-type="kqlValue"
/>
</span>
<span
class="euiSuggestItem__label euiSuggestItem__labelDisplay--fixed euiSuggestItem__label--width50"
>
This is the description
</span>
<span
class="euiSuggestItem__description euiSuggestItem__description--wrap"
>
This is the description
</span>
</div>
`;

exports[`props item with no description has expanded label is rendered 1`] = `
<div
class="euiSuggestItem"
Expand All @@ -36,12 +60,12 @@ exports[`props item with no description has expanded label is rendered 1`] = `
/>
</span>
<span
class="euiSuggestItem__label euiSuggestItem__labelDisplay--fixed euiSuggestItem__labelDisplay--expand"
class="euiSuggestItem__label euiSuggestItem__labelDisplay--expand euiSuggestItem__label--width50"
>
This is the description
</span>
<span
class="euiSuggestItem__description"
class="euiSuggestItem__description euiSuggestItem__description--truncate"
/>
</div>
`;
Expand All @@ -63,7 +87,31 @@ exports[`props labelDisplay as expand is rendered 1`] = `
This is the description
</span>
<span
class="euiSuggestItem__description"
class="euiSuggestItem__description euiSuggestItem__description--truncate"
>
This is the description
</span>
</div>
`;

exports[`props labelWidth is 30% is rendered 1`] = `
<div
class="euiSuggestItem"
>
<span
class="euiSuggestItem__type euiSuggestItem__type--tint2"
>
<span
data-euiicon-type="kqlValue"
/>
</span>
<span
class="euiSuggestItem__label euiSuggestItem__labelDisplay--fixed euiSuggestItem__label--width30"
>
This is the description
</span>
<span
class="euiSuggestItem__description euiSuggestItem__description--truncate"
>
This is the description
</span>
Expand Down
31 changes: 23 additions & 8 deletions src/components/suggest/_suggest_item.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,45 @@
}

.euiSuggestItem__label {
flex-basis: 50%;
min-width: 50%;
@include euiTextTruncate;
font-family: $euiCodeFontFamily;
overflow: hidden;
text-overflow: ellipsis;
padding: $euiSizeXS $euiSizeS;
color: $euiTextColor;
display: block;

// Sets labelWidth from 20% to 90% in increments of 10%
@for $i from 20 through 90 {
&.euiSuggestItem__label--width#{$i} {
flex-basis: $i * 1%;
min-width: $i * 1%;
$i: $i + 10;
}
}

&.euiSuggestItem__labelDisplay--expand {
flex-basis: auto;
flex-shrink: 1;
}
}

.euiSuggestItem__description,
.euiSuggestItem__label {
@include euiTextTruncate;
display: block;
}

.euiSuggestItem__description {
color: $euiColorDarkShade;
flex-basis: auto;
padding-top: $euiSizeXS * .5;
display: block;

&.euiSuggestItem__description--wrap {
@include euiTextBreakWord;
white-space: normal;
line-height: $euiSizeM + 2px;
}

&.euiSuggestItem__description--truncate {
@include euiTextTruncate;
line-height: $euiLineHeight;
}

&:empty {
flex-grow: 0;
Expand Down
16 changes: 7 additions & 9 deletions src/components/suggest/suggest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ export const EuiSuggest: FunctionComponent<EuiSuggestProps> = (
onInputChange ? onInputChange(e.target) : null;
};

const suggestionList = suggestions.map((item: EuiSuggestItemProps, index) => (
<EuiSuggestItem
type={item.type}
key={index}
label={item.label}
onClick={onItemClick ? () => onItemClick(item) : undefined}
description={item.description}
/>
));
const suggestionList = suggestions.map((item: EuiSuggestItemProps, index) => {
const props = { ...item };
if (onItemClick) {
props.onClick = () => onItemClick(item);
}
return <EuiSuggestItem key={index} {...props} />;
});

const suggestInput = (
<EuiSuggestInput
Expand Down
28 changes: 28 additions & 0 deletions src/components/suggest/suggest_item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ describe('props', () => {
});
});

describe('descriptionDisplay as wrap', () => {
test('is rendered', () => {
const component = render(
<EuiSuggestItem
type={sampleItem.type}
description={sampleItem.description}
label={sampleItem.description}
descriptionDisplay="wrap"
/>
);
expect(component).toMatchSnapshot();
});
});

describe('labelWidth is 30%', () => {
test('is rendered', () => {
const component = render(
<EuiSuggestItem
type={sampleItem.type}
description={sampleItem.description}
label={sampleItem.description}
labelWidth="30"
/>
);
expect(component).toMatchSnapshot();
});
});

describe('item with no description has expanded label', () => {
test('is rendered', () => {
const component = render(
Expand Down
Loading