Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directory support for FileTrigger #5444

Merged
merged 6 commits into from Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/react-aria-components/docs/FileTrigger.mdx
Expand Up @@ -120,6 +120,20 @@ A file trigger can accept multiple files by passsing the `allowsMultiple` proper
</FileTrigger>
```

## Directory Selection

To enable selecting directories instead of files, use the `UNSAFE_directory` property.
Copy link
Member

Choose a reason for hiding this comment

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

Since it looks like this is well-supported with browsers we support, I think we can just use directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by the following commit.

refac: Remove unsafe prefix


This reflects the webkitdirectory HTML attribute and allows users to select directories and their contents.
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment. Link added.
refac: Remove unsafe prefix


The `UNSAFE_` prefix is used to signify that this feature's behavior may vary across different browsers and versions.

```tsx example
<FileTrigger UNSAFE_directory>
<Button>Select a directory</Button>
</FileTrigger>
```

## Media capture

To specify the media capture mechanism to capture media on the spot, pass `user` for the user-facing camera or `environment` for the outward-facing camera via the `defaultCamera` prop.
Expand Down
13 changes: 10 additions & 3 deletions packages/react-aria-components/src/FileTrigger.tsx
Expand Up @@ -35,11 +35,16 @@ export interface FileTriggerProps {
/**
* The children of the component.
*/
children?: ReactNode
children?: ReactNode,
/**
* Enables the selection of directories instead of individual files.
* Note: This feature's behavior may vary across different browsers and versions.
*/
UNSAFE_directory?: boolean
}

function FileTrigger(props: FileTriggerProps, ref: ForwardedRef<HTMLInputElement>) {
let {onSelect, acceptedFileTypes, allowsMultiple, defaultCamera, children, ...rest} = props;
let {onSelect, acceptedFileTypes, allowsMultiple, defaultCamera, children, UNSAFE_directory, ...rest} = props;
let inputRef = useObjectRef(ref);
let domProps = filterDOMProps(rest);

Expand All @@ -62,7 +67,9 @@ function FileTrigger(props: FileTriggerProps, ref: ForwardedRef<HTMLInputElement
accept={acceptedFileTypes?.toString()}
onChange={(e) => onSelect?.(e.target.files)}
capture={defaultCamera}
multiple={allowsMultiple} />
multiple={allowsMultiple}
// @ts-expect-error
webkitdirectory={UNSAFE_directory ? '' : undefined} />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this implementation uses an empty string for the webkitdirectory attribute instead of a boolean value because it does not function correctly with boolean.

ref: facebook/react#3468 (comment)

</>
);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/react-aria-components/test/FileTrigger.test.js
Expand Up @@ -88,4 +88,16 @@ describe('FileTrigger', () => {
let input = getByTestId('foo');
expect(ref.current).toBe(input);
});

it('should allow directory uploads when UNSAFE_directory is true', () => {
render(
<FileTrigger UNSAFE_directory>
<Button>Upload Directory</Button>
</FileTrigger>
);

let input = document.querySelector('input[type="file"]');
expect(input).toHaveAttribute('webkitdirectory');
});

});