|
1 |
| -import { |
2 |
| - AfterContentInit, |
3 |
| - ContentChild, |
4 |
| - Directive, |
5 |
| - ElementRef, |
6 |
| - HostBinding, |
7 |
| - inject, |
8 |
| - Input, |
9 |
| - OnChanges, |
10 |
| - Renderer2, |
11 |
| - SimpleChanges |
12 |
| -} from '@angular/core'; |
| 1 | +import { computed, contentChild, Directive, effect, ElementRef, inject, input, Renderer2 } from '@angular/core'; |
13 | 2 |
|
14 | 3 | import { PageLinkDirective } from '../page-link/page-link.directive';
|
15 | 4 |
|
16 | 5 | @Directive({
|
17 | 6 | selector: '[cPageItem]',
|
18 |
| - host: { class: 'page-item' } |
| 7 | + host: { |
| 8 | + class: 'page-item', |
| 9 | + '[class]': 'hostClasses()', |
| 10 | + '[attr.aria-current]': 'ariaCurrent()' |
| 11 | + } |
19 | 12 | })
|
20 |
| -export class PageItemDirective implements AfterContentInit, OnChanges { |
| 13 | +export class PageItemDirective { |
21 | 14 | readonly #renderer = inject(Renderer2);
|
22 | 15 |
|
23 | 16 | /**
|
24 | 17 | * Toggle the active state for the component.
|
25 |
| - * @type boolean |
| 18 | + * @return boolean |
26 | 19 | */
|
27 |
| - @Input() active?: boolean; |
| 20 | + readonly active = input<boolean>(); |
| 21 | + |
28 | 22 | /**
|
29 | 23 | * Toggle the disabled state for the component.
|
30 |
| - * @type boolean |
| 24 | + * @return boolean |
31 | 25 | */
|
32 |
| - @Input() disabled?: boolean; |
| 26 | + readonly disabled = input<boolean>(); |
33 | 27 |
|
34 |
| - @HostBinding('attr.aria-current') |
35 |
| - get ariaCurrent(): string | null { |
36 |
| - return this.active ? 'page' : null; |
37 |
| - } |
| 28 | + readonly ariaCurrent = computed(() => { |
| 29 | + return this.active() ? 'page' : null; |
| 30 | + }); |
38 | 31 |
|
39 |
| - @HostBinding('class') |
40 |
| - get hostClasses(): any { |
| 32 | + readonly hostClasses = computed(() => { |
41 | 33 | return {
|
42 | 34 | 'page-item': true,
|
43 |
| - disabled: this.disabled, |
44 |
| - active: this.active |
45 |
| - }; |
46 |
| - } |
47 |
| - |
48 |
| - @ContentChild(PageLinkDirective, { read: ElementRef }) pageLinkElementRef!: ElementRef; |
| 35 | + disabled: this.disabled(), |
| 36 | + active: this.active() |
| 37 | + } as Record<string, boolean>; |
| 38 | + }); |
49 | 39 |
|
50 |
| - ngAfterContentInit(): void { |
51 |
| - this.setAttributes(); |
52 |
| - } |
| 40 | + readonly pageLinkElementRef = contentChild(PageLinkDirective, { read: ElementRef }); |
53 | 41 |
|
54 |
| - ngOnChanges(changes: SimpleChanges): void { |
55 |
| - if (changes['disabled']) { |
56 |
| - this.setAttributes(); |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - setAttributes(): void { |
61 |
| - if (!this.pageLinkElementRef) { |
| 42 | + readonly pageLinkElementRefEffect = effect(() => { |
| 43 | + const pageLinkElementRef = this.pageLinkElementRef(); |
| 44 | + const disabled = this.disabled(); |
| 45 | + if (!pageLinkElementRef) { |
62 | 46 | return;
|
63 | 47 | }
|
64 |
| - const pageLinkElement = this.pageLinkElementRef.nativeElement; |
| 48 | + const pageLinkElement = pageLinkElementRef.nativeElement; |
65 | 49 |
|
66 |
| - if (this.disabled) { |
| 50 | + if (disabled) { |
67 | 51 | this.#renderer.setAttribute(pageLinkElement, 'aria-disabled', 'true');
|
68 | 52 | this.#renderer.setAttribute(pageLinkElement, 'tabindex', '-1');
|
69 | 53 | } else {
|
70 | 54 | this.#renderer.removeAttribute(pageLinkElement, 'aria-disabled');
|
71 | 55 | this.#renderer.removeAttribute(pageLinkElement, 'tabindex');
|
72 | 56 | }
|
73 |
| - } |
| 57 | + }); |
74 | 58 | }
|
0 commit comments