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
Expand Up @@ -3,10 +3,12 @@
- Improved `EuiButtonGroup` focus, hover, selected and disabled states ([#4142](https://github.com/elastic/eui/pull/4142))
- Added `display` prop to `EuiToolTip` for common display block needs ([#4148](https://github.com/elastic/eui/pull/4148))
- Added support for more colors in `EuiProgress` such as `vis0` through `vis9`, `warning`, `success` and custom colors ([#4130](https://github.com/elastic/eui/pull/4130))
- Added `labelWidth` and `descriptionDisplay` props to `EuiSuggestItem` ([#4180](https://github.com/elastic/eui/pull/4180))

**Bug fixes**

- Fixed issue with duplicate checkmarks in `EuiComboBox` ([#4162](https://github.com/elastic/eui/pull/4162))
- Fixed issue with `labelDisplay` not being passed to `EuiSuggestItem` ([#4180](https://github.com/elastic/eui/pull/4180))

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

Expand Down
6 changes: 5 additions & 1 deletion src-docs/src/views/suggest/suggest_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,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--fixed euiSuggestItem__labelDisplay--expand euiSuggestItem__labelWidth50"
>
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__labelWidth50"
>
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--fixed euiSuggestItem__labelDisplay--expand euiSuggestItem__labelWidth50"
>
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__labelWidth30"
>
This is the description
</span>
<span
class="euiSuggestItem__description euiSuggestItem__description--truncate"
>
This is the description
</span>
Expand Down
30 changes: 22 additions & 8 deletions src/components/suggest/_suggest_item.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,44 @@
}

.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__labelWidth#{$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;
}

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

&:empty {
flex-grow: 0;
Expand Down
3 changes: 3 additions & 0 deletions src/components/suggest/suggest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export const EuiSuggest: FunctionComponent<EuiSuggestProps> = (
type={item.type}
key={index}
label={item.label}
labelDisplay={item.labelDisplay}
descriptionDisplay={item.descriptionDisplay}
labelWidth={item.labelWidth}
onClick={onItemClick ? () => onItemClick(item) : undefined}
description={item.description}
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
/>
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
39 changes: 36 additions & 3 deletions src/components/suggest/suggest_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ interface EuiSuggestItemPropsBase {
* Label display is 'fixed' by default. Label will increase its width beyond 50% if needed with 'expand'.
*/
labelDisplay?: keyof typeof labelDisplayToClassMap;

/**
* With of 'label' when 'labelDisplay' is set to 'fixed'. Defaults to 50. Any value from 20 to 90 with increases of 10.
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
*/
labelWidth?: LabelWidthSize;

/**
* Set the way in which 'description' is displayed, defaults to 'truncate'.
*/
descriptionDisplay?: keyof typeof descriptionDisplayToClassMap;
}

type PropsForDiv = Omit<HTMLAttributes<HTMLDivElement>, 'onClick'>;
Expand Down Expand Up @@ -81,6 +91,16 @@ interface ColorToClassMap {
[key: string]: string;
}

export type LabelWidthSize =
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
| '20'
| '30'
| '40'
| '50'
| '60'
| '70'
| '80'
| '90';

const colorToClassNameMap: ColorToClassMap = {
tint0: 'euiSuggestItem__type--tint0',
tint1: 'euiSuggestItem__type--tint1',
Expand All @@ -102,14 +122,21 @@ const labelDisplayToClassMap = {
expand: 'euiSuggestItem__labelDisplay--expand',
};

const descriptionDisplayToClassMap = {
truncate: 'euiSuggestItem__description--truncate',
wrap: 'euiSuggestItem__description--wrap',
};

export const DISPLAYS = keysOf(labelDisplayToClassMap);

export const EuiSuggestItem: FunctionComponent<EuiSuggestItemProps> = ({
className,
label,
type,
labelDisplay = 'fixed',
labelWidth = '50',
description,
descriptionDisplay = 'truncate',
onClick,
...rest
}) => {
Expand All @@ -123,14 +150,20 @@ export const EuiSuggestItem: FunctionComponent<EuiSuggestItemProps> = ({

let colorClass = '';

const labelDisplayClass = classNames(
const labelClassNames = classNames(
'euiSuggestItem__label',
labelDisplayToClassMap[labelDisplay],
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
{
'euiSuggestItem__labelDisplay--expand': !description,
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
[`euiSuggestItem__labelWidth${labelWidth}`]: labelDisplay === 'fixed',
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
}
);

const descriptionClassNames = classNames(
'euiSuggestItem__description',
descriptionDisplayToClassMap[descriptionDisplay]
);

if (type && type.color) {
if (COLORS.indexOf(type.color as string) > -1) {
colorClass = colorToClassNameMap[type.color];
Expand All @@ -142,8 +175,8 @@ export const EuiSuggestItem: FunctionComponent<EuiSuggestItemProps> = ({
<span className={`euiSuggestItem__type ${colorClass}`}>
<EuiIcon type={type.iconType} />
</span>
<span className={labelDisplayClass}>{label}</span>
<span className="euiSuggestItem__description">{description}</span>
<span className={labelClassNames}>{label}</span>
<span className={descriptionClassNames}>{description}</span>
andreadelrio marked this conversation as resolved.
Show resolved Hide resolved
</React.Fragment>
);

Expand Down