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
8 changes: 8 additions & 0 deletions projects/composition/src/app/api-data/cps-button.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
"default": "solid",
"description": "Type of the button in terms of appearance, it can be 'solid' or 'outlined' or 'borderless'."
},
{
"name": "nativeType",
"optional": false,
"readonly": false,
"type": "'button' | 'submit' | 'reset'",
"default": "button",
"description": "Native HTML button type attribute, it can be 'button', 'submit' or 'reset'."
},
{
"name": "label",
"optional": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<button
type="button"
[attr.type]="nativeType"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's maybe apply this guard

[ngClass]="classesList"
[disabled]="disabled"
(click)="onClick($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('CpsButtonComponent', () => {
expect(component.contentColor).toBe('white');
expect(component.borderRadius).toBe('0.25rem');
expect(component.type).toBe('solid');
expect(component.nativeType).toBe('button');
expect(component.label).toBe('');
expect(component.icon).toBe('');
expect(component.iconPosition).toBe('before');
Expand Down Expand Up @@ -251,6 +252,36 @@ describe('CpsButtonComponent', () => {
expect(component.enterActive).toBe(false);
});

describe('nativeType', () => {
it('should default native type attribute to "button"', () => {
const button = fixture.nativeElement.querySelector('button');
expect(button.getAttribute('type')).toBe('button');
});

it('should set native type attribute to "submit"', () => {
fixture.componentRef.setInput('nativeType', 'submit');
fixture.detectChanges();
const button = fixture.nativeElement.querySelector('button');
expect(button.getAttribute('type')).toBe('submit');
});

it('should set native type attribute to "reset"', () => {
fixture.componentRef.setInput('nativeType', 'reset');
fixture.detectChanges();
const button = fixture.nativeElement.querySelector('button');
expect(button.getAttribute('type')).toBe('reset');
});

it('should not affect styling type when nativeType changes', () => {
fixture.componentRef.setInput('type', 'outlined');
fixture.componentRef.setInput('nativeType', 'submit');
fixture.detectChanges();
expect(component.classesList).toContain('cps-button--outlined');
const button = fixture.nativeElement.querySelector('button');
expect(button.getAttribute('type')).toBe('submit');
});
Comment on lines +255 to +282
});

describe('aria-label', () => {
it('should set aria-label from ariaLabel input', () => {
fixture.componentRef.setInput('ariaLabel', 'Save');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export class CpsButtonComponent implements OnChanges {
*/
@Input() type: 'solid' | 'outlined' | 'borderless' = 'solid';

/**
* Native HTML button type attribute, it can be 'button', 'submit' or 'reset'.
* @group Props
*/
@Input() nativeType: 'button' | 'submit' | 'reset' = 'button';

/**
* Label or text on the button.
* @group Props
Expand Down
Loading