SPFilePicker#2122
Open
joaojmendes wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new SPFilePicker control to the library and wires it into the controls test web part and documentation, enabling consumers to launch the Microsoft-hosted SharePoint/OneDrive File Picker (v8) and receive selected items via callbacks.
Changes:
- Introduces
SPFilePickerReact component plususeSPFilePickerhook, supporting iframe (dialog) and popup hosting modes. - Adds a ControlsTest sample (
TestSPFilePickerControl) and toggle/manifest entries to demo the control. - Adds localization keys and MkDocs documentation for the new control.
Reviewed changes
Copilot reviewed 16 out of 19 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/webparts/controlsTest/propertyPane/controls/ControlToggles.tsx | Adds SPFilePicker to the control toggle list. |
| src/webparts/controlsTest/IControlsTestWebPartProps.ts | Extends ValidControls union with SPFilePicker. |
| src/webparts/controlsTest/ControlsTestWebPart.manifest.json | Adds default visibility flag for SPFilePicker. |
| src/webparts/controlsTest/components/TestSPFilePickerControl.tsx | New sample component demonstrating SPFilePicker usage. |
| src/webparts/controlsTest/components/ControlsTest.tsx | Registers and conditionally renders the SPFilePicker sample. |
| src/SpFilePicker.ts | Adds a new entry-point re-export for SPFilePicker. |
| src/loc/mystrings.d.ts | Declares new string keys for SPFilePicker. |
| src/loc/en-us.ts | Provides en-us values for SPFilePicker strings. |
| src/controls/SPFilePicker/useSPFilePickerStyles.ts | Adds Emotion-based styling for the dialog/iframe surface. |
| src/controls/SPFilePicker/SPFilePicker.tsx | Implements the ready-to-use SPFilePicker component. |
| src/controls/SPFilePicker/ISPFilePickerProps.ts | Defines public props for SPFilePicker. |
| src/controls/SPFilePicker/ISPFilePicker.types.ts | Defines picker configuration/item/auth types. |
| src/controls/SPFilePicker/index.ts | Barrel exports for SPFilePicker module. |
| src/controls/SPFilePicker/hooks/useSPFilePicker.ts | Implements picker launch + MessageChannel workflow and token handling. |
| src/controls/SPFilePicker/hooks/useLogging.ts | Adds SPFx Log wrapper hook for picker logging. |
| docs/documentation/mkdocs.yml | Adds SPFilePicker page to MkDocs nav. |
| docs/documentation/docs/controls/SPFilePicker.md | New documentation page for SPFilePicker. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const resolvedBaseUrl = baseUrl ?? context.pageContext.web.absoluteUrl ?? ''; | ||
| const styles = useSPFilePickerStyles(dialogHeight); | ||
|
|
||
| const { isOpen, isLoading, iframeRef, open, markLoaded } = useSPFilePicker({ |
| {renderTrigger()} | ||
|
|
||
| {target === 'iframe' && ( | ||
| <Dialog open={isOpen} modalType="modal"> |
Comment on lines
+332
to
+352
| (_pickerWindow: Window, channelId: string) => { | ||
| const listener = (event: MessageEvent): void => { | ||
| const data = event.data; | ||
| // Match by channelId (a unique per-launch id). Source identity is not | ||
| // reliable once the iframe navigates cross-origin, so we don't gate on it. | ||
| if (data?.type === 'initialize' && data?.channelId === channelId) { | ||
| const port = event.ports?.[0]; | ||
| if (!port) return; | ||
|
|
||
| portRef.current = port; | ||
| port.addEventListener('message', (m) => { | ||
| void handlePortMessage(m); | ||
| }); | ||
| port.start(); | ||
| port.postMessage({ type: 'activate' }); | ||
| } | ||
| }; | ||
|
|
||
| windowListenerRef.current = listener; | ||
| window.addEventListener('message', listener); | ||
| }, |
Comment on lines
+93
to
+97
| /** Title shown in the dialog header (iframe target). Defaults to `Select a file`. */ | ||
| dialogTitle?: string; | ||
|
|
||
| /** Cancel button text in the dialog. Defaults to `Cancel`. */ | ||
| cancelText?: string; |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| {renderTrigger()} | ||
|
|
||
| {target === 'iframe' && ( | ||
| <Dialog open={isOpen} modalType="modal"> |
| const resolvedBaseUrl = baseUrl ?? context.pageContext.web.absoluteUrl ?? ''; | ||
| const styles = useSPFilePickerStyles(dialogHeight); | ||
|
|
||
| const { isOpen, isLoading, iframeRef, open, markLoaded } = useSPFilePicker({ |
Comment on lines
+84
to
+86
| <span className={className} onClick={disabled ? undefined : open}> | ||
| {trigger} | ||
| </span> |
Comment on lines
+333
to
+337
| const listener = (event: MessageEvent): void => { | ||
| const data = event.data; | ||
| // Match by channelId (a unique per-launch id). Source identity is not | ||
| // reliable once the iframe navigates cross-origin, so we don't gate on it. | ||
| if (data?.type === 'initialize' && data?.channelId === channelId) { |
Comment on lines
+353
to
+354
| [handlePortMessage], | ||
| ); |
Comment on lines
+378
to
+380
| // Token mode: POST the access token so the picker can authenticate. | ||
| const accessToken = await resolveToken(baseUrl); | ||
| const doc = pickerWindow.document; |
Comment on lines
+93
to
+97
| /** Title shown in the dialog header (iframe target). Defaults to `Select a file`. */ | ||
| dialogTitle?: string; | ||
|
|
||
| /** Cancel button text in the dialog. Defaults to `Cancel`. */ | ||
| cancelText?: string; |
| @@ -0,0 +1 @@ | |||
| export * from './controls/SPFilePicker/index'; | |||
Comment on lines
+112
to
+130
| export function useSPFilePicker(options: IUseSPFilePickerOptions): IUseSPFilePickerReturn { | ||
| const { | ||
| context, | ||
| baseUrl, | ||
| entry, | ||
| selectionMode = 'single', | ||
| itemsMode = 'files', | ||
| fileTypes, | ||
| locale = 'en-us', | ||
| authMode = 'token', | ||
| app, | ||
| scenario, | ||
| raw, | ||
| target = 'iframe', | ||
| getToken, | ||
| onPicked, | ||
| onCancel, | ||
| onError, | ||
| } = options; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New Control SPFilePicker