Skip to content

Commit

Permalink
feat(ng-keyboard-sort)!: update to Angular 19 (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhwhite authored Nov 22, 2024
1 parent 2ca8133 commit b15973f
Show file tree
Hide file tree
Showing 22 changed files with 7,339 additions and 5,048 deletions.
6 changes: 2 additions & 4 deletions apps/e2e/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Component, inject } from '@angular/core';
import { ROUTES, RouterLink, RouterOutlet } from '@angular/router';
import { NgFor, NgIf } from '@angular/common';
import { RouterLink, RouterOutlet, ROUTES } from '@angular/router';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: true,
imports: [NgFor, NgIf, RouterLink, RouterOutlet],
imports: [RouterLink, RouterOutlet],
})
export class AppComponent {
public routes = inject(ROUTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CdkDropList,
moveItemInArray,
} from '@angular/cdk/drag-drop';
import { NgFor } from '@angular/common';
import {
KeyboardSortHandleDirective,
KeyboardSortItemDirective,
Expand All @@ -23,7 +22,6 @@ export interface Item {
selector: 'app-example-with-handles',
templateUrl: './example-with-handles.component.html',
styleUrls: ['./example-with-handles.component.css'],
standalone: true,
imports: [
CdkDrag,
CdkDropList,
Expand All @@ -32,7 +30,6 @@ export interface Item {
KeyboardSortItemIfActiveDirective,
KeyboardSortItemIfFocusedDirective,
KeyboardSortListDirective,
NgFor,
],
})
export class ExampleWithHandlesComponent {
Expand Down
17 changes: 14 additions & 3 deletions apps/e2e/src/app/example/example.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ <h1>Keyboard Sort Example</h1>
kbdSortList
cdkDropListOrientation="horizontal"
kbdSortListOrientation="horizontal"
[cdkDropListData]="items"
[(kbdSortListData)]="items">
@for (item of items; track item; let i = $index) {
[cdkDropListData]="items()"
[(kbdSortListData)]="items"
[kbdSortListDisabled]="!enabled()">
@for (item of items(); track item; let i = $index) {
<li cdkDrag cdkDragLockAxis="x" [kbdSortItem]="i">
{{ item }}
<span *kbdSortKeyboardSortItemIfActive class="indicator active-indicator"
Expand All @@ -28,3 +29,13 @@ <h1>Keyboard Sort Example</h1>
</li>
}
</ul>
<div>
<label
><input [(ngModel)]="enabled" type="checkbox" /> Keyboard enabled</label
>
</div>
<div>
<button type="button" (click)="resetData()" (keydown.enter)="resetData()">
Reset data
</button>
</div>
17 changes: 16 additions & 1 deletion apps/e2e/src/app/example/example.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('ExampleComponent', () => {
});

it('should move items up and down', () => {
const items = component.items;
const items = component.items();
const firstItem = items[0];
const secondItem = items[1];
const event = {
Expand All @@ -45,4 +45,19 @@ describe('ExampleComponent', () => {
expect(items[0]).toEqual(secondItem);
expect(items[1]).toEqual(firstItem);
});

it('should reset data', () => {
component.resetData();
expect(component.items()).toEqual([
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
]);
});
});
29 changes: 23 additions & 6 deletions apps/e2e/src/app/example/example.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Component } from '@angular/core';
import { Component, model } from '@angular/core';
import {
CdkDrag,
CdkDragDrop,
CdkDropList,
moveItemInArray,
} from '@angular/cdk/drag-drop';
import { NgFor } from '@angular/common';
import { FormsModule } from '@angular/forms';
import {
KeyboardSortItemDirective,
KeyboardSortItemIfActiveDirective,
Expand All @@ -17,22 +17,39 @@ import {
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
standalone: true,
imports: [
CdkDrag,
CdkDropList,
FormsModule,
KeyboardSortListDirective,
KeyboardSortItemDirective,
KeyboardSortItemIfActiveDirective,
KeyboardSortItemIfFocusedDirective,
NgFor,
],
})
export class ExampleComponent {
public items: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
public readonly items = model<string[]>([
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
]);
public readonly enabled = model<boolean>(true);

public drop($event: CdkDragDrop<string[]>) {
moveItemInArray(this.items, $event.previousIndex, $event.currentIndex);
const items = this.items();
moveItemInArray(items, $event.previousIndex, $event.currentIndex);
this.items.set([...items]);
}

public resetData(): void {
this.items.set(
[...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].slice(0, this.items().length + 1)
);
}
}

Expand Down
16 changes: 15 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ module.exports = tseslint.config(
eslintConfigPrettier,
],
processor: angular.processInlineTemplates,
rules: {},
rules: {
'@typescript-eslint/no-unused-expressions': [
'error',
{
allowShortCircuit: true,
allowTernary: true,
},
],
'@typescript-eslint/no-empty-function': [
'error',
{
allow: [],
},
],
},
},
{
files: ['**/*.html'],
Expand Down
6 changes: 3 additions & 3 deletions libs/ng-keyboard-sort/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"directory": "libs/ng-keyboard-sort"
},
"peerDependencies": {
"@angular/cdk": "^18.2.0",
"@angular/common": "^18.2.0",
"@angular/core": "^18.2.0",
"@angular/cdk": "^19.0.0",
"@angular/common": "^19.0.0",
"@angular/core": "^19.0.0",
"rxjs": "^7.8.0"
},
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Component } from '@angular/core';
import { KeyboardSortHandleDirective } from '../keyboard-sort-handle.directive';

@Component({
standalone: true,
imports: [KeyboardSortHandleDirective],
template: ` <div kbdSortHandle>Handle</div> `,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Component, model, ViewChild } from '@angular/core';
import { CommonModule } from '@angular/common';
import { KeyboardSortHandleDirective } from '../keyboard-sort-handle.directive';
import { KeyboardSortItemDirective } from '../keyboard-sort-item.directive';
import { KeyboardSortListService } from '../keyboard-sort-list.service';
import { KeyboardSortItemIfActiveDirective } from '../keyboard-sort-item-if-active.directive';
import { KeyboardSortItemIfFocusedDirective } from '../keyboard-sort-item-if-focused.directive';

@Component({
standalone: true,
imports: [
CommonModule,
KeyboardSortItemDirective,
KeyboardSortHandleDirective,
KeyboardSortItemIfActiveDirective,
Expand All @@ -23,9 +20,9 @@ import { KeyboardSortItemIfFocusedDirective } from '../keyboard-sort-item-if-foc
[activated]="activated()"
#item
id="example-item">
<div *ngIf="showHandle" kbdSortHandle #handle class="example-handle">
Handle
</div>
@if (showHandle()) {
<div kbdSortHandle #handle class="example-handle">Handle</div>
}
<span>Item 1</span>
<span *kbdSortKeyboardSortItemIfActive #active>Active</span>
<span *kbdSortKeyboardSortItemIfFocused #focus>Focused</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Component, ViewChild } from '@angular/core';
import { CommonModule } from '@angular/common';
import { KeyboardSortListDirective } from '../keyboard-sort-list.directive';

@Component({
standalone: true,
imports: [CommonModule, KeyboardSortListDirective],
imports: [KeyboardSortListDirective],
template: `<ul kbdSortList></ul>`,
})
export class KeyboardSortListEmptyFixtureComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import {
ViewChild,
ViewChildren,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { KeyboardSortItemDirective } from '../keyboard-sort-item.directive';
import { KeyboardSortListDirective } from '../keyboard-sort-list.directive';
import { KeyboardSortItemIfFocusedDirective } from '../keyboard-sort-item-if-focused.directive';
import { KeyboardSortItemIfActiveDirective } from '../keyboard-sort-item-if-active.directive';
import { KeyboardSortEventDrop } from '../keyboard-sort-event-drop';

@Component({
standalone: true,
imports: [
CommonModule,
KeyboardSortListDirective,
KeyboardSortItemDirective,
KeyboardSortItemIfFocusedDirective,
Expand All @@ -25,7 +22,7 @@ import { KeyboardSortEventDrop } from '../keyboard-sort-event-drop';
<a href="#item-0">Start</a>
<ul
kbdSortList
[kbdSortListData]="data() || []"
[(kbdSortListData)]="data"
[kbdSortListOrientation]="direction()"
[kbdSortListDisabled]="disabled()"
(kdbSortDrop)="sortDrop($event)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { KeyboardSortItemIfActiveDirective } from 'ng-keyboard-sort';
import { KeyboardSortItemService } from './keyboard-sort-item.service';

@Component({
standalone: true,
selector: 'kbd-sort-test-component',
template: ` <div *kbdSortKeyboardSortItemIfActive></div> `,
imports: [KeyboardSortItemIfActiveDirective],
Expand All @@ -20,7 +19,7 @@ describe('KeyboardSortItemIfActiveDirective', () => {
providers: [
{
provide: KeyboardSortItemService,
useValue: { item: { activated, isDisabled } },
useValue: { item: signal({ activated, isDisabled }) },
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export class KeyboardSortItemIfActiveDirective {

constructor() {
effect(() => {
const activated = !!this.#item?.activated();
const enabled = !this.#item?.isDisabled();
const shouldShow = activated && enabled;
const shouldShow = !!this.#item()?.activated();
if (shouldShow && !this.#hasView) {
this.#viewContainer.createEmbeddedView(this.#templateRef);
this.#hasView = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { KeyboardSortItemIfFocusedDirective } from 'ng-keyboard-sort';
import { KeyboardSortItemService } from './keyboard-sort-item.service';

@Component({
standalone: true,
selector: 'kbd-sort-test-component',
template: ` <div *kbdSortKeyboardSortItemIfFocused></div> `,
imports: [KeyboardSortItemIfFocusedDirective],
Expand All @@ -20,7 +19,7 @@ describe('KeyboardSortItemIfFocusedDirective', () => {
providers: [
{
provide: KeyboardSortItemService,
useValue: { item: { focused, isDisabled } },
useValue: { item: signal({ focused, isDisabled }) },
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export class KeyboardSortItemIfFocusedDirective {

constructor() {
effect(() => {
const focused = !!this.#item?.focused();
const enabled = !this.#item?.isDisabled();
const shouldShow = focused && enabled;
const shouldShow = !!this.#item()?.focused();
if (shouldShow && !this.#hasView) {
this.#viewContainer.createEmbeddedView(this.#templateRef);
this.#hasView = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ describe('ItemDirective', () => {
setupTest();
const item = component.item;
expect(item).toBeTruthy();
item?.ngAfterViewInit();
expect(item?.activated()).toBeFalse();
item?.elementRef.nativeElement.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter' })
Expand Down
Loading

0 comments on commit b15973f

Please sign in to comment.