Skip to content

Commit 393fc34

Browse files
committed
feat: add classes in host for group, list and separator
1 parent 815bed2 commit 393fc34

20 files changed

+167
-76
lines changed

projects/ngneat/cmdk/src/lib/components/command/command.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { ItemDirective } from '../../directives/item/item.directive';
2222
import { CmdkCommandProps } from '../../types';
2323
import { GroupComponent } from '../group/group.component';
2424
import { SeparatorComponent } from '../separator/separator.component';
25-
import { ActiveDescendantKeyManager } from '@angular/cdk/a11y';
25+
import {
26+
ActiveDescendantKeyManager,
27+
FocusKeyManager,
28+
ListKeyManager,
29+
} from '@angular/cdk/a11y';
2630
import { LoaderDirective } from '../../directives/loader/loader.directive';
2731
import { ListComponent } from '../list/list.component';
2832

@@ -100,6 +104,7 @@ export class CommandComponent
100104
this.keyManager = new ActiveDescendantKeyManager(this.items)
101105
.withWrap()
102106
.skipPredicate((item) => item.disabled || !item.filtered);
107+
103108
if (this.filter) {
104109
this.cmdkService.search$
105110
.pipe(untilDestroyed(this))
@@ -199,10 +204,8 @@ export class CommandComponent
199204
setTimeout(() => {
200205
const firstItem = this.filteredItems?.[0];
201206
if (firstItem) {
202-
console.log('first item found');
203207
this.keyManager.setFirstItemActive();
204208
} else {
205-
console.log('first item not found');
206209
this.valueChanged.emit(undefined);
207210
}
208211
});
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
<ng-container
2-
*ngIf="showGroup"
3-
>
4-
<div role="presentation" *ngIf="label" class="cmdk-group-label">
5-
<ng-container *dynamicView="label"></ng-container>
6-
</div>
7-
<div class="cmdk-group-content" role="group" [attr.aria-label]="ariaLabel">
8-
<ng-content></ng-content>
9-
</div>
10-
</ng-container>
1+
<div role="presentation" *ngIf="label" class="cmdk-group-label">
2+
<ng-container *dynamicView="label"></ng-container>
3+
</div>
4+
<div class="cmdk-group-content" role="group" [attr.aria-label]="ariaLabel">
5+
<ng-content></ng-content>
6+
</div>

projects/ngneat/cmdk/src/lib/components/group/group.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ export class GroupComponent implements CmdkGroupProps {
6060
get activeClass() {
6161
return this.active;
6262
}
63+
64+
@HostBinding('attr.cmdk-hidden')
65+
get hidden() {
66+
return !this.showGroup;
67+
}
6368
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
<ng-container
2-
*ngIf="showList"
3-
>
4-
<div class="cmdk-list-content" role="listbox" [attr.aria-label]="ariaLabel">
5-
<ng-content></ng-content>
6-
</div>
7-
</ng-container>
1+
<div class="cmdk-list-content" role="listbox" [attr.aria-label]="ariaLabel">
2+
<ng-content></ng-content>
3+
</div>

projects/ngneat/cmdk/src/lib/components/list/list.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ export class ListComponent {
5757
get activeClass() {
5858
return this.active;
5959
}
60+
61+
@HostBinding('attr.cmdk-hidden')
62+
get hidden() {
63+
return !this.showList;
64+
}
6065
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<hr *ngIf="showSeparator">
1+
<hr>

projects/ngneat/cmdk/src/lib/components/separator/separator.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ChangeDetectionStrategy,
33
ChangeDetectorRef,
44
Component,
5+
HostBinding,
56
inject,
67
} from '@angular/core';
78

@@ -21,4 +22,9 @@ import {
2122
export class SeparatorComponent {
2223
showSeparator = true;
2324
public cdr = inject(ChangeDetectorRef);
25+
26+
@HostBinding('attr.cmdk-hidden')
27+
get hidden() {
28+
return !this.showSeparator;
29+
}
2430
}

projects/ngneat/cmdk/src/lib/directives/empty/empty.directive.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Directive,
33
inject,
4+
Renderer2,
45
TemplateRef,
56
ViewContainerRef,
67
} from '@angular/core';
@@ -16,10 +17,12 @@ export class EmptyDirective {
1617
private _hasView = false;
1718
private _templateRef = inject(TemplateRef<any>);
1819
private _viewContainer = inject(ViewContainerRef);
20+
private _renderer2 = inject(Renderer2);
1921

2022
set cmdkEmpty(condition: boolean | string) {
2123
if (condition && !this._hasView) {
22-
this._viewContainer.createEmbeddedView(this._templateRef);
24+
const emb = this._viewContainer.createEmbeddedView(this._templateRef);
25+
this._renderer2.addClass(emb.rootNodes[0], 'cmdk-empty');
2326
this._hasView = true;
2427
} else if (!condition && this._hasView) {
2528
this._viewContainer.clear();
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
import { Directive, HostListener, inject } from '@angular/core';
1+
import {
2+
AfterViewInit,
3+
Directive,
4+
ElementRef,
5+
HostListener,
6+
inject,
7+
Input,
8+
OnDestroy,
9+
} from '@angular/core';
210
import { CmdkService } from '../../cmdk.service';
311

412
@Directive({
513
selector: '[cmdkInput]',
614
// eslint-disable-next-line @angular-eslint/no-host-metadata-property
715
host: {
816
class: 'cmdk-input',
9-
type: 'search',
1017
},
1118
})
12-
export class InputDirective {
19+
export class InputDirective implements AfterViewInit, OnDestroy {
20+
@Input() updateOn: 'blur' | 'change' | 'input' = 'input';
1321
private _cmdkService = inject(CmdkService);
22+
private _elementRef = inject<ElementRef<HTMLInputElement>>(
23+
ElementRef<HTMLInputElement>
24+
);
1425

15-
@HostListener('change', ['$event.target.value'])
1626
search(value: string) {
1727
this._cmdkService.setSearch(value);
1828
}
29+
30+
ngAfterViewInit(): void {
31+
this._elementRef.nativeElement.addEventListener(this.updateOn, (ev) =>
32+
this.search((ev.target as HTMLInputElement).value)
33+
);
34+
}
35+
36+
ngOnDestroy(): void {
37+
this._elementRef.nativeElement.removeAllListeners &&
38+
this._elementRef.nativeElement.removeAllListeners(this.updateOn);
39+
}
1940
}

projects/ngneat/cmdk/src/lib/directives/item/item.directive.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Highlightable } from '@angular/cdk/a11y';
1+
import {
2+
FocusableOption,
3+
FocusOrigin,
4+
Highlightable,
5+
ListKeyManagerOption,
6+
} from '@angular/cdk/a11y';
27
import {
38
Directive,
49
ElementRef,
@@ -23,7 +28,7 @@ let cmdkItemId = 0;
2328
class: 'cmdk-item',
2429
},
2530
})
26-
export class ItemDirective implements CmdkItemProps, Highlightable {
31+
export class ItemDirective implements CmdkItemProps, ListKeyManagerOption {
2732
@Input() disabled = false;
2833
@Output() selected = new EventEmitter();
2934
getLabel?(): string {
@@ -65,9 +70,9 @@ export class ItemDirective implements CmdkItemProps, Highlightable {
6570
}
6671
}
6772

68-
@HostBinding('style.display')
69-
get display() {
70-
return !this.filtered ? 'none' : '';
73+
@HostBinding('attr.cmdk-hidden')
74+
get hidden() {
75+
return !this.filtered;
7176
}
7277

7378
@HostBinding('attr.role')
@@ -102,9 +107,6 @@ export class ItemDirective implements CmdkItemProps, Highlightable {
102107

103108
set active(value: boolean) {
104109
this._active = value;
105-
if (value) {
106-
this._elementRef.nativeElement.focus();
107-
}
108110
}
109111

110112
@HostListener('mouseup')

0 commit comments

Comments
 (0)