diff --git a/CHANGELOG.md b/CHANGELOG.md index 9018edf6c15..1c0042bf446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes for each version of this project will be documented in this file. +## 22.4.0 + +### New Functionality + +- `IgxButtonDirective`, `IgxIconButtonDirective` + - Added a `focusVisible` input. It shows the focused state of the button while the focus is on another element, for example a list that marks the button as its active entry with `aria-activedescendant`. The button still shows the focused state when it has keyboard focus. + ## 22.3.1 ### Bug Fixes diff --git a/projects/igniteui-angular/combo/src/combo/combo-add-item.component.html b/projects/igniteui-angular/combo/src/combo/combo-add-item.component.html new file mode 100644 index 00000000000..33c6c87e0d9 --- /dev/null +++ b/projects/igniteui-angular/combo/src/combo/combo-add-item.component.html @@ -0,0 +1,6 @@ +@if (template) { + +} @else { + + +} 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 ca533775615..bcaac97479e 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,27 +1,42 @@ import { IgxComboItemComponent } from './combo-item.component'; -import { Component, HostBinding, ChangeDetectionStrategy, Input, booleanAttribute } from '@angular/core'; +import { Component, HostBinding, ChangeDetectionStrategy, Input, TemplateRef } from '@angular/core'; +import { NgTemplateOutlet } from '@angular/common'; +import { IgxButtonDirective, IgxRippleDirective } from 'igniteui-angular/directives'; /** * @hidden */ @Component({ selector: 'igx-combo-add-item', - template: '', + templateUrl: 'combo-add-item.component.html', changeDetection: ChangeDetectionStrategy.Eager, providers: [{ provide: IgxComboItemComponent, useExisting: IgxComboAddItemComponent }], + imports: [NgTemplateOutlet, IgxButtonDirective, IgxRippleDirective] }) export class IgxComboAddItemComponent extends IgxComboItemComponent { /** * @hidden @internal - * Set when the add item renders a custom `igxComboAddItem` template instead of the default button. + * The custom `igxComboAddItem` template, rendered instead of the default button. * * @example * ```html - * + * * ``` */ - @Input({ transform: booleanAttribute }) - public customTemplate = false; + @Input() + public template: TemplateRef | null = null; + + /** + * @hidden @internal + * The text of the default button. + * + * @example + * ```html + * + * ``` + */ + @Input() + public label?: string; @HostBinding('class.igx-drop-down__item') public get isDropDownItem(): boolean { @@ -37,7 +52,7 @@ export class IgxComboAddItemComponent extends IgxComboItemComponent { * * @example * ```html - * + * * ``` */ public get active(): boolean { @@ -50,7 +65,7 @@ export class IgxComboAddItemComponent extends IgxComboItemComponent { * the focus style of the list items. A custom template keeps that style. */ public override get focused(): boolean { - return this.customTemplate && this.active; + return !!this.template && this.active; } public override set focused(value: boolean) { diff --git a/projects/igniteui-angular/combo/src/combo/combo.component.html b/projects/igniteui-angular/combo/src/combo/combo.component.html index 475f04f031f..9cd30a27195 100644 --- a/projects/igniteui-angular/combo/src/combo/combo.component.html +++ b/projects/igniteui-angular/combo/src/combo/combo.component.html @@ -115,17 +115,11 @@ } @if (isAddButtonVisible()) { - - @if (addItemTemplate) { - - } @else { - - - } + [template]="addItemTemplate" + [label]="resourceStrings.igx_combo_addCustomValues_placeholder"> } 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 c6e428331f0..0fd1aac9517 100644 --- a/projects/igniteui-angular/combo/src/combo/combo.component.spec.ts +++ b/projects/igniteui-angular/combo/src/combo/combo.component.spec.ts @@ -2962,6 +2962,37 @@ describe('igxCombo', () => { expect(dropdown.focusedItem instanceof IgxComboAddItemComponent).toBe(true); expect(addItem.classList.contains(CSS_CLASS_FOCUSED)).toBe(true); }); + it('should switch between the default add item button and a custom template at runtime', async () => { + fixture = TestBed.createComponent(IgxComboCustomAddItemComponent); + fixture.detectChanges(); + combo = fixture.componentInstance.combo; + const customTemplate = combo.addItemTemplate; + + 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.querySelector('button')).toBeNull(); + + // the host is OnPush, so the combo is checked directly after a programmatic change + combo.addItemTemplate = null; + combo.triggerCheck(); + expect(addItem.querySelector('.custom-add-item')).toBeNull(); + expect(addItem.querySelector('button').textContent.trim()) + .toEqual(combo.resourceStrings.igx_combo_addCustomValues_placeholder); + + combo.addItemTemplate = customTemplate; + combo.triggerCheck(); + expect(addItem.querySelector('.custom-add-item')).not.toBeNull(); + expect(addItem.querySelector('button')).toBeNull(); + }); it('should not change the selection when selectItem is called without an item', () => { const selectSpy = vi.spyOn(combo, 'select'); dropdown.selectItem(null); diff --git a/projects/igniteui-angular/combo/src/combo/combo.component.ts b/projects/igniteui-angular/combo/src/combo/combo.component.ts index db4dceab414..a2521352619 100644 --- a/projects/igniteui-angular/combo/src/combo/combo.component.ts +++ b/projects/igniteui-angular/combo/src/combo/combo.component.ts @@ -24,8 +24,6 @@ import { EditorProvider } from 'igniteui-angular/core'; import { IgxVirtualItemDirective, IgxVirtualScrollComponent } from 'igniteui-angular/virtual-scroll'; -import { IgxRippleDirective } from 'igniteui-angular/directives'; -import { IgxButtonDirective } from 'igniteui-angular/directives'; import { IgxComboItemComponent } from './combo-item.component'; import { IgxComboDropDownComponent } from './combo-dropdown.component'; import { IgxComboDataWindowPipe, IgxComboFilteringPipe, IgxComboGroupingPipe, IgxComboRecordWindowPipe } from './combo.pipes'; @@ -130,8 +128,6 @@ const diffInSets = (set1: Set, set2: Set): any[] => { IgxVirtualItemDirective, IgxComboItemComponent, IgxComboAddItemComponent, - IgxButtonDirective, - IgxRippleDirective, IgxReadOnlyInputDirective, IgxComboFilteringPipe, IgxComboGroupingPipe, diff --git a/projects/igniteui-angular/directives/src/directives/button/README.md b/projects/igniteui-angular/directives/src/directives/button/README.md index 33cd329940b..f0aad703c5d 100644 --- a/projects/igniteui-angular/directives/src/directives/button/README.md +++ b/projects/igniteui-angular/directives/src/directives/button/README.md @@ -14,6 +14,7 @@ A walkthrough of how to get started can be found [here](https://www.infragistics | `igxButton` | string | Set the type of igxButton to be used. Default is set to flat. | | `buttonSelected` | EventEmitter | Emitted only when a button gets selected, or deselected, and not on initialization. | | `selected` | boolean | Gets or sets whether the button is selected. Mainly used in the IgxButtonGroup component and it will have no effect if set separately. | +| `focusVisible` | boolean | Gets or sets whether the button shows its focused state while the focus is on another element, for example a list that marks the button as its active entry with `aria-activedescendant`. The button also shows the focused state when it has keyboard focus. Available on `igxIconButton` as well. | # Button types | Name | Description | diff --git a/projects/igniteui-angular/directives/src/directives/button/button-base.ts b/projects/igniteui-angular/directives/src/directives/button/button-base.ts index 66017b99ba5..8e73a244f04 100644 --- a/projects/igniteui-angular/directives/src/directives/button/button-base.ts +++ b/projects/igniteui-angular/directives/src/directives/button/button-base.ts @@ -13,7 +13,7 @@ export const IgxBaseButtonType = { host: { 'role': 'button', '[attr.disabled]': '_disabled() || null', - '[class.igx-button--focused]': '_hasKeyboardFocus()', + '[class.igx-button--focused]': '_hasKeyboardFocus() || _focusVisible()', '[class.igx-button--disabled]': '_disabled()', '(click)': 'buttonClick.emit($event)', }, @@ -30,6 +30,26 @@ export abstract class IgxButtonBaseDirective { */ protected readonly _hasRendered = signal(false); protected readonly _disabled = signal(false); + protected readonly _focusVisible = signal(false); + + /** + * Gets or sets whether the button shows its focused state while the focus is on another element, + * for example a list that marks the button as its active entry with `aria-activedescendant`. + * The button also shows the focused state when it has keyboard focus, whatever the value. + * + * @example + * ```html + * + * ``` + */ + @Input({ transform: booleanAttribute }) + public set focusVisible(value: boolean) { + this._focusVisible.set(value); + } + + public get focusVisible(): boolean { + return this._focusVisible(); + } /** * Gets or sets whether the button is disabled. diff --git a/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts b/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts index 353a6325285..cdbf6ffb53e 100644 --- a/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts +++ b/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts @@ -16,7 +16,8 @@ describe('IgxButton', () => { outlined: 'igx-button--outlined', fab: 'igx-button--fab', disabled: 'igx-button--disabled', - ready: 'igx-button--ready' + ready: 'igx-button--ready', + focused: 'igx-button--focused' } as const; beforeEach(async () => { @@ -25,7 +26,8 @@ describe('IgxButton', () => { NoopAnimationsModule, InitButtonComponent, ButtonWithAttribsComponent, - ButtonWithRoleComponent + ButtonWithRoleComponent, + ButtonWithFocusVisibleComponent ] }).compileComponents(); }); @@ -100,6 +102,51 @@ describe('IgxButton', () => { }); }); + describe('focusVisible', () => { + let fixture: ComponentFixture; + let buttonElement: HTMLElement; + + beforeEach(() => { + fixture = TestBed.createComponent(ButtonWithFocusVisibleComponent); + fixture.detectChanges(); + buttonElement = fixture.debugElement.query(By.css('button')).nativeElement as HTMLElement; + }); + + it('should show the focused state while focusVisible is set', () => { + expect(hasClass(buttonElement, CSS_CLASSES.focused)).toBe(false); + + fixture.componentRef.setInput('focusVisible', true); + fixture.detectChanges(); + expect(fixture.componentInstance.button().focusVisible).toBe(true); + expect(hasClass(buttonElement, CSS_CLASSES.focused)).toBe(true); + + fixture.componentRef.setInput('focusVisible', false); + fixture.detectChanges(); + expect(hasClass(buttonElement, CSS_CLASSES.focused)).toBe(false); + }); + + it('should keep the focused state of its own keyboard focus while focusVisible is not set', () => { + buttonElement.focus(); + buttonElement.dispatchEvent(new KeyboardEvent('keyup', { key: 'Tab', bubbles: true })); + fixture.detectChanges(); + expect(fixture.componentInstance.button().focusVisible).toBe(false); + expect(hasClass(buttonElement, CSS_CLASSES.focused)).toBe(true); + + buttonElement.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })); + fixture.detectChanges(); + expect(hasClass(buttonElement, CSS_CLASSES.focused)).toBe(false); + }); + + it('should keep the focused state on pointerdown while focusVisible is set', () => { + fixture.componentRef.setInput('focusVisible', true); + fixture.detectChanges(); + + buttonElement.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })); + fixture.detectChanges(); + expect(hasClass(buttonElement, CSS_CLASSES.focused)).toBe(true); + }); + }); + it('Should enable transitions only after the first render', () => { const fixture = TestBed.createComponent(InitButtonComponent); @@ -156,3 +203,12 @@ function hasClass(element: HTMLElement, className: string): boolean { }) class ButtonWithRoleComponent { } + +@Component({ + template: ``, + imports: [IgxButtonDirective] +}) +class ButtonWithFocusVisibleComponent { + public button = viewChild.required(IgxButtonDirective); + public focusVisible = input(false); +} diff --git a/projects/igniteui-angular/directives/src/directives/button/icon-button.directive.spec.ts b/projects/igniteui-angular/directives/src/directives/button/icon-button.directive.spec.ts index 12a255fe7d2..d917ac7c445 100644 --- a/projects/igniteui-angular/directives/src/directives/button/icon-button.directive.spec.ts +++ b/projects/igniteui-angular/directives/src/directives/button/icon-button.directive.spec.ts @@ -14,7 +14,8 @@ describe('IgxIconButton', () => { contained: 'igx-icon-button--contained', outlined: 'igx-icon-button--outlined', ready: 'igx-icon-button--ready', - disabled: 'igx-button--disabled' + disabled: 'igx-button--disabled', + focused: 'igx-button--focused' } beforeEach(async () => { @@ -58,6 +59,18 @@ describe('IgxIconButton', () => { expect(hasClass(element, CSS_CLASSES.disabled)).toBe(false); }); + it('should show the focused state while focusVisible is set', () => { + expect(hasClass(element, CSS_CLASSES.focused)).toBe(false); + + fixture.componentRef.setInput('focusVisible', true); + fixture.detectChanges(); + expect(hasClass(element, CSS_CLASSES.focused)).toBe(true); + + fixture.componentRef.setInput('focusVisible', false); + fixture.detectChanges(); + expect(hasClass(element, CSS_CLASSES.focused)).toBe(false); + }); + it('should properly set the correct CSS class on the element using the type input', () => { fixture.detectChanges(); // applies the post-render `--ready` class @@ -89,7 +102,7 @@ describe('IgxIconButton', () => { @Component({ template: ` - `, @@ -100,6 +113,7 @@ class IconButtonComponent { public button = viewChild.required(IgxIconButtonDirective); public type = input(); public disabled = input(false); + public focusVisible = input(false); } function hasClass(element: HTMLElement, className: string): boolean { 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 9d2cc4d3dbc..73b61465685 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 @@ -111,17 +111,11 @@ } @if (isAddButtonVisible()) { - - @if (addItemTemplate) { - - } @else { - - - } + [template]="addItemTemplate" + [label]="resourceStrings.igx_combo_addCustomValues_placeholder"> } diff --git a/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.ts b/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.ts index f967933c050..c19cd65627c 100644 --- a/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.ts +++ b/projects/igniteui-angular/simple-combo/src/simple-combo/simple-combo.component.ts @@ -4,9 +4,7 @@ import { ControlValueAccessor, FormGroupDirective, NG_VALUE_ACCESSOR } from '@an import { takeUntil } from 'rxjs/operators'; import { CancelableEventArgs, IBaseCancelableBrowserEventArgs, IBaseEventArgs, PlatformUtil } from 'igniteui-angular/core'; -import { IgxButtonDirective } from 'igniteui-angular/directives'; import { IgxVirtualItemDirective, IgxVirtualScrollComponent } from 'igniteui-angular/virtual-scroll'; -import { IgxRippleDirective } from 'igniteui-angular/directives'; import { IgxTextSelectionDirective } from 'igniteui-angular/directives'; import { IgxInputGroupComponent, IgxInputDirective, IgxSuffixDirective } from 'igniteui-angular/input-group'; import { IgxIconComponent } from 'igniteui-angular/icon'; @@ -64,7 +62,7 @@ export interface ISimpleComboSelectionChangingEventArgs extends ISimpleComboSele '(keydown.ArrowDown)': 'onArrowDown($any($event))', '(keydown.Alt.ArrowDown)': 'onArrowDown($any($event))' }, - imports: [IgxInputGroupComponent, IgxInputDirective, IgxTextSelectionDirective, IgxSuffixDirective, NgTemplateOutlet, IgxIconComponent, IgxComboDropDownComponent, IgxDropDownItemNavigationDirective, IgxVirtualScrollComponent, IgxVirtualItemDirective, IgxComboItemComponent, IgxComboAddItemComponent, IgxButtonDirective, IgxRippleDirective, IgxComboFilteringPipe, IgxComboGroupingPipe, IgxComboDataWindowPipe, IgxComboRecordWindowPipe] + imports: [IgxInputGroupComponent, IgxInputDirective, IgxTextSelectionDirective, IgxSuffixDirective, NgTemplateOutlet, IgxIconComponent, IgxComboDropDownComponent, IgxDropDownItemNavigationDirective, IgxVirtualScrollComponent, IgxVirtualItemDirective, IgxComboItemComponent, IgxComboAddItemComponent, IgxComboFilteringPipe, IgxComboGroupingPipe, IgxComboDataWindowPipe, IgxComboRecordWindowPipe] }) export class IgxSimpleComboComponent extends IgxComboBaseDirective implements ControlValueAccessor, AfterViewInit, DoCheck { private platformUtil = inject(PlatformUtil);