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(sbb-autocomplete, sbb-autocomplete-grid): avoid form submission on enter press #3243

Merged
merged 2 commits into from
Nov 26, 2024
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
Expand Up @@ -146,21 +146,23 @@ describe(`sbb-autocomplete-grid`, () => {
});

it('select by mouse', async () => {
const willOpenEventSpy = new EventSpy(SbbAutocompleteGridElement.events.willOpen);
const didOpenEventSpy = new EventSpy(SbbAutocompleteGridElement.events.didOpen);
const optionSelectedEventSpy = new EventSpy(
SbbAutocompleteGridOptionElement.events.optionSelected,
);
const optTwo = element.querySelector<SbbAutocompleteGridOptionElement>('#option-2')!;

input.focus();
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
await sendKeys({ press: 'ArrowDown' });
await sendKeys({ press: 'Enter' });
const positionRect = optTwo.getBoundingClientRect();

await sendMouse({
type: 'click',
position: [
Math.round(positionRect.x + window.scrollX + positionRect.width / 2),
Math.round(positionRect.y + window.scrollY + positionRect.height / 2),
],
});
await waitForLitRender(element);

expect(optionSelectedEventSpy.count).to.be.equal(1);
Expand Down Expand Up @@ -244,6 +246,8 @@ describe(`sbb-autocomplete-grid`, () => {
);
const optOne = element.querySelector('#option-1');
const optTwo = element.querySelector('#option-2');
const keydownSpy = new EventSpy('keydown', input);

input.focus();

await didOpenEventSpy.calledOnce();
Expand All @@ -261,6 +265,7 @@ describe(`sbb-autocomplete-grid`, () => {
await sendKeys({ press: 'Enter' });
await didCloseEventSpy.calledOnce();
expect(didCloseEventSpy.count).to.be.equal(1);
expect(keydownSpy.lastEvent?.defaultPrevented).to.be.true;

expect(optTwo).not.to.have.attribute('data-active');
expect(optTwo).to.have.attribute('selected');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class SbbAutocompleteGridElement extends SbbAutocompleteBaseElement {
break;

case 'Enter':
this.selectByKeyboard();
this.selectByKeyboard(event);
break;

case 'ArrowDown':
Expand All @@ -118,7 +118,9 @@ class SbbAutocompleteGridElement extends SbbAutocompleteBaseElement {
* and greater than zero when a button is 'focused', so asking for `querySelectorAll(...)[this._activeColumnIndex]`
* would always return a `SbbAutocompleteGridButtonElement`.
*/
protected selectByKeyboard(): void {
protected selectByKeyboard(event: KeyboardEvent): void {
event.preventDefault();

if (this._activeColumnIndex !== 0) {
(
this._row[this._activeItemIndex].querySelectorAll(
Expand Down
28 changes: 21 additions & 7 deletions src/elements/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,28 @@ describe(`sbb-autocomplete`, () => {
});

it('select by mouse', async () => {
const willOpenEventSpy = new EventSpy(SbbAutocompleteElement.events.willOpen);
const didOpenEventSpy = new EventSpy(SbbAutocompleteElement.events.didOpen);
const optionSelectedEventSpy = new EventSpy(SbbOptionElement.events.optionSelected);
const inputEventSpy = new EventSpy('input', input);
const changeEventSpy = new EventSpy('change', input);
const optTwo = element.querySelector<SbbOptionElement>('#option-2')!;

input.focus();
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
await sendKeys({ press: 'ArrowDown' });
await sendKeys({ press: 'Enter' });
const positionRect = optTwo.getBoundingClientRect();

await sendMouse({
type: 'click',
position: [
Math.round(positionRect.x + window.scrollX + positionRect.width / 2),
Math.round(positionRect.y + window.scrollY + positionRect.height / 2),
],
});
await waitForLitRender(element);

expect(inputEventSpy.count).to.be.equal(1);
expect(changeEventSpy.count).to.be.equal(1);
expect(optionSelectedEventSpy.count).to.be.equal(1);
expect(optionSelectedEventSpy.firstEvent!.target).to.have.property('id', 'option-2');
});
Expand All @@ -139,8 +146,12 @@ describe(`sbb-autocomplete`, () => {
const didOpenEventSpy = new EventSpy(SbbAutocompleteElement.events.didOpen);
const didCloseEventSpy = new EventSpy(SbbAutocompleteElement.events.didClose);
const optionSelectedEventSpy = new EventSpy(SbbOptionElement.events.optionSelected);
const inputEventSpy = new EventSpy('input', input);
const changeEventSpy = new EventSpy('change', input);
const optOne = element.querySelector('#option-1');
const optTwo = element.querySelector('#option-2');
const keydownSpy = new EventSpy('keydown', input);

input.focus();

await didOpenEventSpy.calledOnce();
Expand All @@ -158,9 +169,12 @@ describe(`sbb-autocomplete`, () => {
await sendKeys({ press: 'Enter' });
await didCloseEventSpy.calledOnce();
expect(didCloseEventSpy.count).to.be.equal(1);
expect(keydownSpy.lastEvent?.defaultPrevented).to.be.true;

expect(optTwo).not.to.have.attribute('data-active');
expect(optTwo).to.have.attribute('selected');
expect(inputEventSpy.count).to.be.equal(1);
expect(changeEventSpy.count).to.be.equal(1);
expect(optionSelectedEventSpy.count).to.be.equal(1);
expect(input).to.have.attribute('aria-expanded', 'false');
expect(input).not.to.have.attribute('aria-activedescendant');
Expand Down
5 changes: 3 additions & 2 deletions src/elements/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SbbAutocompleteElement extends SbbAutocompleteBaseElement {
break;

case 'Enter':
this.selectByKeyboard();
this.selectByKeyboard(event);
break;

case 'ArrowDown':
Expand All @@ -92,7 +92,8 @@ class SbbAutocompleteElement extends SbbAutocompleteBaseElement {
}
}

protected selectByKeyboard(): void {
protected selectByKeyboard(event: KeyboardEvent): void {
event.preventDefault();
const activeOption = this.options[this._activeItemIndex];

if (activeOption) {
Expand Down