Skip to content

Commit

Permalink
fix(ui5-tokenizer): fix token-delete event details (#10630)
Browse files Browse the repository at this point in the history
Fixes the token-delete event details, because currently upon clicking the "X" of a token the token-delete event provides wrong token, actually undefined (as this.tokens returns the slot HTML elements, while this._tokens gets the slotted elements, the tokens, later correctly passed with the event as event.details.tokens).

Fixes: #10612
  • Loading branch information
ilhan007 authored Jan 30, 2025
1 parent 6c19703 commit c1206ae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/main/cypress/specs/Tokenizer.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import Tokenizer from "../../src/Tokenizer.js";
import Token from "../../src/Token.js";
import type { UI5CustomEvent } from "@ui5/webcomponents-base/dist/index.js";

const onTokenDelete = (event: UI5CustomEvent<Tokenizer, "token-delete">) => {
event.detail.tokens.forEach(token => {
(event.target as Tokenizer).removeChild(token);
});
};

describe("Tokenizer - multi-line and Clear All", () => {
it("'Clear All' link is rendered for multi-line tokenizer and show-clear-all set to true", () => {
Expand Down Expand Up @@ -124,4 +131,43 @@ describe("Tokenizer - multi-line and Clear All", () => {
cy.get("@delete")
.should("have.been.calledOnce");
});

it("tests token removal", () => {
cy.mount(
<Tokenizer id="test-token-delete" style={{ width: "100px" }} onTokenDelete={onTokenDelete}>
<Token text="aute"></Token>
<Token text="ad"></Token>
<Token text="exercitation"></Token>
<Token text="esse"></Token>
<Token text="labore"></Token>
<Token text="amet"></Token>
<Token text="excepteur"></Token>
</Tokenizer>
);

cy.get("#test-token-delete")
.find("[ui5-token]")
.should("have.length", 7);

cy.get("#test-token-delete")
.shadow()
.find(".ui5-tokenizer-more-text")
.realClick();

cy.get("#test-token-delete")
.shadow()
.find("[ui5-responsive-popover]")
.should("be.visible");

cy.get("#test-token-delete")
.shadow()
.find("[ui5-responsive-popover] [ui5-list] [ui5-li]").eq(0)
.shadow()
.find(".ui5-li-deletebtn [ui5-button]")
.realClick();

cy.get("#test-token-delete")
.find("[ui5-token]")
.should("have.length", 6);
});
});
2 changes: 1 addition & 1 deletion packages/main/src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ class Tokenizer extends UI5Element {
}

getTokenByRefId(refId: string) {
return this.tokens.find(token => token._id === refId)!;
return this._tokens.find(token => token._id === refId)!;
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/main/test/pages/MultiInput.html
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ <h1 class="sample-container-title">Custom Suggestions</h1>
</ui5-multi-input>
</div>


<h1 class="sample-container-title">Token delete</h1>

<ui5-multi-input show-suggestions id="test-token-delete">
<ui5-token slot="tokens" text="Aute"></ui5-token>
<ui5-token slot="tokens" text="ad"></ui5-token>
<ui5-token slot="tokens" text="exercitation"></ui5-token>
<ui5-token slot="tokens" text="esse"></ui5-token>
<ui5-token slot="tokens" text="labore"></ui5-token>
<ui5-token slot="tokens" text="amet"></ui5-token>
<ui5-token slot="tokens" text="aute"></ui5-token>
<ui5-token slot="tokens" text="excepteur"></ui5-token>
</ui5-multi-input>

<script>

var createTokenFromText = function (text) {
Expand Down Expand Up @@ -470,6 +484,7 @@ <h1 class="sample-container-title">Custom Suggestions</h1>
}

document.getElementById("token-unique").addEventListener("ui5-token-delete", handleTokenDelete);
document.getElementById("test-token-delete").addEventListener("ui5-token-delete", handleTokenDelete);
document.getElementById("suggestion-token").addEventListener("ui5-token-delete", handleTokenDelete);
document.getElementById("two-tokens").addEventListener("ui5-token-delete", handleTokenDelete);
document.getElementById("readonly-mi").addEventListener("ui5-token-delete", handleTokenDelete);
Expand Down

0 comments on commit c1206ae

Please sign in to comment.