Skip to content
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
5 changes: 5 additions & 0 deletions src/aria/accordion/accordion-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ export class AccordionTrigger implements OnInit, OnDestroy {
_pattern!: AccordionTriggerPattern;

constructor() {
// Automatically prevent form submission.
if (this.element.tagName === 'BUTTON' && !this.element.hasAttribute('type')) {
this.element.setAttribute('type', 'button');
}

// Check for any violations after the DOM has been updated.
if (typeof ngDevMode === 'undefined' || ngDevMode) {
afterRenderEffect({
Expand Down
6 changes: 6 additions & 0 deletions src/aria/accordion/accordion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ describe('AccordionGroup', () => {
expect(getTriggerAttribute(2, 'role')).toBe('button');
});

it('should set type="button" by default on buttons', () => {
expect(getTriggerAttribute(0, 'type')).toBe('button');
expect(getTriggerAttribute(1, 'type')).toBe('button');
expect(getTriggerAttribute(2, 'type')).toBe('button');
});

it('should have aria-expanded="false" when collapsed', () => {
expect(getTriggerAttribute(0, 'aria-expanded')).toBe('false');
expect(getTriggerAttribute(1, 'aria-expanded')).toBe('false');
Expand Down
5 changes: 5 additions & 0 deletions src/aria/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export class MenuTrigger<V> {
constructor() {
effect(() => this.menu()?.parent.set(this));
effect(() => this._pattern.pendingFocusEffect());

// Automatically prevent form submission.
if (this.element.tagName === 'BUTTON' && !this.element.hasAttribute('type')) {
this.element.setAttribute('type', 'button');
}
}

/** Opens the menu focusing on the first menu item. */
Expand Down
4 changes: 4 additions & 0 deletions src/aria/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ describe('CDK Overlay Menu Pattern', () => {
await keydown(trigger, 'Enter');
expect(document.activeElement).toBe(getItem('Apple'));
});

it('should set type="button" by default on button triggers', () => {
expect(getTrigger().getAttribute('type')).toBe('button');
});
});

describe('Menu Bar Pattern', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/aria/tabs/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export class Tab implements HasElement, OnInit, OnDestroy {
}

constructor() {
// Automatically prevent form submission.
if (this.element.tagName === 'BUTTON' && !this.element.hasAttribute('type')) {
this.element.setAttribute('type', 'button');
}

if (typeof ngDevMode === 'undefined' || ngDevMode) {
afterRenderEffect({
read: () => {
Expand Down
Loading