diff --git a/projects/angular/components/ui-suggest/src/ui-suggest.component.html b/projects/angular/components/ui-suggest/src/ui-suggest.component.html index 2e382281d..6a8e617a3 100644 --- a/projects/angular/components/ui-suggest/src/ui-suggest.component.html +++ b/projects/angular/components/ui-suggest/src/ui-suggest.component.html @@ -9,7 +9,7 @@ Tested in NVDA. --> - +
- + @@ -50,10 +49,16 @@ aria-hidden="true"> {{ placeholder }}: - {{ - value[0] - ? intl.translateLabel(value[0].text) - : defaultValue }} + + + + + + + {{ computeDisplayValue() }} + +
@@ -153,7 +158,7 @@
- - expect(selectedChipsInnerText.includes(valueText)).toBeTrue()); }); + it('should display all selected values in compact mode', () => { + const selectedValues = faker.helpers.shuffle(component.items!).slice(0, 3); + component.value = selectedValues; + component.compact = true; + component.searchable = true; + + fixture.detectChanges(); + + const displayContainer = fixture.debugElement.query(By.css('.display-container')); + const displayValue = displayContainer.query(By.css('.display-value')); + + if (!uiSuggest.isFormControl) { + const displayTitle = displayContainer.query(By.css('.display-title')); + expect(displayTitle.nativeElement.innerText.trim()).toBe(`${component.placeholder}:`); + } else { + const displayTitle = fixture.debugElement.query(By.css('.mat-form-field-label')); + expect(displayTitle.nativeElement.innerText.trim()).toEqual(component.placeholder); + } + + selectedValues + .map(value => value.text) + .forEach(valueText => expect(displayValue.nativeElement.innerText.trim()).toContain(valueText)); + }); + it('should have a checkbox next to each item entry', waitForAsync(async () => { fixture.detectChanges(); @@ -2428,7 +2453,8 @@ describe('Component: UiSuggest', () => { [fetchStrategy]="fetchStrategy" [minChars]="minChars" [drillDown]="drillDown" - [readonly]="readonly"> + [readonly]="readonly" + [compact]="compact"> `, }) @@ -2509,6 +2535,7 @@ describe('Component: UiSuggest', () => { [fetchStrategy]="fetchStrategy" [minChars]="minChars" [drillDown]="drillDown" + [compact]="compact" formControlName="test"> @@ -2682,32 +2709,45 @@ describe('Component: UiSuggest', () => { @Component({ template: ` - - -
{{ item.text }}
-
+ + +
{{ item.text }}
+
+ +
+ {{value?.[0]?.text}} + + (+{{ (value?.length || 0) - 1 }} + {{ value?.length === 2 ? "other" : "others" }}) + +
+
`, }) - class UiSuggestCustomTemplateFixtureComponent extends UiSuggestFixtureDirective { } + class UiSuggestCustomTemplateFixtureComponent extends UiSuggestFixtureDirective {} describe('Type: custom template', () => { let fixture: ComponentFixture; @@ -2762,6 +2802,25 @@ describe('Component: UiSuggest', () => { }); })); + it('should render the compact summary using the provided custom template', () => { + component.items = generateSuggetionItemList('random'); + + const selectedValues = faker.helpers.shuffle(component.items!).slice(0, 3); + component.value = selectedValues; + + component.multiple = true; + component.compact = true; + component.searchable = true; + + fixture.detectChanges(); + + const displayContainer = fixture.debugElement.query(By.css('.display-container')); + const displayValue = displayContainer.query(By.css('.custom-display-value')); + const summaryText = displayValue.nativeElement.innerText.trim(); + + expect(summaryText).toBe(`${selectedValues[0].text} (+2 others)`); + }); + [false, true].forEach((multiple) => { [false, true].forEach((displayTemplateValue) => { it(`should ${!multiple && displayTemplateValue ? '' : 'NOT'} render the displayed value with custom` + diff --git a/projects/angular/components/ui-suggest/src/ui-suggest.component.ts b/projects/angular/components/ui-suggest/src/ui-suggest.component.ts index 0bf31ea18..8164a0bd4 100644 --- a/projects/angular/components/ui-suggest/src/ui-suggest.component.ts +++ b/projects/angular/components/ui-suggest/src/ui-suggest.component.ts @@ -309,7 +309,7 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective !this.isOpen && this._hasValue ) { - return this.value.map(v => this.intl.translateLabel(v.text)).join(', '); + return this._getValueSummary(); } return null; @@ -506,6 +506,20 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective @Input() disableTooltip = false; + /** + * Use compact summary info instead of chips + * + */ + @Input() + compact = false; + + /** + * The template to use for compact summary + * + */ + @Input() + compactSummaryTemplate?: TemplateRef; + /** * Emits `once` when `data` is retrieved for the `first time`. * @@ -1105,6 +1119,12 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective } } + computeDisplayValue() { + return this.value.length > 0 + ? this._getValueSummary() + : this.defaultValue; + } + private _selectActiveItem(closeAfterSelect: boolean) { const item = this.headerItems![this.activeIndex] ?? this.items[this.activeIndex - this.headerItems!.length]; if (!item) { return; } @@ -1385,4 +1405,8 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective if (scenario) { throw new Error(errorText); } }); } + + private _getValueSummary() { + return this.value.map(v => this.intl.translateLabel(v.text)).join(', '); + } } diff --git a/projects/playground/src/app/pages/suggest/suggest.page.html b/projects/playground/src/app/pages/suggest/suggest.page.html index f0869f5ce..a4aba13a9 100644 --- a/projects/playground/src/app/pages/suggest/suggest.page.html +++ b/projects/playground/src/app/pages/suggest/suggest.page.html @@ -24,3 +24,37 @@

Multiple select suggest

[searchSourceFactory]="searchSourceFactory">
+ +

Multiple select with compact summary instead of chips

+

With the default template

+ + + + +

With custom template

+ + + + + +
+ {{value?.[0]?.text}} + + (+{{(value?.length || 0) - 1}} {{value?.length === 2 ? 'other' : 'others'}}) + +
+
+