Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(drag-drop): add predicate function for whether an item can be so…
…rted into an index (#20389) Adds the `cdkDropListSortPredicate` input that allows consumers to pass in a predicate function that determines whether an item can be sorted into a particular index of the list. Fixes #19436.
- Loading branch information
Showing
9 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...mples/cdk/drag-drop/cdk-drag-drop-sort-predicate/cdk-drag-drop-sort-predicate-example.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.example-list { | ||
border: solid 1px #ccc; | ||
min-height: 60px; | ||
background: white; | ||
border-radius: 4px; | ||
overflow: hidden; | ||
display: block; | ||
width: 400px; | ||
max-width: 100%; | ||
} | ||
|
||
.example-box { | ||
padding: 20px 10px; | ||
border-bottom: solid 1px #ccc; | ||
color: rgba(0, 0, 0, 0.87); | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
justify-content: space-between; | ||
box-sizing: border-box; | ||
cursor: move; | ||
background: white; | ||
font-size: 14px; | ||
} | ||
|
||
.cdk-drag-preview { | ||
box-sizing: border-box; | ||
border-radius: 4px; | ||
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2), | ||
0 8px 10px 1px rgba(0, 0, 0, 0.14), | ||
0 3px 14px 2px rgba(0, 0, 0, 0.12); | ||
} | ||
|
||
.cdk-drag-placeholder { | ||
opacity: 0; | ||
} | ||
|
||
.cdk-drag-animating { | ||
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1); | ||
} | ||
|
||
.example-box:last-child { | ||
border: none; | ||
} | ||
|
||
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) { | ||
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1); | ||
} |
11 changes: 11 additions & 0 deletions
11
...ples/cdk/drag-drop/cdk-drag-drop-sort-predicate/cdk-drag-drop-sort-predicate-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div | ||
cdkDropList | ||
class="example-list" | ||
(cdkDropListDropped)="drop($event)" | ||
[cdkDropListSortPredicate]="sortPredicate"> | ||
<div | ||
class="example-box" | ||
*ngFor="let number of numbers" | ||
[cdkDragData]="number" | ||
cdkDrag>{{number}}</div> | ||
</div> |
26 changes: 26 additions & 0 deletions
26
...amples/cdk/drag-drop/cdk-drag-drop-sort-predicate/cdk-drag-drop-sort-predicate-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Component} from '@angular/core'; | ||
import {CdkDragDrop, moveItemInArray, CdkDrag} from '@angular/cdk/drag-drop'; | ||
|
||
/** | ||
* @title Drag&Drop sort predicate | ||
*/ | ||
@Component({ | ||
selector: 'cdk-drag-drop-sort-predicate-example', | ||
templateUrl: 'cdk-drag-drop-sort-predicate-example.html', | ||
styleUrls: ['cdk-drag-drop-sort-predicate-example.css'], | ||
}) | ||
export class CdkDragDropSortPredicateExample { | ||
numbers = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
|
||
drop(event: CdkDragDrop<unknown>) { | ||
moveItemInArray(this.numbers, event.previousIndex, event.currentIndex); | ||
} | ||
|
||
/** | ||
* Predicate function that only allows even numbers to be | ||
* sorted into even indices and odd numbers at odd indices. | ||
*/ | ||
sortPredicate(index: number, item: CdkDrag<number>) { | ||
return (index + 1) % 2 === item.data % 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters