-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cdk/drag-drop): add mixed orientation support #29216
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e91cfcb
test(cdk/drag-drop): fix up flawed test cases
crisbeto b1e8e61
test(cdk/drag-drop): simplify shared tests
crisbeto 9c4f948
refactor(cdk/drag-drop): simplify sort strategy interface
crisbeto c0b5c3f
feat(cdk/drag-drop): add mixed orientation support
crisbeto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1,372 changes: 688 additions & 684 deletions
1,372
src/cdk/drag-drop/directives/drop-list-shared.spec.ts
Large diffs are not rendered by default.
Oops, something went wrong.
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,144 @@ | ||
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core'; | ||
import {fakeAsync, flush} from '@angular/core/testing'; | ||
import {CdkDropList} from './drop-list'; | ||
import {CdkDrag} from './drag'; | ||
import {moveItemInArray} from '../drag-utils'; | ||
import {CdkDragDrop} from '../drag-events'; | ||
import { | ||
ITEM_HEIGHT, | ||
ITEM_WIDTH, | ||
assertStartToEndSorting, | ||
assertEndToStartSorting, | ||
defineCommonDropListTests, | ||
} from './drop-list-shared.spec'; | ||
import {createComponent, dragElementViaMouse} from './test-utils.spec'; | ||
|
||
describe('mixed drop list', () => { | ||
defineCommonDropListTests({ | ||
verticalListOrientation: 'mixed', | ||
horizontalListOrientation: 'mixed', | ||
getSortedSiblings, | ||
}); | ||
|
||
it('should dispatch the `dropped` event in a wrapping drop zone', fakeAsync(() => { | ||
const fixture = createComponent(DraggableInHorizontalWrappingDropZone); | ||
fixture.detectChanges(); | ||
const dragItems = fixture.componentInstance.dragItems; | ||
|
||
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim())).toEqual([ | ||
'Zero', | ||
'One', | ||
'Two', | ||
'Three', | ||
'Four', | ||
'Five', | ||
'Six', | ||
'Seven', | ||
]); | ||
|
||
const firstItem = dragItems.first; | ||
const seventhItemRect = dragItems.toArray()[6].element.nativeElement.getBoundingClientRect(); | ||
|
||
dragElementViaMouse( | ||
fixture, | ||
firstItem.element.nativeElement, | ||
seventhItemRect.left + 1, | ||
seventhItemRect.top + 1, | ||
); | ||
flush(); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.componentInstance.droppedSpy).toHaveBeenCalledTimes(1); | ||
const event = fixture.componentInstance.droppedSpy.calls.mostRecent().args[0]; | ||
|
||
// Assert the event like this, rather than `toHaveBeenCalledWith`, because Jasmine will | ||
// go into an infinite loop trying to stringify the event, if the test fails. | ||
expect(event).toEqual({ | ||
previousIndex: 0, | ||
currentIndex: 6, | ||
item: firstItem, | ||
container: fixture.componentInstance.dropInstance, | ||
previousContainer: fixture.componentInstance.dropInstance, | ||
isPointerOverContainer: true, | ||
distance: {x: jasmine.any(Number), y: jasmine.any(Number)}, | ||
dropPoint: {x: jasmine.any(Number), y: jasmine.any(Number)}, | ||
event: jasmine.anything(), | ||
}); | ||
|
||
expect(dragItems.map(drag => drag.element.nativeElement.textContent!.trim())).toEqual([ | ||
'One', | ||
'Two', | ||
'Three', | ||
'Four', | ||
'Five', | ||
'Six', | ||
'Zero', | ||
'Seven', | ||
]); | ||
})); | ||
|
||
it('should move the placeholder as an item is being sorted to the right in a wrapping drop zone', fakeAsync(() => { | ||
const fixture = createComponent(DraggableInHorizontalWrappingDropZone); | ||
fixture.detectChanges(); | ||
assertStartToEndSorting( | ||
'horizontal', | ||
fixture, | ||
getSortedSiblings, | ||
fixture.componentInstance.dragItems.map(item => item.element.nativeElement), | ||
); | ||
})); | ||
|
||
it('should move the placeholder as an item is being sorted to the left in a wrapping drop zone', fakeAsync(() => { | ||
const fixture = createComponent(DraggableInHorizontalWrappingDropZone); | ||
fixture.detectChanges(); | ||
assertEndToStartSorting( | ||
'horizontal', | ||
fixture, | ||
getSortedSiblings, | ||
fixture.componentInstance.dragItems.map(item => item.element.nativeElement), | ||
); | ||
})); | ||
}); | ||
|
||
function getSortedSiblings(item: Element) { | ||
return Array.from(item.parentElement?.children || []); | ||
} | ||
|
||
@Component({ | ||
styles: ` | ||
.cdk-drop-list { | ||
display: block; | ||
width: ${ITEM_WIDTH * 3}px; | ||
background: pink; | ||
font-size: 0; | ||
} | ||
|
||
.cdk-drag { | ||
height: ${ITEM_HEIGHT * 2}px; | ||
width: ${ITEM_WIDTH}px; | ||
background: red; | ||
display: inline-block; | ||
} | ||
`, | ||
template: ` | ||
<div | ||
cdkDropList | ||
cdkDropListOrientation="mixed" | ||
[cdkDropListData]="items" | ||
(cdkDropListDropped)="droppedSpy($event)"> | ||
@for (item of items; track item) { | ||
<div cdkDrag>{{item}}</div> | ||
} | ||
</div> | ||
`, | ||
standalone: true, | ||
imports: [CdkDropList, CdkDrag], | ||
}) | ||
class DraggableInHorizontalWrappingDropZone { | ||
@ViewChildren(CdkDrag) dragItems: QueryList<CdkDrag>; | ||
@ViewChild(CdkDropList) dropInstance: CdkDropList; | ||
items = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven']; | ||
droppedSpy = jasmine.createSpy('dropped spy').and.callFake((event: CdkDragDrop<string[]>) => { | ||
moveItemInArray(this.items, event.previousIndex, event.currentIndex); | ||
}); | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we don't have that many tests here, because we reuse the ~190 tests from the existing drop list. We add some more below to ensure that sorting works properly on mixed lists as well.