Skip to content
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

#3624 - Fix Multi-Row Drag-and-Drop Issue for Chips in Item Submission Form #3654

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/app/shared/form/chips/chips.component.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<div [className]="'float-left w-100 ' + wrapperClass">
<div role="list" class="nav nav-pills d-flex flex-column flex-sm-row" cdkDropList cdkDropListOrientation="horizontal" (cdkDropListDropped)="onDrop($event)">
<div role="list" class="nav nav-pills d-flex flex-column flex-sm-row" cdkDropListGroup>
<ng-template #tipContent>
<p class="text-left p-0 m-0" *ngFor="let tip of tipText">
{{tip}}
</p>
</ng-template>
<div role="listitem" class="nav-item mr-2 mb-1"
<div role="listitem" class="nav-item mr-2 mb-1 d-flex flex-row"
*ngFor="let c of chips.getChips(); let i = index"
#t="ngbTooltip"
triggers="manual"
cdkDropList
[cdkDropListData]="{ index: i }"
cdkDropListOrientation="horizontal"
(cdkDropListDropped)="onDrop($event)"
[ngbTooltip]="tipContent"
(mouseover)="showTooltip(t, i)"
(mouseout)="t.close()">
Expand Down
3 changes: 2 additions & 1 deletion src/app/shared/form/chips/chips.component.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.cdk-drag-placeholder {
filter: grayscale(100%);
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
margin: 0 0.25rem;
}

.cdk-drag-preview {
color: white;
box-sizing: border-box;
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/form/chips/chips.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ describe('ChipsComponent test suite', () => {

it('should update chips item order when drag and drop end', fakeAsync(() => {
spyOn(chipsComp.chips, 'updateOrder');
const de = chipsFixture.debugElement.query(By.css('div[role="list"]'));
const de = chipsFixture.debugElement.query(By.css('div[role="listitem"]'));

de.triggerEventHandler('cdkDropListDropped', { previousIndex: 0, currentIndex: 1 });
de.triggerEventHandler('cdkDropListDropped', { previousIndex: 0, currentIndex: 1, previousContainer: { data: { index: 0 } }, container: { data: { index: 1 } } });

expect(chipsComp.dragged).toBe(-1);
expect(chipsComp.chips.updateOrder).toHaveBeenCalled();
Expand Down
15 changes: 13 additions & 2 deletions src/app/shared/form/chips/chips.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CdkDrag,
CdkDragDrop,
CdkDropList,
CdkDropListGroup,
moveItemInArray,
} from '@angular/cdk/drag-drop';
import {
Expand Down Expand Up @@ -48,6 +49,7 @@ import { ChipsItem } from './models/chips-item.model';
TranslateModule,
CdkDrag,
CdkDropList,
CdkDropListGroup,
],
standalone: true,
})
Expand Down Expand Up @@ -105,8 +107,17 @@ export class ChipsComponent implements OnChanges {
this.dragged = index;
}

onDrop(event: CdkDragDrop<ChipsItem[]>) {
moveItemInArray(this.chips.chipsItems.getValue(), event.previousIndex, event.currentIndex);
onDrop(event: CdkDragDrop<{ index: number }>) {
const previousContainerIndex = event.previousContainer.data.index;
const currentContainerIndex = event.container.data.index;

const currentPositionInCurrentContainer = event.currentIndex;

const directionAdjuster = currentContainerIndex > previousContainerIndex ? -1 : 0;

moveItemInArray(this.chips.chipsItems.getValue(),
previousContainerIndex,
currentContainerIndex + currentPositionInCurrentContainer + directionAdjuster);
this.dragged = -1;
this.chips.updateOrder();
this.isDragging.next(false);
Expand Down
Loading