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(Button): Set the native disabled attribute #1621

Merged
merged 2 commits into from
Oct 18, 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
48 changes: 46 additions & 2 deletions projects/novo-elements/src/elements/button/Button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// NG2
import { TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
// APP
import { NovoButtonElement } from './Button';
import { Component, Input } from '@angular/core';

describe('Elements: NovoButtonElement', () => {
let fixture;
let fixture: ComponentFixture<NovoButtonElement>;
let component;

beforeAll(() => {
Expand All @@ -21,4 +23,46 @@ describe('Elements: NovoButtonElement', () => {
it('should be compiled', () => {
expect(component).toBeDefined();
});

it('should not assign disabled attribute to self when registered as <novo-button>', () => {
fixture.componentRef.setInput('disabled', true);
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.getAttribute('disabled')).toBe(null);
});
});

@Component({
template: '<button theme="dialogue" [disabled]="disableButton"></button>',
selector: 'test-button-component',
})
class TestButtonContainer {
@Input() disableButton = false;
}

describe('<button> with NovoButtonElement directive', () => {
let fixture: ComponentFixture<TestButtonContainer>;
let component;

beforeAll(() => {
TestBed.configureTestingModule({
declarations: [NovoButtonElement, TestButtonContainer],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TestButtonContainer);
component = fixture.componentInstance;
});

it('should be compiled', () => {
fixture.detectChanges();
expect(component).toBeDefined();
expect(fixture.debugElement.query(By.directive(NovoButtonElement))).toBeTruthy();
});

it('should disable the element when the disabled property is specified', () => {
fixture.componentRef.setInput('disableButton', true);
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('button')).nativeElement.disabled).toBe(true);
});
});
13 changes: 11 additions & 2 deletions projects/novo-elements/src/elements/button/Button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// NG2
import { ChangeDetectionStrategy, Component, ElementRef, HostBinding, HostListener, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, ElementRef, HostBinding, HostListener, Input, OnChanges, SimpleChanges } from '@angular/core';
import { BooleanInput, Helpers, Key } from 'novo-elements/utils';

@Component({
Expand Down Expand Up @@ -63,7 +63,7 @@ import { BooleanInput, Helpers, Key } from 'novo-elements/utils';
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NovoButtonElement {
export class NovoButtonElement implements OnChanges {
/**
* The text color of the button. Should be used for Icon buttons. see theme.
*/
Expand Down Expand Up @@ -107,10 +107,19 @@ export class NovoButtonElement {
@HostBinding('class.novo-button-disabled')
disabled: boolean = false;

@HostBinding('attr.disabled')
disabledAttr: undefined | '' = undefined;

private _icon: string;

constructor(public element: ElementRef) {}

ngOnChanges(changes: SimpleChanges): void {
if (changes.disabled && this.element.nativeElement.tagName === 'BUTTON') {
this.disabledAttr = changes.disabled.currentValue ? '' : undefined;
}
}

@HostListener('keydown', ['$event'])
handleKeydown(event: KeyboardEvent) {
if ((Key.Enter === event.key || Key.Space === event.key) && (this.disabled || this.loading)) {
Expand Down
Loading