Fix: add disable to the drag-drop list component (#13880)

This commit is contained in:
shamoon
2026-08-31 09:09:10 -07:00
committed by GitHub
parent 06e9c1c02b
commit 440049978b
4 changed files with 37 additions and 0 deletions
@@ -3,6 +3,7 @@
<div class="d-flex flex-wrap flex-row gap-2 w-100 mh-1" style="min-height: 1em;"
cdkDropList #selectedList="cdkDropList"
cdkDropListOrientation="mixed"
[cdkDropListDisabled]="disabled"
(cdkDropListDropped)="drop($event)"
[cdkDropListConnectedTo]="[unselectedList]">
@for (item of selectedItems; track item.id) {
@@ -17,6 +18,7 @@
<div class="d-flex flex-wrap flex-row gap-2 w-100" style="min-height: 1em;"
cdkDropList #unselectedList="cdkDropList"
cdkDropListOrientation="mixed"
[cdkDropListDisabled]="disabled"
(cdkDropListDropped)="drop($event)"
[cdkDropListConnectedTo]="[selectedList]">
@for (item of unselectedItems; track item.id) {
@@ -1,3 +1,11 @@
.badge {
cursor: move;
}
.cdk-drop-list-disabled {
cursor: not-allowed !important;
* {
pointer-events: none !important;
}
}
@@ -98,4 +98,29 @@ describe('DragDropSelectComponent', () => {
{ id: '3', name: 'Item 3' },
])
})
it('should disable drag and drop when the control is disabled', () => {
component.items = [
{ id: '1', name: 'Item 1' },
{ id: '2', name: 'Item 2' },
]
component.writeValue(['1', '2'])
component.setDisabledState(true)
fixture.detectChanges()
expect(component.selectedList.disabled).toBe(true)
expect(component.unselectedList.disabled).toBe(true)
component.drop({
previousContainer: component.selectedList,
container: component.selectedList,
previousIndex: 0,
currentIndex: 1,
} as any)
expect(component.selectedItems).toEqual([
{ id: '1', name: 'Item 1' },
{ id: '2', name: 'Item 2' },
])
})
})
@@ -46,6 +46,8 @@ export class DragDropSelectComponent extends AbstractInputComponent<string[]> {
}
public drop(event: CdkDragDrop<string[]>) {
if (this.disabled) return
if (
event.previousContainer === event.container &&
event.container === this.selectedList