Skip to content

Commit 2c46dd2

Browse files
FIX Ensure focus moves to folder when adding new folder. (silverstripe#1677)
1 parent 3600632 commit 2c46dd2

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

client/dist/js/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/src/containers/ThumbnailView/ThumbnailView.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ class ThumbnailView extends Component {
6868
this.fileRefs.current = [];
6969
// Explicitly focus on the grid itself to announce changes to page etc.
7070
this.gridRef.current.focus();
71+
const newState = { forceResetRefs: false };
7172
if (this.state.focusedItem) {
72-
this.setState({ focusedItem: null, forceResetRefs: false });
73+
newState.focusedItem = null;
7374
}
75+
this.setState(newState);
7476
return;
7577
}
7678
// If we removed a file or folder, we have some tidy-up to do.
77-
if (this.props.files.length < oldProps.files.length) {
79+
if (this.props.totalCount < oldProps.totalCount || this.props.files.length < oldProps.files.length) {
7880
// If we have less files/folders than we used to, make sure to remove the extra refs
7981
// Note that all the refs are correct, there's just some extra old ones at the end of
8082
// the arrays.
@@ -127,20 +129,22 @@ class ThumbnailView extends Component {
127129
}
128130

129131
// If we added a folder or file, move focus to the first new item
130-
if (this.props.files.length > oldProps.files.length) {
132+
if (this.props.totalCount > oldProps.totalCount || this.props.files.length > oldProps.files.length) {
131133
const newItems = this.props.files.filter((item) => !oldProps.files.find((oldItem) => this.focusItemsAreIdentical(item, oldItem)));
132134
const newItem = newItems[0];
133-
this.setState({
134-
allowedToSetFocus: true,
135-
focusedItem: this.getFocusDataFromItem(newItem)
136-
});
135+
if (newItem) {
136+
this.setState({
137+
allowedToSetFocus: true,
138+
focusedItem: this.getFocusDataFromItem(newItem)
139+
});
140+
}
137141
}
138142

139143
// For successful uploads, when the file gets assigned a new ID we need to capture that.
140144
// There's a brief period where the file has both a queuedID and a regular ID - and then it
141145
// drops the queuedID. We need to make sure we catch the ID so we can retain focus on the
142146
// item.
143-
if (this.state.focusedItem?.queuedId && !this.state.focusedItem.id && this.props.files.length === oldProps.files.length) {
147+
if (this.state.focusedItem?.queuedId && !this.state.focusedItem.id && this.props.totalCount === oldProps.totalCount) {
144148
const current = this.props.files.find((item) => this.focusItemsAreIdentical(this.state.focusedItem, item));
145149
if (current && current.id !== this.state.focusedItem.id) {
146150
const newState = { focusedItem: this.getFocusDataFromItem(current) };
@@ -193,6 +197,9 @@ class ThumbnailView extends Component {
193197
* @param {object} item A file or folder from props.files
194198
*/
195199
getFocusDataFromItem(item) {
200+
if (!item) {
201+
return null;
202+
}
196203
const itemRefs = item.type === 'folder' ? this.folderRefs.current : this.fileRefs.current;
197204
const index = itemRefs.findIndex((itemRef) => this.focusItemsAreIdentical(item, itemRef));
198205
return { id: item.id, queuedId: item.queuedId, type: item.type, index };

tests/behat/features/accessibility-asset-admin.feature

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ Feature: Accessibility asset-admin
229229
Then I should not see "folder02" in the ".breadcrumb__item--last .breadcrumb__item-title" element
230230
And the folder named "folder01" should have focus
231231

232+
# Adding a folder moves roving tabindex to that folder - but does NOT move focus
233+
When I press the "Shift-Tab" key globally
234+
And I press the "Shift-Tab" key globally
235+
And I press the "Shift-Tab" key globally
236+
Then the "button#add-folder-button" element should have focus
237+
When I press the "Enter" key globally
238+
Then I should see the "Form_folderCreateForm" form
239+
And the "input#Form_folderCreateForm_Name" element should have focus
240+
When I type "new folder" in the field
241+
And I press the "Enter" key globally
242+
Then I should see the folder named "new-folder" in the gallery
243+
And the folder named "new-folder" should have focus
244+
# delete the folder so the rest of the scenario can continue,
245+
# as it was originally written without this folder here
246+
Given I press the "Other actions" button
247+
And I press the "Delete" button
248+
And I press the "Delete" button inside the modal
249+
232250
# Clicking to select an item sets the roving tabindex to the clicked item
233251
When I check the file named "file03" in the gallery
234252
Then the file named "file03" should have focus
@@ -533,7 +551,7 @@ Feature: Accessibility asset-admin
533551
And I press the "Tab" key globally
534552
Then the folder named "folder01" should have focus
535553

536-
# Adding a folder moved roving tabindex to that folder - but does NOT move focus
554+
# Adding a folder moves roving tabindex to that folder - but does NOT move focus
537555
When I press the "Shift-Tab" key globally
538556
And I press the "Shift-Tab" key globally
539557
And I press the "Shift-Tab" key globally

0 commit comments

Comments
 (0)