diff --git a/README.md b/README.md index a7bee8a..dac315a 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,17 @@ const options = { showBranding = false, folderSelectionEnabled = true, driveSelectionEnabled = true, - onFilesPicked = (files) => { - console.log('Selected files:', files); + onFilesPicked = (data) => { + // data may contain files, folders, and/or drives based on what was selected + if (data.files) { + console.log('Selected files:', data.files); + } + if (data.folders) { + console.log('Selected folders:', data.folders); + } + if (data.drives) { + console.log('Selected drives:', data.drives); + } }, onOpen = () => { console.log('File picker opened'); @@ -92,14 +101,31 @@ const filePicker = new FilePicker(options); | **onCancel()** | function | No | Called when the file picker is closed without a file being selected. | | **onError()** | function | No | Called when the file picker has an error. | -### File Type -When you get the callback from the `onFilesPicked` function, you will receive an array of files you selected with the following parameters: +### Callback Data Structure +When you get the callback from the `onFilesPicked` function, you will receive an object that may contain one or more of the following properties based on what was selected: +- `files` (optional): An array of selected files +- `folders` (optional): An array of selected folders when `folderSelectionEnabled` is true +- `drives` (optional): An array of selected drives/sites when `driveSelectionEnabled` is true + +Note: The callback will only include the arrays that have items. For example, if only files are selected, only the `files` property will be present. + +### File and Folder Types +Files and folders share the same structure: | Name | Type | Required | Description | | ----------------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------- | | **id** | string | **Yes** | The Unified Id for the file. | -| **name** | string | No | The Name of the file. | -| **path** | string | No | The URL of the file. | -| **driveId** | string | No | The Drive Id of the file. | +| **name** | string | No | The name of the file or folder. | +| **path** | string | No | The URL or path of the file or folder. | +| **driveId** | string | No | The Drive ID where the file or folder is located. | + +### Drive Type +Drives have a different structure: +| Name | Type | Required | Description | +| ----------------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------- | +| **id** | string | **Yes** | The Unified Id for the drive. | +| **name** | string | No | The name of the drive or site. | +| **type** | string | No | Will be 'site' for drives. | +| **createdAt** | string | No | The creation date of the drive. | ## Contribute & Release This repose uses [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/). The repo use semantic-release and the package version is automatically determined based on the commit messages. diff --git a/src/filePicker.ts b/src/filePicker.ts index 3004bf0..9059dab 100644 --- a/src/filePicker.ts +++ b/src/filePicker.ts @@ -1,4 +1,4 @@ -import { File, FilePickerOptions } from './types'; +import { Drive, File, FilePickerOptions, Folder } from './types'; import { createIFrameInDocument } from './utils/dom'; import { createUrl } from './utils/url'; @@ -13,7 +13,11 @@ export class FilePicker { #fields?: string[]; #folderSelectionEnabled = false; #driveSelectionEnabled = false; - #onFilesPicked: (data: File[]) => void; + #onFilesPicked: (data: { + files?: File[]; + folders?: Folder[]; + drives?: Drive[]; + }) => void; #onClose: () => void; #onOpen: () => void; #onCancel: () => void; diff --git a/src/index.ts b/src/index.ts index 4388457..9a04028 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ export { FilePicker } from './filePicker'; -export type { File } from './types'; +export type { File, Folder, Drive, FilePickerOptions } from './types'; diff --git a/src/types.ts b/src/types.ts index 64285a9..86d440a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,6 +5,20 @@ export type File = { driveId?: string; }; +export type Folder = { + id: string; + name?: string; + path?: string; + driveId?: string; +}; + +export type Drive = { + id: string; + name?: string; + type?: 'site'; + createdAt?: string; +}; + export interface FilePickerOptions { sessionToken: string; containerId?: string; @@ -14,7 +28,11 @@ export interface FilePickerOptions { showBranding?: boolean; folderSelectionEnabled?: boolean; driveSelectionEnabled?: boolean; - onFilesPicked?: (data: File[]) => void; + onFilesPicked?: (data: { + files?: File[]; + folders?: Folder[]; + drives?: Drive[]; + }) => void; onError?: (error: Error) => void; onClose?: () => void; onOpen?: () => void;