Skip to content
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

fix(components/ag-grid): handle focus for adjacent editor cells (#2993) #3005

Merged
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
@@ -1,4 +1,9 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {
ComponentFixture,
TestBed,
fakeAsync,
tick,
} from '@angular/core/testing';
import { expect, expectAsync } from '@skyux-sdk/testing';

import {
Expand Down Expand Up @@ -85,8 +90,9 @@ describe('SkyCellEditorAutocompleteComponent', () => {
expect(component.editorForm.get('selection')?.value).toEqual(selection);
});

it('should respond to focus changes', () => {
it('should respond to focus changes', fakeAsync(() => {
component.agInit(cellEditorParams as SkyCellEditorAutocompleteParams);
tick();

component.onAutocompleteOpenChange(true);
component.onBlur();
Expand All @@ -95,7 +101,7 @@ describe('SkyCellEditorAutocompleteComponent', () => {
component.onAutocompleteOpenChange(false);
component.onBlur();
expect(cellEditorParams.api?.stopEditing).toHaveBeenCalled();
});
}));

it('should set the correct aria label', () => {
api.getDisplayNameForColumn.and.returnValue('Testing');
Expand Down Expand Up @@ -282,20 +288,21 @@ describe('SkyCellEditorAutocompleteComponent', () => {
};
});

it('should focus on the input after it attaches to the DOM', () => {
it('should focus on the input after it attaches to the DOM', fakeAsync(() => {
fixture.detectChanges();

const input = nativeElement.querySelector('input') as HTMLInputElement;
spyOn(input, 'focus');

component.afterGuiAttached();
tick();

expect(input).toBeVisible();
expect(input.focus).toHaveBeenCalled();
});
}));

describe('cellStartedEdit is true', () => {
it('does not select the input value if Backspace triggers the edit', () => {
it('does not select the input value if Backspace triggers the edit', fakeAsync(() => {
component.agInit({
...(cellEditorParams as ICellEditorParams),
eventKey: KeyCode.BACKSPACE,
Expand All @@ -305,12 +312,13 @@ describe('SkyCellEditorAutocompleteComponent', () => {
const selectSpy = spyOn(input, 'select');

component.afterGuiAttached();
tick();

expect(input.value).toBe('');
expect(selectSpy).not.toHaveBeenCalled();
});
}));

it('does not select the input value if Delete triggers the edit', () => {
it('does not select the input value if Delete triggers the edit', fakeAsync(() => {
component.agInit({
...(cellEditorParams as ICellEditorParams),
eventKey: KeyCode.DELETE,
Expand All @@ -320,12 +328,13 @@ describe('SkyCellEditorAutocompleteComponent', () => {
const selectSpy = spyOn(input, 'select');

component.afterGuiAttached();
tick();

expect(input.value).toBe('');
expect(selectSpy).not.toHaveBeenCalled();
});
}));

it('does not select the input value if F2 triggers the edit', () => {
it('does not select the input value if F2 triggers the edit', fakeAsync(() => {
component.agInit({
...(cellEditorParams as ICellEditorParams),
eventKey: KeyCode.F2,
Expand All @@ -335,12 +344,13 @@ describe('SkyCellEditorAutocompleteComponent', () => {
const selectSpy = spyOn(input, 'select');

component.afterGuiAttached();
tick();

expect(input.value).toBe(selection.name);
expect(selectSpy).not.toHaveBeenCalled();
});
}));

it('selects the input value if Enter triggers the edit', () => {
it('selects the input value if Enter triggers the edit', fakeAsync(() => {
component.agInit({
...(cellEditorParams as ICellEditorParams),
eventKey: KeyCode.ENTER,
Expand All @@ -350,12 +360,13 @@ describe('SkyCellEditorAutocompleteComponent', () => {
const selectSpy = spyOn(input, 'select');

component.afterGuiAttached();
tick();

expect(input.value).toBe(selection.name);
expect(selectSpy).toHaveBeenCalledTimes(1);
});
}));

it('does not select the input value when a standard keyboard event triggers the edit', () => {
it('does not select the input value when a standard keyboard event triggers the edit', fakeAsync(() => {
component.agInit({
...(cellEditorParams as ICellEditorParams),
eventKey: 'a',
Expand All @@ -365,11 +376,12 @@ describe('SkyCellEditorAutocompleteComponent', () => {
const selectSpy = spyOn(input, 'select').and.callThrough();

component.afterGuiAttached();
tick();
fixture.detectChanges();

expect(input.value).toBe('a');
expect(selectSpy).toHaveBeenCalledTimes(1);
});
}));
});

describe('cellStartedEdit is false', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,29 @@ export class SkyAgGridCellEditorAutocompleteComponent
}

public afterGuiAttached(): void {
if (this.input) {
this.input.nativeElement.focus();
if (this.#triggerType === SkyAgGridCellEditorInitialAction.Replace) {
const charPress = this.#params?.eventKey as string;
// AG Grid sets focus to the cell via setTimeout, and this queues the input to focus after that.
setTimeout(() => {
if (this.input) {
this.input.nativeElement.focus();
if (this.#triggerType === SkyAgGridCellEditorInitialAction.Replace) {
const charPress = this.#params?.eventKey as string;

this.input.nativeElement.select();
this.input.nativeElement.setRangeText(charPress);
// Ensure the cursor is at the end of the text.
this.input.nativeElement.setSelectionRange(
charPress.length,
charPress.length,
);
this.input.nativeElement.dispatchEvent(new Event('input'));
this.input.nativeElement.select();
this.input.nativeElement.setRangeText(charPress);
// Ensure the cursor is at the end of the text.
this.input.nativeElement.setSelectionRange(
charPress.length,
charPress.length,
);
this.input.nativeElement.dispatchEvent(new Event('input'));
}
if (
this.#triggerType === SkyAgGridCellEditorInitialAction.Highlighted
) {
this.input.nativeElement.select();
}
}
if (this.#triggerType === SkyAgGridCellEditorInitialAction.Highlighted) {
this.input.nativeElement.select();
}
}
});
}

public getValue(): any | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('SkyCellEditorCurrencyComponent', () => {
};
});

it('initializes the SkyAgGridCellEditorCurrencyComponent properties', () => {
it('initializes the SkyAgGridCellEditorCurrencyComponent properties', fakeAsync(() => {
expect(currencyEditorComponent.columnWidth).toBeUndefined();

cellEditorParams.node = new RowNode({} as BeanCollection);
Expand All @@ -113,10 +113,11 @@ describe('SkyCellEditorCurrencyComponent', () => {

currencyEditorComponent.onPressEnter();
currencyEditorComponent.afterGuiAttached();
tick();
expect(cellEditorParams.stopEditing).not.toHaveBeenCalled();
currencyEditorComponent.onPressEnter();
expect(cellEditorParams.stopEditing).toHaveBeenCalled();
});
}));

it('should set the correct aria label', () => {
api.getDisplayNameForColumn.and.returnValue('Testing');
Expand Down Expand Up @@ -189,19 +190,20 @@ describe('SkyCellEditorCurrencyComponent', () => {
};
});

it('sets the form control value correctly', () => {
it('sets the form control value correctly', fakeAsync(() => {
expect(
currencyEditorComponent.editorForm.get('currency')?.value,
).toBeNull();

currencyEditorComponent.agInit(cellEditorParams as ICellEditorParams);
currencyEditorFixture.detectChanges();
currencyEditorComponent.afterGuiAttached();
tick();

expect(
currencyEditorComponent.editorForm.get('currency')?.value,
).toEqual(value);
});
}));

describe('cellStartedEdit is true', () => {
it('initializes with a cleared value unselected when Backspace triggers the edit', fakeAsync(() => {
Expand Down Expand Up @@ -497,7 +499,7 @@ describe('SkyCellEditorCurrencyComponent', () => {
}));
});

it('focuses on the input after it attaches to the DOM', () => {
it('focuses on the input after it attaches to the DOM', fakeAsync(() => {
currencyEditorComponent.agInit(cellEditorParams as ICellEditorParams);
currencyEditorFixture.detectChanges();

Expand All @@ -507,10 +509,11 @@ describe('SkyCellEditorCurrencyComponent', () => {
spyOn(input, 'focus');

currencyEditorComponent.afterGuiAttached();
tick();

expect(input).toBeVisible();
expect(input.focus).toHaveBeenCalled();
});
}));
});

it('returns undefined if the value is not set', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,44 +83,47 @@ export class SkyAgGridCellEditorCurrencyComponent
* afterGuiAttached is called by agGrid after the editor is rendered in the DOM. Once it is attached the editor is ready to be focused on.
*/
public afterGuiAttached(): void {
this.input?.nativeElement.focus();

// This setup is in `afterGuiAttached` due to the lifecycle of autonumeric which will highlight the initial value if it is in place when it renders.
// Since we don't want that, we set the initial value after autonumeric initializes.
this.#triggerType = SkyAgGridCellEditorUtils.getEditorInitialAction(
this.params,
);
const control = this.currency;

if (control) {
switch (this.#triggerType) {
case SkyAgGridCellEditorInitialAction.Delete:
control.setValue(undefined);
break;
case SkyAgGridCellEditorInitialAction.Replace:
control.setValue(
parseFloat(String(this.params?.eventKey)) || undefined,
);
break;
case SkyAgGridCellEditorInitialAction.Highlighted:
case SkyAgGridCellEditorInitialAction.Untouched:
default:
control.setValue(parseFloat(String(this.params?.value)));
break;
// AG Grid sets focus to the cell via setTimeout, and this queues the input to focus after that.
setTimeout(() => {
this.input?.nativeElement.focus();

// This setup is in `afterGuiAttached` due to the lifecycle of autonumeric which will highlight the initial value if it is in place when it renders.
// Since we don't want that, we set the initial value after autonumeric initializes.
this.#triggerType = SkyAgGridCellEditorUtils.getEditorInitialAction(
this.params,
);
const control = this.currency;

if (control) {
switch (this.#triggerType) {
case SkyAgGridCellEditorInitialAction.Delete:
control.setValue(undefined);
break;
case SkyAgGridCellEditorInitialAction.Replace:
control.setValue(
parseFloat(String(this.params?.eventKey)) || undefined,
);
break;
case SkyAgGridCellEditorInitialAction.Highlighted:
case SkyAgGridCellEditorInitialAction.Untouched:
default:
control.setValue(parseFloat(String(this.params?.value)));
break;
}
}
}

this.#changeDetector.markForCheck();
this.#changeDetector.markForCheck();

if (
this.#triggerType === SkyAgGridCellEditorInitialAction.Highlighted &&
(this.params?.value ?? '') !== ''
) {
this.input?.nativeElement.select();
}
if (
this.#triggerType === SkyAgGridCellEditorInitialAction.Highlighted &&
(this.params?.value ?? '') !== ''
) {
this.input?.nativeElement.select();
}

// When the cell is initialized with the Enter key, we need to suppress the first `onPressEnter`.
this.#initialized = this.params?.eventKey !== 'Enter';
// When the cell is initialized with the Enter key, we need to suppress the first `onPressEnter`.
this.#initialized = this.params?.eventKey !== 'Enter';
});
}

/**
Expand Down
Loading
Loading