Skip to content

Commit

Permalink
fix(dropdown): allow setting custom tabindex on items (#4626)
Browse files Browse the repository at this point in the history
Fixes #4592
  • Loading branch information
maxokorokov committed Nov 16, 2023
1 parent 472d842 commit 3d9f052
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/dropdown/dropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,23 @@ describe('ngb-dropdown', () => {
expect(itemEl.tabIndex).toBe(-1);
});

it('should allow using custom tabindex on a dropdown item', () => {
const html = `
<button ngbDropdownItem>one</button>
<button ngbDropdownItem disabled>two</button>
<button ngbDropdownItem tabindex='3'>three</button>
<button ngbDropdownItem tabindex='2' disabled>four</button>
`;

const fixture = createTestComponent(html);
const [one, two, three, four] = Array.from(fixture.nativeElement.querySelectorAll('button')) as HTMLButtonElement[];

expect(one.tabIndex).toBe(0);
expect(two.tabIndex).toBe(-1);
expect(three.tabIndex).toBe(3);
expect(four.tabIndex).toBe(-1);
});

it('should disable a link dropdown item', () => {
const html = `<a ngbDropdownItem [disabled]="disabled">dropDown item</a>`;

Expand Down
8 changes: 7 additions & 1 deletion src/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import { getActiveElement } from '../util/util';
@Directive({
selector: '[ngbDropdownItem]',
standalone: true,
host: { class: 'dropdown-item', '[class.disabled]': 'disabled', '[tabIndex]': 'disabled ? -1 : 0' },
host: {
class: 'dropdown-item',
'[class.disabled]': 'disabled',
'[tabIndex]': 'disabled ? -1 : tabindex',
},
})
export class NgbDropdownItem {
static ngAcceptInputType_disabled: boolean | '';
Expand All @@ -48,6 +52,8 @@ export class NgbDropdownItem {

nativeElement = inject(ElementRef).nativeElement as HTMLElement;

@Input() tabindex: string | number = 0;

@Input()
set disabled(value: boolean) {
this._disabled = <any>value === '' || value === true; // accept an empty attribute as true
Expand Down

0 comments on commit 3d9f052

Please sign in to comment.