Skip to content

Commit

Permalink
Modus Chip - Added max-width to handle text overflow (#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElishaSamPeterPrabhu authored Nov 7, 2023
1 parent 2903c47 commit afcdc4e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
17 changes: 17 additions & 0 deletions stencil-workspace/src/components/modus-chip/modus-chip.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ describe('modus-chip', () => {
expect(await shadowValue.textContent).toEqual('Hello');
});

it('renders changes to max-width', async () => {
const page = await newE2EPage();

await page.setContent('<modus-chip></modus-chip>');
const chip = await page.find('modus-chip');
expect(await chip.getProperty('maxWidth')).toBeFalsy();

chip.setProperty('maxWidth', '100px'); // Set the max-width property
await page.waitForChanges();
expect(await chip.getProperty('maxWidth')).toEqual('100px');

const element = await page.find('modus-chip >>> span');

const computedStyle = await element.getComputedStyle();
expect(computedStyle['maxWidth']).toEqual('100px');
});

it('emits chip click event on chip click', async () => {
const page = await newE2EPage();

Expand Down
4 changes: 4 additions & 0 deletions stencil-workspace/src/components/modus-chip/modus-chip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@
}

span {
display: inline-block;
font-size: $rem-13px;
font-weight: $chip-font-weight;
margin: 0 $rem-4px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

svg:not(.icon-remove) path {
Expand Down
11 changes: 9 additions & 2 deletions stencil-workspace/src/components/modus-chip/modus-chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class ModusChip {
/** (optional) The chip's value. */
@Prop() value: string;

/** (optional) Maximum width for the Chip's text and shows ellipsis when truncated */
@Prop() maxWidth: string;

/** An event that fires on chip click. */
@Event() chipClick: EventEmitter;

Expand Down Expand Up @@ -95,7 +98,11 @@ export class ModusChip {
${!this.showCheckmark && !this.imageUrl ? 'no-left-icon' : null}
${!this.showClose ? 'no-right-icon' : null}
`;

const style = {
style: {
'max-width': this.maxWidth ?? undefined,
},
};
return (
<div
aria-disabled={this.disabled ? 'true' : undefined}
Expand All @@ -108,7 +115,7 @@ export class ModusChip {
) : this.showCheckmark ? (
<IconCheck size={'16'}></IconCheck>
) : null}
<span>{this.value}</span>
<span {...style}>{this.value}</span>
{this.showClose ? (
<IconRemove onClick={this.disabled ? null : (event) => this.onCloseClick(event)} size={'16'}></IconRemove>
) : null}
Expand Down
23 changes: 12 additions & 11 deletions stencil-workspace/src/components/modus-chip/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

## Properties

| Property | Attribute | Description | Type | Default |
| --------------- | ---------------- | ------------------------------------------ | ---------------------- | ----------- |
| `ariaLabel` | `aria-label` | (optional) The chip's aria-label. | `string` | `undefined` |
| `chipStyle` | `chip-style` | (optional) The chip's style. | `"outline" \| "solid"` | `'solid'` |
| `disabled` | `disabled` | (optional) Whether the chip is disabled. | `boolean` | `false` |
| `hasError` | `has-error` | (optional) Whether the chip has an error. | `boolean` | `false` |
| `imageUrl` | `image-url` | (optional) The image's url. | `string` | `undefined` |
| `showCheckmark` | `show-checkmark` | (optional) Whether to show the checkmark. | `boolean` | `false` |
| `showClose` | `show-close` | (optional) Whether to show the close icon. | `boolean` | `false` |
| `size` | `size` | (optional) The chip's size. | `"medium" \| "small"` | `'medium'` |
| `value` | `value` | (optional) The chip's value. | `string` | `undefined` |
| Property | Attribute | Description | Type | Default |
| --------------- | ---------------- | ------------------------------------------------------------------------------ | ---------------------- | ----------- |
| `ariaLabel` | `aria-label` | (optional) The chip's aria-label. | `string` | `undefined` |
| `chipStyle` | `chip-style` | (optional) The chip's style. | `"outline" \| "solid"` | `'solid'` |
| `disabled` | `disabled` | (optional) Whether the chip is disabled. | `boolean` | `false` |
| `hasError` | `has-error` | (optional) Whether the chip has an error. | `boolean` | `false` |
| `imageUrl` | `image-url` | (optional) The image's url. | `string` | `undefined` |
| `maxWidth` | `max-width` | (optional) Maximum width for the Chip's text and shows ellipsis when truncated | `string` | `undefined` |
| `showCheckmark` | `show-checkmark` | (optional) Whether to show the checkmark. | `boolean` | `false` |
| `showClose` | `show-close` | (optional) Whether to show the close icon. | `boolean` | `false` |
| `size` | `size` | (optional) The chip's size. | `"medium" \| "small"` | `'medium'` |
| `value` | `value` | (optional) The chip's value. | `string` | `undefined` |


## Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { Anchor } from '@storybook/addon-docs';
| show-close | Whether to show the close icon | boolean | | false | |
| size | The chip's size | string | 'medium', 'small' | 'medium' | |
| value | The chip's value | string | | | |
| maxWidth | Maximum width for the Chip's text and shows ellipsis when truncated | string | | | |

### DOM Events

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export default {
type: { summary: 'string' },
},
},
maxWidth: {
description: "Chip text's maximum width",
table: {
type: { summary: 'string' },
defaultValue: { summary: '100px' },
},
}
},
parameters: {
controls: { expanded: true },
Expand All @@ -104,6 +111,7 @@ export const Default = ({
showClose,
size,
value,
maxWidth,
}) => html`
<modus-chip
aria-label=${ariaLabel}
Expand All @@ -114,6 +122,7 @@ export const Default = ({
show-checkmark=${showCheckmark}
show-close=${showClose}
size=${size}
max-width=${maxWidth}
value=${value}>
</modus-chip>
`;
Expand All @@ -127,6 +136,7 @@ Default.args = {
showClose: false,
size: 'medium',
value: 'Bryan',
maxWidth:'100px'
};

export const Outline = ({
Expand All @@ -139,6 +149,7 @@ export const Outline = ({
showClose,
size,
value,
maxWidth,
}) => html`
<modus-chip
aria-label=${ariaLabel}
Expand All @@ -149,7 +160,8 @@ export const Outline = ({
show-checkmark=${showCheckmark}
show-close=${showClose}
size=${size}
value=${value}>
value=${value}
max-width=${maxWidth}>
</modus-chip>
`;
Outline.args = {
Expand All @@ -162,4 +174,5 @@ Outline.args = {
showClose: false,
size: 'medium',
value: 'Bryan',
maxWidth:'100px'
};

0 comments on commit afcdc4e

Please sign in to comment.