Skip to content

Commit

Permalink
refactor: remove use of calculateSplices in user tags (#8495)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Jan 21, 2025
1 parent 360bba2 commit 3c6492d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
40 changes: 14 additions & 26 deletions packages/field-highlighter/src/vaadin-user-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import './vaadin-user-tag.js';
import './vaadin-user-tags-overlay.js';
import { calculateSplices } from '@polymer/polymer/lib/utils/array-splice.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { timeOut } from '@vaadin/component-base/src/async.js';
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
Expand Down Expand Up @@ -233,27 +232,6 @@ export class UserTags extends PolymerElement {
return { added, removed };
}

getChangedUsers(users, splices) {
const usersToAdd = [];
const usersToRemove = [];

splices.forEach((splice) => {
splice.removed.forEach((user) => {
usersToRemove.push(user);
});

for (let i = splice.addedCount - 1; i >= 0; i--) {
usersToAdd.push(users[splice.index + i]);
}
});

// Filter out users that are only moved
const addedUsers = usersToAdd.filter((u) => !usersToRemove.some((u2) => u.id === u2.id));
const removedUsers = usersToRemove.filter((u) => !usersToAdd.some((u2) => u.id === u2.id));

return { addedUsers, removedUsers };
}

applyTagsStart({ added, removed }) {
const wrapper = this.wrapper;
removed.forEach((tag) => {
Expand All @@ -279,12 +257,22 @@ export class UserTags extends PolymerElement {
// Apply pending change if needed
this.requestContentUpdate();

const splices = calculateSplices(users, this.users);
if (splices.length === 0) {
return;
let addedUsers = [];
let removedUsers = [];

const hasNewUsers = Array.isArray(users);
const hasOldUsers = Array.isArray(this.users);

if (hasOldUsers) {
const newUserIds = (users || []).map((user) => user.id);
removedUsers = this.users.filter((item) => !newUserIds.includes(item.id));
}

if (hasNewUsers) {
const oldUserIds = (this.users || []).map((user) => user.id);
addedUsers = users.filter((item) => !oldUserIds.includes(item.id)).reverse();
}

const { addedUsers, removedUsers } = this.getChangedUsers(users, splices);
if (addedUsers.length === 0 && removedUsers.length === 0) {
return;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/field-highlighter/test/user-tags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ describe('user-tags', () => {
expect(getComputedStyle(tags[1]).backgroundColor).to.equal('rgb(255, 0, 0)');
});

it('should not clear tags with a new instance of same user', () => {
setUsers([user1]);
setUsers([{ ...user1 }]);

const tags = getTags();
expect(tags).to.have.lengthOf(1);
});

it('should not set custom property if index is null', () => {
addUser({ name: 'xyz', colorIndex: null });
const tags = getTags();
Expand Down

0 comments on commit 3c6492d

Please sign in to comment.