Skip to content

Commit 23a709d

Browse files
ygggnnixaa
authored andcommitted
fix(nbButton): set disabled DOM property (#871)
1 parent e06d3a7 commit 23a709d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/framework/theme/components/accordion/accordion-item-header.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class NbAccordionItemHeaderComponent implements OnInit, OnDestroy {
5858
return !this.accordionItem.collapsed;
5959
}
6060

61+
// issue #794
6162
@HostBinding('attr.tabindex')
6263
get tabbable(): string {
6364
return this.accordionItem.disabled ? '-1' : '0';

src/framework/theme/components/button/button.component.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*/
66

7-
import { Component, Input, HostBinding, HostListener } from '@angular/core';
7+
import { Component, Input, HostBinding, HostListener, Renderer2, ElementRef } from '@angular/core';
88
import { convertToBoolProperty } from '../helpers';
99

1010
/**
@@ -203,6 +203,7 @@ export class NbButtonComponent {
203203
@HostBinding('attr.aria-disabled')
204204
@HostBinding('class.btn-disabled') disabled: boolean;
205205

206+
// issue #794
206207
@HostBinding('attr.tabindex')
207208
get tabbable(): string {
208209
return this.disabled ? '-1' : '0';
@@ -256,6 +257,7 @@ export class NbButtonComponent {
256257
@Input('disabled')
257258
set setDisabled(val: boolean) {
258259
this.disabled = convertToBoolProperty(val);
260+
this.renderer.setProperty(this.hostElement.nativeElement, 'disabled', this.disabled);
259261
}
260262

261263
/**
@@ -276,11 +278,26 @@ export class NbButtonComponent {
276278
this.outline = convertToBoolProperty(val);
277279
}
278280

281+
/**
282+
* @private
283+
* Keep this handler to partially support anchor disabling.
284+
* Unlike button, anchor doesn't have 'disabled' DOM property,
285+
* so handler will be called anyway. We preventing navigation and bubbling.
286+
* Disabling is partial due to click handlers precedence. Consider example:
287+
* <a nbButton [disabled]="true" (click)="clickHandler()">...</a>
288+
* 'clickHandler' will be called before our host listener below. We can't prevent
289+
* such handlers call.
290+
*/
279291
@HostListener('click', ['$event'])
280292
onClick(event: Event) {
281293
if (this.disabled) {
282294
event.preventDefault();
283295
event.stopImmediatePropagation();
284296
}
285297
}
298+
299+
constructor(
300+
protected renderer: Renderer2,
301+
protected hostElement: ElementRef<HTMLElement>,
302+
) {}
286303
}

0 commit comments

Comments
 (0)