Skip to content

Make 'clear selection' button focusable and add keyboard navigation for it - master #15874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 20.0.x
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ng-container>
@if (displayValue) {
<igx-suffix [attr.aria-label]="resourceStrings.igx_combo_clearItems_placeholder" class="igx-combo__clear-button"
(click)="handleClearItems($event)">
(click)="handleClearItems($event)" (keydown)="handleClearKeyDown($event)" [tabindex]="disabled ? -1 : 0" role="button">
@if (clearIconTemplate) {
<ng-container *ngTemplateOutlet="clearIconTemplate"></ng-container>
}
Expand Down
15 changes: 15 additions & 0 deletions projects/igniteui-angular/src/lib/combo/combo.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,21 @@ describe('igxCombo', () => {
cancel: false
});
});
it('should clear the selection on Enter of the focused clear icon', () => {
const selectedItem_1 = combo.dropdown.items[1];
combo.toggle();
fixture.detectChanges();
simulateComboItemClick(1);
expect(combo.selection[0]).toEqual(selectedItem_1.value);
expect(combo.value[0]).toEqual(selectedItem_1.value[combo.valueKey]);

const clearBtn = fixture.debugElement.query(By.css(`.${CSS_CLASS_CLEARBUTTON}`));
UIInteractions.triggerEventHandlerKeyDown('Enter', clearBtn);
fixture.detectChanges();
expect(input.nativeElement.value).toEqual('');
expect(combo.selection.length).toEqual(0);
expect(combo.value.length).toEqual(0);
});
it('should not be able to select group header', () => {
spyOn(combo.selectionChanging, 'emit').and.callThrough();
combo.toggle();
Expand Down
25 changes: 21 additions & 4 deletions projects/igniteui-angular/src/lib/combo/combo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ export class IgxComboComponent extends IgxComboBaseDirective implements AfterVie
/**
* @hidden @internal
*/
public handleClearItems(event: Event): void {
if (this.disabled) {
return;
}
public clearInput(event: Event): void {
this.deselectAllItems(true, event);
if (this.collapsed) {
this.getEditElement().focus();
Expand All @@ -289,6 +286,26 @@ export class IgxComboComponent extends IgxComboBaseDirective implements AfterVie
event.stopPropagation();
}

/**
* @hidden @internal
*/
public handleClearItems(event: Event): void {
if (this.disabled) {
return;
}
this.clearInput(event);
}

/**
* @hidden @internal
*/
public handleClearKeyDown(eventArgs: KeyboardEvent) {
if (eventArgs.key === 'Enter' || eventArgs.key === ' ') {
eventArgs.preventDefault();
this.clearInput(eventArgs);
}
}

/**
* Select defined items
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@
%igx-combo__clear-button {
color: var-get($theme, 'clear-button-foreground-focus');
background: var-get($theme, 'clear-button-background-focus');

&:focus {
color: color($color: 'secondary');
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@if (hasSelectedItem) {
<igx-suffix [attr.aria-label]="resourceStrings.igx_combo_clearItems_placeholder" class="igx-combo__clear-button"
(click)="handleClear($event)">
(click)="handleClear($event)" (keydown)="handleClearKeyDown($event)" [tabindex]="disabled ? -1 : 0" role="button">
@if (clearIconTemplate) {
<ng-container *ngTemplateOutlet="clearIconTemplate"></ng-container>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,19 @@ describe('IgxSimpleCombo', () => {
expect(combo.displayValue).toEqual('Wisconsin');
});

it('should clear the selection on Enter of the focused clear icon', () => {
combo.select(combo.data[2][combo.valueKey]);
fixture.detectChanges();
expect(combo.selection).toBeDefined()
expect(input.nativeElement.value).toEqual('Massachusetts');

const clearBtn = fixture.debugElement.query(By.css(`.${CSS_CLASS_CLEARBUTTON}`));
UIInteractions.triggerEventHandlerKeyDown('Enter', clearBtn);
fixture.detectChanges();
expect(input.nativeElement.value.length).toEqual(0);
expect(combo.selection).not.toBeDefined();
});

it('should display the AddItem button when allowCustomValues is true and there is a partial match', fakeAsync(() => {
fixture.componentInstance.allowCustomValues = true;
fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,11 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
}

/** @hidden @internal */
public handleClear(event: Event): void {
if (this.disabled) {
return;
}

public clearInput(event: Event): void {
const oldSelection = this.selection;
this.clearSelection(true);

if(!this.collapsed){
if (!this.collapsed) {
this.focusSearchInput(true);
}
event.stopPropagation();
Expand All @@ -446,6 +442,23 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
this.comboInput.focus();
}

/** @hidden @internal */
public handleClear(event: Event): void {
if (this.disabled) {
return;
}

this.clearInput(event);
}

/** @hidden @internal */
public handleClearKeyDown(event: KeyboardEvent): void {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
this.clearInput(event);
}
}

/** @hidden @internal */
public handleOpened(): void {
this.triggerCheck();
Expand Down
Loading