Skip to content

Commit

Permalink
feat(dropdown): optional truncation of items (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
yd-allianz authored and GitHub Enterprise committed Jul 15, 2022
1 parent 22d9d85 commit e437cfe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions projects/ng-aquila/src/dropdown/dropdown.control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export abstract class NxDropdownControl extends NxFormfieldControl<any> {
* Note: Please make sure the value you bind is an array. If not an error is thrown! */
isMultiSelect = false;

ignoreItemTrunctation = false;

readonly filterChanges!: Subject<any>;

readonly _closedStream!: Observable<void>;
Expand Down
11 changes: 10 additions & 1 deletion projects/ng-aquila/src/dropdown/dropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,22 @@ describe('NxDropdownComponent', () => {
dropdownElement.style.width = '200px';
}));

it('should not use the whole viewport width', fakeAsync(() => {
it('should not use the whole viewport width by default', fakeAsync(() => {
openDropdownByClick();
fixture.detectChanges();
flush();

expect(getDropdown()!.clientWidth).toBe(453);
}));

it('should use the whole viewport width with _truncateItems', fakeAsync(() => {
dropdownInstance.ignoreItemTrunctation = true;
openDropdownByClick();
fixture.detectChanges();
flush();

expect(getDropdown()!.clientWidth).toBe(document.body.clientWidth - dropdownInstance._overlayViewportMargin);
}));
});

describe('with scrolling', () => {
Expand Down
20 changes: 20 additions & 0 deletions projects/ng-aquila/src/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ export class NxDropdownComponent implements NxDropdownControl, ControlValueAcces
/** @docs-private */
errorState = false;

/**
* Disable truncation of long item texts.
* We recommend following UX guidelines and always truncating long items.
* Please only disable truncation if it's impossible to use short descriptions.
*/
private _ignoreItemTrunctation = false;

/**
* Name of this control that is used inside the formfield component
* @docs-private
Expand Down Expand Up @@ -279,6 +286,19 @@ export class NxDropdownComponent implements NxDropdownControl, ControlValueAcces
this.stateChanges.next();
}

/**
* Disable truncation of long item texts.
* We recommend following UX guidelines and always truncating long items.
* Please only disable truncation if it's impossible to use short descriptions.
*/
@Input('nxIgnoreItemTrunctation')
get ignoreItemTrunctation(): boolean {
return this._ignoreItemTrunctation;
}
set ignoreItemTrunctation(value: BooleanInput) {
this._ignoreItemTrunctation = coerceBooleanProperty(value);
}

/** Whether the dropdown should be shown with an additional filter input. */
@Input('nxShowFilter') showFilter = false;

Expand Down
9 changes: 7 additions & 2 deletions projects/ng-aquila/src/dropdown/item/dropdown-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

<ng-template #contentTemplate>
<div class="nx-dropdown-results__option-label">
<div #content (cdkObserveContent)="_onLabelChange()" [nxTooltip]="viewValue?.length > 35 ? viewValue : null" class="ellipsis">
<div
#content
(cdkObserveContent)="_onLabelChange()"
[nxTooltip]="truncateItems && viewValue?.length > 35 ? viewValue : null"
[ngClass]="{ 'ellipsis': truncateItems }"
>
<ng-content></ng-content>
</div>

<div [nxTooltip]="value?.length > 35 ? value : null" class="ellipsis">
<div [nxTooltip]="truncateItems && value?.length > 35 ? value : null" [ngClass]="{ 'ellipsis': truncateItems }">
<ng-container *ngIf="_isContentEmpty(content)"> {{_formattedValue }} </ng-container>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions projects/ng-aquila/src/dropdown/item/dropdown-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ export class NxDropdownItemComponent implements Highlightable, OnDestroy, AfterV
return this._dropdown?.isMultiSelect;
}

/**
* @docs-private
* Whether the parent dropdown allows item truncation.
*/
get truncateItems(): boolean {
return !this._dropdown?.ignoreItemTrunctation;
}

/** Emits whenever the component is destroyed. */
private readonly _destroyed = new Subject<void>();

/** Event emitted when the option is selected or deselected. */
Expand Down

0 comments on commit e437cfe

Please sign in to comment.