Skip to content

Commit 7a2565d

Browse files
crisbetommalerba
authored andcommitted
docs(drag-drop): add docs and live example for disabled sorting (#15426)
Documents the functionality from #15064 and adds a live example.
1 parent d0cd9c0 commit 7a2565d

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

src/cdk/drag-drop/drag-drop.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,12 @@ using the `cdkDropListDisabled` input on a `cdkDropList` or a particular handle
179179
`cdkDragHandleDisabled` on `cdkDragHandle`.
180180

181181
<!-- example(cdk-drag-drop-disabled) -->
182+
183+
### Disabled sorting
184+
There are cases where draggable items can be dragged out of one list into another, however
185+
the user shouldn't be able to sort them within the source list. For these cases you can set the
186+
`cdkDropListSortingDisabled` input which will prevent the items in a `cdkDropList` from sorting,
187+
in addition to preserving the dragged item's initial position in the source list, if the user
188+
decides to return the item.
189+
190+
<!-- example(cdk-drag-drop-disabled-sorting) -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.example-container {
2+
width: 400px;
3+
max-width: 100%;
4+
margin: 0 25px 25px 0;
5+
display: inline-block;
6+
vertical-align: top;
7+
}
8+
9+
.example-list {
10+
border: solid 1px #ccc;
11+
min-height: 60px;
12+
background: white;
13+
border-radius: 4px;
14+
overflow: hidden;
15+
display: block;
16+
}
17+
18+
.example-box {
19+
padding: 20px 10px;
20+
border-bottom: solid 1px #ccc;
21+
color: rgba(0, 0, 0, 0.87);
22+
display: flex;
23+
flex-direction: row;
24+
align-items: center;
25+
justify-content: space-between;
26+
box-sizing: border-box;
27+
cursor: move;
28+
background: white;
29+
font-size: 14px;
30+
}
31+
32+
.cdk-drag-preview {
33+
box-sizing: border-box;
34+
border-radius: 4px;
35+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
36+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
37+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
38+
}
39+
40+
.cdk-drag-placeholder {
41+
opacity: 0;
42+
}
43+
44+
.cdk-drag-animating {
45+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
46+
}
47+
48+
.example-box:last-child {
49+
border: none;
50+
}
51+
52+
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
53+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
54+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<div cdkDropListGroup>
2+
<div class="example-container">
3+
<h2>Available items</h2>
4+
5+
<div
6+
cdkDropList
7+
[cdkDropListData]="items"
8+
class="example-list"
9+
cdkDropListSortingDisabled
10+
(cdkDropListDropped)="drop($event)">
11+
<div class="example-box" *ngFor="let item of items" cdkDrag>{{item}}</div>
12+
</div>
13+
</div>
14+
15+
<div class="example-container">
16+
<h2>Shopping basket</h2>
17+
18+
<div
19+
cdkDropList
20+
[cdkDropListData]="basket"
21+
class="example-list"
22+
(cdkDropListDropped)="drop($event)">
23+
<div class="example-box" *ngFor="let item of basket" cdkDrag>{{item}}</div>
24+
</div>
25+
</div>
26+
</div>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {Component} from '@angular/core';
2+
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';
3+
4+
/**
5+
* @title Drag&Drop disabled sorting
6+
*/
7+
@Component({
8+
selector: 'cdk-drag-drop-disabled-sorting-example',
9+
templateUrl: 'cdk-drag-drop-disabled-sorting-example.html',
10+
styleUrls: ['cdk-drag-drop-disabled-sorting-example.css'],
11+
})
12+
export class CdkDragDropDisabledSortingExample {
13+
items = [
14+
'Carrots',
15+
'Tomatoes',
16+
'Onions',
17+
'Apples',
18+
'Avocados'
19+
];
20+
21+
basket = [
22+
'Oranges',
23+
'Bananas',
24+
'Cucumbers'
25+
];
26+
27+
drop(event: CdkDragDrop<string[]>) {
28+
if (event.previousContainer === event.container) {
29+
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
30+
} else {
31+
transferArrayItem(event.previousContainer.data,
32+
event.container.data,
33+
event.previousIndex,
34+
event.currentIndex);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)