Skip to content

SPFilePicker#2122

Open
joaojmendes wants to merge 2 commits into
pnp:devfrom
joaojmendes:SPFilePicker
Open

SPFilePicker#2122
joaojmendes wants to merge 2 commits into
pnp:devfrom
joaojmendes:SPFilePicker

Conversation

@joaojmendes

Copy link
Copy Markdown
Collaborator
Q A
Bug fix? [ ]
New feature? [ ]
New sample? [X ]
Related issues? fixes #X, partially #Y, mentioned in #Z

New Control SPFilePicker

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SPFilePicker React component plus useSPFilePicker hook, 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;
Comment thread src/SpFilePicker.ts
Comment thread docs/documentation/docs/controls/SPFilePicker.md Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 19 changed files in this pull request and generated 9 comments.

{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;
Comment thread src/SpFilePicker.ts
@@ -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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants