Skip to content

Commit fc08a7f

Browse files
committed
Merge branch 'dev' of https://github.com/siata13/sp-dev-fx-controls-react into siata13-dev
2 parents b5c58d3 + f972238 commit fc08a7f

File tree

82 files changed

+6427
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+6427
-33
lines changed
166 KB
Loading
682 KB
Loading
875 KB
Loading
185 KB
Loading
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# FilePicker control
2+
3+
File picker control allows to browse and select a file from various places.
4+
Currently supported locations
5+
- Recent files - tab allows to select a file from recently modified files based on the search results.
6+
- Web search - tab uses Bing cognitive services to look for a file. (Only images)
7+
- OneDrive - tab allows to select a file from the user's One Drive.
8+
- Site document libraries - tab allows to select a file from the existing site document libraries.
9+
- Upload - tab allows to upload a file from local drive.
10+
- From a link - tab allows to paste a link to the document.
11+
12+
## Overview
13+
The control supports all types of file, however it also allows to specify list of extensions for the files that are going to be looked displayed. Currently, only single file selection is supported.
14+
![File Picker overview](../assets/FilePickerOverview.png)
15+
16+
17+
## Different display types
18+
File picker support 3 types of views : List, Compact list and Tiles. In case Tiles view is selected, the control shows the thumbnail of the file.
19+
![File Picker views](../assets/FilePickerViews.gif)
20+
21+
22+
## Breadcrumb support
23+
The control displays breadcrumb navigation that allows to easily switch folders or document libraries.
24+
![File Picker breadcrumb](../assets/FilePickerBreadcrumb.gif)
25+
26+
## Paged data load
27+
File picker doesn't load all the files that exist in the folder. Instead, it allows to specify how many results are loaded in a batch, and executes paged requests when new data is required.
28+
![File Picker paged data load](../assets/FilePickerPaging.gif)
29+
30+
31+
## How to use this control
32+
33+
- Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../#getting-started) page for more information about installing the dependency.
34+
- Import the following module to your component:
35+
36+
```TypeScript
37+
import { FilePicker, IFilePickerResult } from '@pnp/spfx-controls-react/lib/FilePicker';
38+
```
39+
40+
- Use the `FilePicker` control in your code as follows:
41+
42+
```TypeScript
43+
<FilePicker
44+
bingAPIKey="<BING API KEY>"
45+
accepts= ".gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png,.jxr,.svg"
46+
buttonIcon="FileImage"
47+
onSave={(filePickerResult: IFilePickerResult) => { this.setState({filePickerResult }) }}
48+
onChanged={(filePickerResult: IFilePickerResult) => { this.setState({filePickerResult }) }}
49+
webPartContext={this.props.context}
50+
/>
51+
```
52+
53+
## Implementation
54+
55+
The FilePicker component can be configured with the following properties:
56+
57+
| Property | Type | Required | Description |
58+
| ---- | ---- | ---- | ---- |
59+
| label | string | no | Specifies the text describing the file picker. |
60+
| buttonLabel | string | no | Specifies the label of the file picker button. |
61+
| buttonIcon | string | no | In case it is provided the file picker will be rendered as an action button. |
62+
| 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. |
65+
| accepts | string[] | no | Array of strings containing allowed files extensions. E.g. [".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"] |
66+
| required | boolean | no | Sets the label to inform that the value is required. |
67+
| bingAPIKey | string | no | Used to execute WebSearch. If not provided SearchTab will not be available. |
68+
| disabled | boolean | no | Specifies if the picker button is disabled |
69+
| itemsCountQueryLimit | number | no | Number of items to obtain when executing REST queries. Default 100. |
70+
| hideRecentTab | boolean | no | Specifies if RecentTab should be hidden. |
71+
| hideWebSearchTab | boolean | no | Specifies if WebSearchTab should be hidden. |
72+
| hideOrganisationalAssetTab | boolean | no | Specifies if OrganisationalAssetTab should be hidden. |
73+
| hideOneDriveTab | boolean | no | Specifies if OneDriveTab should be hidden. |
74+
| hideSiteFilesTab | boolean | no | Specifies if SiteFilesTab should be hidden. |
75+
| hideLocalUploadTab | boolean | no | Specifies if LocalUploadTab should be hidden. |
76+
| hideLinkUploadTab | boolean | no | Specifies if LinkUploadTab should be hidden. |
77+
78+
interface `IFilePickerResult`
79+
80+
Provides options for carousel buttons location.
81+
82+
| Value | Description |
83+
| ---- | ---- |
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. |
88+
89+
90+
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/filePicker/FilePicker)

