diff --git a/CHANGELOG.md b/CHANGELOG.md index ec9fc3f52c4..12829ce313e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -104,6 +104,9 @@ All notable changes for each version of this project will be documented in this - A slide activated through its `active` property now animates in the direction of its position, or in its own `direction` when one is set. - `IgxCircularProgressBarComponent`, `IgxLinearProgressBarComponent` - In Safari, the initial animation no longer overshoots the value and then crawls back to it. The initial fill and later value changes now use the same transition, so a value that changes while the bar is still animating goes straight to the new value in every browser. The Bootstrap circular bar now also animates its initial fill in RTL. +- `IgxComboComponent`, `IgxSimpleComboComponent` + - The default "Add item" button no longer takes the background or outline of a focused list item when it is reached with the keyboard. It shows the focus state of a flat button instead. + - In `IgxSimpleComboComponent`, "Add item" no longer appears focused before it is reached with the keyboard, for example right after typing text that matches no item or when the list reopens. - `IgxCsvExporterService` - Exporting a grid with summaries no longer adds an extra `GRID_LEVEL_COL` column with the nesting level of each record. Only the grid's columns are exported, as with the Excel and PDF exporters. - `IgxDatePickerComponent` diff --git a/projects/igniteui-angular/combo/src/combo/combo-add-item.component.ts b/projects/igniteui-angular/combo/src/combo/combo-add-item.component.ts index 9cc1aded856..ca533775615 100644 --- a/projects/igniteui-angular/combo/src/combo/combo-add-item.component.ts +++ b/projects/igniteui-angular/combo/src/combo/combo-add-item.component.ts @@ -1,5 +1,5 @@ import { IgxComboItemComponent } from './combo-item.component'; -import { Component, HostBinding, ChangeDetectionStrategy } from '@angular/core'; +import { Component, HostBinding, ChangeDetectionStrategy, Input, booleanAttribute } from '@angular/core'; /** * @hidden @@ -11,11 +11,52 @@ import { Component, HostBinding, ChangeDetectionStrategy } from '@angular/core'; providers: [{ provide: IgxComboItemComponent, useExisting: IgxComboAddItemComponent }], }) export class IgxComboAddItemComponent extends IgxComboItemComponent { + /** + * @hidden @internal + * Set when the add item renders a custom `igxComboAddItem` template instead of the default button. + * + * @example + * ```html + * + * ``` + */ + @Input({ transform: booleanAttribute }) + public customTemplate = false; + @HostBinding('class.igx-drop-down__item') public get isDropDownItem(): boolean { return false; } + /** + * @hidden @internal + * Whether the add item is the active entry of the list. The focus stays on the list, + * so the default button shows this state as its own focus state. + * + * @returns `true` when the drop-down's active entry is the add item. + * + * @example + * ```html + * + * ``` + */ + public get active(): boolean { + return this.dropDown.focusedItem === this; + } + + /** + * @hidden @internal + * The default add item is a button that shows its own focus state, so it does not take + * the focus style of the list items. A custom template keeps that style. + */ + public override get focused(): boolean { + return this.customTemplate && this.active; + } + + public override set focused(value: boolean) { + super.focused = value; + } + public override get selected(): boolean { return false; } diff --git a/projects/igniteui-angular/combo/src/combo/combo-dropdown.component.ts b/projects/igniteui-angular/combo/src/combo/combo-dropdown.component.ts index 0bc6540e009..1261c743300 100644 --- a/projects/igniteui-angular/combo/src/combo/combo-dropdown.component.ts +++ b/projects/igniteui-angular/combo/src/combo/combo-dropdown.component.ts @@ -31,6 +31,9 @@ export class IgxComboDropDownComponent extends IgxDropDownComponent implements I @ContentChildren(IgxComboItemComponent, { descendants: true }) public override children: QueryList = null!; + /** Whether the add item was the entry made active, as opposed to an index left over from a longer list. */ + private _addItemActive = false; + /** @hidden @internal */ public override get scrollContainer(): HTMLElement { return this.virtualization!.scrollElement; @@ -48,7 +51,10 @@ export class IgxComboDropDownComponent extends IgxDropDownComponent implements I * @hidden @internal */ public override get focusedItem(): IgxDropDownItemBaseDirective | null { - return super.focusedItem; + const item = super.focusedItem; + // The add item takes the index right after the rows, so an index kept from a longer list + // can point at it after filtering. It is active only when it was the entry made active. + return item instanceof IgxComboAddItemComponent && !this._addItemActive ? null : item; } /** @@ -64,15 +70,13 @@ export class IgxComboDropDownComponent extends IgxDropDownComponent implements I /** @hidden @internal */ public override set focusedItem(item: IgxDropDownItemBaseDirective | null) { - if (!item) { - this._activeDescendantId = null; - } else if (item.id !== undefined) { - this._activeDescendantId = item.id; - } else { + let entry = item; + if (item && item.id === undefined) { // Virtual { value, index } object passed by navigateItem() under virtual scrolling. - const resolved = this.children?.find(e => e.index === item.index); - this._activeDescendantId = resolved?.id ?? null; + entry = this.children?.find(e => e.index === item.index) ?? null; } + this._activeDescendantId = entry?.id ?? null; + this._addItemActive = entry instanceof IgxComboAddItemComponent; super.focusedItem = item; } @@ -120,6 +124,18 @@ export class IgxComboDropDownComponent extends IgxDropDownComponent implements I this.opened.emit(); } + /** + * @hidden @internal + */ + public override onToggleClosed() { + // The add item belongs to the search that showed it, so it is not active again on reopen. + if (this._addItemActive) { + this.focusedItem = null; + this.combo.setActiveDescendant(); + } + super.onToggleClosed(); + } + /** * @hidden */ @@ -168,6 +184,7 @@ export class IgxComboDropDownComponent extends IgxDropDownComponent implements I } this.comboAPI.set_selected_item(item.itemID); this._activeDescendantId = item.id ?? null; + this._addItemActive = false; this._focusedItem = item; this.combo.setActiveDescendant(); } diff --git a/projects/igniteui-angular/combo/src/combo/combo.component.html b/projects/igniteui-angular/combo/src/combo/combo.component.html index a4f06cd1db7..475f04f031f 100644 --- a/projects/igniteui-angular/combo/src/combo/combo.component.html +++ b/projects/igniteui-angular/combo/src/combo/combo.component.html @@ -115,11 +115,17 @@ } @if (isAddButtonVisible()) { - - - + [attr.aria-label]="resourceStrings.igx_combo_addCustomValues_placeholder" [index]="itemWindow.totalCount" + [customTemplate]="!!addItemTemplate"> + @if (addItemTemplate) { + + } @else { + + + } } @@ -137,9 +143,6 @@ {{resourceStrings.igx_combo_empty_message}} - - - {{ item[key] }} diff --git a/projects/igniteui-angular/combo/src/combo/combo.component.spec.ts b/projects/igniteui-angular/combo/src/combo/combo.component.spec.ts index a52c99840cd..c6e428331f0 100644 --- a/projects/igniteui-angular/combo/src/combo/combo.component.spec.ts +++ b/projects/igniteui-angular/combo/src/combo/combo.component.spec.ts @@ -22,7 +22,7 @@ import { IComboFilteringOptions, IGX_COMBO_COMPONENT } from './combo.common'; import { IComboItemAdditionEvent, IComboSearchInputEventArgs, IComboSelectionChangedEventArgs, IComboSelectionChangingEventArgs, IgxComboComponent } from './combo.component'; -import { IgxComboFooterDirective, IgxComboHeaderDirective, IgxComboItemDirective } from './combo.directives'; +import { IgxComboAddItemDirective, IgxComboFooterDirective, IgxComboHeaderDirective, IgxComboItemDirective } from './combo.directives'; import { IgxComboFilteringPipe, comboIgnoreDiacriticsFilter } from './combo.pipes'; import { IgxDropDownItemBaseDirective } from '../../../drop-down/src/drop-down/drop-down-item.base'; import { UIInteractions, wait } from 'igniteui-angular/test-utils/ui-interactions.spec'; @@ -43,6 +43,7 @@ const CSS_CLASS_CLEARBUTTON = 'igx-combo__clear-button'; const CSS_CLASS_ADDBUTTON = 'igx-combo__add-item'; const CSS_CLASS_SELECTED = 'igx-drop-down__item--selected'; const CSS_CLASS_FOCUSED = 'igx-drop-down__item--focused'; +const CSS_CLASS_BUTTON_FOCUSED = 'igx-button--focused'; const CSS_CLASS_HEADERITEM = 'igx-drop-down__header'; const CSS_CLASS_SCROLLBAR_VERTICAL = 'igx-virtual-scroll'; const CSS_CLASS_INPUTGROUP = 'igx-input-group'; @@ -1117,6 +1118,7 @@ describe('igxCombo', () => { IgxComboFormComponent, IgxComboInTemplatedFormComponent, IgxComboWithPrefixComponent, + IgxComboCustomAddItemComponent, ] }).compileComponents(); }); @@ -2876,6 +2878,89 @@ describe('igxCombo', () => { UIInteractions.triggerEventHandlerKeyDown('ArrowDown', dropdownContent); fixture.detectChanges(); expect(dropdown.focusedItem instanceof IgxComboAddItemComponent).toBe(true); + + const addItem: HTMLElement = fixture.debugElement.query(By.directive(IgxComboAddItemComponent)).nativeElement; + expect(addItem.classList.contains(CSS_CLASS_FOCUSED)).toBe(false); + expect(addItem.querySelector('button').classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(true); + }); + it('should show the focus state of the default add item button instead of the item focus style', async () => { + combo.allowCustomValues = true; + combo.toggle(); + await wait(); + fixture.detectChanges(); + + const searchInput = fixture.debugElement.query(By.css(CSS_CLASS_SEARCHINPUT)); + UIInteractions.triggerInputEvent(searchInput, 'My New State'); + await wait(); + fixture.detectChanges(); + expect(combo.filteredData.length).toEqual(0); + + const addItem: HTMLElement = fixture.debugElement.query(By.directive(IgxComboAddItemComponent)).nativeElement; + const button = addItem.querySelector('button'); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(false); + + combo.handleKeyUp(UIInteractions.getKeyboardEvent('keyup', 'ArrowDown')); + fixture.detectChanges(); + expect(dropdown.focusedItem instanceof IgxComboAddItemComponent).toBe(true); + expect(addItem.classList.contains(CSS_CLASS_FOCUSED)).toBe(false); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(true); + + // the add item is the only entry, so ArrowUp goes back to the search input + const dropdownContent = fixture.debugElement.query(By.css(`.${CSS_CLASS_CONTENT}`)); + UIInteractions.triggerEventHandlerKeyDown('ArrowUp', dropdownContent); + fixture.detectChanges(); + expect(dropdown.focusedItem).toBeNull(); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(false); + }); + it('should keep the keyboard focus state of the default add item button while another item is active', async () => { + combo.allowCustomValues = true; + combo.toggle(); + await wait(); + fixture.detectChanges(); + + const searchInput = fixture.debugElement.query(By.css(CSS_CLASS_SEARCHINPUT)); + // matches a single item, so the list is a group header and 'Ohio' + UIInteractions.triggerInputEvent(searchInput, 'Ohi'); + await wait(); + fixture.detectChanges(); + + const dropdownContent = fixture.debugElement.query(By.css(`.${CSS_CLASS_CONTENT}`)); + UIInteractions.triggerEventHandlerKeyDown('ArrowDown', dropdownContent); + fixture.detectChanges(); + expect(dropdown.focusedItem.itemID).toEqual('Ohio'); + + // e.g. Tab reaches the button while a consumer keeps the drop-down open by canceling `closing` + const button: HTMLElement = fixture.debugElement.query(By.directive(IgxComboAddItemComponent)) + .nativeElement.querySelector('button'); + button.focus(); + button.dispatchEvent(new KeyboardEvent('keyup', { key: 'Tab', bubbles: true })); + fixture.detectChanges(); + expect(dropdown.focusedItem.itemID).toEqual('Ohio'); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(true); + }); + it('should keep the item focus style on the add item when it has a custom template', async () => { + fixture = TestBed.createComponent(IgxComboCustomAddItemComponent); + fixture.detectChanges(); + combo = fixture.componentInstance.combo; + dropdown = combo.dropdown; + + combo.toggle(); + await wait(); + fixture.detectChanges(); + + const searchInput = fixture.debugElement.query(By.css(CSS_CLASS_SEARCHINPUT)); + UIInteractions.triggerInputEvent(searchInput, 'Not existing'); + await wait(); + fixture.detectChanges(); + + const addItem: HTMLElement = fixture.debugElement.query(By.directive(IgxComboAddItemComponent)).nativeElement; + expect(addItem.querySelector('.custom-add-item')).not.toBeNull(); + expect(addItem.classList.contains(CSS_CLASS_FOCUSED)).toBe(false); + + combo.handleKeyUp(UIInteractions.getKeyboardEvent('keyup', 'ArrowDown')); + fixture.detectChanges(); + expect(dropdown.focusedItem instanceof IgxComboAddItemComponent).toBe(true); + expect(addItem.classList.contains(CSS_CLASS_FOCUSED)).toBe(true); }); it('should not change the selection when selectItem is called without an item', () => { const selectSpy = vi.spyOn(combo, 'select'); @@ -4171,6 +4256,7 @@ describe('igxCombo', () => { combo.handleKeyUp(UIInteractions.getKeyboardEvent('keyup', 'ArrowDown')); fixture.detectChanges(); + expect(combo.dropdown.focusedItem instanceof IgxComboAddItemComponent).toBe(true); const dropdownContent = fixture.debugElement.query(By.css(`.${CSS_CLASS_CONTENT}`)); UIInteractions.triggerEventHandlerKeyDown('Space', dropdownContent); fixture.detectChanges(); @@ -4178,8 +4264,8 @@ describe('igxCombo', () => { expect(combo.collapsed).toBeFalsy(); expect(combo.displayValue).toEqual(''); - const focusedItem = fixture.debugElement.query(By.css(`.${CSS_CLASS_FOCUSED}`)); - focusedItem.triggerEventHandler('click', UIInteractions.getMouseEvent('click')); + const addItem = fixture.debugElement.query(By.css(`.${CSS_CLASS_ADDBUTTON}`)); + addItem.triggerEventHandler('click', UIInteractions.getMouseEvent('click')); fixture.detectChanges(); expect(combo.collapsed).toBeFalsy(); expect(combo.displayValue).toEqual('My New Custom Item'); @@ -5647,6 +5733,23 @@ class IgxComboWithPrefixComponent { public items = ['One', 'Two', 'Three']; } +@Component({ + template: ` + + + Add a custom item + + + `, + imports: [IgxComboComponent, IgxComboAddItemDirective] +}) +class IgxComboCustomAddItemComponent { + @ViewChild('combo', { read: IgxComboComponent, static: true }) + public combo: IgxComboComponent; + + public items = ['One', 'Two', 'Three']; +} + @Component({ template: ` diff --git a/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.html b/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.html index a9770338b8e..9d2cc4d3dbc 100644 --- a/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.html +++ b/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.html @@ -113,9 +113,15 @@ @if (isAddButtonVisible()) { - - + [attr.aria-label]="resourceStrings.igx_combo_addCustomValues_placeholder" [index]="itemWindow.totalCount" + [customTemplate]="!!addItemTemplate"> + @if (addItemTemplate) { + + } @else { + + + } } @@ -133,9 +139,6 @@ {{resourceStrings.igx_combo_empty_message}} - - - {{ item[key] }} diff --git a/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.spec.ts b/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.spec.ts index fe6aec14711..e4b05b2762e 100644 --- a/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.spec.ts +++ b/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.spec.ts @@ -30,6 +30,7 @@ const CSS_CLASS_TOGGLEBUTTON = 'igx-combo__toggle-button'; const CSS_CLASS_CLEARBUTTON = 'igx-combo__clear-button'; const CSS_CLASS_ADDBUTTON = 'igx-combo__add-item'; const CSS_CLASS_FOCUSED = 'igx-drop-down__item--focused'; +const CSS_CLASS_BUTTON_FOCUSED = 'igx-button--focused'; const CSS_CLASS_INPUTGROUP = 'igx-input-group'; const CSS_CLASS_COMBO_INPUTGROUP = 'igx-input-group__input'; const CSS_CLASS_INPUTGROUP_REQUIRED = 'igx-input-group--required'; @@ -1538,6 +1539,90 @@ describe('IgxSimpleCombo', () => { expect(document.activeElement).toEqual(addItemButton.nativeElement); }); + it('should show the focus state of the default AddItem button instead of the item focus style', async () => { + fixture.componentInstance.allowCustomValues = true; + fixture.detectChanges(); + combo.open(); + fixture.detectChanges(); + UIInteractions.setInputElementValue(input.nativeElement, 'MassachusettsL'); + fixture.detectChanges(); + expect(combo.filteredData.length).toEqual(0); + + combo.handleKeyUp(UIInteractions.getKeyboardEvent('keyup', 'ArrowDown')); + fixture.detectChanges(); + expect(combo.dropdown.focusedItem).toBe(combo.addItem); + + const addItem: HTMLElement = fixture.debugElement.query(By.css(`.${CSS_CLASS_ADDBUTTON}`)).nativeElement; + expect(addItem.classList.contains(CSS_CLASS_FOCUSED)).toBe(false); + expect(addItem.querySelector('button').classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(true); + }); + + it('should not make the AddItem active when the typed text matches no item', async () => { + // without grouping the first match is at index 0, the same index the AddItem takes when nothing matches + combo.groupKey = undefined; + fixture.componentInstance.allowCustomValues = true; + fixture.detectChanges(); + // typing in the input opens the list, and the focus stays in the input + input.nativeElement.focus(); + UIInteractions.setInputElementValue(input.nativeElement, 'Massachusetts'); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + expect(combo.collapsed).toBe(false); + expect(combo.filteredData.length).toBeGreaterThan(0); + + UIInteractions.setInputElementValue(input.nativeElement, 'MassachusettsL'); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + expect(combo.filteredData.length).toEqual(0); + + const button: HTMLElement = fixture.debugElement.query(By.css(`.${CSS_CLASS_ADDBUTTON}`)) + .nativeElement.querySelector('button'); + expect(combo.dropdown.focusedItem).toBeNull(); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(false); + + combo.handleKeyUp(UIInteractions.getKeyboardEvent('keyup', 'ArrowDown')); + fixture.detectChanges(); + expect(combo.dropdown.focusedItem).toBe(combo.addItem); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(true); + }); + + it('should not keep the AddItem active after the list closes', async () => { + fixture.componentInstance.allowCustomValues = true; + fixture.detectChanges(); + input.nativeElement.focus(); + UIInteractions.setInputElementValue(input.nativeElement, 'MassachusettsL'); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + expect(combo.collapsed).toBe(false); + + combo.handleKeyUp(UIInteractions.getKeyboardEvent('keyup', 'ArrowDown')); + fixture.detectChanges(); + expect(combo.dropdown.focusedItem).toBe(combo.addItem); + + combo.close(); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + expect(combo.collapsed).toBe(true); + + // typing again reopens the list with no matches + input.nativeElement.focus(); + UIInteractions.setInputElementValue(input.nativeElement, 'MassachusettsLL'); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + expect(combo.collapsed).toBe(false); + expect(combo.filteredData.length).toEqual(0); + + const button: HTMLElement = fixture.debugElement.query(By.css(`.${CSS_CLASS_ADDBUTTON}`)) + .nativeElement.querySelector('button'); + expect(combo.dropdown.focusedItem).toBeNull(); + expect(button.classList.contains(CSS_CLASS_BUTTON_FOCUSED)).toBe(false); + }); + it('should close when an item is clicked on', () => { vi.spyOn(combo, 'close'); combo.open();