Skip to content

Commit f04bfe1

Browse files
crisbetoVivian Hu
authored and
Vivian Hu
committed
docs(drag-drop): add docs and live example for enter predicate (#13917)
Since we don't have much info on the `cdkDropListEnterPredicate`, these changes add a section about it in the docs, as well as a live example.
1 parent 37620ce commit f04bfe1

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,12 @@ the element that is moved as the user is dragging. This is useful for cases like
133133
draggable.
134134

135135
<!-- example(cdk-drag-drop-root-element) -->
136+
137+
### Controlling which items can be moved into a container
138+
By default, all `cdkDrag` items from one container can be moved into another connected container.
139+
If you want more fine-grained control over which items can be dropped, you can use the
140+
`cdkDropListEnterPredicate` which will be called whenever an item is about to enter a
141+
new container. Depending on whether the predicate returns `true` or `false`, the item may or may not
142+
be allowed into the new container.
143+
144+
<!-- example(cdk-drag-drop-enter-predicate) -->
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<div class="example-container">
2+
<h2>Available numbers</h2>
3+
4+
<div
5+
id="all"
6+
cdkDropList
7+
[cdkDropListData]="all"
8+
cdkDropListConnectedTo="even"
9+
class="example-list"
10+
(cdkDropListDropped)="drop($event)"
11+
[cdkDropListEnterPredicate]="noReturnPredicate">
12+
<div
13+
class="example-box"
14+
*ngFor="let number of all"
15+
[cdkDragData]="number"
16+
cdkDrag>{{number}}</div>
17+
</div>
18+
</div>
19+
20+
<div class="example-container">
21+
<h2>Even numbers</h2>
22+
23+
<div
24+
id="even"
25+
cdkDropList
26+
[cdkDropListData]="even"
27+
cdkDropListConnectedTo="all"
28+
class="example-list"
29+
(cdkDropListDropped)="drop($event)"
30+
[cdkDropListEnterPredicate]="evenPredicate">
31+
<div
32+
class="example-box"
33+
*ngFor="let number of even"
34+
cdkDrag
35+
[cdkDragData]="number">{{number}}</div>
36+
</div>
37+
</div>
38+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Component} from '@angular/core';
2+
import {CdkDragDrop, moveItemInArray, transferArrayItem, CdkDrag} from '@angular/cdk/drag-drop';
3+
4+
/**
5+
* @title Drag&Drop enter predicate
6+
*/
7+
@Component({
8+
selector: 'cdk-drag-drop-enter-predicate-example',
9+
templateUrl: 'cdk-drag-drop-enter-predicate-example.html',
10+
styleUrls: ['cdk-drag-drop-enter-predicate-example.css'],
11+
})
12+
export class CdkDragDropEnterPredicateExample {
13+
all = [1, 2, 3, 4, 5, 6, 7, 8, 9];
14+
even = [10];
15+
16+
drop(event: CdkDragDrop<string[]>) {
17+
if (event.previousContainer === event.container) {
18+
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
19+
} else {
20+
transferArrayItem(event.previousContainer.data,
21+
event.container.data,
22+
event.previousIndex,
23+
event.currentIndex);
24+
}
25+
}
26+
27+
/** Predicate function that only allows even numbers to be dropped into a list. */
28+
evenPredicate(item: CdkDrag<number>) {
29+
return item.data % 2 === 0;
30+
}
31+
32+
/** Predicate function that doesn't allow items to be dropped into a list. */
33+
noReturnPredicate() {
34+
return false;
35+
}
36+
}

0 commit comments

Comments
 (0)