Skip to content

Commit e6c75c7

Browse files
palbizuPatricio Albizu
andauthored
feat: Draggable List (#1483)
* Dragganble List in progress * feat: add draggable list * feat: fixing lint * feat: fixing lint * feat: fix prettier * feat: adding disabled styles and modify child component * feat: adding T to draggable list Co-authored-by: Patricio Albizu <[email protected]>
1 parent a86d03c commit e6c75c7

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
2+
import { ContentHolder, CONTENT_HOLDER_TEMPLATE } from '../../public-api';
3+
4+
@Component({
5+
selector: 'ht-draggable-item',
6+
changeDetection: ChangeDetectionStrategy.OnPush,
7+
template: CONTENT_HOLDER_TEMPLATE
8+
})
9+
export class DraggableItemComponent<T> extends ContentHolder {
10+
@Input()
11+
public disabled: boolean = false;
12+
13+
@Input()
14+
public data?: T;
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import 'color-palette';
2+
3+
.draggable-list .draggable-item {
4+
cursor: move;
5+
6+
&.disabled {
7+
background-color: $gray-2;
8+
color: $gray-5;
9+
cursor: not-allowed;
10+
}
11+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
2+
import {
3+
AfterContentInit,
4+
ChangeDetectionStrategy,
5+
Component,
6+
ContentChildren,
7+
EventEmitter,
8+
Input,
9+
Output,
10+
QueryList
11+
} from '@angular/core';
12+
import { DraggableItemComponent } from './draggable-item/draggable-item.component';
13+
14+
@Component({
15+
selector: 'ht-draggable-list',
16+
changeDetection: ChangeDetectionStrategy.OnPush,
17+
styleUrls: ['./draggable-list.component.scss'],
18+
template: `
19+
<div cdkDropList class="draggable-list" (cdkDropListDropped)="this.dropList($event)">
20+
<div
21+
*ngFor="let draggableItem of this.draggableItems"
22+
cdkDrag
23+
class="draggable-item"
24+
[ngClass]="{ disabled: disabled || draggableItem.disabled }"
25+
[cdkDragDisabled]="disabled || draggableItem.disabled"
26+
>
27+
<ng-container *ngTemplateOutlet="draggableItem.content"></ng-container>
28+
</div>
29+
</div>
30+
`
31+
})
32+
export class DraggableListComponent<T> implements AfterContentInit {
33+
@Input()
34+
public disabled: boolean = false;
35+
36+
@Output()
37+
public readonly draggableListChange: EventEmitter<T[]> = new EventEmitter();
38+
39+
@ContentChildren(DraggableItemComponent)
40+
public draggableItemsRef!: QueryList<DraggableItemComponent<T>>;
41+
42+
public draggableItems: DraggableItemComponent<T>[] = [];
43+
44+
public ngAfterContentInit(): void {
45+
this.draggableItems = this.draggableItemsRef.toArray();
46+
}
47+
48+
public dropList(event: CdkDragDrop<DraggableItemComponent<unknown>[]>): void {
49+
moveItemInArray(this.draggableItems, event.previousIndex, event.currentIndex);
50+
this.draggableListChange.emit(this.draggableItems.map(dragabbleItem => dragabbleItem.data!));
51+
}
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { DragDropModule } from '@angular/cdk/drag-drop';
2+
import { CommonModule } from '@angular/common';
3+
import { NgModule } from '@angular/core';
4+
import { DraggableItemComponent } from './draggable-item/draggable-item.component';
5+
import { DraggableListComponent } from './draggable-list.component';
6+
7+
@NgModule({
8+
declarations: [DraggableListComponent, DraggableItemComponent],
9+
imports: [CommonModule, DragDropModule],
10+
exports: [DraggableListComponent, DraggableItemComponent]
11+
})
12+
export class DraggableListModule {}

projects/components/src/public-api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export * from './description/description.module';
6565
export * from './divider/divider.component';
6666
export * from './divider/divider.module';
6767

68+
// Draggable list
69+
export { DraggableListComponent } from './draggable-list/draggable-list.component';
70+
export { DraggableItemComponent } from './draggable-list/draggable-item/draggable-item.component';
71+
export { DraggableListModule } from './draggable-list/draggable-list.module';
72+
6873
// Dropdown menu
6974
export { MenuDropdownComponent } from './menu-dropdown/menu-dropdown.component';
7075
export { MenuItemComponent } from './menu-dropdown/menu-item/menu-item.component';

0 commit comments

Comments
 (0)