Skip to content

Commit a0520d4

Browse files
committed
chore(): updating drag&drop example
1 parent b185f30 commit a0520d4

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

src/app/app.component.html

+32-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,38 @@ <h3>Destroyed ng-container </h3>
165165

166166
<div style="text-align: center">
167167
<h3>Drag & Drop :)</h3>
168-
<textarea autosize cdkDrag>
169-
I should be draggable and no be throwing errors in the process.
170-
</textarea>
168+
169+
<div class="drag-and-drop-container">
170+
<h2>To do</h2>
171+
172+
<div
173+
cdkDropList
174+
#todoList="cdkDropList"
175+
[cdkDropListData]="todo"
176+
[cdkDropListConnectedTo]="[doneList]"
177+
class="drag-and-drop-list"
178+
(cdkDropListDropped)="drop($event)">
179+
<textarea class="drag-and-drop-box" *ngFor="let item of todo" cdkDrag
180+
autosize
181+
[(ngModel)]="item"></textarea>
182+
</div>
183+
</div>
184+
185+
<div class="drag-and-drop-container">
186+
<h2>Done</h2>
187+
188+
<div
189+
cdkDropList
190+
#doneList="cdkDropList"
191+
[cdkDropListData]="done"
192+
[cdkDropListConnectedTo]="[todoList]"
193+
class="drag-and-drop-list"
194+
(cdkDropListDropped)="drop($event)">
195+
<textarea class="drag-and-drop-box" *ngFor="let item of done" cdkDrag
196+
autosize
197+
[(ngModel)]="item"></textarea>
198+
</div>
199+
</div>
171200
</div>
172201

173202
<hr/>

src/app/app.component.scss

+58
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,61 @@ textarea {
2525
padding-bottom: 15px;
2626
}
2727
}
28+
29+
30+
.drag-and-drop-container {
31+
width: 200px;
32+
max-width: 100%;
33+
margin: 0 25px 25px 0;
34+
display: inline-block;
35+
vertical-align: top;
36+
}
37+
38+
.drag-and-drop-list {
39+
border: solid 1px #ccc;
40+
min-height: 60px;
41+
background: white;
42+
border-radius: 4px;
43+
overflow: hidden;
44+
display: block;
45+
}
46+
47+
.drag-and-drop-box {
48+
margin: 3px;
49+
border-bottom: solid 1px #ccc;
50+
color: rgba(0, 0, 0, 0.87);
51+
/* display: flex; */
52+
/* flex-direction: row; */
53+
/* align-items: center; */
54+
/* justify-content: space-between; */
55+
/* box-sizing: border-box; */
56+
cursor: move;
57+
background: white;
58+
/* font-size: 14px; */
59+
width: 189px;
60+
min-width: auto;
61+
}
62+
63+
.cdk-drag-preview {
64+
box-sizing: border-box;
65+
border-radius: 4px;
66+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
67+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
68+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
69+
}
70+
71+
.cdk-drag-placeholder {
72+
opacity: 0;
73+
}
74+
75+
.cdk-drag-animating {
76+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
77+
}
78+
79+
.drag-and-drop-box:last-child {
80+
border: none;
81+
}
82+
83+
.drag-and-drop-list.cdk-drop-list-dragging .drag-and-drop-box:not(.cdk-drag-placeholder) {
84+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
85+
}

src/app/app.component.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Component, OnInit} from '@angular/core';
22
import {FormControl, FormGroup} from "@angular/forms";
3+
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';
34

45
const longText = `Mega Man X, known in Japan as Rockman X,[a] is an action-platform video game developed and published by Capcom for the Super Nintendo Entertainment System (SNES).
56
1
@@ -30,6 +31,15 @@ export class AppComponent implements OnInit {
3031

3132
public showAreaInContainer = true;
3233

34+
public todo = [
35+
'Get to work',
36+
'Go home',
37+
];
38+
39+
public done = [
40+
'Get up',
41+
];
42+
3343
reactiveText = new FormControl(longText);
3444
reactiveForm = new FormGroup({
3545
reactiveText: new FormControl(longText)
@@ -62,4 +72,19 @@ export class AppComponent implements OnInit {
6272
onResized(newHeight) {
6373
console.log(newHeight);
6474
}
75+
76+
77+
78+
drop(event: CdkDragDrop<string[]>) {
79+
if (event.previousContainer === event.container) {
80+
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
81+
} else {
82+
transferArrayItem(
83+
event.previousContainer.data,
84+
event.container.data,
85+
event.previousIndex,
86+
event.currentIndex
87+
);
88+
}
89+
}
6590
}

0 commit comments

Comments
 (0)