Skip to content

Commit 430fea6

Browse files
authored
Update: Make focusing the grid optional via autoFocus prop (#12)
* Update: Make focusing the grid optional via autoFocus prop * Fix: Readme urls
1 parent 1cc8dd2 commit 430fea6

7 files changed

Lines changed: 76 additions & 18 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ render(
7171
| onUpload | function(Array<[File](https://developer.box.com/reference#file-object)>) | | Callback function for when item(s) are uploaded. |
7272
| onNavigate | function([File](https://developer.box.com/reference#file-object)) | | Callback function for when navigating into a folder. |
7373
| isTouch | boolean | | *See the [developer docs](https://developer.box.com/docs/box-content-explorer#section-options).* |
74+
| autoFocus | boolean | | *See the [developer docs](https://developer.box.com/docs/box-content-explorer#section-options).* |
7475
| logoUrl | string | | *See the [developer docs](https://developer.box.com/docs/box-content-explorer#section-options).* |
7576
| sharedLink | string | | *See the [developer docs](https://developer.box.com/docs/box-content-explorer#section-options).* |
7677
| sharedLinkPassword | string | | *See the [developer docs](https://developer.box.com/docs/box-content-explorer#section-options).* |
@@ -116,6 +117,7 @@ render(
116117
| onCancel | function | | Callback function for when the cancel button is pressed. |
117118
| onChoose | function | | Callback function for when the choose button is pressed. |
118119
| isTouch | boolean | | *See the [developer docs](https://developer.box.com/docs/box-content-picker#section-options).* |
120+
| autoFocus | boolean | | *See the [developer docs](https://developer.box.com/docs/box-content-picker#section-options).* |
119121
| logoUrl | string | | *See the [developer docs](https://developer.box.com/docs/box-content-picker#section-options).* |
120122
| sharedLink | string | | *See the [developer docs](https://developer.box.com/docs/box-content-picker#section-options).* |
121123
| sharedLinkPassword | string | | *See the [developer docs](https://developer.box.com/docs/box-content-picker#section-options).* |
@@ -190,6 +192,7 @@ render(
190192
| type | string | `file, web_link, folder` | Indicates which type of items show up in the tree. Should be a comma seperated combination of `file`, `folder` or `web_link`. |
191193
| onClick | function([Folder](https://developer.box.com/reference#folder-object)|[File](https://developer.box.com/reference#file-object)|[Web Link](https://developer.box.com/reference#web-link-object)) | | Callback function for when an item is clicked. |
192194
| isTouch | boolean | | *See the [developer docs](https://developer.box.com/docs/box-content-tree#section-options).* |
195+
| autoFocus | boolean | | *See the [developer docs](https://developer.box.com/docs/box-content-tree#section-options).* |
193196
| logoUrl | string | | *See the [developer docs](https://developer.box.com/docs/box-content-tree#section-options).* |
194197
| sharedLink | string | | *See the [developer docs](https://developer.box.com/docs/box-content-tree#section-options).* |
195198
| sharedLinkPassword | string | | *See the [developer docs](https://developer.box.com/docs/box-content-tree#section-options).* |

src/components/ContentExplorer/ContentExplorer.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type Props = {
7171
isSmall: boolean,
7272
isLarge: boolean,
7373
isTouch: boolean,
74+
autoFocus: boolean,
7475
className: string,
7576
measureRef: Function,
7677
onDelete: Function,
@@ -113,6 +114,7 @@ type DefaultProps = {|
113114
canPreview: boolean,
114115
canShare: boolean,
115116
canSetShareAccess: boolean,
117+
autoFocus: boolean,
116118
apiHost: string,
117119
uploadHost: string,
118120
className: string,
@@ -145,6 +147,7 @@ class ContentExplorer extends Component<DefaultProps, Props, State> {
145147
canShare: true,
146148
canPreview: true,
147149
canSetShareAccess: true,
150+
autoFocus: false,
148151
apiHost: DEFAULT_HOSTNAME_API,
149152
uploadHost: DEFAULT_HOSTNAME_UPLOAD,
150153
className: '',
@@ -310,12 +313,18 @@ class ContentExplorer extends Component<DefaultProps, Props, State> {
310313
* @return {void}
311314
*/
312315
finishNavigation() {
313-
const { onNavigate }: Props = this.props;
316+
const { onNavigate, autoFocus }: Props = this.props;
314317
const { currentCollection: { percentLoaded, boxItem } }: State = this.state;
315318
onNavigate(cloneDeep(boxItem));
316319

317320
// Don't focus the grid until its loaded and user is not already on an interactable element
318-
if (percentLoaded !== 100 || !this.table || !this.table.Grid || isActionableElement(document.activeElement)) {
321+
if (
322+
!autoFocus ||
323+
percentLoaded !== 100 ||
324+
!this.table ||
325+
!this.table.Grid ||
326+
isActionableElement(document.activeElement)
327+
) {
319328
return;
320329
}
321330
const grid: any = findDOMNode(this.table.Grid);

src/components/ContentPicker/ContentPicker.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Props = {
6161
maxSelectable: number,
6262
canUpload: boolean,
6363
canSetShareAccess: boolean,
64+
autoFocus: boolean,
6465
apiHost: string,
6566
uploadHost: string,
6667
getLocalizedMessage: Function,
@@ -98,6 +99,7 @@ type DefaultProps = {|
9899
maxSelectable: number,
99100
canUpload: boolean,
100101
canSetShareAccess: boolean,
102+
autoFocus: boolean,
101103
apiHost: string,
102104
uploadHost: string,
103105
clientName: string,
@@ -124,6 +126,7 @@ class ContentPicker extends Component<DefaultProps, Props, State> {
124126
maxSelectable: Infinity,
125127
canUpload: true,
126128
canSetShareAccess: true,
129+
autoFocus: false,
127130
className: '',
128131
apiHost: DEFAULT_HOSTNAME_API,
129132
uploadHost: DEFAULT_HOSTNAME_UPLOAD,
@@ -316,6 +319,30 @@ class ContentPicker extends Component<DefaultProps, Props, State> {
316319
}
317320
};
318321

322+
/**
323+
* Focuses the grid
324+
*
325+
* @private
326+
* @return {void}
327+
*/
328+
finishNavigation() {
329+
const { autoFocus }: Props = this.props;
330+
const { currentCollection: { percentLoaded } }: State = this.state;
331+
332+
// Don't focus the grid until its loaded and user is not already on an interactable element
333+
if (
334+
!autoFocus ||
335+
percentLoaded !== 100 ||
336+
!this.table ||
337+
!this.table.Grid ||
338+
isActionableElement(document.activeElement)
339+
) {
340+
return;
341+
}
342+
const grid: any = findDOMNode(this.table.Grid);
343+
grid.focus();
344+
}
345+
319346
/**
320347
* Folder fetch success callback
321348
*
@@ -335,13 +362,7 @@ class ContentPicker extends Component<DefaultProps, Props, State> {
335362
};
336363

337364
// Set the new state and focus the grid for tabbing
338-
this.setState(newState, () => {
339-
if (!this.table || !this.table.Grid || isActionableElement(document.activeElement)) {
340-
return;
341-
}
342-
const grid: any = findDOMNode(this.table.Grid);
343-
grid.focus();
344-
});
365+
this.setState(newState, this.finishNavigation);
345366
};
346367

347368
/**

src/components/ContentTree/ContentTree.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import noop from 'lodash.noop';
1212
import Content from './Content';
1313
import API from '../../api';
1414
import makeResponsive from '../makeResponsive';
15+
import isActionableElement from '../../util/dom';
1516
import {
1617
DEFAULT_HOSTNAME_API,
1718
DEFAULT_ROOT,
@@ -39,6 +40,7 @@ type Props = {
3940
isSmall: boolean,
4041
isLarge: boolean,
4142
isTouch: boolean,
43+
autoFocus: boolean,
4244
className: string,
4345
measureRef: Function,
4446
sharedLink?: string,
@@ -56,6 +58,7 @@ type DefaultProps = {|
5658
onClick: Function,
5759
apiHost: string,
5860
clientName: string,
61+
autoFocus: boolean,
5962
className: string
6063
|};
6164

@@ -71,6 +74,7 @@ class ContentTree extends Component<DefaultProps, Props, State> {
7174
rootFolderId: DEFAULT_ROOT,
7275
onClick: noop,
7376
className: '',
77+
autoFocus: false,
7478
apiHost: DEFAULT_HOSTNAME_API,
7579
clientName: CLIENT_NAME_CONTENT_TREE
7680
};
@@ -199,6 +203,30 @@ class ContentTree extends Component<DefaultProps, Props, State> {
199203
}
200204
};
201205

206+
/**
207+
* Focuses the grid
208+
*
209+
* @private
210+
* @return {void}
211+
*/
212+
finishNavigation() {
213+
const { autoFocus }: Props = this.props;
214+
const { currentCollection: { percentLoaded } }: State = this.state;
215+
216+
// Don't focus the grid until its loaded and user is not already on an interactable element
217+
if (
218+
!autoFocus ||
219+
percentLoaded !== 100 ||
220+
!this.table ||
221+
!this.table.Grid ||
222+
isActionableElement(document.activeElement)
223+
) {
224+
return;
225+
}
226+
const grid: any = findDOMNode(this.table.Grid);
227+
grid.focus();
228+
}
229+
202230
/**
203231
* Folder fetch success callback
204232
*
@@ -230,13 +258,7 @@ class ContentTree extends Component<DefaultProps, Props, State> {
230258
}
231259

232260
// Set the new state and focus the grid for tabbing
233-
this.setState({ currentCollection }, () => {
234-
if (!this.table || !this.table.Grid) {
235-
return;
236-
}
237-
const grid: any = findDOMNode(this.table.Grid);
238-
grid.focus();
239-
});
261+
this.setState({ currentCollection }, this.finishNavigation);
240262
};
241263

242264
/**

test/explorer.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ <h1>Content Explorer</h1>
128128
document.querySelector('.explorer2').innerHTML = '';
129129
explorer2.show(folderId, token, {
130130
container: '.explorer2',
131-
logoUrl: 'https://d30y9cdsu7xlg0.cloudfront.net/png/12458-200.png'
131+
logoUrl: 'https://d30y9cdsu7xlg0.cloudfront.net/png/12458-200.png',
132+
autoFocus: true
132133
});
133134
}
134135
load();

test/pickers.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ <h1>Folder Picker (max 3 items / token generator / no upload)</h1>
116116
});
117117
filePicker1.show(folderId, token, {
118118
container: '.filePicker1',
119+
autoFocus: true,
119120
logoUrl: 'https://d30y9cdsu7xlg0.cloudfront.net/png/12458-200.png'
120121
});
121122

test/tree.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ <h1>Content Tree</h1>
8585
document.querySelector('.tree2').innerHTML = '';
8686
tree2.show(folderId, token, {
8787
container: '.tree2',
88-
type: 'folder,file,web_link'
88+
type: 'folder,file,web_link',
89+
autoFocus: true
8990
});
9091
}
9192
load();

0 commit comments

Comments
 (0)