Skip to content

Commit 37bf177

Browse files
authored
feat(ui5-upload-collection-item): implement keyboard handling (#1702)
1 parent 1a5db12 commit 37bf177

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

packages/fiori/src/UploadCollectionItem.hbs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
id="ui5-uci-edit-input"
1515
value="{{_fileNameWithoutExtension}}"
1616
data-sap-focus-ref
17+
@keyup="{{_onInputKeyUp}}"
18+
@keydown="{{_onInputKeydown}}"
1719
@ui5-change="{{_onInputChange}}"
1820
></ui5-input>
1921
<span class="ui5-uci-file-extension">{{_fileExtension}}</span>
@@ -46,22 +48,22 @@
4648
</div>
4749
<div class="ui5-uci-edit-buttons">
4850
{{#if _editing}}
49-
<ui5-button design="Transparent" class="ui5-uci-edit-rename-btn">{{_renameBtnText}}</ui5-button>
51+
<ui5-button design="Transparent" class="ui5-uci-edit-rename-btn" @click="{{_onRename}}">{{_renameBtnText}}</ui5-button>
5052
<ui5-button design="Transparent" id="ui5-uci-edit-cancel" @click="{{_onRenameCancel}}">{{_cancelRenameBtnText}}</ui5-button>
5153
{{else}}
5254
{{#if _showRetry}}
5355
<ui5-button
5456
icon="refresh"
5557
design="Transparent"
56-
title="{{_retryButtonTooltip}}"
58+
title="{{_retryButtonTooltip}}"
5759
@click="{{_onRetry}}"
5860
></ui5-button>
5961
{{/if}}
6062
{{#if _showTerminate}}
6163
<ui5-button
6264
icon="stop"
6365
design="Transparent"
64-
title="{{_terminateButtonTooltip}}"
66+
title="{{_terminateButtonTooltip}}"
6567
@click="{{_onTerminate}}">
6668
</ui5-button>
6769
{{/if}}

packages/fiori/src/UploadCollectionItem.js

+56-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ListItem from "@ui5/webcomponents/dist/ListItem.js";
88
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
99
import getFileExtension from "@ui5/webcomponents-base/dist/util/getFileExtension.js";
1010
import RenderScheduler from "@ui5/webcomponents-base/dist/RenderScheduler.js";
11+
import { isEnter, isEscape } from "@ui5/webcomponents-base/dist/Keys.js";
1112
import UploadState from "./types/UploadState.js";
1213
import "@ui5/webcomponents-icons/dist/icons/refresh.js";
1314
import "@ui5/webcomponents-icons/dist/icons/stop.js";
@@ -206,6 +207,13 @@ const metadata = {
206207
* @public
207208
*/
208209
retry: {},
210+
211+
/**
212+
* @since 1.0.0-rc.8
213+
* @event
214+
* @private
215+
*/
216+
"_focus-requested": {},
209217
},
210218
};
211219

@@ -255,9 +263,10 @@ class UploadCollectionItem extends ListItem {
255263
this.i18nBundle = getI18nBundle("@ui5/webcomponents-fiori");
256264

257265
this._editPressed = false; // indicates if the edit btn has been pressed
266+
this.doNotCloseInput = false; // Indicates whether the input should be closed when using keybord for navigation
267+
this.isEnter = false;
258268
}
259269

260-
261270
onAfterRendering() {
262271
if (this._editPressed) {
263272
this._editing = true;
@@ -300,6 +309,21 @@ class UploadCollectionItem extends ListItem {
300309
}
301310
}
302311

312+
_onInputKeydown(event) {
313+
this.isEnter = isEnter(event);
314+
this.isEscape = isEscape(event);
315+
}
316+
317+
_onInputKeyUp(event) {
318+
this.doNotCloseInput = true;
319+
this.tempValue = event.target.value + this._fileExtension;
320+
321+
if (this.isEscape) {
322+
[this.fileName, this.tempValue] = [this.tempValue, this.fileName];
323+
return this._onRenameCancel();
324+
}
325+
}
326+
303327
isDetailPressed(event) {
304328
const path = event.path || (event.composedPath && event.composedPath());
305329

@@ -313,13 +337,40 @@ class UploadCollectionItem extends ListItem {
313337
return;
314338
}
315339

340+
if ((!this.isEnter && this.doNotCloseInput) || this.isEscape) {
341+
[this.fileName, this.tempValue] = [this.tempValue, this.fileName];
342+
this.isEscape = false;
343+
return;
344+
}
345+
316346
this._editing = false;
317347
this.fileName = event.target.value + this._fileExtension;
318348
this.fireEvent("rename");
349+
350+
if (this.isEnter) {
351+
this._focus();
352+
}
353+
}
354+
355+
_onRename(event) {
356+
this.doNotCloseInput = false;
357+
this._editing = false;
358+
this._focus();
319359
}
320360

321361
_onRenameCancel(event) {
362+
if (!this.isEscape) {
363+
[this.fileName, this.tempValue] = [this.tempValue, this.fileName];
364+
}
365+
322366
this._editing = false;
367+
this.doNotCloseInput = false;
368+
369+
this._focus();
370+
}
371+
372+
_focus() {
373+
this.fireEvent("_focus-requested");
323374
}
324375

325376
_onFileNameClick(event) {
@@ -334,6 +385,10 @@ class UploadCollectionItem extends ListItem {
334385
this.fireEvent("terminate");
335386
}
336387

388+
get list() {
389+
return this.assignedSlot.parentElement;
390+
}
391+
337392
/**
338393
* @override
339394
*/

packages/fiori/test/pages/UploadCollection.html

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@
105105
>
106106
<ui5-icon name="document-text" slot="thumbnail"></ui5-icon>
107107
</ui5-upload-collection-item>
108+
<ui5-upload-collection-item
109+
id="keyboardNavigation"
110+
file-name="Graph.docx"
111+
type="Detail"
112+
upload-state="Complete"
113+
>
114+
<ui5-icon name="document-text" slot="thumbnail"></ui5-icon>
115+
</ui5-upload-collection-item>
108116
</ui5-upload-collection>
109117

110118
<ui5-upload-collection id="uploadCollectionDnD">

packages/fiori/test/specs/UploadCollection.spec.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe("UploadCollection", () => {
7272
const deleteBtn = firstItem.shadow$(".ui5-li-deletebtn");
7373
deleteBtn.click();
7474

75-
assert.strictEqual(uploadCollection.getProperty("items").length, 3, "item should be deleted when 'fileDeleted' event is fired");
75+
assert.strictEqual(uploadCollection.getProperty("items").length, 4, "item should be deleted when 'fileDeleted' event is fired");
7676
});
7777

7878
it("item should fire 'retry'", () => {
@@ -133,6 +133,22 @@ describe("UploadCollection", () => {
133133
assert.notOk(secondItem.shadow$(".ui5-uci-file-extension").getText(), "no extension is calculated for .gitignore.");
134134

135135
});
136+
137+
it("tests cancelling of name change via keyboard", () => {
138+
const secondItem = browser.$("#keyboardNavigation");
139+
const editButton = secondItem.shadow$(".ui5-li-detailbtn");
140+
141+
editButton.click();
142+
143+
browser.keys("new name");
144+
145+
browser.keys("Tab");
146+
browser.keys("Tab");
147+
148+
browser.keys("Enter"); // Press cancel button
149+
150+
assert.strictEqual(secondItem.shadow$(".ui5-uci-file-name").getText(), "Graph.docx", "The name of the file is not changed");
151+
});
136152
});
137153

138154
describe("Drag and Drop", () => {

packages/main/src/List.js

+9
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class List extends UI5Element {
344344
this.addEventListener("ui5-_forward-after", this.onForwardAfter.bind(this));
345345
this.addEventListener("ui5-_forward-before", this.onForwardBefore.bind(this));
346346
this.addEventListener("ui5-_selection-requested", this.onSelectionRequested.bind(this));
347+
this.addEventListener("ui5-_focus-requested", this.focusUploadCollectionItem.bind(this));
347348
}
348349

349350
get shouldRenderH1() {
@@ -642,6 +643,14 @@ class List extends UI5Element {
642643
item.focus();
643644
}
644645

646+
647+
focusUploadCollectionItem(event) {
648+
setTimeout(() => {
649+
this.setPreviouslyFocusedItem(event.target);
650+
this.focusPreviouslyFocusedItem();
651+
}, 0);
652+
}
653+
645654
setForwardingFocus(forwardingFocus) {
646655
this._forwardingFocus = forwardingFocus;
647656
}

0 commit comments

Comments
 (0)