package-lock.json

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FilePicker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./controls/filePicker/index";

src/common/utilities/GeneralHelper.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as _ from '@microsoft/sp-lodash-subset';
66

77
import * as strings from 'ControlStrings';
88

9+
export const IMG_SUPPORTED_EXTENSIONS = ".gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png,.jxr,.svg";
10+
911
/**
1012
* Helper with general methods to simplify some routines
1113
*/
@@ -217,6 +219,82 @@ export class GeneralHelper {
217219
return xml;
218220
}
219221

222+
/**
223+
* Returns absoulute domain URL.
224+
* @param url
225+
*/
226+
public static getAbsoluteDomainUrl(url: string): string {
227+
if (url !== undefined) {
228+
const myURL = new URL(url.toLowerCase());
229+
return myURL.protocol + "//" + myURL.host;
230+
} else {
231+
return undefined;
232+
}
233+
}
234+
235+
public static formatBytes(bytes, decimals) {
236+
if (bytes == 0) {
237+
return strings.EmptyFileSize;
238+
}
239+
240+
const k: number = 1024;
241+
const dm = decimals <= 0 ? 0 : decimals || 2;
242+
const i = Math.floor(Math.log(bytes) / Math.log(k));
243+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + strings.SizeUnit[i];
244+
}
245+
246+
/**
247+
* Returns file name without extension.
248+
*/
249+
public static getFileNameWithoutExtension(itemUrl : string) {
250+
const fileNameWithExtension = GeneralHelper.getFileNameFromUrl(itemUrl);
251+
const fileNameTokens = fileNameWithExtension.split(".");
252+
const fileName = fileNameTokens[0];
253+
254+
return fileName;
255+
}
256+
257+
/**
258+
* Returns file name with the extension
259+
*/
260+
public static getFileNameFromUrl(itemUrl : string) {
261+
const urlTokens = itemUrl.split("?");
262+
const url = urlTokens[0];
263+
const tokens = url.split("/");
264+
const fileNameWithExtension = tokens[tokens.length - 1];
265+
266+
return fileNameWithExtension;
267+
}
268+
269+
public static isImage(fileName: string): boolean {
270+
const acceptableExtensions: string[] = IMG_SUPPORTED_EXTENSIONS.split(",");
271+
// const IMG_SUPPORTED_EXTENSIONS = ".gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png,.jxr,.svg"
272+
273+
const thisExtension: string = GeneralHelper.getFileExtension(fileName);
274+
return acceptableExtensions.indexOf(thisExtension) > -1;
275+
}
276+
277+
/**
278+
* Returns extension of the file
279+
*/
280+
public static getFileExtension(fileName): string {
281+
282+
// Split the URL on the dots
283+
const splitFileName = fileName.toLowerCase().split('.');
284+
285+
// Take the last value
286+
let extensionValue = splitFileName.pop();
287+
288+
// Check if there are query string params in place
289+
if (extensionValue.indexOf('?') !== -1) {
290+
// Split the string on the question mark and return the first part
291+
const querySplit = extensionValue.split('?');
292+
extensionValue = querySplit[0];
293+
}
294+
295+
return `.${extensionValue}`;
296+
}
297+
220298
private static _getEncodedChar(c): string {
221299
const o = {
222300
"<": "&lt;",
@@ -228,4 +306,4 @@ export class GeneralHelper {
228306
};
229307
return o[c];
230308
}
231-
}
309+
}

0 commit comments

Comments
 (0)