Skip to content

Commit b8c424c

Browse files
committed
added tabOrder as a non required parameter to FilePicker
1 parent ee78c79 commit b8c424c

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

src/controls/filePicker/FilePicker.tsx

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export class FilePicker extends React.Component<
4747
private orgAssetsService: OrgAssetsService;
4848
private fileSearchService: FilesSearchService;
4949

50+
private tabOrder = this.props.tabOrder ?? ["keyRecent", "keyStockImages", "keyWeb", "keyOrgAssets", "keyOneDrive", "keySite", "keyUpload", "keyLink", "keyMultipleUpload"];
51+
5052
constructor(props: IFilePickerProps) {
5153
super(props);
5254

@@ -89,7 +91,7 @@ export class FilePicker extends React.Component<
8991

9092
this.setState({
9193
organisationAssetsEnabled: orgAssetsEnabled,
92-
selectedTab: this.getDefaultSelectedTabKey(this.props, orgAssetsEnabled),
94+
selectedTab: this._getDefaultSelectedTabKey(this.props, orgAssetsEnabled),
9395
});
9496
if (!!this.props.context && !!this.props.webAbsoluteUrl) {
9597
const { title, id } = await this.fileBrowserService.getSiteTitleAndId();
@@ -294,7 +296,7 @@ export class FilePicker extends React.Component<
294296
private _handleOpenPanel = (): void => {
295297
this.setState({
296298
panelOpen: true,
297-
selectedTab: this.getDefaultSelectedTabKey(
299+
selectedTab: this._getDefaultSelectedTabKey(
298300
this.props,
299301
this.state.organisationAssetsEnabled
300302
),
@@ -344,7 +346,7 @@ export class FilePicker extends React.Component<
344346
*/
345347
private _getNavPanelOptions = (): INavLinkGroup[] => {
346348
const addUrl = this.props.storeLastActiveTab !== false;
347-
const links = [];
349+
let links: INavLink[] = [];
348350

349351
if (!this.props.hideRecentTab) {
350352
links.push({
@@ -422,41 +424,57 @@ export class FilePicker extends React.Component<
422424
});
423425
}
424426

427+
if(this.props.tabOrder) {
428+
links = this._getTabOrder(links);
429+
}
430+
425431
const groups: INavLinkGroup[] = [{ links }];
426432
return groups;
427433
}
428434

429-
private getDefaultSelectedTabKey = (
435+
/**
436+
* Sorts navigation tabs based on the tabOrder prop
437+
*/
438+
private _getTabOrder = (links: INavLink[]): INavLink[] => {
439+
const sortedKeys = [
440+
...this.tabOrder,
441+
...links.map(l => l.key).filter(key => !this.tabOrder.includes(key)),
442+
];
443+
444+
links.sort((a, b) => {
445+
return sortedKeys.indexOf(a.key) - sortedKeys.indexOf(b.key);
446+
});
447+
448+
return links;
449+
};
450+
451+
/**
452+
* Returns the default selected tab key
453+
*/
454+
private _getDefaultSelectedTabKey = (
430455
props: IFilePickerProps,
431456
orgAssetsEnabled: boolean
432457
): string => {
433-
if (!props.hideRecentTab) {
434-
return "keyRecent";
435-
}
436-
if (!props.hideStockImages) {
437-
return "keyStockImages";
438-
}
439-
if (props.bingAPIKey && !props.hideWebSearchTab) {
440-
return "keyWeb";
441-
}
442-
if (!props.hideOrganisationalAssetTab && orgAssetsEnabled) {
443-
return "keyOrgAssets";
444-
}
445-
if (!props.hideOneDriveTab) {
446-
return "keyOneDrive";
447-
}
448-
if (!props.hideSiteFilesTab) {
449-
return "keySite";
450-
}
451-
if (!props.hideLocalUploadTab) {
452-
return "keyUpload";
453-
}
454-
if (!props.hideLinkUploadTab) {
455-
return "keyLink";
456-
}
457-
if (!props.hideLocalMultipleUploadTab) {
458-
return "keyMultipleUpload";
459-
}
458+
const tabConfig = [
459+
{ isTabVisible: !props.hideRecentTab, tabKey: "keyRecent" },
460+
{ isTabVisible: !props.hideStockImages, tabKey: "keyStockImages" },
461+
{ isTabVisible: props.bingAPIKey && !props.hideWebSearchTab, tabKey: "keyWeb" },
462+
{ isTabVisible: !props.hideOrganisationalAssetTab && orgAssetsEnabled, tabKey: "keyOrgAssets" },
463+
{ isTabVisible: !props.hideOneDriveTab, tabKey: "keyOneDrive" },
464+
{ isTabVisible: !props.hideSiteFilesTab, tabKey: "keySite" },
465+
{ isTabVisible: !props.hideLocalUploadTab, tabKey: "keyUpload" },
466+
{ isTabVisible: !props.hideLinkUploadTab, tabKey: "keyLink" },
467+
{ isTabVisible: !props.hideLocalMultipleUploadTab, tabKey: "keyMultipleUpload" }
468+
];
460469

470+
const visibleTabs = tabConfig.filter(tab => tab.isTabVisible);
471+
const visibleTabKeys = visibleTabs.map(tab => tab.tabKey);
472+
473+
if (this.props.tabOrder) {
474+
const visibleTabSet = new Set(visibleTabKeys);
475+
return this.tabOrder.find(key => visibleTabSet.has(key));
476+
} else {
477+
return visibleTabKeys[0]; // first visible tab from default order
478+
}
461479
}
462480
}

src/controls/filePicker/IFilePickerProps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,9 @@ export interface IFilePickerProps {
175175
* Specifies if file check should be done
176176
*/
177177
checkIfFileExists?: boolean;
178+
/**
179+
* Specifies tab order
180+
* default ["keyRecent", "keyStockImages", "keyWeb", "keyOrgAssets", "keyOneDrive", "keySite", "keyUpload", "keyLink", "keyMultipleUpload"]
181+
*/
182+
tabOrder?: string[];
178183
}

0 commit comments

Comments
 (0)