Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@if (template) {
<ng-container *ngTemplateOutlet="template"></ng-container>
} @else {
<!-- Focus stays on the list, so the button shows that the add item is active as its focused state. -->
<button type="button" igxButton="flat" igxRipple [focusVisible]="active">{{ label }}</button>
}
Original file line number Diff line number Diff line change
@@ -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: '<ng-content></ng-content>',
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
* <igx-combo-add-item [customTemplate]="!!addItemTemplate"></igx-combo-add-item>
* <igx-combo-add-item [template]="addItemTemplate"></igx-combo-add-item>
* ```
*/
@Input({ transform: booleanAttribute })
public customTemplate = false;
@Input()
public template: TemplateRef<any> | null = null;

/**
* @hidden @internal
* The text of the default button.
*
* @example
* ```html
* <igx-combo-add-item [label]="resourceStrings.igx_combo_addCustomValues_placeholder"></igx-combo-add-item>
* ```
*/
@Input()
public label?: string;

@HostBinding('class.igx-drop-down__item')
public get isDropDownItem(): boolean {
Expand All @@ -37,7 +52,7 @@ export class IgxComboAddItemComponent extends IgxComboItemComponent {
*
* @example
* ```html
* <button igxButton="flat" [class.igx-button--focused]="addItem.active || undefined"></button>
* <button igxButton="flat" [focusVisible]="active"></button>
* ```
*/
public get active(): boolean {
Expand All @@ -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) {
Expand Down
12 changes: 3 additions & 9 deletions projects/igniteui-angular/combo/src/combo/combo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,11 @@
</div>
}
@if (isAddButtonVisible()) {
<igx-combo-add-item #addItem [itemHeight]="itemHeight"
<igx-combo-add-item [itemHeight]="itemHeight"
[tabindex]="dropdown.collapsed ? -1 : customValueFlag ? 1 : -1" class="igx-combo__add-item" role="button"
[attr.aria-label]="resourceStrings.igx_combo_addCustomValues_placeholder" [index]="itemWindow.totalCount"
[customTemplate]="!!addItemTemplate">
@if (addItemTemplate) {
<ng-container *ngTemplateOutlet="addItemTemplate"></ng-container>
} @else {
<!-- Focus stays on the list, so the button shows the add item's focus state as its own.
When the add item is not active, `undefined` leaves the class to the button's own keyboard focus. -->
<button type="button" igxButton="flat" igxRipple [class.igx-button--focused]="addItem.active || undefined">{{ resourceStrings.igx_combo_addCustomValues_placeholder }}</button>
}
[template]="addItemTemplate"
[label]="resourceStrings.igx_combo_addCustomValues_placeholder">
</igx-combo-add-item>
}
</div>
Expand Down
31 changes: 31 additions & 0 deletions projects/igniteui-angular/combo/src/combo/combo.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions projects/igniteui-angular/combo/src/combo/combo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -130,8 +128,6 @@ const diffInSets = (set1: Set<any>, set2: Set<any>): any[] => {
IgxVirtualItemDirective,
IgxComboItemComponent,
IgxComboAddItemComponent,
IgxButtonDirective,
IgxRippleDirective,
IgxReadOnlyInputDirective,
IgxComboFilteringPipe,
IgxComboGroupingPipe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IButtonEventArgs> | 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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
},
Expand All @@ -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
* <button type="button" igxButton="flat" [focusVisible]="isActive"></button>
* ```
*/
@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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -25,7 +26,8 @@ describe('IgxButton', () => {
NoopAnimationsModule,
InitButtonComponent,
ButtonWithAttribsComponent,
ButtonWithRoleComponent
ButtonWithRoleComponent,
ButtonWithFocusVisibleComponent
]
}).compileComponents();
});
Expand Down Expand Up @@ -100,6 +102,51 @@ describe('IgxButton', () => {
});
});

describe('focusVisible', () => {
let fixture: ComponentFixture<ButtonWithFocusVisibleComponent>;
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);

Expand Down Expand Up @@ -156,3 +203,12 @@ function hasClass(element: HTMLElement, className: string): boolean {
})
class ButtonWithRoleComponent {
}

@Component({
template: `<button type="button" igxButton="flat" [focusVisible]="focusVisible()">Test</button>`,
imports: [IgxButtonDirective]
})
class ButtonWithFocusVisibleComponent {
public button = viewChild.required(IgxButtonDirective);
public focusVisible = input(false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -89,7 +102,7 @@ describe('IgxIconButton', () => {

@Component({
template: `
<button [igxIconButton]="type()" [disabled]="disabled()" igxRipple="white">
<button [igxIconButton]="type()" [disabled]="disabled()" [focusVisible]="focusVisible()" igxRipple="white">
<igx-icon>search</igx-icon>
</button>
`,
Expand All @@ -100,6 +113,7 @@ class IconButtonComponent {
public button = viewChild.required(IgxIconButtonDirective);
public type = input<IgxIconButtonType>();
public disabled = input<boolean>(false);
public focusVisible = input<boolean>(false);
}

function hasClass(element: HTMLElement, className: string): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,11 @@
</div>
}
@if (isAddButtonVisible()) {
<igx-combo-add-item #addItem [itemHeight]="itemHeight"
<igx-combo-add-item [itemHeight]="itemHeight"
[tabindex]="dropdown.collapsed ? -1 : customValueFlag ? 1 : -1" class="igx-combo__add-item" role="button"
[attr.aria-label]="resourceStrings.igx_combo_addCustomValues_placeholder" [index]="itemWindow.totalCount"
[customTemplate]="!!addItemTemplate">
@if (addItemTemplate) {
<ng-container *ngTemplateOutlet="addItemTemplate"></ng-container>
} @else {
<!-- Focus stays on the list, so the button shows the add item's focus state as its own.
When the add item is not active, `undefined` leaves the class to the button's own keyboard focus. -->
<button type="button" igxButton="flat" igxRipple [class.igx-button--focused]="addItem.active || undefined">{{ resourceStrings.igx_combo_addCustomValues_placeholder }}</button>
}
[template]="addItemTemplate"
[label]="resourceStrings.igx_combo_addCustomValues_placeholder">
</igx-combo-add-item>
}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
Loading