Skip to content

Commit ddcde9b

Browse files
authoredMay 23, 2024
fix(ui5-multi-input): delete selected value on BACKSPACE (#9051)
Related to: #8905

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
 

‎packages/main/src/MultiInput.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,16 @@ class MultiInput extends Input {
223223
return this._focusFirstToken(e);
224224
}
225225

226-
if (isLeft(e) || isBackSpace(e)) {
226+
if (isLeft(e)) {
227227
this._skipOpenSuggestions = true;
228228
return this._handleLeft(e);
229229
}
230230

231+
if (isBackSpace(e)) {
232+
this._skipOpenSuggestions = true;
233+
return this._handleBackspace(e);
234+
}
235+
231236
this._skipOpenSuggestions = false;
232237

233238
if (isShow(e)) {
@@ -299,6 +304,21 @@ class MultiInput extends Input {
299304
}
300305
}
301306

307+
_handleBackspace(e: KeyboardEvent) {
308+
const cursorPosition = this.getDomRef()!.querySelector(`input`)!.selectionStart;
309+
const selectionEnd = this.getDomRef()!.querySelector(`input`)!.selectionEnd;
310+
const isValueSelected = cursorPosition === 0 && selectionEnd === this.value.length;
311+
const tokens = this.tokens;
312+
const lastToken = tokens.length && tokens[tokens.length - 1];
313+
314+
// selectionStart property applies only to inputs of types text, search, URL, tel, and password
315+
if ((!this.value || (this.value && cursorPosition === 0 && !isValueSelected)) && lastToken) {
316+
e.preventDefault();
317+
lastToken.focus();
318+
this.tokenizer._itemNav.setCurrentItem(lastToken);
319+
}
320+
}
321+
302322
_focusFirstToken(e: KeyboardEvent) {
303323
const tokens = this.tokens;
304324
const firstToken = tokens.length && tokens[0];

‎packages/main/test/specs/MultiInput.spec.js

+37
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const getTokenizerPopoverId = async (inputId) => {
99
}, inputId);
1010
}
1111

12+
const isMacOS = process.platform === 'darwin';
13+
const keyCtrlToPress = isMacOS ? 'Command' : 'Control';
14+
1215
describe("MultiInput general interaction", () => {
1316
before(async () => {
1417
await browser.url(`test/pages/MultiInput.html`);
@@ -563,6 +566,40 @@ describe("Keyboard handling", () => {
563566
assert.ok(await lastToken.getProperty("focused"), "The last token is focused on Backspace");
564567
});
565568

569+
it("should focus token last token when caret is at the beginning of the value", async () => {
570+
const input = await browser.$("#two-tokens");
571+
const innerInput = await input.shadow$("input");
572+
const lastToken = await browser.$("#two-tokens ui5-token#secondToken");
573+
574+
// Act
575+
await innerInput.click();
576+
await browser.keys("ArrowLeft");
577+
await browser.keys("ArrowLeft");
578+
await browser.keys("ArrowLeft");
579+
await browser.keys("Backspace");
580+
581+
assert.ok(await lastToken.getProperty("focused"), "The last token is focused on Backspace");
582+
});
583+
584+
it("should delete value on backspace", async () => {
585+
const input = await browser.$("#two-tokens");
586+
const innerInput = await input.shadow$("input");
587+
const lastToken = await browser.$("#two-tokens ui5-token#secondToken");
588+
589+
// Act
590+
await innerInput.click();
591+
await browser.keys([keyCtrlToPress, "a"]);
592+
await browser.keys("Backspace");
593+
594+
// Assert
595+
assert.strictEqual(await input.getProperty("value"), "", "Value is deleted on Backspace");
596+
597+
await browser.keys("Backspace");
598+
599+
assert.notOk(await input.getProperty("focused"), "The input loses focus on Backspace");
600+
assert.ok(await lastToken.getProperty("focused"), "The last token is focused on Backspace");
601+
});
602+
566603
it("should delete token on backspace", async () => {
567604
const input = await browser.$("#two-tokens");
568605
const innerInput = await input.shadow$("input");

0 commit comments

Comments
 (0)
Please sign in to comment.