Skip to content

Commit 569b37e

Browse files
authored
Merge pull request #370 from siata13/dev
FilePicker improvements.
2 parents 3f3181a + 46d4c03 commit 569b37e

File tree

17 files changed

+222
-95
lines changed

17 files changed

+222
-95
lines changed

docs/documentation/docs/controls/FilePicker.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { FilePicker, IFilePickerResult } from '@pnp/spfx-controls-react/lib/File
4646
buttonIcon="FileImage"
4747
onSave={(filePickerResult: IFilePickerResult) => { this.setState({filePickerResult }) }}
4848
onChanged={(filePickerResult: IFilePickerResult) => { this.setState({filePickerResult }) }}
49-
webPartContext={this.props.context}
49+
context={this.props.context}
5050
/>
5151
```
5252

@@ -60,8 +60,8 @@ The FilePicker component can be configured with the following properties:
6060
| buttonLabel | string | no | Specifies the label of the file picker button. |
6161
| buttonIcon | string | no | In case it is provided the file picker will be rendered as an action button. |
6262
| onSave | (filePickerResult: IFilePickerResult) => void | yes | Handler when the file has been selected and picker has been closed. |
63-
| onChange | (filePickerResult: IFilePickerResult) => void | yes | Handler when the file selection has been changed. |
64-
| webPartContext | WebPartContext | yes | Current context. |
63+
| onChange | (filePickerResult: IFilePickerResult) => void | no | Handler when the file selection has been changed. |
64+
| context | ApplicationCustomizerContext | WebPartContext | yes | Current context. |
6565
| accepts | string[] | no | Array of strings containing allowed files extensions. E.g. [".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"] |
6666
| required | boolean | no | Sets the label to inform that the value is required. |
6767
| bingAPIKey | string | no | Used to execute WebSearch. If not provided SearchTab will not be available. |
@@ -79,12 +79,12 @@ interface `IFilePickerResult`
7979

8080
Provides options for carousel buttons location.
8181

82-
| Value | Description |
82+
| Value | Type | Description |
8383
| ---- | ---- |
84-
| fileName | string | yes | File namr of the result with the extension. |
85-
| fileNameWithoutExtension | string | yes | File namr of the result without the extension. |
86-
| fileAbsoluteUrl | string | yes | Absolute URL of the file. Null in case of file upload. |
87-
| file | File | yes | JS Object representing File. Will be set in case of file upload. |
84+
| fileName | string | File namr of the result with the extension. |
85+
| fileNameWithoutExtension | string | File name of the result without the extension. |
86+
| fileAbsoluteUrl | string | Absolute URL of the file. Null in case of file upload. |
87+
| downloadFileContent | () => Promise<File> | Function allows to download file content. Returns File object. |
8888

8989

9090
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/filePicker/FilePicker)

src/controls/filePicker/FilePicker.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export class FilePicker extends React.Component<IFilePickerProps, IFilePickerSta
3939
telemetry.track('ReactFilePicker', {});
4040

4141
// Initialize file browser services
42-
this.fileBrowserService = new FileBrowserService(props.webPartContext, this.props.itemsCountQueryLimit);
43-
this.oneDriveService = new OneDriveService(props.webPartContext, this.props.itemsCountQueryLimit);
44-
this.orgAssetsService = new OrgAssetsService(props.webPartContext, this.props.itemsCountQueryLimit);
45-
this.fileSearchService = new FilesSearchService(props.webPartContext, this.props.bingAPIKey);
42+
this.fileBrowserService = new FileBrowserService(props.context, this.props.itemsCountQueryLimit);
43+
this.oneDriveService = new OneDriveService(props.context, this.props.itemsCountQueryLimit);
44+
this.orgAssetsService = new OrgAssetsService(props.context, this.props.itemsCountQueryLimit);
45+
this.fileSearchService = new FilesSearchService(props.context, this.props.bingAPIKey);
4646

4747
this.state = {
4848
panelOpen: false,
@@ -68,7 +68,7 @@ export class FilePicker extends React.Component<IFilePickerProps, IFilePickerSta
6868

6969
const linkTabProps = {
7070
accepts: accepts,
71-
context: this.props.webPartContext,
71+
context: this.props.context,
7272
onClose: () => this._handleClosePanel(),
7373
onSave: (value: IFilePickerResult) => { this._handleSave(value); }
7474
};
@@ -201,7 +201,7 @@ export class FilePicker extends React.Component<IFilePickerProps, IFilePickerSta
201201
* On save action
202202
*/
203203
private _handleSave = (filePickerResult: IFilePickerResult) => {
204-
this.props.onChanged(filePickerResult);
204+
this.props.onSave(filePickerResult);
205205
this.setState({
206206
panelOpen: false
207207
});
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
import { WebPartContext } from "@microsoft/sp-webpart-base";
22
import { IBreadcrumbItem } from "office-ui-fabric-react/lib/Breadcrumb";
33
import { IFile, ILibrary } from "../../services/FileBrowserService.types";
4+
import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";
45

56
export interface FilePickerBreadcrumbItem extends IBreadcrumbItem {
67
libraryData?: ILibrary;
78
folderData?: IFile;
89
}
910

1011
export interface IFilePickerTab {
11-
context: WebPartContext;
12+
context: ApplicationCustomizerContext | WebPartContext;
1213
accepts: string[];
1314
onSave: (value: IFilePickerResult) => void;
1415
onClose: () => void;
1516
}
1617

18+
/**
19+
* Represents the result of the FilePicker.
20+
*/
1721
export interface IFilePickerResult {
22+
/**
23+
* Selected file name with extension.
24+
*/
1825
fileName: string;
26+
/**
27+
* Selected file name without extension.
28+
*/
1929
fileNameWithoutExtension: string;
30+
/**
31+
* Absolute file URL. Undefined in case of file upload.
32+
*/
2033
fileAbsoluteUrl: string;
21-
file: File;
34+
35+
/**
36+
* Downloads file picker result content.
37+
*/
38+
downloadFileContent: () => Promise<File>;
2239
}

src/controls/filePicker/IFilePickerProps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { WebPartContext } from "@microsoft/sp-webpart-base";
22
import { IFilePickerResult } from "./FilePicker.types";
3+
import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";
34

45
export interface IFilePickerProps {
56
/**
@@ -24,12 +25,12 @@ export interface IFilePickerProps {
2425
/**
2526
* Handler when file has been changed.
2627
*/
27-
onChanged: (filePickerResult: IFilePickerResult) => void;
28+
onChanged?: (filePickerResult: IFilePickerResult) => void;
2829

2930
/**
3031
* Current context.
3132
*/
32-
webPartContext: WebPartContext;
33+
context: ApplicationCustomizerContext | WebPartContext;
3334

3435
/**
3536
* File extensions to be displayed.

src/controls/filePicker/LinkFilePickerTab/LinkFilePickerTab.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export default class LinkFilePickerTab extends React.Component<ILinkFilePickerTa
6262
*/
6363
private _handleChange = (fileUrl: string) => {
6464
const filePickerResult: IFilePickerResult = fileUrl && this._isUrl(fileUrl) ? {
65-
file: null,
6665
fileAbsoluteUrl: fileUrl,
6766
fileName: GeneralHelper.getFileNameFromUrl(fileUrl),
68-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(fileUrl)
67+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(fileUrl),
68+
downloadFileContent: () => { return this.props.fileSearchService.downloadBingContent(fileUrl, GeneralHelper.getFileNameFromUrl(fileUrl)); }
6969
} : null;
7070
this.setState({
7171
filePickerResult
@@ -95,7 +95,7 @@ export default class LinkFilePickerTab extends React.Component<ILinkFilePickerTa
9595
return strings.NoExternalLinksValidationMessage;
9696
}
9797

98-
const fileExists = await this.props.fileSearchService.fetchFile(value);
98+
const fileExists = await this.props.fileSearchService.checkFileExists(value);
9999
this.setState({ isValid: fileExists });
100100

101101
const strResult = fileExists ? '' : strings.ProvidedValueIsInvalid;

src/controls/filePicker/OneDriveFilesTab/OneDriveFilesTab.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ export class OneDriveFilesTab extends React.Component<IOneDriveFilesTabProps, IO
131131
* Is called when user selects a different file
132132
*/
133133
private _handleSelectionChange = (filePickerResult: IFilePickerResult) => {
134+
if (filePickerResult) {
135+
filePickerResult.downloadFileContent = () => { return this.props.oneDriveService.downloadSPFileContent(filePickerResult.fileAbsoluteUrl, filePickerResult.fileName); };
136+
}
134137
this.setState({
135138
filePickerResult
136139
});

src/controls/filePicker/RecentFilesTab/RecentFilesTab.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ export default class RecentFilesTab extends React.Component<IRecentFilesTabProps
102102
//Get the selected key
103103
const selectedKey: IRecentFile = selectedItems[0] as IRecentFile;
104104
const filePickerResult: IFilePickerResult = {
105-
file: null,
106105
fileAbsoluteUrl: selectedKey.fileUrl,
107106
fileName: GeneralHelper.getFileNameFromUrl(selectedKey.fileUrl),
108-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedKey.fileUrl)
107+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedKey.fileUrl),
108+
downloadFileContent: () => { return this.props.fileSearchService.downloadSPFileContent(selectedKey.fileUrl, GeneralHelper.getFileNameFromUrl(selectedKey.fileUrl)); }
109109
};
110+
110111
// Save the selected file
111112
this.setState({
112113
filePickerResult

src/controls/filePicker/SiteFilePickerTab/SiteFilePickerTab.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export default class SiteFilePickerTab extends React.Component<ISiteFilePickerTa
112112
* Is called when user selects a different file
113113
*/
114114
private _handleSelectionChange = (filePickerResult: IFilePickerResult) => {
115+
if (filePickerResult) {
116+
filePickerResult.downloadFileContent = () => { return this.props.fileBrowserService.downloadSPFileContent(filePickerResult.fileAbsoluteUrl, filePickerResult.fileName); };
117+
}
118+
// this.props.fileBrowserService
115119
this.setState({
116120
filePickerResult
117121
});

src/controls/filePicker/UploadFilePickerTab/UploadFilePickerTab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export default class UploadFilePickerTab extends React.Component<IUploadFilePick
7575
const file: File = files[0];
7676

7777
const filePickerResult: IFilePickerResult = {
78-
file,
7978
fileAbsoluteUrl: null,
8079
fileName: file.name,
81-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(file.name)
80+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(file.name),
81+
downloadFileContent: () => { return Promise.resolve(file); }
8282
};
8383

8484
if (GeneralHelper.isImage(file.name)) {

src/controls/filePicker/WebSearchTab/WebSearchTab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ export default class WebSearchTab extends React.Component<IWebSearchTabProps, IW
102102
// even if it breaks the page.
103103
const selectedUrl: string = selectedItem.contentUrl.replace('http://', 'https://');
104104
selectedFileResult = {
105-
file: null,
106105
fileAbsoluteUrl: selectedUrl,
107106
fileName: GeneralHelper.getFileNameFromUrl(selectedUrl),
108-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedUrl)
107+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedUrl),
108+
downloadFileContent: () => { return this.props.bingSearchService.downloadBingContent(selectedUrl, GeneralHelper.getFileNameFromUrl(selectedUrl)); }
109109
};
110110
}
111111

0 commit comments

Comments
 (0)